|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.spark.ml.feature |
| 19 | + |
| 20 | +import org.apache.spark.SparkFunSuite |
| 21 | +import org.apache.spark.mllib.util.MLlibTestSparkContext |
| 22 | +import org.apache.spark.sql.{DataFrame, Row} |
| 23 | + |
| 24 | +import scala.beans.BeanInfo |
| 25 | + |
| 26 | +@BeanInfo |
| 27 | +case class StopWordsTestData(raw: Array[String], wanted: Array[String]) |
| 28 | + |
| 29 | +object StopWordsRemoverSuite extends SparkFunSuite { |
| 30 | + def testStopWordsRemover(t: StopWordsRemover, dataset: DataFrame): Unit = { |
| 31 | + t.transform(dataset) |
| 32 | + .select("filtered", "wanted") |
| 33 | + .collect() |
| 34 | + .foreach { case Row(tokens, wantedTokens) => |
| 35 | + assert(tokens === wantedTokens) |
| 36 | + } |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +class StopWordsRemoverSuite extends SparkFunSuite with MLlibTestSparkContext { |
| 41 | + import org.apache.spark.ml.feature.StopWordsRemoverSuite._ |
| 42 | + |
| 43 | + test("StopWordsRemover default") { |
| 44 | + val remover = new StopWordsRemover() |
| 45 | + .setInputCol("raw") |
| 46 | + .setOutputCol("filtered") |
| 47 | + val dataset = sqlContext.createDataFrame(Seq( |
| 48 | + StopWordsTestData(Array("test", "test"), Array("test", "test")), |
| 49 | + StopWordsTestData(Array("a", "b", "c", "d"), Array("b", "c", "d")), |
| 50 | + StopWordsTestData(Array("a", "the", "an"), Array()), |
| 51 | + StopWordsTestData(Array("A", "The", "AN"), Array()), |
| 52 | + StopWordsTestData(Array(null), Array(null)), |
| 53 | + StopWordsTestData(Array(), Array()) |
| 54 | + )) |
| 55 | + testStopWordsRemover(remover, dataset) |
| 56 | + } |
| 57 | + |
| 58 | + test("StopWordsRemover case sensitive") { |
| 59 | + val remover = new StopWordsRemover() |
| 60 | + .setInputCol("raw") |
| 61 | + .setOutputCol("filtered") |
| 62 | + .setCaseSensitive(true) |
| 63 | + |
| 64 | + val dataset = sqlContext.createDataFrame(Seq( |
| 65 | + StopWordsTestData(Array("A"), Array("A")), |
| 66 | + StopWordsTestData(Array("The", "the"), Array("The")) |
| 67 | + )) |
| 68 | + testStopWordsRemover(remover, dataset) |
| 69 | + } |
| 70 | + |
| 71 | + test("StopWordsRemover with additional words") { |
| 72 | + val stopWords = StopWords.EnglishSet + "python" + "scala" |
| 73 | + val remover = new StopWordsRemover() |
| 74 | + .setInputCol("raw") |
| 75 | + .setOutputCol("filtered") |
| 76 | + .setStopWords(stopWords) |
| 77 | + |
| 78 | + val dataset = sqlContext.createDataFrame(Seq( |
| 79 | + StopWordsTestData(Array("python", "scala", "a"), Array()), |
| 80 | + StopWordsTestData(Array("Python", "Scala", "swift"), Array("swift")) |
| 81 | + )) |
| 82 | + testStopWordsRemover(remover, dataset) |
| 83 | + } |
| 84 | +} |
0 commit comments