我首先将 pdf 转换为纯文本(我将其打印出来,一切都很好),然后当我尝试从 NLTK 运行 word_tokenize() 时出现 UnicodeDecodeError。
尽管我事先尝试在纯文本上 decode('utf-8').encode('utf-8') ,但我还是收到了这个错误。在回溯中,我注意到 word_tokenize() 中首先引发错误的代码行是 plaintext.split('\n')。这就是为什么我试图通过在纯文本上运行 split('\n') 来重现该错误,但仍然不会产生任何错误。
所以,我既不了解导致错误的原因,也不了解如何避免它。
任何帮助将不胜感激!:) 也许我可以通过更改 pdf_to_txt 文件中的某些内容来避免它?
这是标记化的代码:
from cStringIO import StringIO
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
import os
import string
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
from pdfminer.pdfpage import PDFPage
stopset = stopwords.words('english')
path = 'my_folder'
listing = os.listdir(path)
for infile in listing:
text = self.convert_pdf_to_txt(path+infile)
text = text.decode('utf-8').encode('utf-8').lower()
print text
splitted = text.split('\n')
filtered_tokens = [i for i in word_tokenize(text) if i not in stopset and i not in string.punctuation]
这是我为了从pdf转换为txt而调用的方法:
def convert_pdf_to_txt(self, path):
rsrcmgr = PDFResourceManager()
retstr = StringIO()
codec = 'utf-8'
laparams = LAParams()
device = TextConverter(rsrcmgr, retstr, codec=codec, laparams=laparams)
fp = file(path, 'rb')
interpreter = PDFPageInterpreter(rsrcmgr, device)
password = ""
maxpages = 0
caching = True
pagenos=set()
for page in PDFPage.get_pages(fp, pagenos, maxpages=maxpages, password=password,caching=caching, check_extractable=True):
interpreter.process_page(page)
fp.close()
device.close()
ret = retstr.getvalue()
retstr.close()
return ret
这是我得到的错误的回溯:
Traceback (most recent call last):
File "/home/iammyr/opt/workspace/task-logger/task_logger/nlp/pre_processing.py", line 65, in <module>
obj.tokenizeStopWords()
File "/home/iammyr/opt/workspace/task-logger/task_logger/nlp/pre_processing.py", line 29, in tokenizeStopWords
filtered_tokens = [i for i in word_tokenize(text) if i not in stopset and i not in string.punctuation]
File "/usr/local/lib/python2.7/dist-packages/nltk/tokenize/__init__.py", line 93, in word_tokenize
return [token for sent in sent_tokenize(text)
[...]
File "/usr/local/lib/python2.7/dist-packages/nltk/tokenize/punkt.py", line 586, in _tokenize_words
for line in plaintext.split('\n'):
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 9: ordinal not in range(128)
感谢一百万和大量的好业力给你!;)