本文整理汇总了Python中lexicon.Lexicon.lexEvalSentence方法的典型用法代码示例。如果您正苦于以下问题:Python Lexicon.lexEvalSentence方法的具体用法?Python Lexicon.lexEvalSentence怎么用?Python Lexicon.lexEvalSentence使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类lexicon.Lexicon
的用法示例。
在下文中一共展示了Lexicon.lexEvalSentence方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from lexicon import Lexicon [as 别名]
# 或者: from lexicon.Lexicon import lexEvalSentence [as 别名]
class Feature:
def __init__(self, kind, name, actionName, fields, radius, cutoff, options):
self.name = name
self.kind = kind
self.actionName = actionName
self.fields = fields
self.radius = int(radius)
self.cutoff = int(cutoff)
self.options = options
if kind == "lex":
if self.options != {}:
sys.stderr.write("Lexicon features do not yet support options")
sys.exit(-1)
self.lexicon = Lexicon(actionName)
elif kind in ("token", "sentence"):
if actionName not in features.__dict__:
sys.stderr.write("Unknown operator named " + actionName + "\n")
sys.exit(-1)
self.function = features.__dict__[actionName]
else:
assert False
def evalSentence_Token(self, sentence):
featVec = []
for pos, word in enumerate(sentence):
fieldVec = [word[field] for field in self.fields]
if self.options != {}:
fieldVec += [self.options]
feat = self.function(*fieldVec)
featVec.append(feat)
return featVec
def evalSentence_Lex(self, sentence):
assert len(self.fields) == 1
field = self.fields[0]
wordList = [word[field] for word in sentence]
return self.lexicon.lexEvalSentence(wordList)
def evalSentence_Sentence(self, sentence):
if self.options == {}:
return self.function(sentence, self.fields)
else:
# print; '@'
return self.function(sentence, self.fields, self.options)
def evalSentence(self, sentence):
if self.kind == "token":
featVec = self.evalSentence_Token(sentence)
elif self.kind == "lex":
featVec = self.evalSentence_Lex(sentence)
elif self.kind == "sentence":
featVec = self.evalSentence_Sentence(sentence)
return self.multiplyFeatures(sentence, featVec)
def multiplyFeatures(self, sentence, featVec):
multipliedFeatVec = []
for c, word in enumerate(sentence):
multipliedFeatVec.append([])
for i in range(-self.radius, self.radius + 1):
pos = c + i
if pos < 0 or pos > len(sentence) - 1:
continue
currentWordFeatures = [feat for feat in featVec[pos] if feat != 0]
for feat in currentWordFeatures:
multipliedFeatVec[c].append(str(i) + "_" + self.name + "=" + str(feat))
return multipliedFeatVec