2014-12-26 25 views
1

情景: 我已經爲各自的‘節頭’(存儲爲字符串)執行某些任務,該任務的結果必須被保存對同各自的「現有部分標題「(存儲爲字符串)需要的字符串比較的Python爲部分字符串比較的解決方案

在映射如果各個任務的」部分標題「是」現有部分標題「之一時,任務結果將添加到其中。 如果不是,新的部分標題將被附加到現有部分標題列表。

現有的段段頭看起來是這樣的:

[ 「活動(最近3天)」, 「活動(最近7天)」, 「可執行 從磁盤運行」, 「從文件操作」]

對於設置如下字符串的預期行爲如下:

「活動(最近30天) - 新科應加

「從磁盤運行的可執行文件」 - 應該引用相同的現有「從磁盤運行的可執行文件」[在可執行文件中考慮與「可執行文件」相同的額外的「s」。

「從文件操作」 - 同現有的「從文件的行爲」應該被稱爲[考慮額外的冠詞「a」]

是否有任何內置的功能可蟒蛇,可以幫助包括相同的邏輯。或者有關算法的任何建議對此非常感謝。

回答

0

因爲只需要比較幹或給定字的「詞根」,我建議使用一些詞幹算法。詞幹算法試圖自動刪除後綴(以及某些情況下的前綴),以便查找給定詞的「詞根」或詞幹。這在各種自然語言處理場景中非常有用,例如搜索。幸運的是,有一個用於stemming的python包。您可以從here下載它。

接下來,你要不停字(一,一個的,從等)來比較字符串。所以你需要在比較字符串之前過濾這些單詞。您可以從互聯網獲取停用詞列表,也可以使用nltk軟件包導入停用詞列表。您可以從​​3210

得到nltk如果沒有與nltk任何問題,這裏是停止詞列表:

['i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', 'your', 'yours', 
'yourself', 'yourselves', 'he', 'him', 'his', 'himself', 'she', 'her', 'hers', 'herself', 
'it', 'its', 'itself', 'they', 'them', 'their', 'theirs', 'themselves', 'what', 'which', 
'who', 'whom', 'this', 'that', 'these', 'those', 'am', 'is', 'are', 'was', 'were', 'be', 
'been', 'being', 'have', 'has', 'had', 'having', 'do', 'does', 'did', 'doing', 'a', 'an', 
'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until', 'while', 'of', 'at', 'by', 'for', 
'with', 'about', 'against', 'between', 'into', 'through', 'during', 'before', 'after', 
'above', 'below', 'to', 'from', 'up', 'down', 'in', 'out', 'on', 'off', 'over', 'under', 
'again', 'further', 'then', 'once', 'here', 'there', 'when', 'where', 'why', 'how', 'all', 
'any', 'both', 'each', 'few', 'more', 'most', 'other', 'some', 'such', 'no', 'nor', 'not', 
'only', 'own', 'same', 'so', 'than', 'too', 'very', 's', 't', 'can', 'will', 'just', 'don', 
'should', 'now'] 

現在用這個簡單的代碼來獲取所需輸出:

from stemming.porter2 import stem 
from nltk.corpus import stopwords 
stopwords_ = stopwords.words('english') 
def addString(x): 
    flag = True 
    y = [stem(j).lower() for j in x.split() if j.lower() not in stopwords_] 
    for i in section: 
     i = [stem(j).lower() for j in i.split() if j.lower() not in stopwords_] 
     if y==i: 
     flag = False 
     break 
    if flag: 
     section.append(x) 
     print "\tNew Section Added" 

演示:

>>> from stemming.porter2 import stem 
>>> from nltk.corpus import stopwords 
>>> stopwords_ = stopwords.words('english') 
>>> 
>>> def addString(x): 
... flag = True 
... y = [stem(j).lower() for j in x.split() if j.lower() not in stopwords_] 
... for i in section: 
...  i = [stem(j).lower() for j in i.split() if j.lower() not in stopwords_] 
...  if y==i: 
...   flag = False 
...   break 
... if flag: 
...  section.append(x) 
...  print "\tNew Section Added" 
... 
>>> section = [ "Activity (Last 3 Days)", "Activity (Last 7 days)", "Executable running from disk", "Actions from File"] # initial Section list 
>>> addString("Activity (Last 30 Days)") 
    New Section Added 
>>> addString("Executables running from disk") 
>>> addString("Actions from a file") 
>>> section 
['Activity (Last 3 Days)', 'Activity (Last 7 days)', 'Executable running from disk', 'Actions from File', 'Activity (Last 30 Days)'] # Final section list 
+0

感謝這聽起來很有趣,但我無法訪問停用詞列表。它給我錯誤: –

+0

資源u'corpora/stopwords'找不到。請使用NLTK 下載來獲得資源:>>> nltk.download()搜索在 : - 'C:\\用戶\\ Unnati_Shukla/nltk_data' - 'C:\\ nltk_data' - 「d :\\ nltk_data ' - 'E:\\ nltk_data' - 'C:\\ Python27 \\ nltk_data' - 'C:\\ Python27 \\ LIB \\ nltk_data' - ' C:\\用戶\ \ Unnati_Shukla \\ AppData \\ Roaming \\ nltk_data' –

+1

我已經上傳了'nltk.stopwords'返回的停用詞列表。直接使用它,並從代碼中移除'from nltk.corpus import stopwords'和'stopwords_ = stopwords.words('english')'行。將'stopwords_'分配給我已上傳的列表... –

1

這種情況下,您可能會發現regular expressions有幫助。您可以使用re.sub()查找特定的子字符串並將其替換。它將搜索與正則表達式匹配的不重疊的,並使用指定的字符串重新對其進行修改。

import re #this will allow you to use regular expressions 

def modifyHeader(header): 
    #change the # of days to 30 
    modifiedHeader = re.sub(r"Activity (Last \d+ Days?)", "Activity (Last 30 Days)", header) 
    #add an s to "executable" 
    modifiedHeader = re.sub(r"Executable running from disk", "Executables running from disk", modifiedHeader) 
    #add "a" 
    modifiedHeader = re.sub(r"Actions from File", "Actions from a file", modifiedHeader) 

    return modifiedHeader 

r""raw strings這使得它更容易一點應付需要對正則表達式的\字符,\d任何數字字符相匹配,並+表示「1個或多個」。閱讀我上面鏈接的頁面以獲取更多信息。

+0

有很多章節標題因此不能爲單節提供條件。每次使用任務操作時,部分標題列表都會再次動態生成。 –

+0

感謝您的建議 –

+0

尋找模糊匹配/搜索算法。 – dom0