go语言小练习——给定英语文章统计单词数量

  给定一篇英语文章,要求统计出所有单词的个数,并按一定次序输出。思路是利用go语言的map类型,以每个单词作为关键字存储数量信息,代码实现如下:

 1 package main
 2
 3 import (
 4     "fmt"
 5     "sort"
 6 )
 7
 8 func wordCounterV1(str string) {
 9     /*定义变量*/
10     stringSlice := str[:]
11     temp := str[:]
12     wordStatistic := make(map[string]int)
13
14     /*把所有出现的单词放入map中*/
15     j := 0
16     for i := 0; i < len(stringSlice); i++ {
17         if !((stringSlice[i] >= 65 && stringSlice[i] <= 90) || (stringSlice[i] >= 97 && stringSlice[i] <= 122)) {
18             temp = str[j:i]
19             if len(temp) != 0 {
20                 wordStatistic[temp]++
21             }
22             j = i + 1
23         }
24     }
25
26     /*把首字母为大写的单词转换为小写;去除无效字符*/
27     for i := range wordStatistic {
28         if len(i) > 1 {
29             if (i[0] >= 65 && i[0] <= 90) && (i[1] <= 65 || i[1] >= 90) {
30                 strTemp := make([]byte, len(i), len(i))
31                 copy(strTemp, i)
32                 strTemp[0] += 32
33                 wordStatistic[string(strTemp)] += wordStatistic[i]
34                 delete(wordStatistic, i)
35             }
36         } else {
37             if i[0] != ‘a‘ && i[0] != ‘A‘ {
38                 delete(wordStatistic, i)
39             } else if i[0] == ‘A‘ {
40                 wordStatistic["a"] += wordStatistic[i]
41                 delete(wordStatistic, i)
42             }
43         }
44
45     }
46
47     /*把map的关键字映射到string切片进行排序*/
48     sortSlice := make([]string, 0, len(wordStatistic))
49     for i := range wordStatistic {
50         sortSlice = append(sortSlice, i)
51     }
52     sort.Strings(sortSlice)
53
54     /*输出结果*/
55     for _, v := range sortSlice {
56         fmt.Printf("%s:%d\n", v, wordStatistic[v])
57     }
58     fmt.Printf("word count:%d\n", len(wordStatistic))
59 }

  主函数随便输入一篇英语文章

func main() {
    str := `    There are moments in life when you miss someone so much
    that you just want to pick them from your dreams and hug them for
    real! Dream what you want to dream;go where you want to go;be what
    you want to be,because you have only one life and one chance to do
    all the things you want to do.

      May you have enough happiness to make you sweet,enough trials
     to make you strong,enough sorrow to keep you human,enough hope to
      make you happy? Always put yourself in others’shoes.If you feel
       that it hurts you,it probably hurts the other person, too.

      The happiest of people don’t necessarily have the best of
     everything;they just make the most of everything that comes along
      their way.Happiness lies for those who cry,those who hurt, those
       who have searched,and those who have tried,for only they can
        appreciate the importance of people

      who have touched their lives.Love begins with a smile,grows with
     a kiss and ends with a tear.The brightest future will always be based
      on a forgotten past, you can’t go on well in life until you let go of
       your past failures and heartaches.

      When you were born,you were crying and everyone around you was smiling.
    Live your life so that when you die,you‘re the one who is smiling and
     everyone around you is crying.

      Please send this message to those people who mean something to you,
    to those who have touched your life in one way or another,to those who
    make you smile when you really need it,to those that make you see the
    brighter side of things when you are really down,to those who you want
     to let them know that you appreciate their friendship.And if you don’t,
      don’t worry,nothing bad will happen to you,you will just miss out on
      the opportunity to brighten someone’s day with this message.`

  //调用功能
    wordCounterV1(str)
}

  编译后终端输出结果:

C:\Users\24213\go project>cd src\github.com\go-study\lesson6\practice1

C:\Users\24213\go project\src\github.com\go-study\lesson6\practice1>go build

C:\Users\24213\go project\src\github.com\go-study\lesson6\practice1>practice1
a:4
all:1
along:1
always:2
and:8
another:1
appreciate:2
are:2
around:2
bad:1
based:1
be:3
because:1
begins:1
best:1
born:1
brighten:1
brighter:1
brightest:1
can:2
chance:1
comes:1
cry:1
crying:2
day:1
die:1
do:2
don:3
down:1
dream:2
dreams:1
ends:1
enough:4
everyone:2
everything:2
failures:1
feel:1
for:3
forgotten:1
friendship:1
from:1
future:1
go:4
grows:1
happen:1
happiest:1
happiness:2
happy:1
have:7
heartaches:1
hope:1
hug:1
human:1
hurt:1
hurts:2
if:2
importance:1
in:4
is:2
it:3
just:3
keep:1
kiss:1
know:1
let:2
lies:1
life:5
live:1
lives:1
love:1
make:6
may:1
mean:1
message:2
miss:2
moments:1
most:1
much:1
necessarily:1
need:1
nothing:1
of:6
on:3
one:4
only:2
opportunity:1
or:1
other:1
others:1
out:1
past:2
people:3
person:1
pick:1
please:1
probably:1
put:1
re:1
real:1
really:2
searched:1
see:1
send:1
shoes:1
side:1
smile:2
smiling:2
so:2
someone:2
something:1
sorrow:1
strong:1
sweet:1
tear:1
that:6
the:10
their:3
them:3
there:1
they:2
things:2
this:2
those:9
to:19
too:1
touched:2
trials:1
tried:1
until:1
want:6
was:1
way:2
well:1
were:2
what:2
when:5
where:1
who:10
will:3
with:4
worry:1
you:32
your:4
yourself:1
word count:144

