0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Golang] 英文を渡すと、単語数とその個数を返す関数をつくってみた

Last updated at Posted at 2019-08-31

はじめに

おなじみ、A Tour of Go/Exercise: Mapsの例題をしていて、組んだコードです。
Goの書き方として、セオリーを破ってしまっているかもしれませんが、その場合ご助言頂けると嬉しいです。

なお、この関数の仕様としては、

  • 英文限定
  • スペースなしで「!」や「.」などの記号が付いていた場合は、別の単語としてカウントされる

という感じですので、ご注意を。

今日のコード

英文から単語数と種類をマップで返す関数
func WordCount(s string) map[string]int {

	words := strings.Fields(s)
	m := map[string]int{}
	count := 0
	
	for _, v1 := range words {
		
		for _, v2 := range words {
		
			if v1 == v2 {
				count++
			}
			
		}
		m[v1] = count
		count = 0
		
	}
	return m
}

解説

strings.Fields関数について

いきなり登場するstrings.Fields関数の解説については、参考サイトから引用します。

Fieldsは、文字列sをひとつ以上の連続したホワイトスペースで分割し、sの部分文字列の配列を返します。sにホワイトスペースしか含まれていないときは空リストを返します。

簡単に言うと、この関数では内部的にsplit(" ")的なことをして、配列化しているんですね。
たったこの1行で、単語ごとに切り分けて配列等に格納する作業は完結します。

便利!

2重ループで回す

見ての通り、この関数は2重ループが仕込まれています。
1つ目のループでは、先ほどsrings.Fields関数で単語単位に切り分けられて、配列格納されたwords変数の値を1つずつ呼び出します。
2つ目のループで、単語1つ1つをお互いに比較させます。
2つ目のループが一巡するたびに、count変数は初期化されるため、「数えすぎ問題」に対策してます。

return m

最後に、単語と数をまとめたものを、マップとして返しています。

実行の一例

実行例
package main
import (
    "fmt"
    "strings"
)

func WordCount(s string) map[string]int {

	words := strings.Fields(s)
	m := map[string]int{}
	count := 0
	
	for _, v1 := range words {
		
		for _, v2 := range words {
		
			if v1 == v2 {
				count++
			}
			
		}
		m[v1] = count
		count = 0
		
	}
	return m
}

func main(){
    fmt.Println(WordCount("I have a pen"))
}
出力結果
map[I:1 a:1 have:1 pen:1]

最後に

あまり複雑な流れではなかったので、サクッとした解説になってしまいました。
また、間違えているところ等ありましたら、ツッコミもらえると嬉しいです。
最後まで、ありがとうございました。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?