【发布时间】:2015-09-22 08:54:18
【问题描述】:
所以我目前正在制作一个烧瓶应用程序,它是语音标记器的一部分,该应用程序的一部分使用几个 txt 文件来检查一个词是名词还是动词,通过查看该词是否在文件。例如,这是我使用的对象:
class Word_Ref (object):
#used for part of speech tagging, and word look up.
def __init__(self, selection):
if selection == 'Verbs':
wordfile = open('Verbs.txt', 'r')
wordstring = wordfile.read()
self.reference = wordstring.split()
wordfile.close()
elif selection == 'Nouns':
wordfile = open('Nouns.txt', 'r')
wordstring = wordfile.read()
self.reference = wordstring.split()
wordfile.close()
elif selection == 'Adjectives':
wordfile = open('Adjectives.txt', 'r')
wordstring = wordfile.read()
self.reference = wordstring.split()
wordfile.close()
elif selection == 'Adverbs':
wordfile = open('Adverbs.txt', 'r')
wordstring = wordfile.read()
self.reference = wordstring.split()
wordfile.close()
elif selection == 'Pronouns':
self.reference = ['i', 'me', 'my', 'mine', 'myself', 'you', 'your', 'yours', 'yourself', 'he', 'she', 'it', 'him', 'her'
'his', 'hers', 'its', 'himself', 'herself', 'itself', 'we', 'us', 'our', 'ours', 'ourselves',
'they', 'them', 'their', 'theirs', 'themselves', 'that', 'this']
elif selection == 'Coord_Conjunc':
self.reference = ['for', 'and', 'nor', 'but', 'or', 'yet', 'so']
elif selection == 'Be_Verbs':
self.reference = ['is', 'was', 'are', 'were', 'could', 'should', 'would', 'be', 'can', 'cant', 'cannot'
'does', 'do', 'did', 'am', 'been']
elif selection == 'Subord_Conjunc':
self.reference = ['as', 'after', 'although', 'if', 'how', 'till', 'unless', 'until', 'since', 'where', 'when'
'whenever', 'where', 'wherever', 'while', 'though', 'who', 'because', 'once', 'whereas'
'before']
elif selection =='Prepositions':
self.reference = ['on', 'at', 'in']
else:
raise ReferenceError('Must choose a valid reference library.')
def __contains__(self, other):
if other[-1] == ',':
return other[:-1] in self.reference
else:
return other in self.reference
然后这是我的flask app py文档:
from flask import Flask, render_template, request
from POS_tagger import *
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def index(result=None):
if request.args.get('mail', None):
retrieved_text = request.args['mail']
result = process_text(retrieved_text)
return render_template('index.html', result=result)
def process_text(text):
elem = Sentence(text)
tag = tag_pronouns(elem)
tag = tag_preposition(tag)
tag = tag_be_verbs(tag)
tag = tag_coord_conj(tag)
tag = tag_subord_conj(tag)
tagged = package_sentence(tag)
new = str(tagged)
return new
if __name__ == '__main__':
app.run()
因此,当烧瓶应用程序中的 process_text 函数使用任何使用 open() 然后使用 .read() 的函数时,它会导致一个内部服务器,即使我将它与 Word_Ref 对象一起使用也是如此。另外,我还用一个 3 行的 txt 文件对此进行了测试,它仍然导致相同的内部服务器错误。我的 POS_tagger 的所有其他函数都在烧瓶应用程序中工作,所有这些函数,甚至 open() 都在解释器中工作。
为此目的查看 txt 文件的 open() 方式的任何替代解决方案?
编辑:这里是回溯:
File "/Users/Josh/PycharmProjects/Informineer/POS_tagger.py", line 174, in tag_avna
adverbs = Word_Ref('Adverbs')
File "/Users/Josh/PycharmProjects/Informineer/POS_tagger.py", line 91, in __init__
wordfile = open('Adverbs.txt', 'r')
FileNotFoundError: [Errno 2] No such file or directory: 'Adverbs.txt'
txt 文件与烧瓶应用程序位于同一目录中
【问题讨论】:
-
烧瓶日志说什么?如果您在调试模式下运行应用程序,您将获得 python 回溯:
app.run(debug=True)这些将对您和我们都有帮助 -
ok 贴出来了,奇怪的是txt文件和flask app py文件在同一个目录下。
-
但这不是 python 解释器的working directory,因为它运行文件。这是find data file 类型的问题。
标签: python http python-3.x flask