使用适用方法在熊猫列上使用gensim短语

问题描述 投票:0回答:2

我正在尝试在df的列上使用gensim短语。样本df在下面给出

col1   col2
1      "this is test1 and is used for test1"
2      "this is content of row which is second row"
3      "this is the third row"

我已经为二元组编写了一个方法

def bigrams(text):
    bigram = Phrases(text, min_count=1)
    bigram_mod = Phraser(bigram)
    return [bigram_mod[doc] for doc in text]

我尝试过

df['col2'].apply(bigrams)
df['col2'].apply(lambda x: bigrams([x])) - so that the text is enclosed in list

但是我将字符作为输出而不是双字母组。我在这里想念的是什么。

python pandas gensim n-gram phrase
2个回答
1
投票

Phrases期望已被标记化的语料库。

您的问题当前未显示要为text函数提供的bigrams()值是什么,但不能将这些行值作为纯字符串:您必须将其分解为所需的字词,不知何故,首先。

[单独:不要期望从一个很小的玩具大小的示例中得到任何有意义的结果,因为Phrases需要大量数据,基于统计的单词配对才有用。并请注意,即使有用,配对通常也不会与人类对有意义的分组/实体将是什么的想法相匹配-既缺少我们想要的配对,也不想进行配对,甚至进行了仔细的参数调整这样的“非自然”选择。但是,此类Phrases处理的文本对于后端分类/信息检索目的仍然经常有用。


0
投票

因此gensim短语编辑器需要令牌列表的列表所以我有效的解决方案是将文本转换为令牌将令牌转换为列表列表

df['tokens']=df['text'].apply(tokenization_function)
df['tokens']=df['tokens'].apply(lambda x:[x])
df['bigrams']=df['tokens'].apply(bigrams)
© www.soinside.com 2019 - 2024. All rights reserved.