我刚开始学Python。我正在使用一个API来构建TFIDFs模型,但是我遇到了一些无法解决的lambda函数错误。这是生成TFIDF的类的一部分:
class tfidf(ModelBuilder, Model):
def __init__(self, max_ngram=1, normalize = True):
self.max_ngram = max_ngram
self.normalize = normalize
def build(self, mentions, idfs):
m = mentions\
.map(lambda (target, (span, text)): (target, text))\ """error is triggered here """
.mapValues(lambda v: ngrams(v, self.max_ngram))\
.flatMap(lambda (target, tokens): (((target, t), 1) for t in tokens))\
.reduceByKey(add)\
.map(lambda ((target, token), count): (token, (target, count)))\
.leftOuterJoin(idfs)\
下面是mentions
类的输出示例(这是导致tdfidf
类出错的输入):
错误消息是:
.map(lambda (target, (span, text)): (target, text))\
ValueError: too many values to unpack
我尝试了:.map(lambda ( src, target, span, text) : (target, text))\
,因为我只需要在mentions\
中引起相同错误的目标和文本。在
一个简单且可编译的示例:
import math
import numpy
Data = [{'_id': '333981',
'source': 'Apple',
'span': (100, 119),
'text': ' It is native to the northern Pacific.'}, {'_id': '27262',
'source': 'Apple',
'span': (4, 20),
'text': ' Apples are yummy.'}]
m = map(lambda (ID, (span, text)) : (ID, text) , Data)
print(list(m))
我使用的是python2.7。任何帮助或指导将不胜感激。在
非常感谢
如果要创建一个只包含
source
和text
字段的新字典数组,可以使用如果要为键
^{pr2}$source
和text
创建一个包含内容的元组数组:相关问题 更多 >
编程相关推荐