Skip to content

Commit b8105d8

Browse files
authored
Revert "Use native Spark regexp_replace for stopword removal instead of a UDF…"
This reverts commit 4f067fd.
1 parent ffd7147 commit b8105d8

4 files changed

Lines changed: 126 additions & 2 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package zingg.common.core.preprocess.stopwords;
2+
3+
import java.io.Serializable;
4+
5+
public class RemoveStopWords implements Serializable {
6+
7+
private static final long serialVersionUID = 1L;
8+
private String name = "removeStopWordsUDF";
9+
10+
public RemoveStopWords() {
11+
super();
12+
}
13+
14+
protected String removeStopWordsUsingRegex(String s,String stopWordsRegexString) {
15+
if (s == null || stopWordsRegexString==null) return null;
16+
return s.replaceAll(stopWordsRegexString, "");
17+
}
18+
19+
public String getName() {
20+
return name;
21+
}
22+
23+
public void setName(String name) {
24+
this.name = name;
25+
}
26+
27+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package zingg.common.core.preprocess.stopwords;
2+
3+
import org.apache.commons.logging.Log;
4+
import org.apache.commons.logging.LogFactory;
5+
6+
import zingg.common.client.arguments.model.IArguments;
7+
import zingg.common.client.ZFrame;
8+
import zingg.common.client.ZinggClientException;
9+
import zingg.common.client.util.ColName;
10+
import zingg.common.client.util.PipeUtilBase;
11+
12+
public class StopWords<S,D,R,C,T> {
13+
14+
protected static String name = "zingg.preprocess.stopwords.StopWords";
15+
public static final Log LOG = LogFactory.getLog(StopWords.class);
16+
protected static String stopWordColumn = ColName.COL_WORD;
17+
protected static final int COLUMN_INDEX_DEFAULT = 0;
18+
protected PipeUtilBase<S,D,R,C> pipeUtil;
19+
20+
public PipeUtilBase<S, D, R, C> getPipeUtil() {
21+
return pipeUtil;
22+
}
23+
24+
public void setPipeUtil(PipeUtilBase<S, D, R, C> pipeUtil) {
25+
this.pipeUtil = pipeUtil;
26+
}
27+
28+
29+
30+
public ZFrame<D,R,C> preprocessForStopWords(S session, IArguments args, ZFrame<D,R,C> ds) throws ZinggClientException {
31+
/*
32+
List<String> wordList = new ArrayList<String>();
33+
for (FieldDefinition def : args.getFieldDefinition()) {
34+
if (!(def.getStopWords() == null || def.getStopWords() == "")) {
35+
ZFrame<D,R,C> stopWords = getPipeUtil().read(false, false, getPipeUtil().getStopWordsPipe(args, def.getStopWords()));
36+
//if (!Arrays.asList(stopWords.schema().fieldNames()).contains(stopWordColumn)) {
37+
stopWordColumn = stopWords.columns()[COLUMN_INDEX_DEFAULT];
38+
//}
39+
wordList = stopWords.select(stopWordColumn).as(Encoders.STRING()).collectAsList();
40+
String pattern = wordList.stream().collect(Collectors.joining("|", "\\b(", ")\\b\\s?"));
41+
ds = ds.withColumn(def.getFieldName(), removeStopWords(pattern.toLowerCase()).apply(ds.col(def.getFieldName())));
42+
}
43+
}
44+
45+
return ds;
46+
*/
47+
return ds;
48+
}
49+
50+
/*
51+
public static UserDefinedFunction removeStopWords(String stopWordsRegexString) {
52+
return udf((String s) -> {
53+
if (s == null) return null;
54+
return s.toLowerCase().replaceAll(stopWordsRegexString, "");
55+
}, DataTypes.StringType);
56+
}
57+
*/
58+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package zingg.spark.core.preprocess.stopwords;
2+
3+
import org.apache.spark.sql.api.java.UDF2;
4+
5+
import zingg.common.core.preprocess.stopwords.RemoveStopWords;
6+
7+
public class RemoveStopWordsUDF extends RemoveStopWords implements UDF2<String,String,String>{
8+
9+
private static final long serialVersionUID = 1L;
10+
11+
public RemoveStopWordsUDF() {
12+
super();
13+
}
14+
15+
@Override
16+
public String call(String s,String stopWordsRegexString) throws Exception {
17+
return removeStopWordsUsingRegex(s,stopWordsRegexString);
18+
}
19+
20+
}
21+
Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package zingg.spark.core.preprocess.stopwords;
22

3-
import static org.apache.spark.sql.functions.regexp_replace;
3+
import static org.apache.spark.sql.functions.callUDF;
4+
import static org.apache.spark.sql.functions.lit;
45

56
import java.io.Serializable;
67

@@ -10,41 +11,58 @@
1011
import org.apache.spark.sql.Dataset;
1112
import org.apache.spark.sql.Row;
1213
import org.apache.spark.sql.types.DataType;
14+
import org.apache.spark.sql.types.DataTypes;
1315

1416
import zingg.common.client.FieldDefinition;
1517
import zingg.common.client.ZFrame;
1618
import zingg.common.core.context.IContext;
1719
import zingg.common.core.preprocess.stopwords.StopWordsRemover;
1820
import zingg.spark.client.SparkFrame;
1921
import org.apache.spark.sql.SparkSession;
22+
import zingg.spark.core.util.SparkFnRegistrar;
2023

2124
public class SparkStopWordsRemover extends StopWordsRemover<SparkSession,Dataset<Row>,Row,Column,DataType> implements Serializable {
2225

2326
private static final long serialVersionUID = 1L;
2427
protected static String name = "zingg.spark.preprocess.SparkStopWordsRemover";
2528
public static final Log LOG = LogFactory.getLog(SparkStopWordsRemover.class);
29+
30+
private String udfName;
2631

2732
public SparkStopWordsRemover(){
2833
}
2934

3035
public SparkStopWordsRemover(IContext<SparkSession, Dataset<Row>, Row, Column,DataType> context) {
3136
super(context);
37+
registerUDF();
3238
}
3339

3440
public SparkStopWordsRemover(IContext<SparkSession, Dataset<Row>, Row, Column,DataType> context, FieldDefinition fd) {
3541
super(context,fd);
42+
registerUDF();
3643
}
3744

3845
@Override
3946
protected ZFrame<Dataset<Row>, Row, Column> removeStopWordsFromDF(ZFrame<Dataset<Row>, Row, Column> ds,
4047
String fieldName, String pattern) {
41-
Dataset<Row> dfAfterRemoval = ds.df().withColumn(fieldName, regexp_replace(ds.df().col(fieldName), pattern, ""));
48+
Dataset<Row> dfAfterRemoval = ds.df().withColumn(fieldName,callUDF(udfName, ds.df().col(fieldName),lit(pattern)));
4249

4350
return new SparkFrame(dfAfterRemoval);
4451
}
4552

53+
protected void registerUDF() {
54+
RemoveStopWordsUDF removeStopWordsUDF = new RemoveStopWordsUDF();
55+
// Each field will have different pattern
56+
this.udfName = removeStopWordsUDF.getName();
57+
// register the UDF
58+
SparkSession zSession = getContext().getSession();
59+
60+
SparkFnRegistrar.registerUDF2(zSession, udfName, removeStopWordsUDF, DataTypes.StringType);
61+
}
62+
4663
@Override
4764
public void init() {
65+
registerUDF();
4866
}
4967

5068
}

0 commit comments

Comments
 (0)