除非 POS 显式,否则 WordNetLemmatizer 不会返回正确的引理 - Python NLTK

2024-05-18

我正在对 Ted 数据集成绩单进行词形还原。我注意到一些奇怪的事情: 并非所有单词都被词形还原。要说的是,

selected -> select

哪个是对的。

然而,involved !-> involve and horsing !-> horse除非我明确输入“v”(动词)属性。

在 python 终端上,我得到了正确的输出,但在我的终端中却没有得到正确的输出code http://pastebin.com/midgUJ02:

>>> from nltk.stem import WordNetLemmatizer
>>> from nltk.corpus import wordnet
>>> lem = WordNetLemmatizer()
>>> lem.lemmatize('involved','v')
u'involve'
>>> lem.lemmatize('horsing','v')
u'horse'

代码的相关部分是这样的:

for l in LDA_Row[0].split('+'):
    w=str(l.split('*')[1])
    word=lmtzr.lemmatize(w)
    wordv=lmtzr.lemmatize(w,'v')
    print wordv, word
    # if word is not wordv:
    #   print word, wordv

整个代码是here http://pastebin.com/midgUJ02.

问题是什么?


如果您使用默认设置,则词形还原器需要正确的 POS 标签才能准确WordNetLemmatizer.lemmatize(),默认标签是名词,参见https://github.com/nltk/nltk/blob/develop/nltk/stem/wordnet.py#L39 https://github.com/nltk/nltk/blob/develop/nltk/stem/wordnet.py#L39

要解决该问题,请始终在词形还原之前对数据进行 POS 标记,例如

>>> from nltk.stem import WordNetLemmatizer
>>> from nltk import pos_tag, word_tokenize
>>> wnl = WordNetLemmatizer()
>>> sent = 'This is a foo bar sentence'
>>> pos_tag(word_tokenize(sent))
[('This', 'DT'), ('is', 'VBZ'), ('a', 'DT'), ('foo', 'NN'), ('bar', 'NN'), ('sentence', 'NN')]
>>> for word, tag in pos_tag(word_tokenize(sent)):
...     wntag = tag[0].lower()
...     wntag = wntag if wntag in ['a', 'r', 'n', 'v'] else None
...     if not wntag:
...             lemma = word
...     else:
...             lemma = wnl.lemmatize(word, wntag)
...     print lemma
... 
This
be
a
foo
bar
sentence

请注意“is -> be”,即

>>> wnl.lemmatize('is')
'is'
>>> wnl.lemmatize('is', 'v')
u'be'

用你的例子中的词来回答这个问题:

>>> sent = 'These sentences involves some horsing around'
>>> for word, tag in pos_tag(word_tokenize(sent)):
...     wntag = tag[0].lower()
...     wntag = wntag if wntag in ['a', 'r', 'n', 'v'] else None
...     lemma = wnl.lemmatize(word, wntag) if wntag else word
...     print lemma
... 
These
sentence
involve
some
horse
around

请注意,WordNetLemmatizer 有一些怪癖:

  • python 中的 wordnet 词形还原和词性标记 https://stackoverflow.com/questions/15586721/wordnet-lemmatization-and-pos-tagging-in-python
  • Python NLTK 使用 wordnet 对单词“further”进行词形化 https://stackoverflow.com/questions/22999273/python-nltk-lemmatization-of-the-word-further-with-wordnet

此外,NLTK 的默认词性标注器正在进行一些重大更改以提高准确性:

  • Python NLTK pos_tag 未返回正确的词性标记 https://stackoverflow.com/questions/30821188/python-nltk-pos-tag-not-returning-the-correct-part-of-speech-tag
  • https://github.com/nltk/nltk/issues/1110 https://github.com/nltk/nltk/issues/1110
  • https://github.com/nltk/nltk/pull/1143 https://github.com/nltk/nltk/pull/1143

对于词形还原器的开箱即用/现成解决方案,您可以看看https://github.com/alvations/pywsd https://github.com/alvations/pywsd以及我如何进行一些 try- excepts 来捕获 WordNet 中不存在的单词,请参阅https://github.com/alvations/pywsd/blob/master/pywsd/utils.py#L66 https://github.com/alvations/pywsd/blob/master/pywsd/utils.py#L66

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

