Python:TaggedCorpusReader如何从STTS到通用标记

2024-05-29 02:11:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Python和Keras开发POS标记器。我得到的数据是使用STTS标记,但是我应该为通用标记集创建一个标记器。所以我需要翻译这个。你知道吗

首先,我想到制作一个字典并简单地搜索替换标记,但是后来我看到了使用TaggedCorpusReader设置标记集的选项。(例如“棕色”)

但是我错过了一个可以在那里使用的标记集列表。我可以用STTS标记集吗?还是我必须自己编一本字典?你知道吗

示例来源: 代码#3:将语料库标记映射到通用标记集 https://www.geeksforgeeks.org/nlp-customization-using-tagged-corpus-reader/

corpus = TaggedCorpusReader(filePath, "standard_pos_tagged.txt", tagset='STTS') #?? doesn't work sadly
# ....
trainingCorpus.tagged_sents(tagset='universal')[1]

最后看起来是这样的:(非常感谢alexis

with open(resultFileName, "w") as output:
    for sent in stts_corpus.tagged_sents():
        for word, tag in sent:
            try:
                newTag = mapping_dict[tag];
                output.write(word+"/"+newTag+" ")               
            except:
                print("except "  + str(word) + " - " + str(tag))
        output.write("\n")

Tags: in标记foroutput字典tagcorpusword
1条回答
网友
1楼 · 发布于 2024-05-29 02:11:13

只需创建一个字典并替换标记,就像您考虑的那样。nltk的通用标记集支持由模块nltk/tag/mapping.py提供。它依赖于一组映射文件,您可以在NLTK_DATA/taggers/universal_tagset中找到这些文件。例如,在en-brown.map中可以找到这样的行,它将一大堆标记映射到PRTABXDET,依此类推:

ABL     PRT
ABN     PRT
ABN-HL  PRT
ABN-NC  PRT
ABN-TL  PRT
ABX     DET
AP      ADJ

这些文件被读入用于翻译的词典。通过以相同的格式创建映射文件,您可以使用nltk的函数来执行翻译,但是老实说,如果您的任务只是以通用格式生成一个语料库,那么我只需要手工进行翻译。但不是通过“searchreplace”:使用nltk的语料库读取器提供的元组,通过直接在映射字典中查找来替换POS标记。你知道吗

假设您知道如何说服nltkTaggedCorpusReader读取语料库,现在您有了一个方法为tagged_words()tagged_sents()stts_corpusreader对象。您还需要映射字典,它的键是STTS标记,值是通用标记;如果ABL是STTS标记,mapping_dict["ABL"]应该返回值PRT。然后重新映射如下:

for filename in stts_corpus.fileids():
    with open("new_dir/"+filename, "w") as output:
        for word, tag in stts_corpus.tagged_words():
            output.write(word+"/"+mapping_dict[tag]+" ")
        output.write("\n")

这就是它的全部,除非你想添加一些奢侈品,比如把文字分成几行。你知道吗

相关问题 更多 >

    热门问题