给定一篇英语文章,要求统计出所有单词的个数,并按一定次序输出。思路是利用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语言小练习——给定英语文章统计单词数量的更多相关文章

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

    编写map程序 package com.cvicse.ump.hadoop.mapreduce.map; import java.io.IOException; import org.apache.h ...

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

    练习题:统计一段英语文章的单词频率,取出频率最高的5个单词和个数(用python实现) 先全部转为小写再判定 lower() 怎么判定单词? 1 不是字母的特殊字符作为分隔符分割字符串 (避免特殊字符 ...

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

    监控指定文件夹,读取文件(新文件动态读取)里的内容,统计单词的数量. FileSpout.java,监控文件夹,读取新文件内容 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...

  4. 【第二周】Java实现英语文章词频统计(改进1)

    本周根据杨老师的spec对英语文章词频统计进行了改进 1.需求分析: 对英文文章中的英文单词进行词频统计并按照有大到小的顺序输出, 2.算法思想: (1)构建一个类用于存放英文单词及其出现的次数 cl ...

  5. 【第二周】Java实现英语文章词频统计

    1.需求:对于给定的英文文章进行单词频率的统计 2.分析: (1)建立一个如下图所示的数据库表word_frequency用来存放单词和其对应数量 (2)Scanner输入要查询的英文文章存入Stri ...

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

    ​ 内容简介 1.课程大纲 2.第一部分第八课:第一个C语言小游戏 3.第一部分第九课预告: 函数 课程大纲 我们的课程分为四大部分,每一个部分结束后都会有练习题,并会公布答案.还会带大家用C语言编写 ...

  7. 【微信小程序开发•系列文章六】生命周期和路由

    这篇文章理论的知识比较多一些,都是个人观点,描述有失妥当的地方希望读者指出. [微信小程序开发•系列文章一]入门 [微信小程序开发•系列文章二]视图层 [微信小程序开发•系列文章三]数据层 [微信小程 ...

  8. Atian inputmethod 输入法解决方案 方言与多语言多文字支持 英语汉字汉语阿拉伯文的支持 (au

    Atian inputmethod 输入法解决方案 方言与多语言多文字支持 英语汉字汉语阿拉伯文的支持 (au 1.1. Overview概论 支持母语优先的战略性产品,主要是针对不想以及不愿使用普通 ...

  9. 狗屁不通的“视频专辑:零基础学习C语言(小甲鱼版)”(2)

    前文链接:狗屁不通的“视频专辑:零基础学习C语言(小甲鱼版)”(1) 小甲鱼在很多情况下是跟着谭浩强鹦鹉学舌,所以谭浩强书中的很多错误他又重复了一次.这样,加上他自己的错误,错谬之处难以胜数. 由于拙 ...

随机推荐

  1. Concurrent包下用过哪些类?

    1.executor接口,使用executor接口的子接口ExecutorService用来创建线程池2.Lock接口下的ReentrantLock类,实现同步,比如三个线程循环打印ABCABCABC ...

  2. 2018年第九届蓝桥杯【C++省赛B组】(未完)

    第一题 标题:第几天 2000年的1月1日,是那一年的第1天. 那么,2000年的5月4日,是那一年的第几天? 注意:需要提交的是一个整数,不要填写任何多余内容 利用excel更加快捷: 答案是125 ...

  3. leetcode922 Sort Array By Parity II

    """ Given an array A of non-negative integers, half of the integers in A are odd, and ...

  4. linux 查看运行进程详细信息

    Linux在启动一个进程时,系统会在/proc下创建一个以PID命名的文件夹,在该文件夹下会有我们的进程的信息 通过ll或ls –l命令即可查看. ll /proc/PID cwd符号链接的是进程运行 ...

  5. linux后台运行jar

    1. 在linux服务器上运行Jar文件时通常的方法是: $ java -jar test.jar 这种方式当ssh窗口关闭时,程序中止运行,或者是运行时没法切出去执行其他任务 2. 保持程序后台运行 ...

  6. JavaScript动态实现div窗口弹出&消失功能

    先积累一个JavaScript动态实现div窗口弹出&消失功能 首先是index.jsp代码 <html> <head> <link rel="styl ...

  7. Window Server 2019 配置篇(4)- 配置WDS实现自动部署服务

    上次我们建立了hyper-v集群,那么我们这次需要对集群内的客户机和外部的服务器进行自动部署,这时候需要WDS服务 WDS服务是一种自动部署服务,能够对无OS的计算机进行操作系统的部署 首先建立虚拟机 ...

  8. POJ 1852:Ants

    Ants Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 11754   Accepted: 5167 Description ...

  9. 06--Java--Scanner类读入控制台

    Scanner类读入控制台 1.什么是Scanner类 Scanner类是java中从控制台读入用户输入的类 import java.util.Scanner; public class a_Lear ...

  10. 107-PHP类成员属性赋值

    <?php class mao{ //定义猫类 public $age; //定义多个成员属性 protected $weight; private $color; } $mao1=new ma ...