2016-02-08 22 views
19

Uczę się przetwarzania języka naturalnego przy użyciu NLTK. Natknąłem się na kod przy użyciu PunktSentenceTokenizer, którego faktycznego użycia nie mogę zrozumieć w danym kodzie. Kod jest podany:Używanie obiektu PunktSentenceTokenizer w NLTK

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() 

Dlaczego używamy więc PunktSentenceTokenizer. I co dzieje się w linii oznaczonej A i B. Mam na myśli tekst szkoleniowy, a drugi przykładowy tekst, ale jaka jest potrzeba dwóch zestawów danych, aby uzyskać znacznik Część Mowy.

Wiersz oznaczony jako A i B jest tym, czego nie jestem w stanie zrozumieć.

PS: I nie próbować szukać w książce NLTK ale nie mógł zrozumieć, co jest realne wykorzystanie PunktSentenceTokenizer

+0

źródło kodu https://pythonprogramming.net/part-of-speech-tagging-nltk-tutorial/ –

Odpowiedz

18

PunktSentenceTokenizer stanowi klasę domyślnego zdanie tokenizera tj sent_tokenize(), znajdujące się w NLTK. Jest to domena Unsupervised Multilingual Sentence Boundary Detection (Kiss and Strunk (2005). Zobacz https://github.com/nltk/nltk/blob/develop/nltk/tokenize/init.py#L79

Biorąc pod paragraf o zdaniu wielokrotnej, np:

>>> 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. ' 

Można użyć sent_tokenize():

>>> 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. 
-------- 

The sent_tokenize() wykorzystuje wstępnie przeszkoleni modelu z nltk_data/tokenizers/punkt/english.pickle.Można również określić inne języki, lista dostępnych języków z wcześniej przeszkoleni modeli NLTK są:

[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 

Biorąc tekst w innym języku, to zrobić:

>>> 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. 
--------- 

Aby trenować swój własny punkt modelu, patrz https://github.com/nltk/nltk/blob/develop/nltk/tokenize/punkt.py i training data format for nltk punkt

+0

Dzięki za odpowiedź. Nie wiem, dlaczego ktoś to zajął. W każdym razie są to wstępnie wyszkolone modele. Ale czy możesz mi powiedzieć, jak zmieni się działanie 'PunktSentenceTokenizer', jeśli użyję własnego zestawu treningowego? Mam na myśli to, co dzieje się w "Treningu". – Arqam

9

PunktSentenceTokenizer jest algorytm wykrywania granicy zdanie, które muszą być przeszkoleni, aby być stosowany [1]. NLTK zawiera już wcześniej wyszkoloną wersję PunktSentenceTokenizer.

Więc jeśli używasz zainicjować tokenizera bez żadnych argumentów, to domyślnie wstępnie przeszkolony wersji:

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.'] 

Można też podać własne dane treningowe do szkolenia tokenizera przed użyciem. Punkt tokenizer używa nienadzorowanego algorytmu, co oznacza, że ​​po prostu ćwiczysz go zwykłym tekstem.

custom_sent_tokenizer = PunktSentenceTokenizer(train_text)

Dla większości przypadków, jest to całkowicie w porządku, aby użyć wstępnie przeszkolony wersję. Możesz więc zainicjować tokenizera bez podawania argumentów.

"Co to ma wspólnego z tagowaniem POS"? Tagger NLTK POS działa z tokenizowanymi zdaniami, więc przed tagiem POS musisz podzielić tekst na zdania i tokeny słów.

NLTK's documentation.

[1] Kiss i Strunk " Unsupervised Multilingual Sentence Boundary Detection"

+0

Zauważ, że 'PunktSentenceTokenizer' nie trenujesz tokenizera ale ładuje pre-Train modele. Aby wyszkolić nowego tokenizera, użyj 'PunktTrainer' https://github.com/nltk/nltk/blob/develop/nltk/tokenize/punkt.py#L607 – alvas

+0

@alvas. Szkoli tokenizera. Jeśli argument train_text nie jest żadnym, wywołuje moduł pociągu tokenizera 'if train_text: self.train (train_text, verbose, finalize = True)' – CentAu

+0

tak, miałem zamiar wpisać, zbyt wolno pisać. Lub użyj 'PunktSentenceTokenizer.train()' – alvas