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/