go語言小練習——給定英語文章統計單詞數量

来源:https://www.cnblogs.com/ycf-studio/archive/2020/01/28/12237335.html
-Advertisement-
Play Games

給定一篇英語文章,要求統計出所有單詞的個數,並按一定次序輸出。思路是利用go語言的map類型,以每個單詞作為關鍵字存儲數量信息,代碼實現如下: 1 package main 2 3 import ( 4 "fmt" 5 "sort" 6 ) 7 8 func wordCounterV1(str st ...


  給定一篇英語文章,要求統計出所有單詞的個數,並按一定次序輸出。思路是利用go語言的map類型,以每個單詞作為關鍵字存儲數量信息,代碼實現如下:

 1 package main
 2 
 3 import (
 4     "fmt"
 5     "sort"
 6 )
 7 
 8 func wordCounterV1(str string) {
 9     /*定義變數*/
10     stringSlice := str[:]
11     temp := str[:]
12     wordStatistic := make(map[string]int)
13 
14     /*把所有出現的單詞放入map中*/
15     j := 0
16     for i := 0; i < len(stringSlice); i++ {
17         if !((stringSlice[i] >= 65 && stringSlice[i] <= 90) || (stringSlice[i] >= 97 && stringSlice[i] <= 122)) {
18             temp = str[j:i]
19             if len(temp) != 0 {
20                 wordStatistic[temp]++
21             }
22             j = i + 1
23         }
24     }
25 
26     /*把首字母為大寫的單詞轉換為小寫;去除無效字元*/
27     for i := range wordStatistic {
28         if len(i) > 1 {
29             if (i[0] >= 65 && i[0] <= 90) && (i[1] <= 65 || i[1] >= 90) {
30                 strTemp := make([]byte, len(i), len(i))
31                 copy(strTemp, i)
32                 strTemp[0] += 32
33                 wordStatistic[string(strTemp)] += wordStatistic[i]
34                 delete(wordStatistic, i)
35             }
36         } else {
37             if i[0] != 'a' && i[0] != 'A' {
38                 delete(wordStatistic, i)
39             } else if i[0] == 'A' {
40                 wordStatistic["a"] += wordStatistic[i]
41                 delete(wordStatistic, i)
42             }
43         }
44 
45     }
46 
47     /*把map的關鍵字映射到string切片進行排序*/
48     sortSlice := make([]string, 0, len(wordStatistic))
49     for i := range wordStatistic {
50         sortSlice = append(sortSlice, i)
51     }
52     sort.Strings(sortSlice)
53 
54     /*輸出結果*/
55     for _, v := range sortSlice {
56         fmt.Printf("%s:%d\n", v, wordStatistic[v])
57     }
58     fmt.Printf("word count:%d\n", len(wordStatistic))
59 }

 

  主函數隨便輸入一篇英語文章

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

 

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • ![](https://img2018.cnblogs.com/blog/1853166/202001/1853166-20200127150544826-1977734878.png) ![](https://img2018.cnblogs.com/blog/1853166/202001/1853... ...
  • javascript中實現一個鏈表的快速排序 ...
  • ![](https://img2018.cnblogs.com/blog/1853166/202001/1853166-20200127125653374-1168375184.png) ![](https://img2018.cnblogs.com/blog/1853166/202001/1853... ...
  • ![](https://img2018.cnblogs.com/blog/1853166/202001/1853166-20200127111253815-61600007.png) ![](https://img2018.cnblogs.com/blog/1853166/202001/185316... ...
  • "exec的使用文檔" ...
  • 事件操作對象: var EventUtil= { //添加事件 addHandler: function (element, type, handler) { if (element.addEventListener) { element.addEventListener(type, handler ...
  • 1. 儘量指定類的final修飾符 帶有final修飾符的類是不可派生的。指定一個類為final,則該類所有方法都是final。Java編譯器會會找機會內聯所有否final方法,這樣能夠使性能平均提高50%. 2.儘量重用對象。 特別是String對象的使用中,出現字元串鏈接情況時應用StringB ...
  • 1.代碼生成器: [正反雙向](單表、主表、明細表、樹形表,快速開發利器)freemaker模版技術 ,0個代碼不用寫,生成完整的一個模塊,帶頁面、建表sql腳本、處理類、service等完整模塊2.多數據源:(支持同時連接無數個資料庫,可以不同的模塊連接不同數的據庫)支持N個數據源3.阿裡資料庫連 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...