-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for the 'TfidfVectorizer' transformation type
- Loading branch information
Showing
6 changed files
with
143 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/main/java/sklearn/feature_extraction/text/TfidfTransformer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright (c) 2017 Villu Ruusmann | ||
* | ||
* This file is part of JPMML-SkLearn | ||
* | ||
* JPMML-SkLearn is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* JPMML-SkLearn is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with JPMML-SkLearn. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package sklearn.feature_extraction.text; | ||
|
||
import java.util.List; | ||
|
||
import net.razorvine.pickle.objects.ClassDict; | ||
import scipy.sparse.CSRMatrix; | ||
|
||
public class TfidfTransformer extends ClassDict { | ||
|
||
public TfidfTransformer(String module, String name){ | ||
super(module, name); | ||
} | ||
|
||
public Number getWeight(int index){ | ||
CSRMatrix idfDiag = (CSRMatrix)get("_idf_diag"); | ||
|
||
List<?> data = idfDiag.getData(); | ||
|
||
return (Number)data.get(index); | ||
} | ||
|
||
public String getNorm(){ | ||
return (String)get("norm"); | ||
} | ||
|
||
public Boolean getSublinearTf(){ | ||
return (Boolean)get("sublinear_tf"); | ||
} | ||
|
||
public Boolean getUseIdf(){ | ||
return (Boolean)get("use_idf"); | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
src/main/java/sklearn/feature_extraction/text/TfidfVectorizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright (c) 2017 Villu Ruusmann | ||
* | ||
* This file is part of JPMML-SkLearn | ||
* | ||
* JPMML-SkLearn is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* JPMML-SkLearn is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with JPMML-SkLearn. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package sklearn.feature_extraction.text; | ||
|
||
import java.util.List; | ||
|
||
import org.dmg.pmml.Expression; | ||
import org.jpmml.converter.Feature; | ||
import org.jpmml.converter.PMMLUtil; | ||
import org.jpmml.sklearn.SkLearnEncoder; | ||
|
||
public class TfidfVectorizer extends CountVectorizer { | ||
|
||
public TfidfVectorizer(String module, String name){ | ||
super(module, name); | ||
} | ||
|
||
@Override | ||
public List<Feature> encodeFeatures(List<String> ids, List<Feature> features, SkLearnEncoder encoder){ | ||
TfidfTransformer transformer = getTransformer(); | ||
|
||
String norm = transformer.getNorm(); | ||
if(norm != null){ | ||
throw new IllegalArgumentException(norm); | ||
} | ||
|
||
return super.encodeFeatures(ids, features, encoder); | ||
} | ||
|
||
@Override | ||
public Expression encodeWeight(Expression expression, int index){ | ||
TfidfTransformer transformer = getTransformer(); | ||
|
||
Boolean sublinearTf = transformer.getSublinearTf(); | ||
if(sublinearTf){ | ||
expression = PMMLUtil.createApply("+", PMMLUtil.createApply("log", expression), PMMLUtil.createConstant(1d)); | ||
} // End if | ||
|
||
Boolean useIdf = transformer.getUseIdf(); | ||
if(useIdf){ | ||
Number weight = transformer.getWeight(index); | ||
|
||
expression = PMMLUtil.createApply("*", expression, PMMLUtil.createConstant(weight)); | ||
} | ||
|
||
return expression; | ||
} | ||
|
||
public TfidfTransformer getTransformer(){ | ||
return (TfidfTransformer)get("_tfidf"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters