繁体   English   中英

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

[英]Create bigrams from list of sentences in pandas dataframe

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

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

调用map ,第一个参数必须是函数名称 ,而不是函数调用 ngrams(2)是一个函数调用。 您不能直接将ngramsmap一起使用。 定义一个lambda函数:

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

或使用列表理解:

lambda row: [ngrams(x,2) for x in row]

或使用功能bigrams ,它也是NLTK的一部分:

lambda row: list(map(bigrams, row))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM