首页 > 其他 > 详细

spark scala word2vec 和多层分类感知器在情感分析中的实际应用

时间:2017-11-10 12:20:54      阅读:403      评论:0      收藏:0      [点我收藏+]

转自:http://www.cnblogs.com/canyangfeixue/p/7227998.html 对于威胁检测算法使用神经网络训练有用!!!TODO待实验

技术分享
/**
  * Created by lkl on 2017/7/21.
  */
//import com.ibm.spark.exercise.util.LogUtils
//import com.ibm.spark.exercise.util.LogUtils
import org.apache.spark.ml.Pipeline
import org.apache.spark.ml.classification.MultilayerPerceptronClassifier
import org.apache.spark.ml.evaluation.MulticlassClassificationEvaluator
import org.apache.spark.ml.feature.{IndexToString, StringIndexer, Word2Vec}
import org.apache.spark.sql.SQLContext
import org.apache.spark.{SparkContext, SparkConf}
import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.sql.SQLContext
object mllib {


  final val VECTOR_SIZE = 1000
//  def main(args: Array[String]) {
//    if (args.length < 1) {
//      println("Usage:SMSClassifier SMSTextFile")
//      sys.exit(1)
//    }
  def main(args: Array[String]) {
    val conf = new SparkConf().setMaster("local").setAppName("test")
       val sc = new SparkContext(conf)
      val sqlContext = new org.apache.spark.sql.SQLContext(sc)

//    val role = "jdbc:mysql://192.168.0.37:3306/emotional?user=root&password=123456&useUnicode=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false"
//    import sqlContext.implicits._
//    val df = sc.textFile("hdfs://192.168.0.211:9000/user/hadoop/emotion/SMS.txt").map(line=>(line.split(" ")(0),line.split(" ")(1),line.split(" ")(2),line.split(" ")(3))).toDF("id","innserSessionid","words","value")
//    df.printSchema()
//    df.insertIntoJDBC(role, "SMS", true)
    val sqlCtx =new org.apache.spark.sql.SQLContext(sc)
    import sqlContext.implicits._
// 读取hdfs 数据源,格式如下:以空格隔开,最后一列数字列是分析标题后,人为打上的标签,
值是按照情绪程度,值选择于【-1,-0.75,-0.5,-0.25,,0.25,0.50,0.75,1】其中之一。

// 10090 C779C882AA39436A89C463BCB406B838 涨停板,复盘,全,靠,新,股,撑,门面,万科,A,尾盘,封板 0.75
// 10091 519A9C6AD0A845298B0B3924117C0B4F 一,行业,再现,重大,利好,板块,反弹,仍,将,继续 0.75
// 10092 C86CEC7DB9794311AF386C3D7B0B7CBD 藁城区,3,大,项目,新,获,规划证,开发,房企,系,同,一家 0
// 10093 FCEA2FFC1C2F4D6C808F2CBC2FF18A8C 完善,对,境外,企业,和,对外,投资,统计,监测 0.5
// 10094 204A77847F03404986331810E039DFC2 财联社,电报 0
// 10095 E571B9EF451F4D5F8426A1FA06CD9EE6 审计署,部分,央企,业绩,不,实 -0.5
// 10096 605264A2F6684CC4BB4B2A0B6A8FA078 厨卫,品牌,新,媒体,榜,看看,谁家,的,官微,最,爱,卖萌 0.25
val parsedRDD = sc.textFile("hdfs://192.168.0.211:9000/user/hadoop/emotion/SMS.txt").map(line=>{
val a = line.split(" ")
if(a.length == 4 ){
(line.split(" ")(3),line.split(" ")(2).split(","))
}else{
("","".split(","))
}
})

val msgDF = sqlCtx.createDataFrame(parsedRDD).toDF("label","message")
val labelIndexer = new StringIndexer().setInputCol("label").setOutputCol("indexedLabel").fit(msgDF)
val word2Vec = new Word2Vec().setInputCol("message").setOutputCol("features").setVectorSize(VECTOR_SIZE).setMinCount(1)

val layers = Array[Int](VECTOR_SIZE,250,500,200)
val mlpc = new MultilayerPerceptronClassifier().setLayers(layers).setBlockSize(512).setSeed(1234L).setMaxIter(128).setFeaturesCol("features").setLabelCol("indexedLabel").setPredictionCol("prediction")

val labelConverter = new IndexToString().setInputCol("prediction").setOutputCol("predictedLabel").setLabels(labelIndexer.labels)

val Array(trainingData, testData) = msgDF.randomSplit(Array(0.8, 0.2))
val pipeline = new Pipeline().setStages(Array(labelIndexer,word2Vec,mlpc,labelConverter))
val model = pipeline.fit(trainingData)
val predictionResultDF = model.transform(testData)
//below 2 lines are for debug use
predictionResultDF.printSchema
predictionResultDF.select("message","label","predictedLabel").show(30)
val evaluator = new MulticlassClassificationEvaluator().setLabelCol("indexedLabel").setPredictionCol("prediction").setMetricName("precision")
val predictionAccuracy = evaluator.evaluate(predictionResultDF)
println("Testing Accuracy is %2.4f".format(predictionAccuracy * 100) + "%")
// sc.stop

}
}
 
