gpt4 book ai didi

python - NLTK 中单个单词的标记器

转载 作者:行者123 更新时间:2023-11-30 23:13:46 25 4
gpt4 key购买 nike

是否有一个标记器可以在任何上下文中为单词返回单个标记?

我的要求是我需要从非结构化文本中提取单词,其中句子没有结构化语法。

词性标注器旨在处理句子,并根据该句子中单词的上下文返回单词的标签。因此,我要么必须使用另一个标记器,每次都会为特定单词提供相同的标记,要么在分块时使用单词的所有可能标记。

任何其他解决方案将不胜感激。另外,如何查看可以为特定单词分配的所有标签?

最佳答案

参见:http://www.nltk.org/_modules/nltk/tag.html

特别是:

>>> from nltk.corpus import brown
>>> from nltk.tag import UnigramTagger
>>> tagger = UnigramTagger(brown.tagged_sents(categories='news')[:500])
>>> sent = ['Mitchell', 'decried', 'the', 'high', 'rate', 'of', 'unemployment']
>>> for word, tag in tagger.tag(sent):
... print(word, '->', tag)
Mitchell -> NP
decried -> None
the -> AT
high -> JJ
rate -> NN
of -> IN
unemployment -> None

UnigramTagger 的想法是,它总是为训练语料库中的特定单词分配最突出的标签。或者(就在文档中的代码片段上方:

This package defines several taggers, which take a token list (typically a sentence), assign a tag to each token, and return the resulting list of tagged tokens. Most of the taggers are built automatically based on a training corpus. For example, the unigram tagger tags each word w by checking what the most frequent tag for w was in a training corpus:

不确定是否有内置方法来查看可以分配给特定单词的所有标签。而且;理论上这可能与识别的标签总数一样长,因为它取决于上下文。如果你想得到一个想法;我要做的就是标记您的整个词汇表,并使用该特定语料库中分配的所有不同标签打印出您的词汇表。

关于python - NLTK 中单个单词的标记器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29142230/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com