首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >NLTK使用语料库标注西班牙语单词

NLTK使用语料库标注西班牙语单词
EN

Stack Overflow用户
提问于 2013-02-06 23:19:25
回答 4查看 32.2K关注 0票数 19

我正在尝试学习如何使用NLTK标记西班牙语单词。

nltk book中,使用英文单词的示例来标记它们是非常容易的。因为我对nltk和所有的语言处理都是新手,所以我对如何处理感到非常困惑。

我已经下载了cess_esp语料库。有没有办法在nltk.pos_tag中指定语料库。我查看了pos_tag文档,没有看到任何建议我可以这样做的内容。我觉得我遗漏了一些关键概念。我必须在cess_esp语料库中手动标记文本中的单词吗?(我指的是手动标记我的sentance,然后在语料库上运行它),或者我完全错了。谢谢

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-02-07 10:12:06

首先,您需要从语料库中读取标记的句子。 NLTK提供了一个很好的接口,无需为来自不同语料库的不同格式而烦恼;您可以简单地导入语料库,使用语料库对象函数来访问数据。参见http://nltk.googlecode.com/svn/trunk/nltk_data/index.xml

然后,您必须选择您选择的标记器,并训练标记器。还有更多花哨的选择,但你可以从N-gram标记器开始。

然后你可以使用标记器来标记你想要的句子。下面是一个示例代码:

代码语言:javascript
运行
复制
from nltk.corpus import cess_esp as cess
from nltk import UnigramTagger as ut
from nltk import BigramTagger as bt

# Read the corpus into a list, 
# each entry in the list is one sentence.
cess_sents = cess.tagged_sents()

# Train the unigram tagger
uni_tag = ut(cess_sents)

sentence = "Hola , esta foo bar ."

# Tagger reads a list of tokens.
uni_tag.tag(sentence.split(" "))

# Split corpus into training and testing set.
train = int(len(cess_sents)*90/100) # 90%

# Train a bigram tagger with only training data.
bi_tag = bt(cess_sents[:train])

# Evaluates on testing data remaining 10%
bi_tag.evaluate(cess_sents[train+1:])

# Using the tagger.
bi_tag.tag(sentence.split(" "))

在大型语料库上训练标记者可能需要相当长的时间。而不是每次我们需要一个标记器时训练一个标记器,将训练好的标记器保存在一个文件中以供以后重用是很方便的。

请查看http://nltk.googlecode.com/svn/trunk/doc/book/ch05.html中的存储标记器部分

票数 18
EN

Stack Overflow用户

发布于 2013-12-29 01:04:44

根据上一个答案中的教程,下面是来自意大利面标记器的更面向对象的方法:https://github.com/alvations/spaghetti-tagger

代码语言:javascript
运行
复制
#-*- coding: utf8 -*-

from nltk import UnigramTagger as ut
from nltk import BigramTagger as bt
from cPickle import dump,load

def loadtagger(taggerfilename):
    infile = open(taggerfilename,'rb')
    tagger = load(infile); infile.close()
    return tagger

def traintag(corpusname, corpus):
    # Function to save tagger.
    def savetagger(tagfilename,tagger):
        outfile = open(tagfilename, 'wb')
        dump(tagger,outfile,-1); outfile.close()
        return
    # Training UnigramTagger.
    uni_tag = ut(corpus)
    savetagger(corpusname+'_unigram.tagger',uni_tag)
    # Training BigramTagger.
    bi_tag = bt(corpus)
    savetagger(corpusname+'_bigram.tagger',bi_tag)
    print "Tagger trained with",corpusname,"using" +\
                "UnigramTagger and BigramTagger."
    return

# Function to unchunk corpus.
def unchunk(corpus):
    nomwe_corpus = []
    for i in corpus:
        nomwe = " ".join([j[0].replace("_"," ") for j in i])
        nomwe_corpus.append(nomwe.split())
    return nomwe_corpus

class cesstag():
    def __init__(self,mwe=True):
        self.mwe = mwe
        # Train tagger if it's used for the first time.
        try:
            loadtagger('cess_unigram.tagger').tag(['estoy'])
            loadtagger('cess_bigram.tagger').tag(['estoy'])
        except IOError:
            print "*** First-time use of cess tagger ***"
            print "Training tagger ..."
            from nltk.corpus import cess_esp as cess
            cess_sents = cess.tagged_sents()
            traintag('cess',cess_sents)
            # Trains the tagger with no MWE.
            cess_nomwe = unchunk(cess.tagged_sents())
            tagged_cess_nomwe = batch_pos_tag(cess_nomwe)
            traintag('cess_nomwe',tagged_cess_nomwe)
            print
        # Load tagger.
        if self.mwe == True:
            self.uni = loadtagger('cess_unigram.tagger')
            self.bi = loadtagger('cess_bigram.tagger')
        elif self.mwe == False:
            self.uni = loadtagger('cess_nomwe_unigram.tagger')
            self.bi = loadtagger('cess_nomwe_bigram.tagger')

def pos_tag(tokens, mmwe=True):
    tagger = cesstag(mmwe)
    return tagger.uni.tag(tokens)

def batch_pos_tag(sentences, mmwe=True):
    tagger = cesstag(mmwe)
    return tagger.uni.batch_tag(sentences)

tagger = cesstag()
print tagger.uni.tag('Mi colega me ayuda a programar cosas .'.split())
票数 7
EN

Stack Overflow用户

发布于 2020-02-18 19:46:13

我在这里搜索其他语言的POS标签,而不是英语。另一个解决问题的方法是使用Spacy库。它提供多种语言的POS标签,例如荷兰语、德语、法语、葡萄牙语、西班牙语、挪威语、意大利语、希腊语和立陶宛语。

来自Spacy文档:

代码语言:javascript
运行
复制
import es_core_news_sm
nlp = es_core_news_sm.load()
doc = nlp("El copal se usa principalmente para sahumar en distintas ocasiones como lo son las fiestas religiosas.")
print([(w.text, w.pos_) for w in doc])

通向:

('El','DET'),('copal',‘名词’),('se','PRON'),(‘美国’,‘动词’),(‘主要’,‘高级’),(‘段落’,'ADP'),('sahumar',‘动词’),('en','ADP'),('distintas','DET'),('ocasiones',‘名词’),('como',‘'SCONJ'),('lo','PRON'),('son','AUX'),('las','DET'),('fiestas',’PRON‘),( 'PUNCT'),('.',’PUNCT‘)

并在笔记本中可视化:

代码语言:javascript
运行
复制
displacy.render(doc, style='dep', jupyter = True, options = {'distance': 120})

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14732465

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档