问题描述
我正在努力减轻我的工作。我需要对 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')