Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从nltk中的freqDist获取元组列表_Python_Dataframe_Pyspark_Nltk_Word Count - Fatal编程技术网

Python 如何从nltk中的freqDist获取元组列表

Python 如何从nltk中的freqDist获取元组列表,python,dataframe,pyspark,nltk,word-count,Python,Dataframe,Pyspark,Nltk,Word Count,我有一个数据框,有两列id和text 我想添加一个新列,其中包含每行文本的字数 我创建了一个用户定义函数,在该函数中,我从nltk word = f.udf(lambda token: word_count) def word_count(token): freq_dict = nltk.probability.FreqDist(token) return [(word, freq) for word, freq in freq_dict.most_common()] df

我有一个数据框,有两列
id
text

我想添加一个新列,其中包含每行文本的字数

我创建了一个用户定义函数,在该函数中,我从
nltk

word = f.udf(lambda token: word_count)

def word_count(token):
    freq_dict = nltk.probability.FreqDist(token)
    return [(word, freq) for word, freq in freq_dict.most_common()]

df = df.withColumn('wordcount',word(nltk.word_tokenize(df['text']))
在标记器之后,我调用
word\u count
,希望得到一个包含word及其频率的元组列表,但是在列中得到它

[[Ljava.lang.Object;@9b4c4d5, [Ljava.lang.Object;@6a5d7d39, ...

您需要为UDF指定返回类型。 定义这样的模式

schema = ArrayType(StructType([StructField("word",StringType(), True), StructField("freq",LongType(), True)]))
并将udf更改为

f.udf(word_count, schema)

我认为这里的问题是使用
nltk.word\u tokenize
,因为您是在整列而不是每行上使用它。请在
word\u count
udf中尝试使用
nltk.word\u标记化
,以便word count将文本作为输入,标记化它并返回最常用的单词?