我想使用python 2实现分类应用程序,并且在分类完成之前应该预处理文本。分类器和预处理器位于不同的包中。然后我在分类包中的类中创建了preprocessing class
的对象。
这是我的项目浏览器
preprocessing class
类预处理:
def preprocess(self, file): inputFile = "text" outputFile = "plainText.txt" # infile = io.open(inputFile, "r", encoding='utf-8').read() outfile = io.open(outputFile, "w", encoding='utf-8') text = unnecessaryCharsObj.removeChars(file) text = stopWrdsObj.removeStopwords(text) text = text.lower() plain = text.split() stemmObj.stemminig(plain) for x in plain: outfile.write(x) outfile.write(u'\u0020') # plaintext = " ".join(str(x) for x in plain) # outfile.write(plaintext) return outfile outfile.close()
预处理在classidier包中创建的类对象
def classify(self): dao = DAO(); procc = preprocessing(); # Get IDs of uncatergerized news to uncatNewsList uncatNewsList = dao.selectUncategerizedNews(); for news in uncatNewsList: description = dao.getDescriptionById(news[0]) wf = io.open('news.txt', 'w', encoding='utf-8') x = description[0][0] wf.write(x) rf = io.open('news.txt', 'r', encoding='utf-8').read() txt = procc.preprocess(rf) category = MultinomialNBClassifier().classifier(txt) dao.updateNews(news[0],category[0])
但是在预处理类中,它在同一个预处理包中使用了一个文本文件。所以我不能按照自己的意愿完成工作,因为它返回错误"No such file or directory: 'stopWordList.txt'"
我能做些什么来解决这个问题?
答案 0 :(得分:0)
请检查该文件与当前执行文件的路径位于同一路径。 这是个主意: How do I get the path of the current executed file in Python?