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) 小甲鱼在很多情况下是跟着谭浩强鹦鹉学舌,所以谭浩强书中的很多错误他又重复了一次.这样,加上他自己的错误,错谬之处难以胜数. 由于拙 ...
随机推荐
- Django(十二)视图--利用jquery从后台发送ajax请求并处理、ajax登录案例
一.Ajax基本概念 [参考]:https://www.runoob.com/jquery/jquery-ajax-intro.html 异步的javascript.在不全部加载某一个页面部的情况下, ...
- tab选项卡,不带自动切换定时器
<!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content ...
- UVA - 10382 Watering Grass(几何)
题意:有一个矩形,n个圆.已知矩形的长宽和圆的半径,问最少需多少个圆将矩形完全覆盖. 分析: 1.首先求圆与矩形的长的交点,若无交点,则一定不能对用最少的圆覆盖矩形有贡献. 2.如果两个圆与矩形相交所 ...
- 官网英文版学习——RabbitMQ学习笔记(三)Hello World!
参考http://www.rabbitmq.com/tutorials/tutorial-one-java.html,我们直接上代码,由于我们的RabbitMQ服务是安装在虚拟机上的,具体参考上一节. ...
- 指令——touch
一个完整的指令的标准格式: Linux通用的格式——#指令主体(空格) [选项](空格) [操作对象] 一个指令可以包含多个选项,操作对象也可以是多个. 指令:touch 作用:创建文件 语法: ...
- 《Java面试全解析》1000道面试题大全详解(转)
<Java面试全解析>1000道 面试题大全详解 本人是 2009 年参加编程工作的,一路上在技术公司摸爬滚打,前几年一直在上海,待过的公司有 360 和游久游戏,因为自己家庭的原因,放弃 ...
- doc转docx
# -*- coding: utf-8-*- import win32com from win32com.client import Dispatch w = win32com.client.Disp ...
- Linux 文件夹和文件大小排序
Linux 文件夹和文件大小排序 文件夹排序 du -k | sort -rn 文件排序 ls -lS -r, –reverse 依相反次序排列 -R, –recursive 同时列出所有子目录层 - ...
- 132-PHP子类和父类同名函数的调用
<?php class father{ //定义father类 public function cook(){ return '烹饪'; } } class son extends father ...
- 干货分享:学术Essay写作流程及写作技巧详解
Academic essay是指留学生作业中的一种,其范围非常广泛,可以是任何一种话题.而学术essay主要是指其中比较正式的.客观的话题,有明确的研究目的与研究对象.例如“Research on t ...