Python文本处理

语料访问

语料访问详细操作教程
Corpora是一个展示多个文本文档集合的组。 单个集合称为语料库。 其中一个着名的语料库是古腾堡语料库,其中包含大约25,000本免费电子书,由 http://www.gutenberg.org/ 托管。 在下面的例子中,只访问语料库中那些文件的名称,这些文件是纯文本,以.txt结尾的文件名。
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-23
from nltk.corpus import gutenberg
fields = gutenberg.fileids()
print(fields)
执行上面示例代码,得到以下结果 -
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-23
[austen-emma.txt', austen-persuasion.txt', austen-sense.txt', bible-kjv.txt',
blake-poems.txt', bryant-stories.txt', burgess-busterbrown.txt',
carroll-alice.txt', chesterton-ball.txt', chesterton-brown.txt',
chesterton-thursday.txt', edgeworth-parents.txt', melville-moby_dick.txt',
milton-paradise.txt', shakespeare-caesar.txt', shakespeare-hamlet.txt',
shakespeare-macbeth.txt', whitman-leaves.txt']

访问原始文本

可以使用sent_tokenize函数从这些文件中访问原始文本,该函数也可以在nltk中使用。 在下面的例子中,将检索blake-poen文本的前两段。
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-23
from nltk.tokenize import sent_tokenize
from nltk.corpus import gutenberg
sample = gutenberg.raw("blake-poems.txt")
token = sent_tokenize(sample)
for para in range(2):
    print(token[para])
当运行上面的程序时,我们得到以下输出 -
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-23
[Poems by William Blake 1789]
SONGS OF INNOCENCE and OF EXPERIENCE
and THE BOOK of THEL
 SONGS OF INNOCENCE
 INTRODUCTION
 Piping down the valleys wild,
   Piping songs of pleasant glee,
 On a cloud I saw a child,
   and he laughing said to me:
 "Pipe a song about a Lamb!"
So I piped with merry cheer.
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4