通过numpy向量化对某些pyspark数据帧列进行NLP分析

问题描述

我想对pyspark数据帧中的字符串列进行一些NLP分析。

df:

 year month u_id rating_score p_id review
 2010 09    tvwe  1           p_5  I do not like it because its size is not for me.  
 2011 11    frsa  1           p_7  I am allergic to the peanut elements.  
 2015 5     ybfd  1           p_2  It is a repeated one,please no more.
 2016 7     tbfb  2           p_2  It is not good for my oil hair.
 

每个p_id代表一个项目。 每个u_id可能对每个项目都有一些评论评论可能是几个字,一个句子或一个段落,甚至是表情符号。

我想找到物品被评为低或高的根本原因。 例如,有多少“ u_id”抱怨物品的尺寸,化学元素过敏或其他与物品功能有关的问题。

How to iterate over rows in a DataFrame in Pandas,我了解到将数据帧转换为numpy数组,然后使用矢量化进行NLP分析,效率更高。

我正在尝试使用SparkNLP为年,月,u_id,p_id的每个注释提取形容词和名词短语。

我不确定如何应用numpy向量化以非常有效地做到这一点。

我的py3代码

from sparknlp.pretrained import PretrainedPipeline
df = spark.sql('select year,month,u_id,p_id,comment from MY_DF where rating_score = 1 and isnull(comment) = false')
import numpy as np

trainseries = df['comment'].apply(lambda x : np.array(x.toArray())).as_matrix().reshape(-1,1)

text = np.apply_along_axis(lambda x : x[0],1,trainseries) # TypeError: 'Column' object is not callable

pipeline_dl = PretrainedPipeline('explain_document_dl',lang='en') # 
result = pipeline_dl.fullAnnotate(text)

代码不起作用。 我还需要在向量化中保留其他列(例如,年,月,u_id,p_id),并确保NLP分析结果可以与年,月,u_id,p_id保持一致。

我不喜欢这样 How to convert a pyspark dataframe column to numpy array,因为collect()太慢。

谢谢

解决方法

IIUC,您不需要Numpy(Spark内部处理矢量化),只需执行transform,然后从结果数据框中选择并过滤适当的信息即可:

from sparknlp.pretrained import PretrainedPipeline

df = spark.sql('select year,month,u_id,p_id,comment from MY_DF where rating_score = 1 and isnull(comment) = false')

df1 = df.withColumnRenamed('comment','text')

pipeline_dl = PretrainedPipeline('explain_document_dl',lang='en')

result = pipeline_dl.transform(df1)

df_new = result.selectExpr(
  *df1.columns,'transform(filter(pos,p -> p.result rlike "^(?:NN|JJ)"),x -> x.metadata.word) as words'
)

输出:

df_new.show(10,0)
+-----+-----+----+------------+----+------------------------------------------------+----------------------------+
|years|month|u_id|rating_score|p_id|text                                            |words                       |
+-----+-----+----+------------+----+------------------------------------------------+----------------------------+
|2010 |09   |tvwe|1           |p_5 |I do not like it because its size is not for me.|[size]                      |
|2011 |11   |frsa|1           |p_7 |I am allergic to the peanut elements.           |[allergic,peanut,elements]|
|2015 |5    |ybfd|1           |p_2 |It is a repeated one,please no more.           |[more]                      |
|2016 |7    |tbfb|2           |p_2 |It is not good for my oil hair.                 |[good,oil,hair]           |
+-----+-----+----+------------+----+------------------------------------------------+----------------------------+

注意:

(1)result = pipeline.fullAnnotate(df,'comment')是将comment重命名为text然后执行pipeline.transform(df1)的快捷方式。 fullAnnotate的第一个参数可以是DataFrame,List或String。

(2)https://www.ling.upenn.edu/courses/Fall_2003/ling001/penn_treebank_pos.html中的POS标签列表

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...