皈依舞
经过几年的研究之后,下面是更新的教程如何使用文本文件目录创建NLTK语料库?主要思想是利用nltk.corpu.Reader包裹。中有一个文本文件目录的情况下英语,英国的,英国人的,最好使用PlaintextCorposReader.如果您有一个如下所示的目录:newcorpus/
file1.txt
file2.txt ...只需使用这些代码行,您就可以得到一个语料库:import osfrom nltk.corpus.reader.plaintext import PlaintextCorpusReadercorpusdir = 'newcorpus/' # Directory of corpus.newcorpus = PlaintextCorpusReader(corpusdir, '.*')注:认为PlaintextCorpusReader将使用默认的nltk.tokenize.sent_tokenize()和nltk.tokenize.word_tokenize()要将你的课文分成句子和单词,并且这些功能是为英语而建立的,它可以不为所有语言工作。下面是创建测试文本文件的完整代码,以及如何使用NLTK创建一个语料库,以及如何在不同级别访问该语料库:import osfrom 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 = 0for 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 fileprint# 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语料库,您必须首先确保具有python可调用性。字标记化和句子标记化接受字符串/基字符串输入并产生这样的输出的模块:>>> 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', '.']