2013-10-15 13 views
6

Bir paragrafı kelimelere ayırmaya çalışıyorum. Güzel nltk.tokenize.word_tokenize (gönderim) elimde var, ancak yardım (word_tokenize), "Bu belirteç bir seferde bir cümle üzerinde çalışacak şekilde tasarlanmıştır." Diyor.Kötüye kullanma nltk'nin sonuçları word_tokenize (gönderilmiş)

Paragrafta kullanırsanız ne olabileceğini bilen var mı, yani en fazla 5 cümle? Birkaç kısa paragrafta kendim denedim ve işe yarıyor gibi görünüyor, ama bu neredeyse kesin bir kanıt.

+1

'nltk.word_tokenize()' şimdi metin üzerinde çalışır Birden çok cümle içerir. – nedned

cevap

7

nltk.tokenize.word_tokenize(text) basitçe görünüşte bir cümle ayrıştırmak için basit normal ifade kullanan bir TreebankWordTokenizer sınıfının bir örneğinin tokenize yöntemini çağıran ince wrapper function olduğunu.

Bu tokenizer metin zaten cümle içine segmentli varsayar:

o sınıf için dokümantasyon belirtiyor. Herhangi bir periyot - bir dizginin sonundakiler hariç - , eklendikleri kelimenin bir parçası olarak kabul edilir (örneğin kısaltmaları, vb.) Ve ayrı olarak belirtilmemişlerdir.

:

yatan tokenize yöntem çok basittir: bu dizgenin sonunda düşer, temel olarak

def tokenize(self, text): 
    for regexp in self.CONTRACTIONS2: 
     text = regexp.sub(r'\1 \2', text) 
    for regexp in self.CONTRACTIONS3: 
     text = regexp.sub(r'\1 \2 \3', text) 

    # Separate most punctuation 
    text = re.sub(r"([^\w\.\'\-\/,&])", r' \1 ', text) 

    # Separate commas if they're followed by space. 
    # (E.g., don't separate 2,500) 
    text = re.sub(r"(,\s)", r' \1', text) 

    # Separate single quotes if they're followed by a space. 
    text = re.sub(r"('\s)", r' \1', text) 

    # Separate periods that come before newline or end of string. 
    text = re.sub('\. *(\n|$)', ' . ', text) 

    return text.split() 

, hangi yöntem normalde ayrı bir belirteci olarak dönem tokenize olduğu

Dizenin içine düşen tüm dönemler, kelimenin kısaltması olduğu varsayılarak şu şekilde kısaltılır:

>>> nltk.tokenize.word_tokenize("Hello, world. How are you?") 
['Hello', ',', 'world.', 'How', 'are', 'you', '?'] 

Bu davranış kabul edilebilir olduğu sürece, iyi olmalısınız.

+0

Ah ha, bu davranış kabul edilemez, metin sınıflandırması yapmak için kelimelerin sıklığını kullanıyorum. Ne kadar iyi bir cevap, teşekkürler! –

+1

Bu tavsiye artık güncel değil. 'nltk.word_tokenize()' şimdi jetonları belirlemeden önce Punk cümle belirtecini kullanarak cümleleri ayırır. – nedned

1

hack bu tür deneyin: büyük ihtimalle takip kodu çok frekansını saymak gerekenler Sonra

>>> from string import punctuation as punct 
>>> sent = "Mr President, Mr President-in-Office, indeed we know that the MED-TV channel and the newspaper Özgür Politika provide very in-depth information. And we know the subject matter. Does the Council in fact plan also to use these channels to provide information to the Kurds who live in our countries? My second question is this: what means are currently being applied to integrate the Kurds in Europe?" 
# Add spaces before punctuations 
>>> for ch in sent: 
...  if ch in punct: 
...    sent = sent.replace(ch, " "+ch+" ") 
# Remove double spaces if it happens after adding spaces before punctuations. 
>>> sent = " ".join(sent.split()) 

=)

>>> from nltk.tokenize import word_tokenize 
>>> from nltk.probability import FreqDist 
>>> fdist = FreqDist(word.lower() for word in word_tokenize(sent)) 
>>> for i in fdist: 
...  print i, fdist[i] 
+0

Harika hack! Bir şans vereceğim! –

İlgili konular