pyspark后缀替换避免某些单词而不映射到panda或rdd



我继承了一个程序,该程序可以修改pyspark数据帧中的一些字符串。其中一个步骤涉及从字符串中的一些单词中删除后缀,并添加一个额外的例外列表,即使它们有后缀,这些例外也会被单独列出。目前,这是通过使用udf将数据帧转换为panda来完成的,然后在读取回pyspark之前,将自定义函数应用于生成的panda数据帧中的字符串。不幸的是,对需求的更改意味着代码在任何情况下都不能使用pandas-udf或映射到rdd。我需要直接在pyspark中执行相同的功能。

后缀移除函数逐字读取字符串,检查该单词是否在例外列表中,如果不在,则检查接受的后缀(这是一个严格的列表,我不能只使用现有的词干词干(,如果有,则检查词干单词是否超过4个字符,如果是,则执行替换。

以下是当前实现的MWE,在pyspark数据帧转换为panda之后。

import pandas as pd

exception_list = ['WOODLAND', 'FISHING', 'LAUGHING']
suffix_list = ['ING', 'AND']
cols = ['input']
data = [
["CAT DOG FROG WOODLAND FARMLAND LAUGHING UNICORN"],
["BOG FISHING CARTING MISSING AND SOGGY"],
["SEARCHING"],
["FINDING"],
["SING SINGING"]
]
df = pd.DataFrame(data, columns=cols)
df.head()

def strip_sufx_word(word, suffix, exception, min_stem_length=4):
for sufx in suffix:
if word[-len(sufx):] == sufx:
if len(word[:-len(sufx)])>=min_stem_length:
if word not in exception:
word = word[:-len(sufx)]
return word

def strip_sufx_string(phrase, suffix, exception):
new_phrase = [strip_sufx_word(word, suffix, exception)
for word in phrase.split()]
return ' '.join(new_phrase)

df['output'] = df['input'].apply(strip_sufx_string,
suffix=suffix_list,
exception=exception_list)
df.head()

有办法在pyspark里做到这一点吗?我对使用RegexTokenizer之类的东西持开放态度,稍后再加入它,并创建额外的true列,然后删除。它只需要在数据帧不离开pyspark或映射到其他任何东西的情况下完成。

这里的高阶函数会有所帮助:

import pyspark.sql.functions as F
exception_list = ['WOODLAND', 'FISHING', 'LAUGHING']
suffix_list = ['ING', 'AND']
min_stem_length = 4
result = sdf.withColumn(
'exception_list', 
F.array(*[F.lit(w) for w in exception_list])
).withColumn(
'suffix_list', 
F.array(*[F.lit(w) for w in suffix_list])
).withColumn(
'output', 
F.expr(f"""
concat_ws(' ', 
transform(
split(input, ' '), 
word -> 
aggregate(
suffix_list, 
word, 
(acc, s) -> 
case when substring(acc, -length(s)) = s 
and length(substring(acc, 1, length(acc)-length(s))) >= {min_stem_length} 
and not array_contains(exception_list, acc) 
then substring(acc, 1, length(acc)-length(s)) 
else acc 
end
)
)
)
"""
)
).drop('exception_list', 'suffix_list')
result.show(truncate=False)
+-----------------------------------------------+--------------------------------------------+
|input                                          |output                                      |
+-----------------------------------------------+--------------------------------------------+
|CAT DOG FROG WOODLAND FARMLAND LAUGHING UNICORN|CAT DOG FROG WOODLAND FARML LAUGHING UNICORN|
|BOG FISHING CARTING MISSING AND SOGGY          |BOG FISHING CART MISS AND SOGGY             |
|SEARCHING                                      |SEARCH                                      |
|FINDING                                        |FIND                                        |
|SING SINGING                                   |SING SING                                   |
+-----------------------------------------------+--------------------------------------------+

最新更新