我有以下功能,在一个句子中标记不同的短语,例如:
在运行以下代码时,我收到了一条错误消息:
sentence=listw1 w2 ..。关于w^SyntaxError中的单词:无效语法
def features(sentence, index):
sentence=list[w1, w2, ...]
index= w[index] for i in w
return {
'word': sentence[index],
'prefix-1': "due to/due" in sentence[index],
'prefix-2': "other" in sentence[index],
'prefix-3': "with" in sentence[index],
'prefix-4': "without" in sentence[index],
'is_numeric': sentence[index].isdigit(),
'prev_word': '' if index == 0 else sentence[index - 1],
'next_word': '' if index == len(sentence) - 1 else sentence[index + 1],
} 发布于 2018-08-06 11:51:18
抱歉,还不能发表评论。
这听起来很像家务活:p我不确定我是否理解你的问题/问题,但有一件事我绝对注意到了。可能是,这个'word': 'c'}是您意想不到的结果吗?正如features的docstring所显示的那样,您需要用一个列表来调用它。
尝试:features(['cholera', 'due', 'virus', 'A', '890'], 0)
或者:features('cholera due virus A 890'.split(), 0),您可以使用split方法将字符串拆分为以空格作为分隔符的列表。
l ='cholera due virus A 890'.split()
l == ['cholera', 'due', 'virus', 'A', '890'] # is Truehttps://stackoverflow.com/questions/51706137
复制相似问题