go语言之给定英语文章统计单词数量(go语言小练习)
作者:传参 发布时间:2023-09-16 09:11:12
标签:go,统计,单词,数量
给定一篇英语文章,要求统计出所有单词的个数,并按一定次序输出。思路是利用go语言的map类型,以每个单词作为关键字存储数量信息,代码实现如下:
package main
import (
"fmt"
"sort"
)
func wordCounterV1(str string) {
/*定义变量*/
stringSlice := str[:]
temp := str[:]
wordStatistic := make(map[string]int)
/*把所有出现的单词放入map中*/
j := 0
for i := 0; i < len(stringSlice); i++ {
if !((stringSlice[i] >= 65 && stringSlice[i] <= 90) || (stringSlice[i] >= 97 && stringSlice[i] <= 122)) {
temp = str[j:i]
if len(temp) != 0 {
wordStatistic[temp]++
}
j = i + 1
}
}
/*把首字母为大写的单词转换为小写;去除无效字符*/
for i := range wordStatistic {
if len(i) > 1 {
if (i[0] >= 65 && i[0] <= 90) && (i[1] <= 65 || i[1] >= 90) {
strTemp := make([]byte, len(i), len(i))
copy(strTemp, i)
strTemp[0] += 32
wordStatistic[string(strTemp)] += wordStatistic[i]
delete(wordStatistic, i)
}
} else {
if i[0] != 'a' && i[0] != 'A' {
delete(wordStatistic, i)
} else if i[0] == 'A' {
wordStatistic["a"] += wordStatistic[i]
delete(wordStatistic, i)
}
}
}
/*把map的关键字映射到string切片进行排序*/
sortSlice := make([]string, 0, len(wordStatistic))
for i := range wordStatistic {
sortSlice = append(sortSlice, i)
}
sort.Strings(sortSlice)
/*输出结果*/
for _, v := range sortSlice {
fmt.Printf("%s:%d\n", v, wordStatistic[v])
}
fmt.Printf("word count:%d\n", len(wordStatistic))
}
主函数随便输入一篇英语文章
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
总结
以上所述是小编给大家介绍的go语言之给定英语文章统计单词数量(go语言小练习),希望对大家有所帮助!
来源:https://www.cnblogs.com/ycf-studio/archive/2020/01/28/12237335.html


猜你喜欢
- 【杨辉三角简介】 杨辉三角,是二项式系数在三角形中的一种几何排列。在欧洲,这个表叫做帕斯卡三角形。【代码实现】n = eval(input
- Mootools 1.2手风琴(Accordion)教程原文地址:30 Days of Mootools 1.2 Tutoria
- 在我们学习python的过程中,学习序列是一门必修课。当我们掌握了序列过后,便会学习常用的两个排序函数sort()与sorted()。但很少
- 尝试用python写文件,但是无法写入文件,文件内容为空。原代码片段如下,poem = "This is a poem"
- 本文实例为大家分享了微信小程序实现登陆注册滑块验证的具体代码,供大家参考,具体内容如下一、创建自定义组件MoveVerifyMoveVeri
- 有时候会遇到和上面类似的表单字段。我们可以给每个字段限制输入长度,当达到输入长度时自动切换焦点,以增强表单的易用性 <form id=
- 本文实例讲述了Python将xml和xsl转换为html的方法。分享给大家供大家参考。具体分析如下:这里需要用libxml2,所以还要先安装
- 一个对AJAX的封装//url就是请求的地址//successFunc就是一个请求返回成功之后的一个function,有一个参数,参数就是服
- 一、需求说明需要使用Python实现将内容转为base64编码,解码,方便后续的数据操作。二、base64简介Base64是一种二进制到文本
- 本文实例讲述了Python使用logging模块实现打印log到指定文件的方法。分享给大家供大家参考,具体如下:可能我们经常会使用print
- 导语说到童年爱玩的电脑游戏,你会想到什么?最国民的莫过于金山打字通,接着是扫雷、红心大战,而红极一时的单机游戏当属《大富翁》。嘻嘻 打字游戏
- 一、前端代码<el-upload class="step_content" drag
- 知乎上有人说,Python3.6以后字典有序且更高效了。群里有同学推荐了这篇文章给我看,并咨询字典排序的问题。大致浏览了一下,我当即表示不能
- numpy array存储为.npy存储:import numpy as npnumpy_array = np.array([1,2,3])
- 总结了一下使用Python对xml文件的解析,用到的模块儿如下:分别从xml字符串和xml文件转换为xml对象,然后解析xml内容,查询指定
- 一、什么是事务?数据库事务(简称:事务)是数据库管理系统执行过程中的一个逻辑单位,由一个有限的数据库操作序列构成。二、事务的四大属性分别是原
- 使用iframe嵌入网页,页面可自适应在项目中遇到要嵌入第三方网页的需求,因为没有同第三方页面交互的需求,只需展示即可,所以最终决定使用 i
- KindEditor简介: KindEditor是一套开源的在线HTML编辑器,主要用于让用户在网站上获得所见即所得编辑效果,开发人员可以用
- 考察对于知识的理解,除了实际的代码运用,还有一种方法就是问答类的题型。不同于普通的概念叙述,小编认为即使是面试题也会带有一些数学题目的影响,
- 前言最近已经播完第一季的电视剧《雪中悍刀行》,从播放量就可以看出观众对于这部剧的期待,总播放量达到50亿,可让人遗憾的是,豆瓣评分只有5.7