首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用BERT迭代多个文件以获取QA不返回任何结果

使用BERT迭代多个文件以获取QA不返回任何结果
EN

Stack Overflow用户
提问于 2021-03-01 19:25:29
回答 1查看 29关注 0票数 0

我正在努力使我的工作轻松一些。我需要对BERT给我的数千个文件的答案做一些分析。我的主要目标是遍历每个文件并提出一个问题。

我一直在尝试用下面的代码实现自动化

代码语言:javascript
复制
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')

上面的代码不返回任何内容。然而,如果我一个接一个地做。它工作得很好。所以我试着改变它。

代码语言:javascript
复制
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。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-03-01 20:35:38

出于某种原因,当循环遍历所有文件时,print()实际上确实返回了答案。这很奇怪,因为通常你不需要调用print来让它工作。

工作代码:

代码语言:javascript
复制
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')
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66421258

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档