簡體   English   中英

您可以使用字典(文本)進行正則表達式標記化嗎?

[英]Can you use dictionary( text) to regex tokenization?

我想知道我們是否可以使用文本文件作為標記化的手段。 例如,假設有一個文件(字典),並且您想要標記化,請檢查第一個詞典以標記化。

例如:

Dict_list = [環境測試,蘋果貓,測試休息]

文字:環境測試是世界上最好的蘋果蘋果貓是測試的其余部分。

假設文本列表很大,而dict也很大,所以如果我們要標記化它會用空格標記化,但是我需要對整個文本進行標記化,但是我想檢查dict_list看看是否應該是一個標記。

因此令牌應為:

令牌:“ The”,“環境測試”,“ is”,“ the”,“ best apple”,“ in”,“ the”,“ world”,“ apple cat”,“ is”,“ the”,“測試休息”。

我希望這是有道理的。

先感謝您。

使用nltk.tokenize軟件包,您可以輕松地做到這一點。 例如:

>>> tokenizer.tokenize('Testing testing testing one two three'.split())
['Testing', 'testing', 'testing', 'one', 'two', 'three']

>>> tokenizer = MWETokenizer([('hors', "d'oeuvre")], separator='+')
>>> tokenizer.tokenize("An hors d'oeuvre tonight, sir?".split())
['An', "hors+d'oeuvre", 'tonight,', 'sir?']

這是一種解決方法:

Python3版本:

from nltk.tokenize import regexp_tokenize

sent = "I like apple fruit but grape fruit more"
dict_list = ["apple fruit", "grape fruit"]
newdict = {}
for item in dict_list:
    dk = item.replace(" ", "_")
    newdict[item] = dk

for key, val in newdict.items():
    if key in sent:
        sent = sent.replace(key, val)

res = regexp_tokenize(sent, pattern='\S+')
print(res)

輸出:

['I','like','apple_fruit','but','grape_fruit','more']

然后,您可以根據需要用空格替換所有下划線。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM