Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用NLTK获取单词的所有可能的词性标记_Python_Nltk_Part Of Speech - Fatal编程技术网

Python 使用NLTK获取单词的所有可能的词性标记

Python 使用NLTK获取单词的所有可能的词性标记,python,nltk,part-of-speech,Python,Nltk,Part Of Speech,某些单词可以有多个可能的词性(pos)标记。 “棍子”既是名词又是动词 NLTK中的pos标记器尝试根据上下文猜测正确的标记,并仅返回1个猜测。如何取而代之的是获得任何给定单词的所有可能标记的列表?TL;博士 否,不适用于默认的pos\u标签功能 长期 对于默认的pos\u标签功能,这是不可能的 默认的pos\u tag函数来自AveragedPerceptron对象,该对象使用predict()函数获取最可能的标记: 函数从可能的标记列表中返回argmax: def predict(self

某些单词可以有多个可能的词性(pos)标记。 “棍子”既是名词又是动词

NLTK中的pos标记器尝试根据上下文猜测正确的标记,并仅返回1个猜测。如何取而代之的是获得任何给定单词的所有可能标记的列表?

TL;博士 否,不适用于默认的
pos\u标签
功能


长期 对于默认的
pos\u标签
功能,这是不可能的

默认的
pos\u tag
函数来自
AveragedPerceptron
对象,该对象使用
predict()
函数获取最可能的标记:

函数从可能的标记列表中返回argmax:

def predict(self, features):
    '''Dot-product the features and current weights and return the best label.'''
    scores = defaultdict(float)
    for feat, value in features.items():
        if feat not in self.weights or value == 0:
            continue
        weights = self.weights[feat]
        for label, weight in weights.items():
            scores[label] += value * weight
    # Do a secondary alphabetic sort, for stability
    return max(self.classes, key=lambda label: (scores[label], label))
实际上,如果您更改代码,您可以通过让每个可能的标记返回
self.classes
来获得每个标记的分数

但是因为
tag()
中使用的功能需要前两个标记作为功能


返回n个最佳标记的任务必须将标记器简单的一个最佳“贪婪”特性更改为需要梁的特性

感谢@alvas的洞察力。检查一个单词所有不同POS标签的一种近似方法是从特定的wordnet版本数据库下载并搜索该单词。或者只需在线查看wordnet:最新版本。wordnet是详尽的。
def tag(self, tokens):
    '''
    Tag tokenized sentences.
    :params tokens: list of word
    :type tokens: list(str)
    '''
    prev, prev2 = self.START
    output = []

    context = self.START + [self.normalize(w) for w in tokens] + self.END
    for i, word in enumerate(tokens):
        tag = self.tagdict.get(word)
        if not tag:
            features = self._get_features(i, word, context, prev, prev2)
            tag = self.model.predict(features)
        output.append((word, tag))
        prev2 = prev
        prev = tag

    return output