मैं एनएलटीके पुस्तकालय का उपयोग कर पाठ के एक कोष को टोकननाइज करना चाहता हूं।
मेरा कोष इस तरह दिखता है:
['Did you hear about the Native American man that drank 200 cups of tea?',
"What's the best anti diarrheal prescription?",
'What do you call a person who is outside a door and has no arms nor legs?',
'Which Star Trek character is a member of the magic circle?',
"What's the difference between a bullet and a human?",
मैंने कोशिश की:
tok_corp = [nltk.word_tokenize(sent.decode('utf-8')) for sent in corpus]
जो उठाया:
विशेषता त्रुटि: 'str' ऑब्जेक्ट में कोई विशेषता नहीं है 'डीकोड'
मदद की सराहना की जाएगी। धन्यवाद।
2 जवाब
जैसा कि इस पेज से पता चलता है कि word_tokenize तरीका एक स्ट्रिंग की अपेक्षा करता है एक तर्क, बस कोशिश करो
tok_corp = [nltk.word_tokenize(sent) for sent in corpus]
संपादित करें: निम्नलिखित कोड के साथ मैं टोकनयुक्त कोष प्राप्त कर सकता हूं,
कोड:
import pandas as pd
from nltk import word_tokenize
corpus = ['Did you hear about the Native American man that drank 200 cups of tea?',
"What's the best anti diarrheal prescription?",
'What do you call a person who is outside a door and has no arms nor legs?',
'Which Star Trek character is a member of the magic circle?',
"What's the difference between a bullet and a human?"]
tok_corp = pd.DataFrame([word_tokenize(sent) for sent in corpus])
आउटपुट:
0 1 2 3 4 ... 13 14 15 16 17
0 Did you hear about the ... tea ? None None None
1 What 's the best anti ... None None None None None
2 What do you call a ... no arms nor legs ?
3 Which Star Trek character is ... None None None None None
4 What 's the difference between ... None None None None None
मुझे लगता है कि आपके कॉर्पस में कुछ गैर-तार या गैर-बाइट जैसी वस्तुएं छिपी हुई हैं। मैं आपको इसे फिर से जांचने की सलाह देता हूं।
त्रुटि वहीं है, sent
में decode
विशेषता नहीं है। आपको केवल उन्हें .decode()
की आवश्यकता है यदि वे पहले एन्कोड किए गए थे, अर्थात, str
ऑब्जेक्ट के बजाय bytes
ऑब्जेक्ट। इसे हटा दें और यह ठीक होना चाहिए।
संबंधित सवाल
नए सवाल
python
पायथन एक बहु-प्रतिमान है, गतिशील रूप से टाइप किया हुआ, बहुउद्देशीय प्रोग्रामिंग भाषा है। यह एक साफ और एक समान वाक्यविन्यास सीखने, समझने और उपयोग करने के लिए त्वरित होने के लिए डिज़ाइन किया गया है। कृपया ध्यान दें कि अजगर 2 आधिकारिक तौर पर 01-01-2020 के समर्थन से बाहर है। फिर भी, संस्करण-विशिष्ट पायथन सवालों के लिए, [अजगर -२.०] या [अजगर -३.x] टैग जोड़ें। पायथन वेरिएंट (जैसे, ज्योथन, PyPy) या लाइब्रेरी (उदा।, पांडस और न्यूमपी) का उपयोग करते समय, कृपया इसे टैग में शामिल करें।