-
Notifications
You must be signed in to change notification settings - Fork 28.2k
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
[ML][FEATURE] SPARK-5566: RegEx Tokenizer #4504
Changes from all commits
01cd26f
9547e9d
9e07a78
9f8685a
11ca50f
196cd7a
2e89719
77ff9ca
7f930bb
f6a5002
19f9e53
9082fc3
d3ef6d3
cb9c9a7
201a107
132b00b
cd6642e
e262bac
b66313f
38b95a1
6a85982
daf685e
12dddb4
148126f
e88d7b8
2338da5
f96526d
9651aec
556aa27
a164800
5f09434
cb07021
716d257
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.spark.ml.feature; | ||
|
||
import com.google.common.collect.Lists; | ||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import org.apache.spark.api.java.JavaRDD; | ||
import org.apache.spark.api.java.JavaSparkContext; | ||
import org.apache.spark.sql.DataFrame; | ||
import org.apache.spark.sql.Row; | ||
import org.apache.spark.sql.SQLContext; | ||
|
||
public class JavaTokenizerSuite { | ||
private transient JavaSparkContext jsc; | ||
private transient SQLContext jsql; | ||
|
||
@Before | ||
public void setUp() { | ||
jsc = new JavaSparkContext("local", "JavaTokenizerSuite"); | ||
jsql = new SQLContext(jsc); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
jsc.stop(); | ||
jsc = null; | ||
} | ||
|
||
@Test | ||
public void regexTokenizer() { | ||
RegexTokenizer myRegExTokenizer = new RegexTokenizer() | ||
.setInputCol("rawText") | ||
.setOutputCol("tokens") | ||
.setPattern("\\s") | ||
.setGaps(true) | ||
.setMinTokenLength(3); | ||
|
||
JavaRDD<TokenizerTestData> rdd = jsc.parallelize(Lists.newArrayList( | ||
new TokenizerTestData("Test of tok.", new String[] {"Test", "tok."}), | ||
new TokenizerTestData("Te,st. punct", new String[] {"Te,st.", "punct"}) | ||
)); | ||
DataFrame dataset = jsql.createDataFrame(rdd, TokenizerTestData.class); | ||
|
||
Row[] pairs = myRegExTokenizer.transform(dataset) | ||
.select("tokens", "wantedTokens") | ||
.collect(); | ||
|
||
for (Row r : pairs) { | ||
Assert.assertEquals(r.get(0), r.get(1)); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.spark.ml.feature | ||
|
||
import scala.beans.BeanInfo | ||
|
||
import org.scalatest.FunSuite | ||
|
||
import org.apache.spark.mllib.util.MLlibTestSparkContext | ||
import org.apache.spark.sql.{DataFrame, Row, SQLContext} | ||
|
||
@BeanInfo | ||
case class TokenizerTestData(rawText: String, wantedTokens: Seq[String]) { | ||
/** Constructor used in [[org.apache.spark.ml.feature.JavaTokenizerSuite]] */ | ||
def this(rawText: String, wantedTokens: Array[String]) = this(rawText, wantedTokens.toSeq) | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove extra empty line |
||
class RegexTokenizerSuite extends FunSuite with MLlibTestSparkContext { | ||
import org.apache.spark.ml.feature.RegexTokenizerSuite._ | ||
|
||
@transient var sqlContext: SQLContext = _ | ||
|
||
override def beforeAll(): Unit = { | ||
super.beforeAll() | ||
sqlContext = new SQLContext(sc) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. initialize |
||
} | ||
|
||
test("RegexTokenizer") { | ||
val tokenizer = new RegexTokenizer() | ||
.setInputCol("rawText") | ||
.setOutputCol("tokens") | ||
|
||
val dataset0 = sqlContext.createDataFrame(Seq( | ||
TokenizerTestData("Test for tokenization.", Seq("Test", "for", "tokenization", ".")), | ||
TokenizerTestData("Te,st. punct", Seq("Te", ",", "st", ".", "punct")) | ||
)) | ||
testRegexTokenizer(tokenizer, dataset0) | ||
|
||
val dataset1 = sqlContext.createDataFrame(Seq( | ||
TokenizerTestData("Test for tokenization.", Seq("Test", "for", "tokenization")), | ||
TokenizerTestData("Te,st. punct", Seq("punct")) | ||
)) | ||
|
||
tokenizer.setMinTokenLength(3) | ||
testRegexTokenizer(tokenizer, dataset1) | ||
|
||
tokenizer | ||
.setPattern("\\s") | ||
.setGaps(true) | ||
.setMinTokenLength(0) | ||
val dataset2 = sqlContext.createDataFrame(Seq( | ||
TokenizerTestData("Test for tokenization.", Seq("Test", "for", "tokenization.")), | ||
TokenizerTestData("Te,st. punct", Seq("Te,st.", "", "punct")) | ||
)) | ||
testRegexTokenizer(tokenizer, dataset2) | ||
} | ||
} | ||
|
||
object RegexTokenizerSuite extends FunSuite { | ||
|
||
def testRegexTokenizer(t: RegexTokenizer, dataset: DataFrame): Unit = { | ||
t.transform(dataset) | ||
.select("tokens", "wantedTokens") | ||
.collect() | ||
.foreach { | ||
case Row(tokens, wantedTokens) => | ||
assert(tokens === wantedTokens) | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please append
: IntParam
tominTokenLength
. See SPARK-6428. Please also update other method declarations.