我正在努力使我的工作轻松一些。我需要对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有谁有什么见解吗?上面的代码非常适合艾伦的ELMo。
发布于 2021-03-01 20:35:38
出于某种原因,当循环遍历所有文件时,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')https://stackoverflow.com/questions/66421258
复制相似问题