go语言之给定英语文章统计单词数量(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语言小练习),希望对大家有所帮助!

(0)

相关推荐

  • django项目用higcharts统计最近七天文章点击量

    下载higcharts插件放在static文件夹下 前端引入 <script src="/static/highcharts/highcharts.js"></script> <script src="/static/highcharts/modules/exporting.js"></script> <script src="/static/highcharts/modules/oldie.js&qu

  • 基于Django统计博客文章阅读量

    如何精确地记录一篇文章的阅读量是一个比较复杂的问题,不过对于我们的博客来说,没有必要记录的那么精确.因此我们使用一种简单但有效的方式来记录博客文章的阅读量:文章每被浏览一次,则其阅读量 +1,即所谓的文章页面 PV(Page View)数.虽然简单粗暴,但却高效实用. 增加新字段 为了记录文章的浏览量,需要在文章的数据库表中新增一个用于存储阅读量的字段.因此给博客文章的模型新增一个 views 字段: blog/models.py class Post(models.Model): # ...

  • Golang 函数执行时间统计装饰器的一个实现详解

    背景 最近在搭一个新项目的架子,在生产环境中,为了能实时的监控程序的运行状态,少不了逻辑执行时间长度的统计.时间统计这个功能实现的期望有下面几点: 实现细节要剥离:时间统计实现的细节不期望在显式的写在主逻辑中.因为主逻辑中的其他逻辑和时间统计的抽象层次不在同一个层级 用于时间统计的代码可复用 统计出来的时间结果是可被处理的. 对并发编程友好 实现思路 统计细节的剥离 最朴素的时间统计的实现,可能是下面这个样子: func f() { startTime := time.Now() logicSt

  • MongoDB中强大的统计框架Aggregation使用实例解析

    听说项目里面Aggregation用的多,那就专门针对这个多多练习一下. 基本的操作包括: •$project - 可以从子文档中提取字段,可以重命名字段 •$match - 可以实现查找的功能 •$limit - 接受一个数字n,返回结果集中的前n个文档. •$skip - 接受一个数字n,丢弃结果集中的前n个文档.效率比较低,依然会遍历前n个文档. •$unwind - 可以将一个包含数组的文档切分成多个, 比如你的文档有 中有个数组字段 A, A中有10个元素, 那么经过 $unwind处

  • Google 统计图表(Flash)小插件

    Fusioncharts与Google开发的统计图表小插件.你可以将其添加到自己的igoogle中.添加地址:http://www.fusioncharts.com/gg/ 600)?'600px':'auto'; }" alt=uploads/200709/17_144304_3.gif src="http://files.jb51.net/upload/20070927201722547.gif">

  • 使用django的ORM框架按月统计近一年内的数据方法

    如下所示: # 计算时间 time = datetime.datetime.now() - relativedelta(years=1) # 获取近一年数据 one_year_data = Data.objects.filter(create_time__gte=time_ago) # 分组统计每个月的数据 count_res = one_year_data\ .annotate(year=ExtractYear('create_time'),month=ExtractMonth('create

  • 利用Celery实现Django博客PV统计功能详解

    前言 前几天给网站的文章增加了pv统计,之前只有uv统计.之前没加pv统计是觉得每个用户每访问一次文章,我都需要做一次数据库写操作实在是有损性能,毕竟从用户在the5fire博客的的一次访问来看,只需要从数据库里拿到对应的文章(通常情况下是从缓存中拿),然后返回给浏览器.写操作无意义.之前的uv,也是针对每个用户24小时内只会有一次写操作. 不过话说回来,就对于the5fire博客这么个小站点来说,就算每次访问我写十几次数据库都没啥影响,毕竟量小的可怜.但是咱们码农不是得有颗抗亿级流量的心嘛.

  • MongoDB 中聚合统计计算--$SUM表达式

    我们一般通过表达式$sum来计算总和.因为MongoDB的文档有数组字段,所以可以简单的将计算总和分成两种: 1,统计符合条件的所有文档的某个字段的总和: 2,统计每个文档的数组字段里面的各个数据值的和.这两种情况都可以通过$sum表达式来完成. 以上两种情况的聚合统计,分别对应与聚合框架中的 $group 操作步骤和 $project 操作步骤. 1.$group 直接看例子吧. Case 1 测试集合mycol中的数据如下: { title: 'MongoDB Overview', desc

  • Vue自定义指令上报Google Analytics事件统计的方法

    发现问题 一般前端开发离不开数据统计,我们经常需要接入统计服务以方便运营,例如现在需要统计一个按钮 <template> <button @click="handleClick" /> </template> <script> export default { methods: { handleClick() { window.alert('button click') } } } </script> 引入 ga 后是这样上报

  • Golang 统计字符串字数的方法示例

    比如新浪微博发微博的输入框有一个已输入字数的统计,它的规则推测是:汉字和中文标点算 1 个字数,英文和其他符号算 0.5 个字数.不足 1 个字算 1 个.大家可以去微博体验一下计算方式. golang 可以使用正则和 unicode 包的方法判断. 以下函数 GetStrLength 返回输入的字符串的字数,每个汉字和中文标点算 1 个字数,英文和其他字符算半个字数,不足 1 个字算 1 个. // GetStrLength 返回输入的字符串的字数,汉字和中文标点算 1 个字数,英文和其他字符

随机推荐