将Spacy单词向量化应用于元组列表

时间:2019-01-14 12:38:58

标签: python list tuples spacy

我正在尝试对以下结构(包含元组的列表列表)中的每个元组元素应用一个函数。该函数是Spacy函数,每个元组的每个元素的类型均为'spacy.tokens.span.Span'

import spacy
tuple_vectors = []

for list_item in tuples_list: #iterate through list items
    for tuple_ in list_item: #there are sometimes multiple tuples in each list item
        for tuple_element in tuple_: #iterate through tuple elements

            #tuple_element to Spacy doc
            tuple_element_vector = tuple_element.vector #get vector
            tuple_vectors = tuple_vectors.append(tuple_element_vector) #update tuple_vectors

我当前的实现返回:

  

---> 11个tuple_vectors = tuple_vectors.append(tuple_element_vector)#update元组向量

     

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

tuples_list = [[(Sky proposal, is, matter), (Sky proposal, is, Mays 
spokesman)], [(Women, 
lag, Intel report)], [(Amazon, expected, to unveil)], [(Goldman Sachs, raising, 
billion)], [(MHP, opens, books)], [(Disney, hurls, magic), (Disney, hurls, 
moolah)], [(Amazon, offering, loans), (Amazon, offering, to)], [(JPMorgan, 
seeks, billion), (JPMorgan, seeks, WaMu claims)], [(Comcast, accuses, 
Discovery)], [(Boeing, sees, sales)], [(BRIEFNetflix Inc, reports, earnings)], 
[(Broadcom deal, may stunt, Valley investment)], [(Apple, sell, iPads)], [(oil, 
pull, Street)], [(Fed, tells, Goldman), (Fed, tells, to improve)], [(ideas, 
undermine, Brexit), (ideas, undermine, Facebook)] ...

1 个答案:

答案 0 :(得分:1)

您的代码说

tuple_vectors = tuple_vectors.append(tuple_element_vector)

append方法不会返回列表,而是返回None,因此会出现错误。 实际上,此时您将tuple_vectors重置为None

将此更改为

tuple_vectors.append(tuple_element_vector)