首页 > 解决方案 > 如何在没有预定义的多词列表的情况下识别句子中的多词表达

问题描述

我正在尝试识别句子中的每个多词表达并标记该句子。例如,示例输入语句是“总之,这个商品供不应求”。我希望输出如下所示:

['In short', ',', 'this', 'merchandise', 'is', 'in short supply', '.']

我已经通过使用预定义列表和以下 python 代码实现了上述结果。

from nltk import sent_tokenize, word_tokenize
from nltk.tokenize import MWETokenizer

multiwordExpressionList = [("In", "short"), ("in", "short", "supply" )]  ## this is a predefined list 
sentence = "In short, this merchandise is in short supply."

mwe = MWETokenizer(multiwordExpressionList, separator = ' ')
resultList = mwe.tokenize(word_tokenize(sentence))
print(resultList)

但是,缺点很明显。这个程序需要一个预定义的多词表达列表来识别句子中是否存在任何多词表达。是否有任何建议的 python 包、模块或方法可以识别句子中存在的任何多词表达式?

标签: pythonnlp

解决方案


您有 2 个选择:要么使用一些现有模型(例如,也支持 MWE https://stanfordnlp.github.io/stanza/mwt.html的 Stanza ),或者如果您有足够的数据构建自己的 MWE 模型,例如使用 Gensim:https ://radimrehurek.com/gensim/models/phrases.html。我已经相当成功地使用了第二个选项。


推荐阅读