如何解决使用 BERT 迭代多个文件进行 QA 不返回任何内容
我正在努力减轻我的工作。我需要对 BERT 给我的数千个文件的答案做一些分析。我的主要目标是遍历每个文件并提出一个问题。
import os
directory = '/content/dva/'
for filename in os.listdir(directory):
with open(directory + filename) as infile:
try:
nlp({
'question': 'How is artificial intelligence being used in real time health delivery?','context': data
})
except:
print(filename + ' is throwing an error')
上面的代码什么都不返回。然而,如果我一一做的话。它工作正常。所以我尝试改变它。
x = ["How is artificial intelligence being used in real time health delivery?",\
"What adjunctive or supportive methods can help patients?",\
"How does hypertension affect patients?",\
"What does the computer do?"]
y = [item.strip() for item in x]
def testing(theList):
nlp = pipeline('question-answering')
for each_element in theList:
nlp({'question': each_element,'context': data})
testing(y) # returns nothing
print(testing(y)) # returns None
有没有人有任何见解?上面的代码非常适合 Allen 的 ELMo。
解决方法
出于某种原因,在遍历所有文件时,print() 实际上确实返回了答案。这很奇怪,因为通常你不需要调用 print 来让它工作。
工作代码:
import os
directory = '/content/dva/'
for filename in os.listdir(directory):
with open(directory + filename) as infile:
try:
print(nlp({
'question': 'How is artificial intelligence being used in real time health delivery?','context': data
}))
except:
print(filename + ' is throwing an error')
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。