2011-02-09 18 views
62

के साथ एक नया कॉर्पस बनाना, मुझे लगता है कि अक्सर मेरे शीर्षक का जवाब दस्तावेजों को जाना और पढ़ना है, लेकिन मैं NLTK book के माध्यम से भाग गया लेकिन यह जवाब नहीं देता है। मैं अजगर के लिए नया हूँ।एनएलटीके

मेरे पास .txt फ़ाइलों का एक गुच्छा है और मैं कॉर्पस कार्यों का उपयोग करने में सक्षम होना चाहता हूं जो एनएलटीके कॉर्पस nltk_data के लिए प्रदान करता है।

मैं PlaintextCorpusReader की कोशिश की है, लेकिन मैं से आगे नहीं मिल सका: punkt का उपयोग कर

>>>import nltk 
>>>from nltk.corpus import PlaintextCorpusReader 
>>>corpus_root = './' 
>>>newcorpus = PlaintextCorpusReader(corpus_root, '.*') 
>>>newcorpus.words() 

मैं कैसे करूँ खंड newcorpus वाक्य? मैंने पंक फ़ंक्शन का उपयोग करने का प्रयास किया लेकिन पंक फ़ंक्शन PlaintextCorpusReader कक्षा नहीं पढ़ सके?

क्या आप मुझे यह भी ले जा सकते हैं कि मैं खंडित डेटा को टेक्स्ट फ़ाइलों में कैसे लिख सकता हूं?

संपादित करें: इस प्रश्न का एक बार उपहार था, और अब यह दूसरा उपहार है। बाउंटी बॉक्स में टेक्स्ट देखें।

उत्तर

32

मुझे लगता है कि PlaintextCorpusReader पहले से ही एक पंकट टोकननाइज़र के साथ इनपुट को विभाजित करता है, कम से कम यदि आपकी इनपुट भाषा अंग्रेजी है।

PlainTextCorpusReader's constructor

def __init__(self, root, fileids, 
      word_tokenizer=WordPunctTokenizer(), 
      sent_tokenizer=nltk.data.LazyLoader(
       'tokenizers/punkt/english.pickle'), 
      para_block_reader=read_blankline_block, 
      encoding='utf8'): 

आप पाठक एक शब्द और वाक्य tokenizer पारित कर सकते हैं, लेकिन बाद के लिए डिफ़ॉल्ट पहले से ही nltk.data.LazyLoader('tokenizers/punkt/english.pickle') है।

एक स्ट्रिंग के लिए, एक टोकनेज़र का उपयोग निम्नानुसार किया जाएगा (here समझाया गया है, पंकट टोकनेज़र के लिए सेक्शन 5 देखें)।

>>> import nltk.data 
>>> text = """ 
... Punkt knows that the periods in Mr. Smith and Johann S. Bach 
... do not mark sentence boundaries. And sometimes sentences 
... can start with non-capitalized words. i is a good variable 
... name. 
... """ 
>>> tokenizer = nltk.data.load('tokenizers/punkt/english.pickle') 
>>> tokenizer.tokenize(text.strip()) 
+0

धन्यवाद। समझ गया। लेकिन मैं खंडित वाक्यों को एक अलग txt फ़ाइल में कैसे आउटपुट करूं? – alvas

+0

दोनों लिंक त्रुटि, 404. क्या कुछ मीठी आत्मा लिंक अपडेट कर सकती हैं? – mtk

+0

पहला लिंक फिक्स्ड। कोई फर्क नहीं पड़ता कि दूसरा दस्तावेज़ किस बिंदु पर इंगित करता था। – alexis

9
>>> import nltk 
>>> from nltk.corpus import PlaintextCorpusReader 
>>> corpus_root = './' 
>>> newcorpus = PlaintextCorpusReader(corpus_root, '.*') 
""" 
if the ./ dir contains the file my_corpus.txt, then you 
can view say all the words it by doing this 
""" 
>>> newcorpus.words('my_corpus.txt') 
+0

देवनागरी भाषा के लिए कुछ समस्या को गोली मारता है। – ashim888

44

पता लगाना यह कैसे काम करता से कुछ वर्षों के बाद, यहाँ

textfiles की एक निर्देशिका के साथ एक NLTK कोष बनाने के लिए कैसे की अद्यतन ट्यूटोरियल है?

मुख्य विचार nltk.corpus.reader पैकेज का उपयोग करना है। यदि आपके पास अंग्रेजी में टेक्स्टफाइल की निर्देशिका है, तो PlaintextCorpusReader का उपयोग करना सबसे अच्छा है।

आप एक निर्देशिका है कि इस तरह दिखता है, तो:

newcorpus/ 
     file1.txt 
     file2.txt 
     ... 

सीधे शब्दों में कोड की इन पंक्तियों का उपयोग करें और आप एक कोष प्राप्त कर सकते हैं:

