使用 BERT 迭代多个文件以进行 QA returns 无
Iterating through multiple files with BERT for QA returns nothing
我想减轻我的工作。我需要对 BERT 为我提供的数千个文件的答案进行一些分析。我的主要 objective 是遍历每个文件并提出问题。
我一直在尝试使用以下代码使其自动化
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')
上面的代码returns没什么。但是,如果我一个一个地做。它工作正常。所以我试着改变它。
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。
出于某种原因,当遍历所有文件时,print() 实际上会 return 给出答案。这很奇怪,因为通常你不需要调用 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')
我想减轻我的工作。我需要对 BERT 为我提供的数千个文件的答案进行一些分析。我的主要 objective 是遍历每个文件并提出问题。
我一直在尝试使用以下代码使其自动化
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')
上面的代码returns没什么。但是,如果我一个一个地做。它工作正常。所以我试着改变它。
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。
出于某种原因,当遍历所有文件时,print() 实际上会 return 给出答案。这很奇怪,因为通常你不需要调用 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')