从熊猫数据框中的句子列表创建双字母组

时间:2018-08-05 04:35:09

标签: python pandas nlp

经过一些预处理后,我有一个像这样的数据框。我想从数据框行中的每个列表创建双字母组。下面是我的尝试方式。我说

时出错
lambda row: list((map(ngrams(2), row))))
TypeError: ngrams() missing 1 required positional argument: 'n'

ngrams的第一个参数应该是什么?我应该如何修改此代码?

我可能还会在每个功能上问一些问题。但是我很难理解我正在使用的lamda和map函数。请解释一下我将来应该如何在此数据帧上应用lamda和map函数?

  

数据框

 [[ive, searching, right, word, thank, breather], [i, promise, wont, take, help, granted, fulfil, promise], [you, wonderful, blessing, time]]                       

 [[free, entry, 2, wkly, comp, win, fa, cup, final, tkts, 21st, may, 2005], [text, fa, 87121, receive, entry, questionstd, txt, ratetcs, apply, 08452810075over18s]]

 [[nah, dont, think, go, usf, life, around, though]]                                                                                                                

 [[even, brother, like, speak, me], [they, treat, like, aid, patent]]                                                                                               

 [[i, date, sunday, will], []]  
  

我需要什么

 [(even, brother), (brother,like), (like,speak), (speak,me), (they, treat), (treat,like), (like,aid), (aid,patent)]  
  

我尝试过的

 def toBigram(fullCorpus):
    bigram = fullCorpus['lemmatized'].apply(
       lambda row: list((map(ngrams(2), row))))
    return bigram

1 个答案:

答案 0 :(得分:2)

调用docker container exec -it 0778c0e3ae05 ash 时,第一个参数必须是函数 name ,而不是函数 call map是一个函数调用。您不能将ngrams(2)ngrams直接使用。定义一个lambda函数:

map

或使用列表理解:

lambda row: list(map(lambda x:ngrams(x,2), row))

或者使用功能lambda row: [ngrams(x,2) for x in row] ,它也是NLTK的一部分:

bigrams
相关问题