http://spark.apache.org/docs/latest/ml-features.html#tf-idf
importation org.apache.spark.ml.feature._
importation org.apache.spark.ml.linalg.SparseVector
importation org.apache.spark.sql.SparkSession
importation scala.collection.mutable
scala.io.Source import
/ **
* créé par xubc sur 03.06.2017.
* /
Objet Testx {
def principaux (args: Array [String]): Unité = {
étincelle val = SparkSession.builder
.master ( "local [5]")
. .AppName (this.getClass.getName () stripSuffix ( "$ « ))
.getOrCreate ()
val sentenceData = spark.createDataFrame (Seq (
(0.0, "Salut , j'ai entendu parler sont Spark"),
(1.0, "Je souhaite que Java pourrait utiliser des classes d'allumage de cas"),
(2.0, " les modèles de régression de régression logistique I propre")
)). ToDF ( "label", "phrase")
val tokenizer = new Tokenizer (). SetInputCol ( "phrase"). SetOutputCol ( "mots")
val wordsData = tokenizer.transform (sentenceData)
// HashingTF arc模型
// val hashingTF = new HashingTF ()
Les setNumFeatures (100) // .setInputCol ( "mots"). de setOutputCol ( "rawFeatures").
// val featurizedData = hashingTF.transform ( wordsData)
// CountVectorizer arc模型
val cvModel: CountVectorizerModel = new CountVectorizer ()
. .setInputCol ( "mots") setOutputCol ( "rawFeatures")
.adapter (wordsData)
val = featurizedData cvModel.transform (wordsData)
val idf = new IDF (). setInputCol ( "rawFeatures"). setOutputCol ( "caractéristiques")
val = idfModel idf.fit (featurizedData)
val = rescaledData idfModel.transform (featurizedData)
rescaledData.printSchema ()
val = vocabulaire cvModel.vocabulary
println (vocabulary.mkString ( », "))
rescaledData.show (false)
rescaledData.foreach (e => {
étiquette val = e.getAs [Double] (" label ")
val str = e.getAs [chaîne] (" phrase ")
mots = e.getAs val [mutable.WrappedArray [chaîne]] (" mots "). mkString (", ")
val = tf e.getAs [sparsevector] (" rawFeatures ")
val = originWords tf.indices. carte (i => vocabulaire (i)).mkString ( "")
val idf = e.getAs [sparsevector] ( "caractéristiques")
println (
s "" "$ étiquette $ str
| $ mots
| $ tf originWords de $
| $ idf". "" stripMargin)
})
}
}