python - 尝试在 nltk_contrib.timex 中使用 ground 方法时出现错误

标签 python nltk

import datetime
from nltk_contrib import timex

now = datetime.date.today()
basedate = timex.Date(now.year, now.month, now.day)

print timex.ground(timex.tag("Hai i would like to go to mumbai 22nd of next month"), basedate)

print str(datetime.date.day)

当我尝试运行上面的代码时,出现以下错误

File "/usr/local/lib/python2.7/dist-packages/nltk_contrib/timex.py", line 250, in ground
    elif re.match(r'last ' + month, timex, re.IGNORECASE):
UnboundLocalError: local variable 'month' referenced before assignment

我应该怎么做才能纠正这个错误?

最佳答案

timex 模块存在一个错误,即在 ground 函数中未赋值的情况下引用了全局变量。

要修复该错误,请添加以下代码,该代码应从第 171 行开始:

def ground(tagged_text, base_date):

# Find all identified timex and put them into a list
timex_regex = re.compile(r'<TIMEX2>.*?</TIMEX2>', re.DOTALL)
timex_found = timex_regex.findall(tagged_text)
timex_found = map(lambda timex:re.sub(r'</?TIMEX2.*?>', '', timex), \
            timex_found)

# Calculate the new date accordingly
for timex in timex_found:
    global month # <--- here is the global reference assignment

关于python - 尝试在 nltk_contrib.timex 中使用 ground 方法时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41850809/

相关文章:

python - scikit-optimize 中 cv_results_ 和 best_score_ 中的测试分数是如何计算的?

python - 从 C 到 Python

Python:连接字符串并在它们之间插入字符串的最简单方法?

python - 使用 nltk pos 标记器时出现错误的 zip 文件错误

python - 根据自定义字典对字符串进行分词

python - 在 matplotlib 中创建一个离散的颜色条

python - 使用 ruamel.yaml 更新包含多个 yaml 的 yaml 文件中的 yaml block

python - 如何从文本文件创建自己的 NLTK 文本?

python - NLTK 无法打开文件(UnicodeDecoreError)

python ,nltk : Cannot import mkdtemp