go语言小练习——给定英语文章统计单词数量
给定一篇英语文章,要求统计出所有单词的个数,并按一定次序输出。思路是利用go语言的map类型,以每个单词作为关键字存储数量信息,代码实现如下:
package main import (
"fmt"
"sort"
) func wordCounterV1(str string) {
/*定义变量*/
stringSlice := str[:]
temp := str[:]
wordStatistic := make(map[string]int) /*把所有出现的单词放入map中*/
j :=
for i := ; i < len(stringSlice); i++ {
if !((stringSlice[i] >= && stringSlice[i] <= ) || (stringSlice[i] >= && stringSlice[i] <= )) {
temp = str[j:i]
if len(temp) != {
wordStatistic[temp]++
}
j = i +
}
} /*把首字母为大写的单词转换为小写;去除无效字符*/
for i := range wordStatistic {
if len(i) > {
if (i[] >= && i[] <= ) && (i[] <= || i[] >= ) {
strTemp := make([]byte, len(i), len(i))
copy(strTemp, i)
strTemp[] +=
wordStatistic[string(strTemp)] += wordStatistic[i]
delete(wordStatistic, i)
}
} else {
if i[] != 'a' && i[] != 'A' {
delete(wordStatistic, i)
} else if i[] == 'A' {
wordStatistic["a"] += wordStatistic[i]
delete(wordStatistic, i)
}
} } /*把map的关键字映射到string切片进行排序*/
sortSlice := make([]string, , 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\\go project>cd src\github.com\go-study\lesson6\practice1 C:\Users\\go project\src\github.com\go-study\lesson6\practice1>go build C:\Users\\go project\src\github.com\go-study\lesson6\practice1>practice1
a:
all:
along:
always:
and:
another:
appreciate:
are:
around:
bad:
based:
be:
because:
begins:
best:
born:
brighten:
brighter:
brightest:
can:
chance:
comes:
cry:
crying:
day:
die:
do:
don:
down:
dream:
dreams:
ends:
enough:
everyone:
everything:
failures:
feel:
for:
forgotten:
friendship:
from:
future:
go:
grows:
happen:
happiest:
happiness:
happy:
have:
heartaches:
hope:
hug:
human:
hurt:
hurts:
if:
importance:
in:
is:
it:
just:
keep:
kiss:
know:
let:
lies:
life:
live:
lives:
love:
make:
may:
mean:
message:
miss:
moments:
most:
much:
necessarily:
need:
nothing:
of:
on:
one:
only:
opportunity:
or:
other:
others:
out:
past:
people:
person:
pick:
please:
probably:
put:
re:
real:
really:
searched:
see:
send:
shoes:
side:
smile:
smiling:
so:
someone:
something:
sorrow:
strong:
sweet:
tear:
that:
the:
their:
them:
there:
they:
things:
this:
those:
to:
too:
touched:
trials:
tried:
until:
want:
was:
way:
well:
were:
what:
when:
where:
who:
will:
with:
worry:
you:
your:
yourself:
word count:

go语言小练习——给定英语文章统计单词数量的更多相关文章
- hadoop-mapreduce-(1)-统计单词数量
编写map程序 package com.cvicse.ump.hadoop.mapreduce.map; import java.io.IOException; import org.apache.h ...
- 统计一段文章的单词频率,取出频率最高的5个单词和个数(python)
练习题:统计一段英语文章的单词频率,取出频率最高的5个单词和个数(用python实现) 先全部转为小写再判定 lower() 怎么判定单词? 1 不是字母的特殊字符作为分隔符分割字符串 (避免特殊字符 ...
- Storm监控文件夹变化 统计文件单词数量
监控指定文件夹,读取文件(新文件动态读取)里的内容,统计单词的数量. FileSpout.java,监控文件夹,读取新文件内容 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...
- 【第二周】Java实现英语文章词频统计(改进1)
本周根据杨老师的spec对英语文章词频统计进行了改进 1.需求分析: 对英文文章中的英文单词进行词频统计并按照有大到小的顺序输出, 2.算法思想: (1)构建一个类用于存放英文单词及其出现的次数 cl ...
- 【第二周】Java实现英语文章词频统计
1.需求:对于给定的英文文章进行单词频率的统计 2.分析: (1)建立一个如下图所示的数据库表word_frequency用来存放单词和其对应数量 (2)Scanner输入要查询的英文文章存入Stri ...
- 【C语言探索之旅】 第一部分第八课:第一个C语言小游戏
内容简介 1.课程大纲 2.第一部分第八课:第一个C语言小游戏 3.第一部分第九课预告: 函数 课程大纲 我们的课程分为四大部分,每一个部分结束后都会有练习题,并会公布答案.还会带大家用C语言编写 ...
- 【微信小程序开发•系列文章六】生命周期和路由
这篇文章理论的知识比较多一些,都是个人观点,描述有失妥当的地方希望读者指出. [微信小程序开发•系列文章一]入门 [微信小程序开发•系列文章二]视图层 [微信小程序开发•系列文章三]数据层 [微信小程 ...
- Atian inputmethod 输入法解决方案 方言与多语言多文字支持 英语汉字汉语阿拉伯文的支持 (au
Atian inputmethod 输入法解决方案 方言与多语言多文字支持 英语汉字汉语阿拉伯文的支持 (au 1.1. Overview概论 支持母语优先的战略性产品,主要是针对不想以及不愿使用普通 ...
- 狗屁不通的“视频专辑:零基础学习C语言(小甲鱼版)”(2)
前文链接:狗屁不通的“视频专辑:零基础学习C语言(小甲鱼版)”(1) 小甲鱼在很多情况下是跟着谭浩强鹦鹉学舌,所以谭浩强书中的很多错误他又重复了一次.这样,加上他自己的错误,错谬之处难以胜数. 由于拙 ...
随机推荐
- 七、Vue组件库:Element、Swiper(轮播专用组件)
一.vue的Element组件库 官网:https://element.eleme.cn/#/zh-CN 1.1安装 推荐安装方法: 首先要进入项目目录 cnpm i element-ui -S 或 ...
- c# quartz
quartz调度核心元素: Scheduler:任务调度器,是实际执行任务调度的控制器.在spring中通过SchedulerFactoryBean封装起来. Trigger:触发器,用于定义任务调度 ...
- [BJDCTF2020]EasySearch
0x00 知识点 Apache SSI 远程命令执行漏洞 链接: https://www.cnblogs.com/yuzly/p/11226439.html 当目标服务器开启了SSI与CGI支持,我们 ...
- P1049 数列的片段和
P1049 数列的片段和 转跳点:
- pr cs6安装教程
这是通过我自己实践操作,网上查询整理的安装流程: 安装 1.下载:http://www.smzy.com/smzy/smzy93225.html 2.断网,安装 如果到2%显示安装失败,在这里有详细解 ...
- CentOS 6.x 重置root 密码
1.重启,进入启动界面,快速按e,进入GNU GRUB界面. 2.选择第二项,按e,进行编辑. 3.在末尾输入1或single,回车,返回上一界面,还是选第二项,按b,进入单用户模式. 此时输入命令 ...
- 官网英文版学习——RabbitMQ学习笔记(三)Hello World!
参考http://www.rabbitmq.com/tutorials/tutorial-one-java.html,我们直接上代码,由于我们的RabbitMQ服务是安装在虚拟机上的,具体参考上一节. ...
- 批量处理文件的Python程序
经常批量处理文件,这里有个python的模板,保存一下 这个例子是把目录里面所有子目录的mp3文件放慢0.85倍并保存到./processed/目录下面. #coding=utf-8 import s ...
- 用 Python监控了另一半的每天都在看的网站,我发现了一个秘密
阅读文本大概需要 5 分钟. ! 需求: (1) 获取你对象chrome前一天的浏览记录中的所有网址(url)和访问时间,并存在一个txt文件中 (2)将这个txt文件发送给指定的邮箱地址(你的邮 ...
- jQuery获取display为none的隐藏元素的宽度和高度的解决方案
1.利用给元素添加行内样式:visibility:hidden;display:block 2.让隐藏元素变成有物理尺寸存在,但不可见,获取元素宽高 3.再给它还原成display为none,去除vi ...