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) 小甲鱼在很多情况下是跟着谭浩强鹦鹉学舌,所以谭浩强书中的很多错误他又重复了一次.这样,加上他自己的错误,错谬之处难以胜数. 由于拙 ...
随机推荐
- Day4 - M - Roads in Berland CodeForces - 25C
There are n cities numbered from 1 to n in Berland. Some of them are connected by two-way roads. Eac ...
- 浅谈Spring 5的响应式编程
这篇使用Spring 5进行响应式编程的入门文章展示了你现在可以使用的一些新的non-blocking, asynchronous.感谢优锐课老师给予的指导! 近年来,由于响应式编程能够以声明性的方式 ...
- JS: 复选框——ALL与A、B、C(选中ALL同时选中各子项)
<!DOCTYPE html><html> <head> <meta charset="utf-8"> <title> ...
- IQueryable 转DataTable
public static DataTable CopyToDataTable<T>(IEnumerable<T> array) { var ret = new DataTab ...
- win上java1.7和1.8版本修改环境变量无效.md
网上找了很多办法都没用. 解决办法: 看看自己 "系统环境变量" 中是不是有 "C:\ProgramData\Oracle\Java\javapath" 这项配 ...
- springcloud--ruul(路由网关)
Zuul的主要功能就是路由转发和过滤器 实例: 1:添加依赖pom.xml: <dependencies> <dependency> <groupId>org.sp ...
- plsql和navicat连接远程oracle(易错点)
plsql和navicat连接远程oracle,只需要安装oracle客户端即可.注意此处是oracle客户端(Instant Client),并不是oracle数据库. oracle客户端下载地址: ...
- 利用ThoughtWorks.QRCode生成二维码
一.项目添加ThoughtWorks.QRCode.dll和System.Drawing.dll的引用 二.创建二维码公共处理类(QRCodeHandler.cs) /// <summary&g ...
- [题解] LuoguP4609 [FJOI2016]建筑师
传送门 首先对于高度为\(n\)的建筑,他的左边有\(A-1\)个建筑能被看到,右边有\(B-1\)个建筑能被看到,这两者类似,所以先来看左边. 一个建筑将会遮挡住它后面的高度比它矮的建筑,直到一个高 ...
- Atom :奥特曼的使用
最近在使用atom的编译器,很不爽,什么快捷键,还有识别vue的页面,还有注释这种快捷下载下来的都没有 必须到setting里面的install里下载,我能大声的说我很不爽吗............ ...