读取字符串并创建提到的子字符串数组
Reading a string and creating an array of mentioned sub-strings
我目前正在尝试解决一个问题,即我有一大串文本(摘要)并且我正在该摘要中搜索某些词。基于某个类别中存在的多个单词之一,我希望能够创建一个相应标签的数组,如下所述:
ground = ['car', 'motorbike']
air = ['plane']
colour = ['blue', 'red']
| Summary | Tag_Array |
|------------------------|----------------------|
| This is a blue car | ['ground', 'colour'] |
| This is red motorbike | ['ground', 'colour'] |
| This is a plane | ['air'] |
这里的想法是它读取每个摘要,然后在 Tag_Array 列中创建一个数组,其中包含与摘要文本关联的各个标签。地面标签可以基于任何数量的潜在选项,在这种情况下,摩托车和汽车 return 标签地面。
我在功能上使用了一种非常糟糕的方法并且它非常冗长,所以我在这里的目的是找出在 Pyspark 中实现这一点的最合适的方法。
df = (df
.withColumn("summary_as_array", f.split('summary', " "))
.withColumn("tag_array", f.array(
f.when(f.array_contains('summary_as_array', "car"), "ground").otherwise(""),
f.when(f.array_contains('summary_as_array', "motorbike"), "ground").otherwise("")
)
)
)
如果你能把标签转换成这样的键值对,
tagDict = {'ground':['car', 'motorbike'],'air':['plane'],'colour':['blue','red']}
然后我们可以创建一个 UDF 来迭代 summary
中的单词和值以获取键,这将是标签。
一个简单的解决方案,
l = [('This is a blue car',),('This is red motorbike',),('This is a plane',)]
df = spark.createDataFrame(l,['summary'])
tag_udf = F.udf(lambda x : [k for k,v in tagDict.items() if any(itm in x for itm in v)])
df = df.withColumn('tag_array',tag_udf(df['summary']))
df.show()
+---------------------+----------------+
|summary |tag_array |
+---------------------+----------------+
|This is a blue car |[colour, ground]|
|This is red motorbike|[colour, ground]|
|This is a plane |[air] |
+---------------------+----------------+
希望对您有所帮助。
我目前正在尝试解决一个问题,即我有一大串文本(摘要)并且我正在该摘要中搜索某些词。基于某个类别中存在的多个单词之一,我希望能够创建一个相应标签的数组,如下所述:
ground = ['car', 'motorbike']
air = ['plane']
colour = ['blue', 'red']
| Summary | Tag_Array |
|------------------------|----------------------|
| This is a blue car | ['ground', 'colour'] |
| This is red motorbike | ['ground', 'colour'] |
| This is a plane | ['air'] |
这里的想法是它读取每个摘要,然后在 Tag_Array 列中创建一个数组,其中包含与摘要文本关联的各个标签。地面标签可以基于任何数量的潜在选项,在这种情况下,摩托车和汽车 return 标签地面。
我在功能上使用了一种非常糟糕的方法并且它非常冗长,所以我在这里的目的是找出在 Pyspark 中实现这一点的最合适的方法。
df = (df
.withColumn("summary_as_array", f.split('summary', " "))
.withColumn("tag_array", f.array(
f.when(f.array_contains('summary_as_array', "car"), "ground").otherwise(""),
f.when(f.array_contains('summary_as_array', "motorbike"), "ground").otherwise("")
)
)
)
如果你能把标签转换成这样的键值对,
tagDict = {'ground':['car', 'motorbike'],'air':['plane'],'colour':['blue','red']}
然后我们可以创建一个 UDF 来迭代 summary
中的单词和值以获取键,这将是标签。
一个简单的解决方案,
l = [('This is a blue car',),('This is red motorbike',),('This is a plane',)]
df = spark.createDataFrame(l,['summary'])
tag_udf = F.udf(lambda x : [k for k,v in tagDict.items() if any(itm in x for itm in v)])
df = df.withColumn('tag_array',tag_udf(df['summary']))
df.show()
+---------------------+----------------+
|summary |tag_array |
+---------------------+----------------+
|This is a blue car |[colour, ground]|
|This is red motorbike|[colour, ground]|
|This is a plane |[air] |
+---------------------+----------------+
希望对您有所帮助。