nlp提取html文件中的文字,nlp – 如何从NLTK中的文本中提取关系

该博客讨论了如何从文本中提取人物与组织的关系,如'Michael James'是'Publishers Weekly'的编辑。使用nltk库进行词性标注和命名实体识别,通过正则表达式匹配模式来定位关系。作者遇到问题无法正确输出结果,寻求帮助以理解nltk的`extract_rels`函数期待的输入格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

嗨,我正在尝试根据第二个最后一个例子从一串文本中提取关系:

https://web.archive.org/web/20120907184244/http://nltk.googlecode.com/svn/trunk/doc/howto/relextract.html

从诸如“出版商周刊的迈克尔詹姆斯编辑”这样的字符串中,我想要的结果是输出如下:

[PER: ‘Michael James’] ‘, editor of’ [ORG: ‘Publishers Weekly’]

这样做的最佳方法是什么? extract_rels期望什么格式以及如何格式化输入以满足该要求?

试图自己做,但它没有用.

这是我从本书改编的代码.我没有打印任何结果.我究竟做错了什么?

class doc():

pass

doc.headline = ['this is expected by nltk.sem.extract_rels but not used in this script']

def findrelations(text):

roles = """

(.*(

analyst|

editor|

librarian).*)|

researcher|

spokes(wo)?man|

writer|

,\sof\sthe?\s* # "X, of (the) Y"

"""

ROLES = re.compile(roles, re.VERBOSE)

tokenizedsentences = nltk.sent_tokenize(text)

for sentence in tokenizedsentences:

taggedwords = nltk.pos_tag(nltk.word_tokenize(sentence))

doc.text = nltk.batch_ne_chunk(taggedwords)

print doc.text

for rel in relextract.extract_rels('PER', 'ORG', doc, corpus='ieer', pattern=ROLES):

print relextract.show_raw_rtuple(rel) # doctest: +ELLIPSIS

text =”Michael James editor of Publishers Weekly”

findrelations(text)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值