当前位置: 首页>>代码示例>>Python>>正文


Python Lexicon.lexEvalSentence方法代码示例

本文整理汇总了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
开发者ID:xuanhan863,项目名称:HunTag,代码行数:74,代码来源:feature.py


注:本文中的lexicon.Lexicon.lexEvalSentence方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。