我正在嘗試使用StanfordNERTagger和nltk從一段文本中提取關鍵字。nltk StanfordNERTagger:如何獲得沒有大寫的專有名詞
docText="John Donk works for POI. Brian Jones wants to meet with Xyz Corp. for measuring POI's Short Term performance Metrics."
words = re.split("\W+",docText)
stops = set(stopwords.words("english"))
#remove stop words from the list
words = [w for w in words if w not in stops and len(w) > 2]
str = " ".join(words)
print str
stn = StanfordNERTagger('english.all.3class.distsim.crf.ser.gz')
stp = StanfordPOSTagger('english-bidirectional-distsim.tagger')
stanfordPosTagList=[word for word,pos in stp.tag(str.split()) if pos == 'NNP']
print "Stanford POS Tagged"
print stanfordPosTagList
tagged = stn.tag(stanfordPosTagList)
print tagged
這給了我
John Donk works POI Brian Jones wants meet Xyz Corp measuring POI Short Term performance Metrics
Stanford POS Tagged
[u'John', u'Donk', u'POI', u'Brian', u'Jones', u'Xyz', u'Corp', u'POI', u'Short', u'Term']
[(u'John', u'PERSON'), (u'Donk', u'PERSON'), (u'POI', u'ORGANIZATION'), (u'Brian', u'ORGANIZATION'), (u'Jones', u'ORGANIZATION'), (u'Xyz', u'ORGANIZATION'), (u'Corp', u'ORGANIZATION'), (u'POI', u'O'), (u'Short', u'O'), (u'Term', u'O')]
這麼清楚,像Short
和Term
被標記爲NNP
。我所擁有的數據包含很多這樣的情況,其中非NNP
單詞大寫。這可能是由於錯別字或者他們是頭文件。我沒有太多的控制權。
如何解析或清理數據,以便我可以檢測到非NNP
條款,即使它可能是大寫字母? 我不希望像Short
和Term
條款被歸類爲NNP
而且,不知道爲什麼John Donk
被抓獲的人,但Brian Jones
沒有。是否可以歸因於我的數據中的其他大寫非NNP
?這可能會對StanfordNERTagger
如何處理所有事情產生影響嗎?
更新,一個可能的解決方案
這是我打算做
- 每次取字,並轉換爲小寫
- 標籤小寫字
- 如果標籤
NNP
那麼我們知道原始單詞也必須是NNP
- 如果不是,那麼或者iginal詞被誤資本
這是我試圖做
str = " ".join(words)
print str
stp = StanfordPOSTagger('english-bidirectional-distsim.tagger')
for word in str.split():
wl = word.lower()
print wl
w,pos = stp.tag(wl)
print pos
if pos=="NNP":
print "Got NNP"
print w
,但是這給了我錯誤
John Donk works POI Jones wants meet Xyz Corp measuring POI short term performance metrics
john
Traceback (most recent call last):
File "X:\crp.py", line 37, in <module>
w,pos = stp.tag(wl)
ValueError: too many values to unpack
我曾嘗試多種方法,但有些錯誤總是顯示出來。 我如何標記一個單詞?
我不想將整個字符串轉換爲小寫,然後標記。如果我這樣做,StanfordPOSTagger
返回一個空字符串
非常感謝您的幫助:)作爲跟進,什麼POS是英語中常用的專有名詞? – AbtPst
從Penntree Bank標記集:'NNP'和'NNPS'(請參閱https://www.ling.upenn.edu/courses/Fall_2003/ling001/penn_treebank_pos.html) – alvas
正確,但是在給定的文本中POS標籤可能在專有名詞周圍?有這種可能嗎? – AbtPst