Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add XML invalid chars filter instead punctuation interrupt #136

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

import com.google.common.base.Joiner;
import com.sun.org.apache.xml.internal.utils.XMLChar;
import org.apache.spark.ml.feature.CountVectorizerModel;
import org.dmg.pmml.DataType;
import org.dmg.pmml.DefineFunction;
Expand All @@ -41,7 +43,6 @@
import org.jpmml.sparkml.FeatureConverter;
import org.jpmml.sparkml.SparkMLEncoder;
import org.jpmml.sparkml.TermFeature;
import org.jpmml.sparkml.TermUtil;

public class CountVectorizerModelConverter extends FeatureConverter<CountVectorizerModel> {

Expand Down Expand Up @@ -105,20 +106,22 @@ public List<Feature> encodeFeatures(SparkMLEncoder encoder){
List<Feature> result = new ArrayList<>();

String[] vocabulary = transformer.vocabulary();
for(int i = 0; i < vocabulary.length; i++){
String term = vocabulary[i];

if(TermUtil.hasPunctuation(term)){
throw new IllegalArgumentException("Punctuated vocabulary terms (" + term + ") are not supported");
}

result.add(new TermFeature(encoder, defineFunction, documentFeature, term));
}
for (String term : vocabulary) {
if (term.chars().anyMatch(XMLChar::isInvalid)) {
term = term.chars()
.filter(XMLChar::isValid)
.mapToObj(Character::toChars)
.map(String::valueOf)
.collect(Collectors.joining(""));
}

result.add(new TermFeature(encoder, defineFunction, documentFeature, term));
}

return result;
}

private static final Joiner JOINER = Joiner.on("|");

private static final AtomicInteger SEQUENCE = new AtomicInteger(1);
}
}