2016-02-08 7 views
19

NLTKを使用して自然言語処理を学習しています。 PunktSentenceTokenizerを使ってコードを見つけましたが、そのコードでは実際に私が理解することはできません。コードは次のように与えられます:NLTKでのPunktSentenceTokenizerの使用

import nltk 
from nltk.corpus import state_union 
from nltk.tokenize import PunktSentenceTokenizer 

train_text = state_union.raw("2005-GWBush.txt") 
sample_text = state_union.raw("2006-GWBush.txt") 

custom_sent_tokenizer = PunktSentenceTokenizer(train_text) #A 

tokenized = custom_sent_tokenizer.tokenize(sample_text) #B 

def process_content(): 
try: 
    for i in tokenized[:5]: 
     words = nltk.word_tokenize(i) 
     tagged = nltk.pos_tag(words) 
     print(tagged) 

except Exception as e: 
    print(str(e)) 


process_content() 

なぜ、私たちはPunktSentenceTokenizerを使用しますか?そして、AとBと記された行には何が起きていますか?私はトレーニングテキストとサンプルテキストがあることを意味しますが、Part of Speechタグを取得するには2つのデータセットが必要です。

ABと表示されている行はわかりません。

PS:私はNLTK帳に見てみてくださいでしたが[1] PunktSentenceTokenizer

+0

ソースhttps://pythonprogramming.net/part-of-speech-tagging-nltk-tutorial/ –

答えて

18

PunktSentenceTokenizerはNLTKで提供されるデフォルトの文トークナイザ、すなわちsent_tokenize()、抽象クラスです。それはUnsupervised Multilingual Sentence Boundary Detection (Kiss and Strunk (2005)のインプリメンテーションです。

>>> from nltk.corpus import state_union 
>>> train_text = state_union.raw("2005-GWBush.txt").split('\n') 
>>> train_text[11] 
u'Two weeks ago, I stood on the steps of this Capitol and renewed the commitment of our nation to the guiding ideal of liberty for all. This evening I will set forth policies to advance that ideal at home and around the world. ' 

あなたはsent_tokenize()を使用することができます:https://github.com/nltk/nltk/blob/develop/nltk/tokenize/init.py#L79

複数の文と段落を考えるには、例えば参照

>>> sent_tokenize(train_text[11]) 
[u'Two weeks ago, I stood on the steps of this Capitol and renewed the commitment of our nation to the guiding ideal of liberty for all.', u'This evening I will set forth policies to advance that ideal at home and around the world. '] 
>>> for sent in sent_tokenize(train_text[11]): 
...  print sent 
...  print '--------' 
... 
Two weeks ago, I stood on the steps of this Capitol and renewed the commitment of our nation to the guiding ideal of liberty for all. 
-------- 
This evening I will set forth policies to advance that ideal at home and around the world. 
-------- 

sent_tokenize()

nltk_data/tokenizers/punkt/english.pickleから事前に訓練されたモデルを使用しています。あなたはまた、他の言語を指定することができ、NLTKで事前に訓練されたモデルで利用可能な言語のリストは以下のとおりです。

[email protected]:~/nltk_data/tokenizers/punkt$ ls 
czech.pickle  finnish.pickle norwegian.pickle slovene.pickle 
danish.pickle french.pickle polish.pickle  spanish.pickle 
dutch.pickle  german.pickle portuguese.pickle swedish.pickle 
english.pickle greek.pickle PY3    turkish.pickle 
estonian.pickle italian.pickle README 

は、別の言語でテキストを指定すると、この操作を行います。

>>> german_text = u"Die Orgellandschaft Südniedersachsen umfasst das Gebiet der Landkreise Goslar, Göttingen, Hameln-Pyrmont, Hildesheim, Holzminden, Northeim und Osterode am Harz sowie die Stadt Salzgitter. Über 70 historische Orgeln vom 17. bis 19. Jahrhundert sind in der südniedersächsischen Orgellandschaft vollständig oder in Teilen erhalten. " 

>>> for sent in sent_tokenize(german_text, language='german'): 
...  print sent 
...  print '---------' 
... 
Die Orgellandschaft Südniedersachsen umfasst das Gebiet der Landkreise Goslar, Göttingen, Hameln-Pyrmont, Hildesheim, Holzminden, Northeim und Osterode am Harz sowie die Stadt Salzgitter. 
--------- 
Über 70 historische Orgeln vom 17. bis 19. Jahrhundert sind in der südniedersächsischen Orgellandschaft vollständig oder in Teilen erhalten. 
--------- 

を独自のPUNKTを訓練するにはモデル、 `PunktSentenceTokenizer`はトークナイザを訓練するが、前の列車をロードしないことがコードのhttps://github.com/nltk/nltk/blob/develop/nltk/tokenize/punkt.pytraining data format for nltk punkt

+0

答えをありがとう。なぜ誰もがこれを捨てたのか分かりません。とにかく、これらはあらかじめ訓練されたモデルです。しかし、自分のトレーニングセットを使用すると、「PunktSentenceTokenizer」の動作がどのように変化するか教えてください。私は実際に 'トレーニング'で何が起こっているのかを意味します。 – Arqam

9

PunktSentenceTokenizerの実際の使用を使用するように訓練されなければならない文境界検出アルゴリズムですか理解できませんでした。 NLTKには既にPunktSentenceTokenizerのトレーニング済みバージョンが含まれています。

引数なしトークナイザを初期化する使用のであれば、それは事前に訓練されたバージョンがデフォルトになります:

In [1]: import nltk 
In [2]: tokenizer = nltk.tokenize.punkt.PunktSentenceTokenizer() 
In [3]: txt = """ This is one sentence. This is another sentence.""" 
In [4]: tokenizer.tokenize(txt) 
Out[4]: [' This is one sentence.', 'This is another sentence.'] 

また、それを使用する前に、トークナイザを訓練するために、独自のトレーニングデータを提供することができます。 Punkt tokenizerは監督されていないアルゴリズムを使用しています。つまり、通常のテキストでトレーニングするだけです。ほとんどの場合のために

custom_sent_tokenizer = PunktSentenceTokenizer(train_text)

は、前の訓練を受けたバージョンを使用して、完全に罰金です。したがって、引数を指定せずに単にトークナイザを初期化することができます。

「これはPOSタグ付けとは何が関係がありますか」 NLTK POSタガーはトークン化されたセンテンスで動作するため、POSタグを使用する前にテキストを文章や単語トークンに分割する必要があります。

NLTK's documentation.

[1]キス及びStrunk、 " Unsupervised Multilingual Sentence Boundary Detection"

+0

ノートを参照してくださいモデル。新しいトークナイザをトレーニングするには、 'PunktTrainer' https://github.com/nltk/nltk/blob/develop/nltk/tokenize/punkt.py#L607 – alvas

+0

@alvasを使用します。それはトークナイザーを訓練する。 train_text引数がNoneでない場合は、train_text:train.text(verbose、finalize = True)の場合、tokenizerの列モジュールを呼び出します。 – CentAu

+0

yop、入力するのが遅すぎます。または 'PunktSentenceTokenizer.train()'を使用してください – alvas

関連する問題