• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Scala词频统计

武飞扬头像
不懂开发的程序猿
帮助1

⚠申明: 未经许可,禁止以任何形式转载,若要引用,请标注链接地址。 全文共计4177字,阅读大概需要3分钟
🌈更多学习内容, 欢迎👏关注👀我的个人微信公众号:不懂开发的程序猿
个人网站:https://jerry-jy.co/

1. 实验室名称:

大数据实验教学系统

2. 实验项目名称:

词频统计

3. 实验学时:

4. 实验原理:

统计原理
  读取整个文件,通过空格分割成每个单词,然后映射成(word,1)这样的元组,对所有元组分组统计每个单词出现的次数。

5. 实验目的:

应用所学习到的Scala编程知识,解决文本中词频统计问题。

6. 实验内容:

世界上最遥远的距离,莫过于心与心之间的冷漠。
  在《十八岁的天空》中有“世界上最遥远的距离不是生与死,而是我站在你面前,你却不知道我爱你。”这句台词,在戏里面说是出自《飞鸟集》的。可是泰戈尔的《飞鸟集》里根本没有任何相关的句子。
  看了资料,有很多的说法。
  如果问一下网友,这首诗是谁写的,几乎众口一词:泰戈尔。
  但去查找泰戈尔的诗集,居然查不到这首诗。
  据不完全统计《世界上最遥远的距离》的相关网页PAGEVIEW(页面浏览量)超过千万。随机将网页点开,发现这首诗及诗中句子以各种方式被引用着,有的将诗制作在个人主页上,有的给它配上音乐,有的衬上精美贴图,有的将诗设计成自己在BBS上发贴时的签名。个别网页还推出传递游戏,说将这首诗发给20个以上的网友,会心想事成,和自己相爱的人成为眷属……这首关于暗恋的哀婉诗歌感动的读者不下百万。
  打开实验对应的虚拟机,学习使用Scala实现对《世界上最遥远的距离》诗集中”world“该词的统计。

7. 实验器材(设备、虚拟机名称):

硬件:x86_64 ubuntu 16.04服务器
  软件:JDK 1.8.162,Scala-2.11.11

8. 实验步骤:

1、导入代码运行所依赖的包。
  在scala Shell中执行如下代码:

1.	import scala.io.Source

结果:

import scala.io.Source

2、探索数据。
  在paste模式下,输入以下代码,并执行:

1.	// 读取源文件
2.	val lines = Source.fromFile("/data/dataset/batch/wordCound.txt").getLines()
3.	     
4.	// 查看源数据
5.	while (lines.hasNext){
6.	  println(lines.next())
7.	}

按下Ctrl D,执行以上代码。输出结果如下:

The Furthest Distance in the world Tagore
The furthest distance in the world
Is not between life and death
But when I stand in front of you
Yet you don’t know that
I love you.
The furthest distance in the world
Is not when I stand in front of you
Yet you can’t see my love
But when undoubtly knowing the love from both
Yet cannot be together.
The furthest distance in the world
Is not being apart while being in love
But when I plainly cannot resist the yearning
Yet pretending you have never been in my heart.
The furthest distance in the world
Is not struggling against the tides
But using one’s indifferent heart
To dig an uncrossable river
For the one who loves you

3、加载数据文件,并依次读取每一行文本内容,按空格分割,返回由拆分后的单词组成的数组。
  在paste模式下,输入以下代码,并执行:

1.	//重新读取
2.	val lines = Source.fromFile("/data/dataset/batch/wordCound.txt").getLines()
3.	val words = lines.flatMap(line => line.split(" "))

按下Ctrl D,执行以上代码。输出结果如下:

lines: Iterator[String] = non.empty iterator
words: Iterator[String] = non.empty iterator

4、数据转换。将每一个单词映射成(word,1)的元组形式,然后放在列表中。
  在paste模式下输入以下代码,并执行:

1.	val pairs = words.map(word => (word,1)).toList

按下Ctrl D,执行以上代码。输出结果如下(运行结果以实际为准):

pairs: List[(String, Int)] = List((The,1), (Furthest,1), (Distance,1), (in,1), (the,1), (world,1), (Tagore,1), (“”,1), (The,1), (furthest,1), (distance,1), (in,1), (the,1), (world,1), (“”,1), (Is,1), (not,1), (between,1), (life,1), (and,1), (death,1), (“”,1), (But,1), (when,1), (I,1), (stand,1), (in,1), (front,1), (of,1), (you,1), (“”,1), (Yet,1), (you,1), (don’t,1), (know,1), (that,1), (“”,1), (I,1), (love,1), (you.,1), (“”,1), (The,1), (furthest,1), (distance,1), (in,1), (the,1), (world,1), (“”,1), (Is,1), (not,1), (when,1), (I,1), (stand,1), (in,1), (front,1), (of,1), (you,1), (“”,1), (Yet,1), (you,1), (can’t,1), (see,1), (my,1), (love,1), (“”,1), (But,1), (when,1), (undoubtly,1), (knowing,1), (the,1), (love,1), (from,1), (both,1), (“”,1), (Yet,1), (cannot,1), (be,1), (together.,1), …

5、分组。将转换后的数据按照key(即单词)分组。
  在paste模式下输入以下代码,并执行:

1.	val group = pairs.groupBy(_._1)

按下Ctrl D,执行以上代码。输出结果如下案例:词频统:

group: Map[String,List[(String, Int)]] = Map(Tagore -> List((Tagore,1)), “” -> List((“”,1), (“”,1), (“”,1), (“”,1), (“”,1), (“”,1), (“”,1), (“”,1), (“”,1), (“”,1), (“”,1), (“”,1), (“”,1), (“”,1), (“”,1), (“”,1), (“”,1), (“”,1), (“”,1)), knowing -> List((knowing,1)), For -> List((For,1)), death -> List((death,1)), plainly -> List((plainly,1)), in -> List((in,1), (in,1), (in,1), (in,1), (in,1), (in,1), (in,1), (in,1), (in,1)), have -> List((have,1)), using -> List((using,1)), loves -> List((loves,1)), front -> List((front,1), (front,1)), world -> List((world,1), (world,1), (world,1), (world,1), (world,1)), Yet -> List((Yet,1), (Yet,1), (Yet,1), (Yet,1)), tides -> List((tides,1)), river -> List((river,1)), furthest -> List((furthest,1), (furthest,1), (furthest,1), (furthest,1)), apart -> L…

6、聚合。对同一个key(即单词)计数求和,即得到每个单词出现的次数。
  在paste模式下输入以下代码,并执行:

1.	val wordCount = group.map {
2.	      case (word, list) => (word, list.size)
3.	}
4.	 
5.	wordCount.foreach(t => if(t._1 == "world"){ 
6.	    println(t._1   "出现了"   t._2   "次") 
7.	})

按下Ctrl D,执行以上代码。输出结果如下:

world出现了5次。

9. 实验结果及分析:

实验结果运行准确,无误

10. 实验结论:

经过本节实验的学习,通过学习Scala词频统计,进一步巩固了我们的scala基础。

11. 总结及心得体会:

经过本节实验的学习,通过学习Scala词频统计实验,了解Scala的应用场景。
学新通
–end–

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgaceci
系列文章
更多 icon
同类精品
更多 icon
继续加载