2013-10-15 9 views
6

मैं शब्दों में एक अनुच्छेद विभाजित करने का प्रयास कर रहा हूं। मुझे सुंदर nltk.tokenize.word_tokenize (भेजा गया) हाथ मिला है, लेकिन मदद (word_tokenize) कहती है, "यह टोकनज़र एक समय में वाक्य पर काम करने के लिए डिज़ाइन किया गया है।"nltk के word_tokenize (भेजे गए) का दुरुपयोग करने के परिणाम

क्या किसी को पता है कि अगर आप इसे पैराग्राफ पर उपयोग करते हैं तो क्या हो सकता है, यानी अधिकतम 5 वाक्य, इसके बजाय? मैंने इसे अपने कुछ छोटे पैराग्राफ पर आजमाया है और ऐसा लगता है कि यह काम करता है, लेकिन यह शायद ही निर्णायक सबूत है।

+1

'nltk.word_tokenize()' अब पाठ पर काम करता है कई वाक्य शामिल हैं। – nedned

उत्तर

7

nltk.tokenize.word_tokenize(text) बस एक पतली wrapper function कि एक TreebankWordTokenizer वर्ग है, जो जाहिरा तौर पर साधारण regex का उपयोग करता है एक वाक्य को पार्स करने का एक उदाहरण के tokenize प्रणाली को बुलाती है।

उस वर्ग के लिए दस्तावेज़ कहा गया है कि:

यह tokenizer मानता है कि पाठ को पहले से ही वाक्य में खंडित किया गया है। किसी भी अवधि - स्ट्रिंग के अंत में उन लोगों के अलावा - को उनके द्वारा जुड़े शब्द का हिस्सा माना जाता है (उदाहरण के लिए संक्षेप आदि), और अलग-अलग टोकननाइज्ड नहीं हैं।

अंतर्निहित tokenize विधि ही बहुत सरल है:

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

असल में, क्या विधि सामान्य एक अलग निशानी के रूप में अवधि tokenize है अगर यह स्ट्रिंग के अंत में आता है:

>>> nltk.tokenize.word_tokenize("Hello, world.") 
['Hello', ',', 'world', '.'] 

स्ट्रिंग के अंदर आने वाली किसी भी अवधि को शब्द के एक हिस्से के रूप में टोकननाइज्ड किया जाता है, यह धारणा के तहत कि यह संक्षिप्त नाम है:

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

जब तक कि व्यवहार स्वीकार्य है, आपको ठीक होना चाहिए।

+0

आह हे, वह व्यवहार स्वीकार्य नहीं है, मैं पाठ वर्गीकरण करने के लिए शब्दों की आवृत्ति का उपयोग कर रहा हूं। क्या एक बहुत अच्छा जवाब, धन्यवाद! –

+1

यह सलाह अब पुरानी है। 'nltk.word_tokenize()' अब टोकन निर्धारित करने से पहले पंक वाक्य टोकनेज़र का उपयोग करके वाक्यों को विभाजित करता है। – nedned

1

हैक की इस तरह का प्रयास करें: सबसे शायद पालन कोड क्या आप भी आवृत्ति गिनती करने की आवश्यकता है

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

ग्रेट हैक! मैं इसे एक शॉट दूंगा! –

संबंधित मुद्दे