From 38b95a189ac034172721a7594d207e0bcecf272c Mon Sep 17 00:00:00 2001 From: Augustin Borsu Date: Thu, 19 Mar 2015 09:44:19 +0100 Subject: [PATCH] Added Java unit test for RegexTokenizer This unit tests only tests that the getters and setters work properly from java and tries one transform. Other tests have already been carried out in the TokenizerSuite.scala test. --- .../spark/ml/feature/JavaTokenizerSuite.java | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 mllib/src/test/java/org/apache/spark/ml/feature/JavaTokenizerSuite.java diff --git a/mllib/src/test/java/org/apache/spark/ml/feature/JavaTokenizerSuite.java b/mllib/src/test/java/org/apache/spark/ml/feature/JavaTokenizerSuite.java new file mode 100644 index 0000000000000..0fea933df50d3 --- /dev/null +++ b/mllib/src/test/java/org/apache/spark/ml/feature/JavaTokenizerSuite.java @@ -0,0 +1,73 @@ +/* + * 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 java.util.Arrays; +import java.util.List; + +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(0); + + List t = Arrays.asList( + "{\"rawText\": \"Test of tok.\", \"wantedTokens\": [\"Test\", \"of\", \"tok.\"]}", + "{\"rawText\": \"Te,st. punct\", \"wantedTokens\": [\"Te,st.\",\"\",\"punct\"]}"); + + JavaRDD myRdd = jsc.parallelize(t); + DataFrame dataset = jsql.jsonRDD(myRdd); + + Row[] pairs = myRegExTokenizer.transform(dataset) + .select("tokens","wantedTokens") + .collect(); + + Assert.assertEquals(pairs[0].get(0), pairs[0].get(1)); + Assert.assertEquals(pairs[1].get(0), pairs[1].get(1)); + } +}