import os 
from nltk.corpus.reader.plaintext import PlaintextCorpusReader 

corpusdir = 'newcorpus/' # Directory of corpus. 

newcorpus = PlaintextCorpusReader(corpusdir, '.*') 

नोट: कि PlaintextCorpusReader का उपयोग करेगा अपने ग्रंथों को वाक्यों और शब्दों में विभाजित करने के लिए डिफ़ॉल्ट nltk.tokenize.sent_tokenize() और nltk.tokenize.word_tokenize() और ये फ़ंक्शंस अंग्रेज़ी के लिए बनाए जाते हैं, यह नहीं सभी के लिए काम कर सकता है भाषाओं।

import os 
from nltk.corpus.reader.plaintext import PlaintextCorpusReader 

# Let's create a corpus with 2 texts in different textfile. 
txt1 = """This is a foo bar sentence.\nAnd this is the first txtfile in the corpus.""" 
txt2 = """Are you a foo bar? Yes I am. Possibly, everyone is.\n""" 
corpus = [txt1,txt2] 

# Make new dir for the corpus. 
corpusdir = 'newcorpus/' 
if not os.path.isdir(corpusdir): 
    os.mkdir(corpusdir) 

# Output the files into the directory. 
filename = 0 
for text in corpus: 
    filename+=1 
    with open(corpusdir+str(filename)+'.txt','w') as fout: 
     print>>fout, text 

# Check that our corpus do exist and the files are correct. 
assert os.path.isdir(corpusdir) 
for infile, text in zip(sorted(os.listdir(corpusdir)),corpus): 
    assert open(corpusdir+infile,'r').read().strip() == text.strip() 


# Create a new corpus by specifying the parameters 
# (1) directory of the new corpus 
# (2) the fileids of the corpus 
# NOTE: in this case the fileids are simply the filenames. 
newcorpus = PlaintextCorpusReader('newcorpus/', '.*') 

# Access each file in the corpus. 
for infile in sorted(newcorpus.fileids()): 
    print infile # The fileids of each file. 
    with newcorpus.open(infile) as fin: # Opens the file. 
     print fin.read().strip() # Prints the content of the file 
print 

# Access the plaintext; outputs pure string/basestring. 
print newcorpus.raw().strip() 
print 

# Access paragraphs in the corpus. (list of list of list of strings) 
# NOTE: NLTK automatically calls nltk.tokenize.sent_tokenize and 
#  nltk.tokenize.word_tokenize. 
# 
# Each element in the outermost list is a paragraph, and 
# Each paragraph contains sentence(s), and 
# Each sentence contains token(s) 
print newcorpus.paras() 
print 

# To access pargraphs of a specific fileid. 
print newcorpus.paras(newcorpus.fileids()[0]) 

# Access sentences in the corpus. (list of list of strings) 
# NOTE: That the texts are flattened into sentences that contains tokens. 
print newcorpus.sents() 
print 

# To access sentences of a specific fileid. 
print newcorpus.sents(newcorpus.fileids()[0]) 

# Access just tokens/words in the corpus. (list of strings) 
print newcorpus.words() 

# To access tokens of a specific fileid. 
print newcorpus.words(newcorpus.fileids()[0]) 

अंत में, ग्रंथों की एक निर्देशिका पढ़ सकते हैं और एक NLTK बनाने के लिए:

यहाँ परीक्षण textfiles के निर्माण और कैसे NLTK के साथ एक कोष बनाने के लिए और कैसे विभिन्न स्तरों पर कोष का उपयोग करने के साथ पूर्ण कोड है एक और भाषाओं में कोष, आपको पहले यह सुनिश्चित करना चाहिए कि आप एक अजगर-प्रतिदेय शब्द tokenization और वाक्य tokenization मॉड्यूल है कि स्ट्रिंग/basestring इनपुट लेता है और इस तरह के उत्पादन का उत्पादन किया है कि:

>>> from nltk.tokenize import sent_tokenize, word_tokenize 
>>> txt1 = """This is a foo bar sentence.\nAnd this is the first txtfile in the corpus.""" 
>>> sent_tokenize(txt1) 
['This is a foo bar sentence.', 'And this is the first txtfile in the corpus.'] 
>>> word_tokenize(sent_tokenize(txt1)[0]) 
['This', 'is', 'a', 'foo', 'bar', 'sentence', '.'] 
स्पष्टीकरण के लिए
+0

स्पष्टीकरण के लिए धन्यवाद। हालांकि, कई भाषाओं को डिफ़ॉल्ट रूप से समर्थित किया जाता है। –

+0

अगर किसी को 'विशेषता Error: __exit__' त्रुटि मिलती है। '() ' –

+0

@TasdikRahman के बजाय' open() 'का उपयोग करें, कृपया विस्तृत करें? मैं इस माध्यम से गुजर नहीं सकता .. – yashhy

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