【问题标题】:TypeError: 'NoneType' object is not iterable - text summarisation with kerasTypeError:“NoneType”对象不可迭代 - 使用 keras 进行文本摘要
【发布时间】:2019-08-12 13:52:39
【问题描述】:

我是机器学习的新手,我正在努力通过tutorial for text summarization using Keras. 工作

我已经达到了对数据进行矢量化的程度,但是我遇到了一个错误,我已经尽我所能尝试了。我真的很想让这个程序正常工作,并希望有人能解释为什么它会给我这个错误以及我该如何解决它。我确实看过以前的帖子,但到目前为止没有任何帮助,谢谢。这是我的代码:

#vectorise data
input_texts = []
target_texts = []
input_characters = set()
target_characters = set()

for story in stories:
    input_text = story['story']
    for highlight in story['highlights']:
        target_text = highlight
    target_text = '\t' + target_text + '\n'
    input_texts.append(input_text)
    target_texts.append(target_text)
    for char in input_text:
        if char not in input_characters:
            input_characters.add(char)
    for char in target_text:
        if char not in target_characters:
            target_characters.add(char)

input_characters = sorted(list(input_characters))
target_characters = sorted(list(target_characters))
num_encoder_tokens = len(input_characters)
num_decoder_tokens = len(target_characters)
max_encoder_seq_length = max([len(txt) for txt in input_texts])
max_decoder_seq_length = max([len(txt) for txt in target_texts])
print('Number of samples:', len(input_texts))
print('Number of unique input tokens:', num_encoder_tokens)
print('Number of unique output tokens:', num_decoder_tokens)
print('Max sequence length for inputs:', max_encoder_seq_length)
print('Max sequence length for outputs:', max_decoder_seq_length)

这是引发错误的代码行

for highlight in story['highlights']:

这是我用来清理和腌制数据的代码

#remove all unneeded features and null values
reviews = reviews.dropna()
reviews = reviews.drop(['Id','ProductId','UserId','ProfileName','HelpfulnessNumerator','HelpfulnessDenominator', 'Score','Time'], 1)
reviews = reviews.reset_index(drop=True) 
print(reviews.head())

for i in range(5):
    print("Review #",i+1) 

print(reviews.Summary[i]) 
print(reviews.Text[i]) 
print()

#define contractions eg slang words and their correct spellings
contractions = {
        "ain't": "am not",
        "aren't": "are not",
        "can't": "cannot",
        "can't've": "cannot have",
        "'cause": "because",
        "could've": "could have",
        "couldn't": "could not",
        "couldn't've": "could not have",
        "didn't": "did not",
        "doesn't": "does not",
        "don't": "do not",
        "hadn't": "had not",
        "hadn't've": "had not have",
        "hasn't": "has not",
        "haven't": "have not",
        "he'd": "he would",
        "he'd've": "he would have"}

#clean the text of contractions and stop words 
def clean_text(text, remove_stopwords=True): 
    text = text.lower() 
    if True: 
        text = text.split() 
        new_text = []
        for word in text:
            if word in contractions:new_text.append(contractions[word])
            else:
                new_text.append(word)
            text = " ".join(new_text)
            text = re.sub(r'https?:\/\/.*[\r\n]*', '', text, flags=re.MULTILINE)
            text = re.sub(r'\<a href', ' ', text)
            text = re.sub(r'&amp;', '', text)
            text = re.sub(r'[_"\-;%()|+&=*%.,!?:#$@\[\]/]', ' ', text)
            text = re.sub(r'<br />', ' ', text)
            text = re.sub(r'\'', ' ', text)
        if remove_stopwords:
            text = text.split()
            stops = set(stopwords.words("english"))
            text = [w for w in text if not w in stops]
            text = " ".join(text) 
            return text

#clean summaries and texts
clean_summaries = []
for summary in reviews.Summary:
    clean_summaries.append(clean_text(summary, remove_stopwords=False)) 
print("Summaries are complete.")

clean_texts = []
for text in reviews.Text:
    clean_texts.append(clean_text(text))
print("Texts are complete.")

stories = list()
for i, text in enumerate(clean_texts):
    stories.append({'story': text, 'highlights': clean_summaries[i]}) # save to file
dump(stories, open('data/review_dataset.pkl', 'wb'))

【问题讨论】:

    标签: python text typeerror spyder summarization


    【解决方案1】:

    您的故事词典中至少有一个似乎没有键“亮点”的键值对。如果这仅适用于某些故事,您可以在迭代之前简单地检查是否存在 NoneType。如果所有故事都是如此,那么您的代码和您正在使用的数据之间可能存在差异。

    另外,我认为存在缩进错误(可能只是错误的 SO 格式),但我认为 target_text = highlight 之后的代码应该再次向右缩进。

    for story in stories:
        input_text = story['story']
        # check for None to make sure you are not iterating over NoneType
        if story['highlights'] is not None:
            for highlight in story['highlights']:
                target_text = highlight
                # I believe the following code should be indented as well
                target_text = '\t' + target_text + '\n'
                input_texts.append(input_text)
                target_texts.append(target_text)
                ...
    

    【讨论】:

    • 感谢您的建议,缩进不是问题,所以一定是格式错误。我尝试检查 null 并在此行上给了我另一个错误: max_encoder_seq_length = max([len(txt) for txt in input_texts]) ValueError: max() arg is an empty sequence
    • @TIsNotHere 因为您最初的问题已经解决,如果您能将我的答案标记为正确答案,我将不胜感激。您关于ValueError: max() arg is an empty sequence 的下一个错误的问题可能是输入列表为空:input_texts = [] // leads to the error you mention max_encoder_seq_length = max([len(txt) for txt in input_texts]) print(max_encoder_seq_length) input_texts = ["foo", "bar"] max_encoder_seq_length = max([len(txt) for txt in input_texts]) print(max_encoder_seq_length) // prints 3
    猜你喜欢
    • 2012-08-25
    • 1970-01-01
    • 1970-01-01
    • 2020-10-12
    • 1970-01-01
    • 2017-08-29
    相关资源
    最近更新 更多