AttributeError: объект 'NoneType' не имеет атрибута 'strip' jupyter notebook - PullRequest
0 голосов
/ 05 февраля 2020

Я пытаюсь запустить " rel="nofollow noreferrer"> этот ноутбук , который является реализацией системы ответа на вопросы. после запуска 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' enter image description here

Он использовал split в следующих функциях:

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

Я не могу найти, что отсутствует и как это исправить.

1 Ответ

0 голосов
/ 05 февраля 2020

Насколько я понимаю, есть сценарий, когда параметры, передаваемые в tokenize(), не имеют объекта. Отсюда и ошибка 'NoneType' object has no attribute 'strip'. Возможно, вы захотите настроить несколько try-catch вокруг вызовов tokenize(), чтобы увидеть, когда в функцию передаются пустые строки.

Чтобы решить эту проблему, вам, возможно, придется изучить данные.

...