Scala案例:全文单词统计

2.txt内容如下

复制代码
Thank you very much.


Well I want to thank you all very much this is great, these are our friends, we have thousands of friends in this incredible movement.


This was a movement like no nobody's ever seen before, and frankly this was I believe the greatest political movement of all time.


There's never been anything like this in this country, and maybe beyond and now it's going to reach a new level of importance, because we're going to help our country heal, we're going to help our country heal, we have a country that needs help and it needs help very badly, we're going to fix our borders, we're going to fix everything about our country, and we made history for a reason tonight, and the reason is going to be just that.


We overcame obstacles that nobody thought possible, and it is now clear that we've achieved the most incredible political thing, look what happened is this crazy?


But it's a political victory that our country has never seen before nothing like this.



I want to thank the American people for the extraordinary honor of being elected your 47th president and your 45th president.



And every citizen I will fight for you for your family and your future every single s day.


I will be fighting for you, and with every breath in my body.


I will not rest until we have delivered the strong safe and prosperous America that our children deserve, and that you deserve.


This will truly be the Golden Age of America that's what we have to have.


This is a magnificent victory for the American people that will allow us to make America great again.


And in addition to having won the battleground states of North Carolina, I love these places.


Georgia Pennsylvania and Wisconsin we are now winning in Michigan Arizona Nevada and Alaska, which would result in US carrying at least 315 electoral votes, but as much easier doing what the networks did or whoever called it, because there was no other path.


There was no other path to victory.


We also have won the popular vote that was great. Thank you very much thank you.


Winning the popular vote was very nice very nice, I will tell you.


It's a great a great feeling of love we have a great feeling of love in this very large room with unbelievable people, standing by my side.


These people have been incredible they've made the journey with me and we're going to make you very happy, we're going to make you very proud of your vote.


I hope that you're going to be looking back someday, and say that was one of the truly important moments of my life, when I voted for this group of people beyond the president, this group of great people.


America has given us an unprecedented and powerful mandate.


We have taken back control of the Senate. That's great And the Senate races in Montana Nevada Texas Ohio Michigan Wisconsin, the great Commonwealth of Pennsylvania we all won by the MAGA movement they helped so much.


And in those cases every one of them we worked with the Senators.


They were tough races and I mean the number of victories in the Senate was absolutely incredible, and we did tell rallies we did tell rallies, with each one of them, and sometimes we did two or three for and it was amazing to look at all of those victories, nobody expected that nobody.



So I just wanted to thank you very much for that and we have.


You have some great senators and some great new senators, and it also looks like we'll be keeping control of the House of Representatives, and I want to thank Mike Johnson, I think he's doing a terrific job terrific job.


I want to also thank my beautiful wife Melania first lady, who has the number one bestselling book in the country can you believe that?


Now she's done a great job, works very hard, works very hard to help people, so I just want to thank her, but I want to thank my whole family, my amazing children and they are amazing children.


Now we all thank our children are, everybody here thinks their children are amazing but that's a good thing when you think they are, but Don, Eric, Ivanka, Tiffany, Baron, Lura, Jared, Kimberly, Michael, thank you all what a help.
Scala 复制代码
import java.io.PrintWriter
import scala.collection.mutable
import scala.io.Source
object t20 {
  def main(args: Array[String]): Unit = {
    //1.读入文件
    val content=Source.fromFile("C:\\Users\\Administrator\\Desktop\\2.txt").mkString
    println(content)

    //2.拆分字符串 --> 单词数组
//    val rs=content.split(" ")   //正则表达式
//    \\正则
//    W:表示非字符(,空格 ?...)
//    W+:多个非字符

    val rs=content.split("\\W+")
    println("-"*40)
    rs.foreach(println)

    //3.统计出现的频率
    val wordsMap=mutable.Map[String,Int]()
    rs.foreach(w=>{
      val word=w.toLowerCase()//全小写
      //是否出现
      if(wordsMap.contains(word)){
        wordsMap(word)+=1
      }else{
        wordsMap(word)=1
      }
    })

    //4.根据出现的次数从高到低排序
    // Map不能直接排序,需要转成有序的集合
    val orderlist=wordsMap.toList.sortWith((a,b)=>a._2>b._2)

    //5.保存结果,涉及写入文件
    val writer=new PrintWriter("output.txt")
    for(e<-orderlist){
      println(e._1,e._2)//打印到屏幕
      writer.println(s"${e._1} : ${e._2}次")
    }
    writer.close()
  }
}
相关推荐
WorkAgent1 小时前
windows下本地部署安装hadoop+scala+spark-【不需要虚拟机】
hadoop·spark·scala
JoneMaster7 天前
[读书日志]从零开始学习Chisel 第十二篇:Scala的抽象成员(敏捷硬件开发语言Chisel与数字系统设计)
开发语言·学习·scala
wlyang6669 天前
4. scala高阶之隐式转换与泛型
大数据·开发语言·后端·spark·scala
一杯拿铁go11 天前
[sparkstreaming]java.lang.NoSuchMethodError:错误以及更改
scala·noclassdeffound
百流11 天前
scala基础学习(数据类型)-集合
开发语言·学习·scala
JoneMaster11 天前
[读书日志]从零开始学习Chisel 第十篇:Scala的模式匹配(敏捷硬件开发语言Chisel与数字系统设计)
开发语言·学习·scala
JoneMaster11 天前
[读书日志]从零开始学习Chisel 第十一篇:Scala的类型参数化(敏捷硬件开发语言Chisel与数字系统设计)
开发语言·学习·scala
小白学大数据12 天前
如何使用Scala和Selenium爬取知乎视频并保存到本地
chrome·python·selenium·scala
小_太_阳12 天前
scala_【JVM】概述
开发语言·jvm·scala
wlyang66612 天前
2. Scala 高阶语法之集合与元组
开发语言·后端·scala