【发布时间】:2014-08-18 13:41:39
【问题描述】:
大家好。我现在正在做我最后一年的项目,名为“使用 Brill 标注器的马来语词性标注器”。
我想问一下如何训练我保存在txt文件中的标记句子?输入应该在 txt 文件中,然后使用 brill tagger 进行训练。之后,我将使用一个 txt 文件作为测试数据。但是,我卡在了火车部分。你能帮帮我吗?
这是我的一些代码。
import nltk
f = open('gayahidupsihat_tagged.txt')
malay_tagged = f.read()
def train_brill_tagger(train_data):
# Modules for creating the templates.
from nltk.tag import UnigramTagger
from nltk.tag.brill import SymmetricProximateTokensTemplate, ProximateTokensTemplate
from nltk.tag.brill import ProximateTagsRule, ProximateWordsRule
# The brill tagger module in NLTK.
from nltk.tag.brill import FastBrillTaggerTrainer
unigram_tagger = UnigramTagger(train_data)
templates = [SymmetricProximateTokensTemplate(ProximateTagsRule, (1,1)),
SymmetricProximateTokensTemplate(ProximateTagsRule, (2,2)),
SymmetricProximateTokensTemplate(ProximateTagsRule, (1,2)),
SymmetricProximateTokensTemplate(ProximateTagsRule, (1,3)),
SymmetricProximateTokensTemplate(ProximateWordsRule, (1,1)),
SymmetricProximateTokensTemplate(ProximateWordsRule, (2,2)),
SymmetricProximateTokensTemplate(ProximateWordsRule, (1,2)),
SymmetricProximateTokensTemplate(ProximateWordsRule, (1,3)),
ProximateTokensTemplate(ProximateTagsRule, (-1, -1), (1,1)),
ProximateTokensTemplate(ProximateWordsRule, (-1, -1), (1,1))]
trainer = FastBrillTaggerTrainer(initial_tagger=unigram_tagger,
templates=templates, trace=3,
deterministic=True)
brill_tagger = trainer.train(train_data, max_rules=10)
print
return brill_tagger
malay_train = (malay_tagged[:10])
malay_test = (malay_tagged[10:15])
malay20 = malay_tagged[20]
mt = train_brill_tagger(malay_train)
print mt.tag(malay20)
实际上,我想训练一个标记段落,之后,我将使用其他段落对其进行测试。之后,我将使用标记的句子来评估 brill tagger 作为结果。
例子:
我训练了这个(gayahidupsihat_train.txt)——真的是所有一行输入:
Gaya\NN hidup\NN sihat\VB boleh\MD lah\UH ditakrifkan\VBZ sebagai\DT
satu\CD amalan\VBZ kehidupan\NN yang\DT membawa\VBZ impak\NN positif\NN
kepada\TO diri\NN seseorang\NN ,\, keluarganya\NN dan\CC masyarakat\NN.
Antara\IN contoh\NN kehidupan\NN yang\DT sihat\VB ialah\DT individu\NN
tersebut\EX hidup\VB dengan\DT penuh\RB ceria\RB tanpa\NN mengalami\VBZ
sebarang\NN masalah\NN yang\DT boleh\MD menjejaskan\VBZ kehidupannya\NN
untuk\TO satu\CD tempoh\NN tertentu\EX pula\DT .\. Sudah\EX pasti\RB
dalam\DT kehidupan\NN era\NN moden\NN yang\DT begitu\DT banyak\RB
tekanan\VB ini\DT gaya\NN hidup\NN sihat\VB menjadi\VBZ satu\NUM
matlamat\NN yang\DT perlu\MD dicapai\VBZ segera\VB. Oleh\PDT itu\DT ,\,
terdapat\EX pelbagai\NN tindakan\VBZ yang\DT boleh\MD dilakukan\VBZ
untuk\TO mencapai\VBZ matlamat\NN ini\DT .\.
然后我想用这个(gayahidupsihat_test.txt)进行测试:
Tindakan\VBP awal\VB ialah\DT seseorang\NN itu\DT perlu\MD
mengamalkan\VBD satu\CD bentuk\NN pemakanan\NN yang\DT seimbang\NN
dalam\IN kehidupannya\VBZ .\.Dalam\IN keadaan\NN kehidupan\NN sebenar\JJ
,\, orang\NN ramai\JJ lebih\JJR suka\VB mengambil\VBZ makanan\NN yang\DT
bersifat\VBZ mudah\JJ seperti\DT mengamalkan\VBZ pengambilan\VBD makanan\NN
ringan\JJ ataupun\CC makanan\NN segera\NN .\. TidaK\DT kurang\JJR juga\DT
masyarakat\NN kita\PRP hari\NN ini\DT yang\DT lupa\VB kesan\NN pengambilan\VBZ
makanan\NN berlemak\JJR ataupun\CC makanan\NN yang\DT mempunyai\VBZ
kandungan\NN garam\NN ,\. gula\NN atau\DT sodium\FW glutamit\FW yang\DT
tinggi\JJ .\. Hal\IN ini\DT boleh\MD mendatangkan\VBZ pelbagai\NN penyakit\NN
kronik\JJ seperti\DT sakit\JJ jantung\NN ,\, darah\NN tinggi\JJ
ataupun\CC kencing\NN manis\JJ yang\DT juga\DT menjadi\MD punca\NN kematian\NN
tertinggi\JJS di\IN negara\NN kita\PRP .\.
之后,我将使用一些tagged_words 来尝试标记器并对其进行评估。
英文版显示输出如下:
Training Brill tagger on 500 sentences...
Finding initial useful rules...
Found 10210 useful rules.
B |
S F r O | Score = Fixed - Broken
c i o t | R Fixed = num tags changed incorrect -> correct
o x k h | u Broken = num tags changed correct -> incorrect
r e e e | l Other = num tags changed incorrect -> incorrect
e d n r | e
------------------+-------------------------------------------------------
46 46 0 0 | TO -> IN if the tag of the following word is 'AT'
18 20 2 0 | TO -> IN if the tag of words i+1...i+3 is 'CD'
14 14 0 0 | IN -> IN-TL if the tag of the preceding word is
| 'NN-TL', and the tag of the following word is
| 'NN-TL'
11 11 0 1 | TO -> IN if the tag of the following word is 'NNS'
10 10 0 0 | TO -> IN if the tag of the following word is 'JJ'
8 8 0 0 | , -> ,-HL if the tag of the preceding word is 'NP-
| HL'
7 7 0 1 | NN -> VB if the tag of the preceding word is 'MD'
7 13 6 0 | NN -> VB if the tag of the preceding word is 'TO'
7 7 0 0 | NP-TL -> NP if the tag of words i+1...i+2 is 'NNS'
7 7 0 0 | VBN -> VBD if the tag of the preceding word is
| 'NP'`
【问题讨论】:
-
哦,编码似乎没有格式化。
-
是的。谢谢桑达尔·纳塔拉杰。我是新来的。 :)
-
输入是什么样的?十个句子(如果我们正在研究的话)是一个非常小的训练语料库。
-
哦,我只是先尝试制作一个非常简单的系统。因为我试了很多次,还是不能运行。
-
这是 gayahidupsihat_tagged.txt 中的一些词.. Gaya\NN hidup\NN sihat\VB boleh\MD lah\UH ditakrifkan\VBZ sebagai\DT satu\CD amalan\VBZ kehidupan\NN yang\DT membawa\VBZ impak\NN positif\NN
标签: python nltk pos-tagger