【问题标题】:AttributeError: 'NoneType' object has no attribute 'strip' jupyter notebookAttributeError:“NoneType”对象没有属性“strip”jupyter notebook
【发布时间】:2020-02-05 00:17:57
【问题描述】:

我正在尝试运行this notebook,这是一个问答系统的实现。 运行第 8 个单元后:

challenges = {
    # QA1 with 10,000 samples
    'single_supporting_fact_10k': 'tasks_1-20_v1-2/en-10k/qa1_single-supporting-fact_{}.txt',
    # QA2 with 10,000 samples
    'two_supporting_facts_10k': 'tasks_1-20_v1-2/en-10k/qa2_two-supporting-facts_{}.txt',
}
challenge_type = 'single_supporting_fact_10k'
challenge = challenges[challenge_type]

print('Extracting stories for the challenge:', challenge_type)
train_stories = get_stories(tar.extractfile(challenge.format('train')))
test_stories = get_stories(tar.extractfile(challenge.format('test')))

我收到此错误:

AttributeError: 'NoneType' 对象没有属性 'strip'

它在这些函数中使用了拆分:

def tokenize(sent):
    return [ x.strip() for x in re.split('(\W+)?', sent) if x.strip()]

def parse_stories(lines, only_supporting=False):
    '''Parse stories provided in the bAbi tasks format
    If only_supporting is true, only the sentences
    that support the answer are kept.
    '''
    data = []
    story = []
    for line in lines:
        line = line.decode('utf-8').strip()
        nid, line = line.split(' ', 1)
        nid = int(nid)
        if nid == 1:
            story = []
        if '\t' in line:
            q, a, supporting = line.split('\t')
            q = tokenize(q)
            substory = None
            if only_supporting:
                # Only select the related substory
                supporting = map(int, supporting.split())
                substory = [story[i - 1] for i in supporting]
            else:
                # Provide all the substories
                substory = [x for x in story if x]
            data.append((substory, q, a))
            story.append('')
        else:
            sent = tokenize(line)
        story.append(sent)
return data
def get_stories(f, only_supporting=False, max_length=None):
    data = parse_stories(f.readlines(), only_supporting=only_supporting)
    flatten = lambda data: reduce(lambda x, y: x + y, data)
    data = [(flatten(story), q, answer) for story, q, answer in data if not max_length or len(flatten(story)) < max_length]
return data

我不知道缺少什么以及如何修复它。

【问题讨论】:

标签: python deep-learning jupyter-notebook nonetype question-answering


【解决方案1】:

据我了解,有一种情况是传递给tokenize() 的参数没有对象。因此出现错误'NoneType' object has no attribute 'strip'。您可能希望围绕 tokenize() 调用设置一些 try-catch,以查看何时将空字符串传递给函数。

为了解决这个问题,您可能需要探索数据。

【讨论】:

    猜你喜欢
    • 2017-05-03
    • 2021-07-10
    • 1970-01-01
    • 1970-01-01
    • 2015-02-02
    • 2020-03-25
    • 2016-08-11
    • 1970-01-01
    • 2021-12-30
    相关资源
    最近更新 更多