-
Notifications
You must be signed in to change notification settings - Fork 394
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added concrete wrappers for HashingTF, NGram and StopWordsRemover (#314)
- Loading branch information
Showing
10 changed files
with
303 additions
and
27 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
72 changes: 72 additions & 0 deletions
72
core/src/main/scala/com/salesforce/op/stages/impl/feature/OpHashingTF.scala
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,72 @@ | ||
/* | ||
* Copyright (c) 2017, Salesforce.com, Inc. | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* | ||
* * Redistributions of source code must retain the above copyright notice, this | ||
* list of conditions and the following disclaimer. | ||
* | ||
* * Redistributions in binary form must reproduce the above copyright notice, | ||
* this list of conditions and the following disclaimer in the documentation | ||
* and/or other materials provided with the distribution. | ||
* | ||
* * Neither the name of the copyright holder nor the names of its | ||
* contributors may be used to endorse or promote products derived from | ||
* this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
package com.salesforce.op.stages.impl.feature | ||
|
||
import com.salesforce.op.UID | ||
import com.salesforce.op.features.types._ | ||
import com.salesforce.op.stages.sparkwrappers.specific.OpTransformerWrapper | ||
import org.apache.spark.ml.feature.HashingTF | ||
|
||
/** | ||
* Wrapper for [[org.apache.spark.ml.feature.HashingTF]] | ||
* | ||
* Maps a sequence of terms to their term frequencies using the hashing trick. | ||
* Currently we use Austin Appleby's MurmurHash 3 algorithm (MurmurHash3_x86_32) | ||
* to calculate the hash code value for the term object. | ||
* Since a simple modulo is used to transform the hash function to a column index, | ||
* it is advisable to use a power of two as the numFeatures parameter; | ||
* otherwise the features will not be mapped evenly to the columns. | ||
* | ||
* @see [[HashingTF]] for more info | ||
*/ | ||
class OpHashingTF(uid: String = UID[HashingTF]) | ||
extends OpTransformerWrapper[TextList, OPVector, HashingTF](transformer = new HashingTF(), uid = uid) { | ||
|
||
/** | ||
* Number of features. Should be greater than 0. | ||
* (default = 2^18^) | ||
*/ | ||
def setNumFeatures(value: Int): this.type = { | ||
getSparkMlStage().get.setNumFeatures(value) | ||
this | ||
} | ||
|
||
/** | ||
* Binary toggle to control term frequency counts. | ||
* If true, all non-zero counts are set to 1. This is useful for discrete probabilistic | ||
* models that model binary events rather than integer counts. | ||
* (default = false) | ||
*/ | ||
def setBinary(value: Boolean): this.type = { | ||
getSparkMlStage().get.setBinary(value) | ||
this | ||
} | ||
} |
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
64 changes: 64 additions & 0 deletions
64
core/src/main/scala/com/salesforce/op/stages/impl/feature/OpNGram.scala
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,64 @@ | ||
/* | ||
* Copyright (c) 2017, Salesforce.com, Inc. | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* | ||
* * Redistributions of source code must retain the above copyright notice, this | ||
* list of conditions and the following disclaimer. | ||
* | ||
* * Redistributions in binary form must reproduce the above copyright notice, | ||
* this list of conditions and the following disclaimer in the documentation | ||
* and/or other materials provided with the distribution. | ||
* | ||
* * Neither the name of the copyright holder nor the names of its | ||
* contributors may be used to endorse or promote products derived from | ||
* this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
package com.salesforce.op.stages.impl.feature | ||
|
||
import com.salesforce.op.UID | ||
import com.salesforce.op.features.types._ | ||
import com.salesforce.op.stages.sparkwrappers.specific.OpTransformerWrapper | ||
import org.apache.spark.ml.feature.NGram | ||
|
||
/** | ||
* Wrapper for [[org.apache.spark.ml.feature.NGram]] | ||
* | ||
* A feature transformer that converts the input array of strings into an array of n-grams. Null | ||
* values in the input array are ignored. | ||
* It returns an array of n-grams where each n-gram is represented by a space-separated string of | ||
* words. | ||
* | ||
* When the input is empty, an empty array is returned. | ||
* When the input array length is less than n (number of elements per n-gram), no n-grams are | ||
* returned. | ||
* | ||
* @see [[NGram]] for more info | ||
*/ | ||
class OpNGram(uid: String = UID[NGram]) | ||
extends OpTransformerWrapper[TextList, TextList, NGram](transformer = new NGram(), uid = uid) { | ||
|
||
/** | ||
* Minimum n-gram length, greater than or equal to 1. | ||
* Default: 2, bigram features | ||
*/ | ||
def setN(value: Int): this.type = { | ||
getSparkMlStage().get.setN(value) | ||
this | ||
} | ||
|
||
} |
70 changes: 70 additions & 0 deletions
70
core/src/main/scala/com/salesforce/op/stages/impl/feature/OpStopWordsRemover.scala
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,70 @@ | ||
/* | ||
* Copyright (c) 2017, Salesforce.com, Inc. | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* | ||
* * Redistributions of source code must retain the above copyright notice, this | ||
* list of conditions and the following disclaimer. | ||
* | ||
* * Redistributions in binary form must reproduce the above copyright notice, | ||
* this list of conditions and the following disclaimer in the documentation | ||
* and/or other materials provided with the distribution. | ||
* | ||
* * Neither the name of the copyright holder nor the names of its | ||
* contributors may be used to endorse or promote products derived from | ||
* this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
package com.salesforce.op.stages.impl.feature | ||
|
||
import com.salesforce.op.UID | ||
import com.salesforce.op.features.types._ | ||
import com.salesforce.op.stages.sparkwrappers.specific.OpTransformerWrapper | ||
import org.apache.spark.ml.feature.StopWordsRemover | ||
|
||
/** | ||
* Wrapper for [[org.apache.spark.ml.feature.StopWordsRemover]] | ||
* | ||
* A feature transformer that filters out stop words from input. | ||
* | ||
* @note null values from input array are preserved unless adding null to stopWords explicitly. | ||
* | ||
* @see <a href="http://en.wikipedia.org/wiki/Stop_words">Stop words (Wikipedia)</a> | ||
* @see [[StopWordsRemover]] for more info | ||
*/ | ||
class OpStopWordsRemover(uid: String = UID[StopWordsRemover]) | ||
extends OpTransformerWrapper[TextList, TextList, StopWordsRemover](transformer = new StopWordsRemover(), uid = uid) { | ||
|
||
/** | ||
* The words to be filtered out. | ||
* Default: English stop words | ||
* | ||
* @see `StopWordsRemover.loadDefaultStopWords()` | ||
*/ | ||
def setStopWords(value: Array[String]): this.type = { | ||
getSparkMlStage().get.setStopWords(value) | ||
this | ||
} | ||
|
||
/** | ||
* Whether to do a case sensitive comparison over the stop words. | ||
* Default: false | ||
*/ | ||
def setCaseSensitive(value: Boolean): this.type = { | ||
getSparkMlStage().get.setCaseSensitive(value) | ||
this | ||
} | ||
} |
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
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
Oops, something went wrong.