【发布时间】:2020-02-01 08:06:04
【问题描述】:
我制作了一个 n-gram 提取器,可以从文本中提取组织的名称。但是,程序只提取第一个单词和最后一个单词的第一个字母。例如,如果短语"Sprint International Corporation" 出现在文本中,程序将返回"s corporation" 作为n-gram。你知道我做错了什么吗?我已经在下面发布了代码和输出。谢谢。
这是 n-gram 提取器的代码。
def org_ngram(classified_text):
orgs = [c for c in classified_text if (c[1]=="ORGANIZATION")]
#print(orgs)
combined_orgs = []
prev_org = False
new_org = ("", "ORGANIZATION")
for i in range(len(classified_text)):
if classified_text[i][1] != "ORGANIZATION":
prev_org = False
else:
if prev_org:
new_org = new_org[0] + " " + classified_text[i][0].lower()
else:
combined_orgs.append(new_org)
new_org = classified_text[i][0].lower()
prev_org = True
combined_orgs.append(new_org)
combined_orgs = combined_orgs[1:]
return combined_orgs
这是我分析的文本和我用来分析它的程序。
from nltk.tag import StanfordNERTagger
from nltk.tokenize import word_tokenize
st = StanfordNERTagger('C:\\path\\english.all.3class.distsim.crf.ser.gz',
'C:\\Users\\path\\stanford-ner.jar',
encoding='utf-8')
text = "Trump met with representatives from Sprint International Corporation, Nike Inc, and Wal-Mart Company regarding the trade war."
tokenized_text = word_tokenize(text)
classified_text = st.tag(tokenized_text)
orgs = org_ngram(classified_text)
print(orgs)
这是当前的输出。
['s corporation', 'n inc', 'w company']
这就是我想要输出的样子。
['sprint international corporation', 'nike inc', 'wal-mart company']
【问题讨论】: