I am trying to use the StanfordNERTagger and nltk to extract keywords from a piece of text.
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
this gives me
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')]
so clearly, things like Short
and Term
were tagged as NNP
. The data that i have contains many such instances where non NNP
words are capitalized. This might be due to typos or maybe they are headers. I dont have much control over that.
How can i parse or clean up the data so that i can detect a non NNP
term even though it may be capitalized? I dont want terms like Short
and Term
to be categorized as NNP
Also, not sure why John Donk
was captured as a person but Brian Jones
was not. Could it be due to the other capitalized non NNP
s in my data? Could that be having an effect on how the StanfordNERTagger
treats everything else?
Update, one possible solution
Here is what i plan to do
- Take each word and convert to lower case
- Tag the lowercase word
- If the tag is
NNP
then we know that the original word must also be anNNP
- If not, then the original word was mis-capitalized
Here is what i tried to do
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
but this gives me error
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
i have tried multiple approaches but some error always shows up. How can i Tag a single word?
I dont want to convert the whole string to lower case and then Tag. If i do that, the StanfordPOSTagger
returns an empty string