如何将句子分成相关词(术语提取)?

2024-06-08 14:45:22 发布

您现在位置:Python中文网/ 问答频道 /正文

有没有NLP python库可以拆分句子或将单词连接成相关的单词对?例如:

That is not bad example -> "That" "is" "not bad" "example"

“不坏”和“好”的意思一样,所以在机器学习中把它当作“不”和“坏”处理是没有用的。 我甚至不知道如何称呼这些相关的词对。(术语提取?相位提取?) 或者用名词分成形容词更好,例如:

dishonest media relating about tax cuts -> "dishonest media", "relating", "about", "tax cuts"

我发现了白芷提取物但它不适用于Python3。在


Tags: 机器thatnlpisexamplenot单词media
2条回答

签出Spacy library(参见链接)。在

它没有现成的功能,因为您需要构建规则,但是规则是非常可读的,您可以输入许多选项(POS标记、regex、lemma或它们的任何组合,等等)

特别值得注意的是PhraseMarker()class的部分。在

直接从文档中复制的是一个代码示例:

import spacy
from spacy.matcher 
import PhraseMatcher

nlp = spacy.load('en')
matcher = PhraseMatcher(nlp.vocab)
terminology_list = ['Barack Obama', 'Angela Merkel', 'Washington, D.C.']
patterns = [nlp(text) for text in terminology_list]
matcher.add('TerminologyList', None, *patterns)

doc = nlp(u"German Chancellor Angela Merkel and US President Barack Obama "
          u"converse in the Oval Office inside the White House in Washington, D.C.")
matches = matcher(doc)

要从句子流中自动检测常见短语,我建议您检查Gensim Phrase (collocation) detection

这是一个很好的例子:

bigram = Phraser(phrases)

sent = [u'the', u'mayor', u'of', u'new', u'york', u'was', u'there']

print(bigram[sent])

Output: [u'the', u'mayor', u'of', u'new_york', u'was', u'there']

相关问题 更多 >