【发布时间】:2021-04-08 22:37:49
【问题描述】:
我对 Python 非常陌生,我需要从头开始创建语料库。我的 .DS_Store 文件有问题。我尝试手动擦除它,使用终端擦除它或使用 Python 擦除它,但没有任何效果。当 .DS_Store 存在时,我无法进行 NLP 计算。这是我的代码:
import nltk
import random
nltk.download('cmudict')
nltk.download('wordnet')
nltk.download('stopwords')
nltk.download('averaged_perceptron_tagger')
nltk.download('punkt')
from nltk.corpus import cmudict
from nltk.stem.wordnet import WordNetLemmatizer
from nltk.corpus import stopwords
import string
from nltk import word_tokenize
import os
from nltk.corpus.reader.plaintext import PlaintextCorpusReader
corpusdir = '/Users/username/nltk_data/corpusfilename'
corp = PlaintextCorpusReader(corpusdir, '.*')
corp.fileids() # gives me 6 fileids, 5 existing and one .DS_Store
corp.sents() # error: 'utf-8' codec can't decode byte 0xd5 in position 161: invalid
continuation byte
我使用的是 Mac,建议使用 if 语句,这样语料库只能读取 .txt 而不能读取 .DS_Store。我不知道该怎么做。
【问题讨论】:
-
这里的主要目标是将
PlaintextCorpusReader指向正确的文件,在这方面您应该接受@ygorg 给出的答案并重新表述您的问题及其标题以帮助未来的读者。谢谢!