技术分享

 结果如下:

技术分享
+--------------------+-----+--------------+
|             message|label|predictedLabel|
+--------------------+-----+--------------+
|[价格会, 一飞, 冲天, 神秘,...|  0.5|           0.5|
|[审计署, 部分, 央企, 业绩,...| -0.5|           0.5|
|[广电, 总局, 新浪, 微博, ...| -0.5|           0.5|
|[叶檀, 若, 粤, 港澳湾区, ...| 0.25|           0.5|
|      [万达, 崩, 万科, 起]|    0|           0.5|
|[外汇, 小白, 必, 看, 视频...| 0.25|           0.5|
|[乐视, 回, 应发, 不, 出,...|-0.75|           0.5|
|[万达, 电影, 高开, 1.69...|  0.5|           0.5|
|[万科, A, 股, 6月, 23...| 0.75|           0.5|
|[金价, 周一, 反弹, 扭转, ...|  0.5|           0.5|
|[收评, 两, 市, 震荡, 沪指...| 0.25|           0.5|
|[点睛, 军工, 混改, 加速, ...|  0.5|           0.5|
|[棉花, 日报, 棉花, 短期, ...| 0.25|           0.5|
|[探秘, 巴铁, 试验线, 部分,...|-0.75|           0.5|
|[万达, 复星, 股价, 暴跌, ...|-0.75|           0.5|
|[油价, 迎, 年内, 最, 大,...|-0.25|           0.5|
|[2017年, IPO, 被, 否...|-0.75|           0.5|
|[股, 转, 监事长, 邓映翎, ...| -0.5|           0.5|
|[发改委, 国内, 汽, 柴油, ...|-0.25|           0.5|
|[周报, 明晟, MSCI, 宣布...|  0.5|           0.5|
|[夏季, 达沃斯, 共识, 中国,...|  0.5|           0.5|
|[重磅, 又, 一, 家, 公司,...|-0.75|           0.5|
|[麦格里, 重磅, 警告, OPE...| -0.5|           0.5|
|[韩国, 娱乐, 公司, TO-W...|  0.5|           0.5|
|       [新, 三, 板, 周报]|    0|           0.5|
|[分享, 华尔街, 对, 美国, ...|  0.5|           0.5|
|[盛和, 资源, 2015年, 公...|    0|           0.5|
|[交易, 实况, 黄金, 两, 连...| -0.5|           0.5|
|[徽商, 银行, 内斗戏, 第二,...| -0.5|           0.5|
|[2017, 夏季, 达沃斯, 论...| 0.25|           0.5|
技术分享

spark scala word2vec 和多层分类感知器在情感分析中的实际应用

原文:http://www.cnblogs.com/bonelee/p/7813835.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!