除非 POS 显式,否则 WordNetLemmatizer 不会返回正确的引理 - Python NLTK 的相关文章

  • 如何使用 cython 编译扩展?

    我正在尝试从示例页面编译一个简单的 cython 扩展here http docs cython org src userguide tutorial html在我安装了 Python 2 6 64 位版本的 Windows 7 64 位计
  • 在 Pandas 中按日期获取有效合约

    我在检测 pandas DataFrame 中的活动合约方面遇到了一些困难 假设每一行都是一个协商 对于每一行 我有两列 initial date 和 end date 我想知道的是按日期划分的活跃合约数量 到目前为止我做了一个非常低效的方
  • Django:如何测试“HttpResponsePermanentRedirect”

    我正在为我的 django 应用程序编写一些测试 在我看来 它使用 HttpResponseRedirect 重定向到其他一些网址 那么我该如何测试呢 姜戈TestCase类有一个方法assertRedirects https docs d
  • 从正在运行的 python 脚本检测优化标志是否为 -O 或 -OO

    有时我想生成一个子进程 其优化标志与启动父进程时使用的优 化标志相同 我可以使用类似的东西 optimize not debug 但这样我就可以匹配两者 O and OO flags 是否有一些 python 内部状态包含该信息 经过一番深
  • 在Python3.6中调用C#代码

    由于完全不了解 C 编码 我希望在我的 python 代码中调用 C 函数 我知道有很多关于同一问题的问答 但由于一些奇怪的原因 我无法从示例 python 模块导入简单的 c 类库 以下是我所做的事情 C 类库设置 我使用的是 VS 20
  • on_delete=models.PROTECT 和 on_delete=models.CASCADE 在 Django 模型上有什么作用?

    我对 Django 很熟悉 但最近注意到有一个on delete models CASCADE and on delete models PROTECT模型的选项 on delete models CASCADE and on delete
  • PIL Image.size 返回相反的宽度/高度

    使用PIL确定图像的宽度和高度 在特定图像上 幸运的是只有这一个 但这很麻烦 从 image size 返回的宽度 高度是相反的 图片 http storage googleapis com cookila 533ebf752b9d1f7c
  • 错误:permission_manager_qt.cpp(82) 不支持的权限类型:13

    我正在开发具有内置浏览器功能的 python 代码 PyQt 5 13 import sys from PyQt5 QtCore import from PyQt5 QtGui import from PyQt5 QtWidgets imp
  • Python igraph:从图中删除顶点

    我正在使用安然电子邮件数据集 并尝试删除没有 enron com 的电子邮件地址 即我只想拥有安然电子邮件 当我尝试删除那些没有 enron com 的地址时 一些电子邮件由于某些原因被跳过 下面显示了一个小图 其中顶点是电子邮件地址 这是
  • 定义函数后对其进行修饰?

    I think答案是否定的 但我似乎找不到明确的说法 我有以下情况 def decorated function function functools wraps function def my function print Hello s
  • “char”/“character”类型的类型提示

    char 或 character 没有内置的原始类型 因此显然必须使用长度为 1 的字符串 但是为了暗示这一点并暗示它应该被视为一个字符 如何通过类型提示来实现这一点 grade chr A 一种方法可能是使用内置的 chr 函数来表示这一
  • 创建一个类似于 Tkinter 的表

    我希望创建类似于 Tkinter 中的表格的东西 但它不一定是这样的 例如 我想创建标题 Name1 Name2 Value 并在每个标题下面有几个空白行 然后 我希望稍后用我计算的值或名称的字符串值填充这些行 因此是标签 对于 Name2
  • spacy 如何使用词嵌入进行命名实体识别 (NER)?

    我正在尝试使用以下方法训练 NER 模型spaCy识别位置 人 名和组织 我试图理解如何spaCy识别文本中的实体 但我无法找到答案 从这个问题 https github com explosion spaCy issues 491在 Gi
  • 如何使用Python的super()来更新父值?

    我对继承很陌生 之前所有关于继承和 Python 的 super 函数的讨论都有点超出我的理解 我当前使用以下代码来更新父对象的值 usr bin env python test py class Master object mydata
  • 在可编辑的QSqlQueryModel中实现setEditStrategy

    这是后续这个问题 https stackoverflow com questions 49752388 editable qtableview of complex sql query 在那里 我们创建了 QSqlQueryModel 的可
  • select() 可以在 Windows 下使用 Python 中的文件吗?

    我正在尝试在 Windows 下运行以下 python 服务器 An echo server that uses select to handle multiple clients at a time Entering any line o
  • Scipy 稀疏 Cumsum

    假设我有一个scipy sparse csr matrix代表下面的值 0 0 1 2 0 3 0 4 1 0 0 2 0 3 4 0 我想就地计算非零值的累积和 这会将数组更改为 0 0 1 3 0 6 0 10 1 0 0 3 0 6
  • 如何在sphinx中启用数学?

    我在用sphinx http sphinx pocoo org index html与pngmath http sphinx pocoo org ext math html module sphinx ext pngmath扩展来记录我的代
  • 检查字符串是否只有字母和空格 - Python

    试图让 python 返回一个字符串仅包含字母和空格 string input Enter a string if all x isalpha and x isspace for x in string print Only alphabe
  • 如何将列表字典写入字符串而不是 CSV 文件?

    This 堆栈溢出问题 https stackoverflow com questions 37997085 how to write a dictionary of lists to a csv file将列表字典写入 CSV 文件的答案

随机推荐