我正在尝试处理存储在文本文件中的推文。我的代码读取推文(一一(,对其进行处理,然后将Watson的结果保存在CSV文件中。速度仅为每分钟28个推文。数据文件处理导致此延迟吗?
while 1:
where = file.tell()
line = file.readline()
if not line:
print "no line found, waiting for a 1 seconds"
time.sleep(1)
file.seek(where)
else:
if (re.search('[a-zA-Z]', line)):
print "-----------------------------"
print "the line is: "
print line
print "-----------------------------"
response = natural_language_understanding.analyze(
text=line,
features=Features(
entities=EntitiesOptions(
emotion=True,
sentiment=True,
limit=2),
keywords=KeywordsOptions(
emotion=True,
sentiment=True,
limit=2)),
language='en'
)
response["tweet"] = line
print(json.dumps(response, indent=2))
with open('#DDvKXIP.csv', 'a') as csv_file:
writer = csv.writer(csv_file)
for key, value in response.items():
writer.writerow([key, value])
else:
print "--------------------------------"
print "found a line without any alphabet in it, hence not considering."
print line
print "--------------------------------"
简短的答案是,您应该在代码的主要部分之间放置定时标记,以确定最慢的内容。
提高速度的其他选项。
- 您可以创建一个螺纹应用程序,该应用程序一次发送10-20个电话。这应该将您的费率提高到每分钟的280-560推文。
如果您使用的是Lite版本,则要确保自己不会限制自己的评分。
- 您可以从同一用户中批量推文,并作为大块发送。而不是单个电话。例如,如果您只是想捕获整体情绪,那么这可能无济于事。