2017-09-05 24 views
4

我有一個線串的:如何行文本轉換成有意義的話

"specificationsinaccordancewithqualityaccreditedstandards" 

需要被分成標記化的詞語,如:

"specifications in accordance with quality accredited standards" 

我已經試過nltkword_tokenize但它不能轉換,

上下文:我解析PDF文檔到文本文件,這是t ext我從pdf轉換器回來,將pdf轉換成文本我在Python

+0

是否有其他PDF轉換器,你可以試試嗎?它不應該像所有這些詞彙一起幹擾。 – sniperd

+3

你很可能會遇到模棱兩可的問題。例如:是字符串「特定」中的第一個單詞(後跟「at」和「ion」,是唯一的,有效的單詞)還是「規範」? – Zinki

+0

你是否試圖通過搜索字典中的所有單詞強制你的方式?很確定你可以在英文字典中找到一個包含所有單詞的圖書館。 – semicolon

回答

2

您可以使用遞歸來解決此問題。首先,你要下載一本字典的txt文件,你可以在這裏:https://github.com/Ajax12345/My-Python-Projects/blob/master/the_file.txt

dictionary = [i.strip('\n') for i in open('the_file.txt')] 
def get_options(scrambled, flag, totals, last): 
    if flag: 
     return totals 

    else: 
     new_list = [i for i in dictionary if scrambled.startswith(i)] 
     if new_list: 

      possible_word = new_list[-1] 
      new_totals = totals 
      new_totals.append(possible_word) 
      new_scrambled = scrambled[len(possible_word):] 
      return get_options(new_scrambled, False, new_totals, possible_word) 

     else: 
      return get_options("", True, totals, '') 


s = "specificationsinaccordancewithqualityaccreditedstandards" 
print(' '.join(get_options(s, False, [], ''))) 

輸出:

'specifications in accordance with quality accredited standards' 
+0

這就是我正在尋找,謝謝,也字典也可以動態以及包含我們已經找到的單詞 –

3

您可以使用trie。 A trie是允許單詞驗證的數據結構。
它是一棵樹,您可以在其中導航分支以獲取有效的前綴,並在您打滿整個世界時收到通知。

雖然我從來沒有用過「具體」,但我發現這個python實現,datrie

我的想法是導入datrie,用它從txt字典(例如here)生成trie,然後解析字符串。當您在trie中找到匹配項時,每個字符都會讀取一個字符,如果您沒有找到合適的單詞,請將其添加到拆分字符串中。

你可以找到更多的triehere on wikipediain this video(這是誰教我什麼trie是)。