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', '?']
जब तक कि व्यवहार स्वीकार्य है, आपको ठीक होना चाहिए।
'nltk.word_tokenize()' अब पाठ पर काम करता है कई वाक्य शामिल हैं। – nedned