系统城装机大师 - 固镇县祥瑞电脑科技销售部宣传站!

当前位置:首页 > 脚本中心 > python > 详细页面

go语言之给定英语文章统计单词数量(go语言小练习)

时间:2020-01-28来源:系统城作者:电脑系统城

给定一篇英语文章,要求统计出所有单词的个数,并按一定次序输出。思路是利用go语言的map类型,以每个单词作为关键字存储数量信息,代码实现如下:

package main

 


 
  1. import (
  2. "fmt"
  3. "sort"
  4. )
  5.  
  6. func wordCounterV1(str string) {
  7. /*定义变量*/
  8. stringSlice := str[:]
  9. temp := str[:]
  10. wordStatistic := make(map[string]int)
  11.  
  12. /*把所有出现的单词放入map中*/
  13. j := 0
  14. for i := 0; i < len(stringSlice); i++ {
  15. if !((stringSlice[i] >= 65 && stringSlice[i] <= 90) || (stringSlice[i] >= 97 && stringSlice[i] <=122)) {
  16. temp = str[j:i]
  17. if len(temp) != 0 {
  18. wordStatistic[temp]++
  19. }
  20. j = i + 1
  21. }
  22. }
  23.  
  24. /*把首字母为大写的单词转换为小写;去除无效字符*/
  25. for i := range wordStatistic {
  26. if len(i) > 1 {
  27. if (i[0] >= 65 && i[0] <= 90) && (i[1] <= 65 || i[1] >= 90) {
  28. strTemp := make([]byte, len(i), len(i))
  29. copy(strTemp, i)
  30. strTemp[0] += 32
  31. wordStatistic[string(strTemp)] += wordStatistic[i]
  32. delete(wordStatistic, i)
  33. }
  34. } else {
  35. if i[0] != 'a' && i[0] != 'A' {
  36. delete(wordStatistic, i)
  37. } else if i[0] == 'A' {
  38. wordStatistic["a"] += wordStatistic[i]
  39. delete(wordStatistic, i)
  40. }
  41. }
  42.  
  43. }
  44.  
  45. /*把map的关键字映射到string切片进行排序*/
  46. sortSlice := make([]string, 0, len(wordStatistic))
  47. for i := range wordStatistic {
  48. sortSlice = append(sortSlice, i)
  49. }
  50. sort.Strings(sortSlice)
  51.  
  52. /*输出结果*/
  53. for _, v := range sortSlice {
  54. fmt.Printf("%s:%d\n", v, wordStatistic[v])
  55. }
  56. fmt.Printf("word count:%d\n", len(wordStatistic))
  57. }

主函数随便输入一篇英语文章


 
  1. func main() {
  2.  
  3. str := ` There are moments in life when you miss someone so much
  4. that you just want to pick them from your dreams and hug them for
  5. real! Dream what you want to dream;go where you want to go;be what
  6. you want to be,because you have only one life and one chance to do
  7. all the things you want to do.
  8.  
  9.   May you have enough happiness to make you sweet,enough trials
  10. to make you strong,enough sorrow to keep you human,enough hope to
  11. make you happy? Always put yourself in others'shoes.If you feel
  12. that it hurts you,it probably hurts the other person, too.
  13.  
  14.   The happiest of people don't necessarily have the best of
  15. everything;they just make the most of everything that comes along
  16. their way.Happiness lies for those who cry,those who hurt, those
  17. who have searched,and those who have tried,for only they can
  18. appreciate the importance of people
  19.  
  20.   who have touched their lives.Love begins with a smile,grows with
  21. a kiss and ends with a tear.The brightest future will always be based
  22. on a forgotten past, you can't go on well in life until you let go of
  23. your past failures and heartaches.
  24.  
  25.   When you were born,you were crying and everyone around you was smiling.
  26. Live your life so that when you die,you're the one who is smiling and
  27. everyone around you is crying.
  28.  
  29.   Please send this message to those people who mean something to you,
  30. to those who have touched your life in one way or another,to those who
  31. make you smile when you really need it,to those that make you see the
  32. brighter side of things when you are really down,to those who you want
  33. to let them know that you appreciate their friendship.And if you don't,
  34. don't worry,nothing bad will happen to you,you will just miss out on
  35. the opportunity to brighten someone's day with this message.`
  36.   //调用功能
  37. wordCounterV1(str)
  38. }

编译后终端输出结果:


 
  1. C:\Users\24213\go project>cd src\github.com\go-study\lesson6\practice1
  2.  
  3. C:\Users\24213\go project\src\github.com\go-study\lesson6\practice1>go build
  4.  
  5. C:\Users\24213\go project\src\github.com\go-study\lesson6\practice1>practice1
  6. a:4
  7. all:1
  8. along:1
  9. always:2
  10. and:8
  11. another:1
  12. appreciate:2
  13. are:2
  14. around:2
  15. bad:1
  16. based:1
  17. be:3
  18. because:1
  19. begins:1
  20. best:1
  21. born:1
  22. brighten:1
  23. brighter:1
  24. brightest:1
  25. can:2
  26. chance:1
  27. comes:1
  28. cry:1
  29. crying:2
  30. day:1
  31. die:1
  32. do:2
  33. don:3
  34. down:1
  35. dream:2
  36. dreams:1
  37. ends:1
  38. enough:4
  39. everyone:2
  40. everything:2
  41. failures:1
  42. feel:1
  43. for:3
  44. forgotten:1
  45. friendship:1
  46. from:1
  47. future:1
  48. go:4
  49. grows:1
  50. happen:1
  51. happiest:1
  52. happiness:2
  53. happy:1
  54. have:7
  55. heartaches:1
  56. hope:1
  57. hug:1
  58. human:1
  59. hurt:1
  60. hurts:2
  61. if:2
  62. importance:1
  63. in:4
  64. is:2
  65. it:3
  66. just:3
  67. keep:1
  68. kiss:1
  69. know:1
  70. let:2
  71. lies:1
  72. life:5
  73. live:1
  74. lives:1
  75. love:1
  76. make:6
  77. may:1
  78. mean:1
  79. message:2
  80. miss:2
  81. moments:1
  82. most:1
  83. much:1
  84. necessarily:1
  85. need:1
  86. nothing:1
  87. of:6
  88. on:3
  89. one:4
  90. only:2
  91. opportunity:1
  92. or:1
  93. other:1
  94. others:1
  95. out:1
  96. past:2
  97. people:3
  98. person:1
  99. pick:1
  100. please:1
  101. probably:1
  102. put:1
  103. re:1
  104. real:1
  105. really:2
  106. searched:1
  107. see:1
  108. send:1
  109. shoes:1
  110. side:1
  111. smile:2
  112. smiling:2
  113. so:2
  114. someone:2
  115. something:1
  116. sorrow:1
  117. strong:1
  118. sweet:1
  119. tear:1
  120. that:6
  121. the:10
  122. their:3
  123. them:3
  124. there:1
  125. they:2
  126. things:2
  127. this:2
  128. those:9
  129. to:19
  130. too:1
  131. touched:2
  132. trials:1
  133. tried:1
  134. until:1
  135. want:6
  136. was:1
  137. way:2
  138. well:1
  139. were:2
  140. what:2
  141. when:5
  142. where:1
  143. who:10
  144. will:3
  145. with:4
  146. worry:1
  147. you:32
  148. your:4
  149. yourself:1
  150. word count:144

总结

以上所述是小编给大家介绍的go语言之给定英语文章统计单词数量(go语言小练习),希望对大家有所帮助!

分享到:

相关信息

系统教程栏目

栏目热门教程

人气教程排行

站长推荐

热门系统下载