这是我之前提问的跟进问题。我正在使用nltk来解析人物、组织和它们之间的关系。使用这个例子,我能够创建人物和组织的块;然而,在nltk.sem.extract_rel命令中,我遇到了一个错误:
AttributeError: 'Tree' object has no attribute 'text'
以下是完整的代码:
import nltk
import re
#billgatesbio from http://www.reuters.com/finance/stocks/officerProfile?symbol=MSFT.O&officerId=28066
with open('billgatesbio.txt', 'r') as f:
sample = f.read()
sentences = nltk.sent_tokenize(sample)
tokenized_sentences = [nltk.word_tokenize(sentence) for sentence in sentences]
tagged_sentences = [nltk.pos_tag(sentence) for sentence in tokenized_sentences]
chunked_sentences = nltk.batch_ne_chunk(tagged_sentences)
# tried plain ne_chunk instead of batch_ne_chunk as given in the book
#chunked_sentences = [nltk.ne_chunk(sentence) for sentence in tagged_sentences]
# pattern to find <person> served as <title> in <org>
IN = re.compile(r'.+\s+as\s+')
for doc in chunked_sentences:
for rel in nltk.sem.extract_rels('ORG', 'PERSON', doc,corpus='ieer', pattern=IN):
print nltk.sem.show_raw_rtuple(rel)
这个例子与书中给出的例子非常相似,但是它使用了准备好的“分析文档”,这个概念突然出现,我不知道在哪里可以找到它的对象类型。我已经在git库中搜索过了,任何帮助都会被赞赏。
我的最终目标是提取一些公司的人员、组织和职称(日期),然后创建人员和组织的网络图。