nlp分析

vhipe2zx  于 2021-05-27  发布在  Spark
关注(0)|答案(1)|浏览(430)

我想对pysparkDataframe中的字符串列做一些nlp分析。
测向:

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\ U id代表一个项目。每个u\u id可能对每个项目都有一些评论。复习可以是几个单词,一句话,一段话,甚至是表情符号。
我想找出项目被评为低或高的根本原因。例如,有多少“u\u id”抱怨物品的大小、化学元素过敏或其他与物品特征相关的问题。
通过在pandas中迭代Dataframe中的行,我了解到将Dataframe转换为numpy数组然后使用矢量化进行nlp分析更有效。
我试着用sparknlp为每个评论提取形容词和名词短语。
我不知道如何应用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\u id、p\u id),并确保nlp分析结果可以与年、月、u\u id、p\u id很好地一致。
我不喜欢这种将pysparkDataframe列转换为numpy数组的方法,因为collect()太慢。
谢谢

nbewdwxp

nbewdwxp1#

iiuc,你不需要numpy(spark在内部处理矢量化),只要做就行了 transform 然后从生成的Dataframe中选择并过滤适当的信息:

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') 是重命名的快捷方式 commenttext 然后呢 pipeline.transform(df1) . fullannotate的第一个参数可以是dataframe、list或字符串。
(2) 来自的pos标记列表https://www.ling.upenn.edu/courses/fall_2003/ling001/penn_treebank_pos.html

相关问题