次のスクリプトは、スペイン語の文章で「単語の袋」を取得するための簡単な方法を提供します。正しく行いたい場合は、タグの前に文をトークン化する必要があることに注意してください。'religiosas','.' の 2 つのトークンで区切る必要があります。
#-*- coding: utf8 -*-
# about the tagger: http://nlp.stanford.edu/software/tagger.shtml
# about the tagset: nlp.lsi.upc.edu/freeling/doc/tagsets/tagset-es.html
import nltk
from nltk.tag.stanford import POSTagger
spanish_postagger = POSTagger('models/spanish.tagger', 'stanford-postagger.jar', encoding='utf8')
sentences = ['El copal se usa principalmente para sahumar en distintas ocasiones como lo son las fiestas religiosas.','Las flores, hojas y frutos se usan para aliviar la tos y también se emplea como sedante.']
for sent in sentences:
words = sent.split()
tagged_words = spanish_postagger.tag(words)
nouns = []
for (word, tag) in tagged_words:
print(word+' '+tag).encode('utf8')
if isNoun(tag): nouns.append(word)
print(nouns)
与えます:
El da0000
copal nc0s000
se p0000000
usa vmip000
principalmente rg
para sp000
sahumar vmn0000
en sp000
distintas di0000
ocasiones nc0p000
como cs
lo pp000000
son vsip000
las da0000
fiestas nc0p000
religiosas. np00000
[u'copal', u'ocasiones', u'fiestas', u'religiosas.']
Las da0000
flores, np00000
hojas nc0p000
y cc
frutos nc0p000
se p0000000
usan vmip000
para sp000
aliviar vmn0000
la da0000
tos nc0s000
y cc
también rg
se p0000000
emplea vmip000
como cs
sedante. nc0s000
[u'flores,', u'hojas', u'frutos', u'tos', u'sedante.']