从Python中的epub提取文本

时间:2018-06-21 09:58:15

标签: python nltk text-mining epub

我编写了以下代码来提取电子书中的单词并将其添加到语料库中以进行文本挖掘。

# loading the german corpus
from ebooklib import epub
import ebooklib
import os
import nltk
input_path = r"C:\Users\jzeh\Desktop\Directory"
german_corpus = []
book = epub.read_epub(os.path.join(input_path,'grimms-maerchen.epub'))
for doc in book.get_items():
    german_corpus += str(doc.content)
    german_corpus = [w.lower() for w in nltk.word_tokenize(german_corpus)]

不幸的是,运行代码给我错误:

TypeError  ---> 12     german_corpus = [w.lower() for w in nltk.word_tokenize(german_corpus)]
TypeError: expected string or bytes-like object

谁能告诉我,我想念什么?

1 个答案:

答案 0 :(得分:1)

nltk.word_tokenize将字符串作为输入,您已将其传递给列表。如果我理解正确,我想你会这样:

...

for doc in book.get_items():
    doc_content = str(doc.content)
    for w in nltk.word_tokenize(doc_content):
        german_corpus.append(w.lower())