原文地址:https://www.cnblogs.com/ycf-studio/p/12237335.html

时间: 2024-08-21 00:39:11

go语言小练习——给定英语文章统计单词数量的相关文章

Java的TreeMap统计单词数量

使用TreeMap统计单词个数,并输出单词和书目,单词按升序排列 TreeMap的特点是无重复元素,且元素的key值既可以按默认的Comparable接口排序也可以按Comparator比较器排序,为TreeMap设计一个比较器,此比较器要实现Comparator接口 //可以扩展到从文件中或者从控制台输入单词,来统计,只需要加入流就行了 import java.util.Map; import java.util.Set; import java.util.TreeMap; public cl

hadoop-mapreduce-(1)-统计单词数量

编写map程序 package com.cvicse.ump.hadoop.mapreduce.map; import java.io.IOException; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Mapper; public cl

统计一段文章的单词频率,取出频率最高的5个单词和个数(python)

练习题:统计一段英语文章的单词频率,取出频率最高的5个单词和个数(用python实现) 怎么判定单词?1 不是字母的特殊字符作为分隔符分割字符串 (避免特殊字符的处理不便,全部替换成'-')2 遍历字符串,取每个word3 正则匹配 怎么统计个数?将wordlist的word和word的个数放入dict,排序 ''' dinghanhua 2018-11-11 练习:一段英文文章,统计每个单词的频率,返回出现频率最高的5个单词和次数 ''' import re art = ' If we wan

Storm监控文件夹变化 统计文件单词数量

监控指定文件夹,读取文件(新文件动态读取)里的内容,统计单词的数量. FileSpout.java,监控文件夹,读取新文件内容 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65

Java实现英语文章词频统计

1.需求:对于给定的英文文章进行单词频率的统计 2.分析: (1)建立一个如下图所示的数据库表word_frequency用来存放单词和其对应数量 (2)Scanner输入要查询的英文文章存入String中 (3)对String根据空格进行拆分存入word_frequency表中,并统计相应数量 (4)对word_frequency表中的数据按照频率由大到小,频率相同的情况下按照字母表顺序排序并输出 3.具体实现代码: 4.输入语句:You should help to set the dinn

【C语言探索之旅】 第一部分第八课:第一个C语言小游戏

? 内容简介 1.课程大纲 2.第一部分第八课:第一个C语言小游戏 3.第一部分第九课预告: 函数 课程大纲 我们的课程分为四大部分,每一个部分结束后都会有练习题,并会公布答案.还会带大家用C语言编写三个游戏. C语言编程基础知识 什么是编程? 工欲善其事,必先利其器 你的第一个程序 变量的世界 运算那点事 条件表达式 循环语句 实战:第一个C语言小游戏 函数 练习题 习作:完善第一个C语言小游戏 C语言高级技术 模块化编程 进击的指针,C语言王牌 数组 字符串 预处理 创建你自己的变量类型 文

草滩小恪与英语单词--弱爆的小程序

草滩小恪一直为如何学习英语而苦恼, 特别是单词的记忆.临近考试啦,草滩小恪想恶补一下英语单词, 但是草滩小恪又是very lazy 所以 草滩小恪就找到了草滩大学的历年英语考试卷, 想背一下 阅读 里面出现的高频词汇.草滩小恪认为这idea真TM太机智啦!!!. 但是, 很快草滩小恪就发现, 寻找短文里面的高频词汇真TN的不是人能干的事.那么问题来啦, 咋办呢? 机智的读者想必早已知道了咋办. 是的, 就是这么办的. 程序说明: 主要功能: 统计一篇英语文章里的高频词汇 附加功能:练习拼写这些高

C语言小游戏设计报告

课程设计名称:贪吃蛇小游戏 专业班级:计科15-2 学号:150809229 姓名:XXX 一.设计目标 通过设计,培养学生对电脑的动手能力,使学生巩固<C语言程序设计>课程学习的内容,掌握编写程序的基本方法,强化对其的动手能力,可以独自完成程序的编写. 二.设计内容和要求 设计内容 编写贪吃蛇的小游戏,使其可以成功运行并且操作玩耍. 设计要求 1)源程序要有适当的注释,使程序便于阅读. 2)要有程序运行结果作为依据 三.程序流程 1.编写地图 运用函数,数组编写地图 2.相同的方法把蛇添加进

Linux下简单C语言小程序的反汇编分析

韩洋原创作品转载请注明出处<Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 写在开始,本文为因为参加MOOC相关课程而写的作业,如有疏漏,还请指出. 选了一门Linux内核分析课程,因为阅读内核代码中或多或少要涉及到At&T汇编代码的阅读,所以这里写下一个对一个简单C命令行程序的反汇编分析过程,一方面完成作业,另一方面当作练手.下面开始: 1.编写我们的C语言小程序 这里我们使用简单的例子,代码如下: 1