go语言之给定英语文章统计单词数量(go语言小练习)
Go  /  管理员 发布于 4年前   430
给定一篇英语文章,要求统计出所有单词的个数,并按一定次序输出。思路是利用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\practice1C:\Users\24213\go project\src\github.com\go-study\lesson6\practice1>go buildC:\Users\24213\go project\src\github.com\go-study\lesson6\practice1>practice1a:4all:1along:1always:2and:8another:1appreciate:2are:2around:2bad:1based:1be:3because:1begins:1best:1born:1brighten:1brighter:1brightest:1can:2chance:1comes:1cry:1crying:2day:1die:1do:2don:3down:1dream:2dreams:1ends:1enough:4everyone:2everything:2failures:1feel:1for:3forgotten:1friendship:1from:1future:1go:4grows:1happen:1happiest:1happiness:2happy:1have:7heartaches:1hope:1hug:1human:1hurt:1hurts:2if:2importance:1in:4is:2it:3just:3keep:1kiss:1know:1let:2lies:1life:5live:1lives:1love:1make:6may:1mean:1message:2miss:2moments:1most:1much:1necessarily:1need:1nothing:1of:6on:3one:4only:2opportunity:1or:1other:1others:1out:1past:2people:3person:1pick:1please:1probably:1put:1re:1real:1really:2searched:1see:1send:1shoes:1side:1smile:2smiling:2so:2someone:2something:1sorrow:1strong:1sweet:1tear:1that:6the:10their:3them:3there:1they:2things:2this:2those:9to:19too:1touched:2trials:1tried:1until:1want:6was:1way:2well:1were:2what:2when:5where:1who:10will:3with:4worry:1you:32your:4yourself:1word count:144
总结
以上所述是小编给大家介绍的go语言之给定英语文章统计单词数量(go语言小练习),希望对大家有所帮助!
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号