go语言之给定英语文章统计单词数量(go语言小练习)
给定一篇英语文章,要求统计出所有单词的个数,并按一定次序输出。思路是利用go语言的map类型,以每个单词作为关键字存储数量信息,代码实现如下:
packagemain
import(
"fmt"
"sort"
)
funcwordCounterV1(strstring){
/*定义变量*/
stringSlice:=str[:]
temp:=str[:]
wordStatistic:=make(map[string]int)
/*把所有出现的单词放入map中*/
j:=0
fori:=0;i=65&&stringSlice[i]<=90)||(stringSlice[i]>=97&&stringSlice[i]<=122)){
temp=str[j:i]
iflen(temp)!=0{
wordStatistic[temp]++
}
j=i+1
}
}
/*把首字母为大写的单词转换为小写;去除无效字符*/
fori:=rangewordStatistic{
iflen(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{
ifi[0]!='a'&&i[0]!='A'{
delete(wordStatistic,i)
}elseifi[0]=='A'{
wordStatistic["a"]+=wordStatistic[i]
delete(wordStatistic,i)
}
}
}
/*把map的关键字映射到string切片进行排序*/
sortSlice:=make([]string,0,len(wordStatistic))
fori:=rangewordStatistic{
sortSlice=append(sortSlice,i)
}
sort.Strings(sortSlice)
/*输出结果*/
for_,v:=rangesortSlice{
fmt.Printf("%s:%d\n",v,wordStatistic[v])
}
fmt.Printf("wordcount:%d\n",len(wordStatistic))
}
主函数随便输入一篇英语文章
funcmain(){
str:=`Therearemomentsinlifewhenyoumisssomeonesomuch
thatyoujustwanttopickthemfromyourdreamsandhugthemfor
real!Dreamwhatyouwanttodream;gowhereyouwanttogo;bewhat
youwanttobe,becauseyouhaveonlyonelifeandonechancetodo
allthethingsyouwanttodo.
Mayyouhaveenoughhappinesstomakeyousweet,enoughtrials
tomakeyoustrong,enoughsorrowtokeepyouhuman,enoughhopeto
makeyouhappy?Alwaysputyourselfinothers'shoes.Ifyoufeel
thatithurtsyou,itprobablyhurtstheotherperson,too.
Thehappiestofpeopledon'tnecessarilyhavethebestof
everything;theyjustmakethemostofeverythingthatcomesalong
theirway.Happinessliesforthosewhocry,thosewhohurt,those
whohavesearched,andthosewhohavetried,foronlytheycan
appreciatetheimportanceofpeople
whohavetouchedtheirlives.Lovebeginswithasmile,growswith
akissandendswithatear.Thebrightestfuturewillalwaysbebased
onaforgottenpast,youcan'tgoonwellinlifeuntilyouletgoof
yourpastfailuresandheartaches.
Whenyouwereborn,youwerecryingandeveryonearoundyouwassmiling.
Liveyourlifesothatwhenyoudie,you'retheonewhoissmilingand
everyonearoundyouiscrying.
Pleasesendthismessagetothosepeoplewhomeansomethingtoyou,
tothosewhohavetouchedyourlifeinonewayoranother,tothosewho
makeyousmilewhenyoureallyneedit,tothosethatmakeyouseethe
brightersideofthingswhenyouarereallydown,tothosewhoyouwant
toletthemknowthatyouappreciatetheirfriendship.Andifyoudon't,
don'tworry,nothingbadwillhappentoyou,youwilljustmissouton
theopportunitytobrightensomeone'sdaywiththismessage.`
//调用功能
wordCounterV1(str)
}
编译后终端输出结果:
C:\Users\24213\goproject>cdsrc\github.com\go-study\lesson6\practice1 C:\Users\24213\goproject\src\github.com\go-study\lesson6\practice1>gobuild C:\Users\24213\goproject\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 wordcount:144
总结
以上所述是小编给大家介绍的go语言之给定英语文章统计单词数量(go语言小练习),希望对大家有所帮助!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。