2017-06-06 147 views
1

有沒有一種方法可以使用NLTK獲得單字母字符串的一組可能的詞性,並考慮到不同詞可能具有同音異義詞?NLTK單詞詞性標註

例如:報告 - > {名詞,動詞},善良 - > {形容詞,名詞}

我一直沒能找到一個POS-標記者標記部分的語音單詞之外完整句子的背景。這似乎是NLTK的一個非常基本的要求,所以我很困惑爲什麼我很難找到它。

回答

1

是的。最簡單的方法不是使用標記器,而是簡單地加載一個或多個語料庫並收集您感興趣的單詞的所有標記的集合。如果您對多個單詞感興趣,最簡單的方法是收集標記對於語料庫中的所有單詞,然後查找任何你想要的東西。我會加上頻率計數,只是因爲我可以。例如,使用Brown語料庫和簡單的「通用」標記集:

>>> wordtags = nltk.ConditionalFreqDist((w.lower(), t) 
     for w, t in nltk.corpus.brown.tagged_words(tagset="universal")) 
>>> wordtags["report"] 
FreqDist({'NOUN': 135, 'VERB': 39}) 
>>> list(wordtags["kind"]) 
['ADJ', 'NOUN'] 
2

因爲POS模型是基於句子/文檔的數據進行訓練的,所以對預先訓練的模型的預期輸入是句子/文檔。當只有一個單詞時,它將其視爲單個單詞句子,因此在單個單詞句子上下文中應該只有一個標籤。

如果您想要查找每個英語單詞的所有可能的POS標籤,您需要一個包含多個不同用法的語料庫,然後標記該語料庫並計算/提取否。每個詞的標籤。例如。

>>> from nltk import pos_tag 
>>> sent1 = 'The coaches are going from Singapore to Frankfurt' 
>>> sent2 = 'He coaches the football team' 
>>> pos_tag(sent1.split()) 
[('The', 'DT'), ('coaches', 'NNS'), ('are', 'VBP'), ('going', 'VBG'), ('from', 'IN'), ('Singapore', 'NNP'), ('to', 'TO'), ('Frankfurt', 'NNP')] 
>>> pos_tag(sent2.split()) 
[('He', 'PRP'), ('coaches', 'VBZ'), ('the', 'DT'), ('football', 'NN'), ('team', 'NN')] 

>>> from collections import defaultdict, Counter 
>>> counts = defaultdict(Counter) 
>>> tagged_sents = [pos_tag(sent) for sent in [sent1.split(), sent2.split()]] 

>>> for word, pos in chain(*tagged_sents): 
...  counts[word][pos] += 1 
... 

>>> counts 
defaultdict(<class 'collections.Counter'>, {'from': Counter({'IN': 1}), 'to': Counter({'TO': 1}), 'Singapore': Counter({'NNP': 1}), 'football': Counter({'NN': 1}), 'coaches': Counter({'VBZ': 1, 'NNS': 1}), 'going': Counter({'VBG': 1}), 'are': Counter({'VBP': 1}), 'team': Counter({'NN': 1}), 'The': Counter({'DT': 1}), 'Frankfurt': Counter({'NNP': 1}), 'the': Counter({'DT': 1}), 'He': Counter({'PRP': 1})}) 

>>> counts['coaches'] 
Counter({'VBZ': 1, 'NNS': 1}) 

另外,還有共發現:

>>> from nltk.corpus import wordnet as wn 
>>> wn.synsets('coaches') 
[Synset('coach.n.01'), Synset('coach.n.02'), Synset('passenger_car.n.01'), Synset('coach.n.04'), Synset('bus.n.01'), Synset('coach.v.01'), Synset('coach.v.02')] 
>>> [ss.pos() for ss in wn.synsets('coaches')] 
[u'n', u'n', u'n', u'n', u'n', u'v', u'v'] 
>>> Counter([ss.pos() for ss in wn.synsets('coaches')]) 
Counter({u'n': 5, u'v': 2}) 

但是請注意,共發現是人工製作的資源,所以你不能指望每一個英語單詞是在它。