如何计算文本中每个单词的出现次数

时间:2018-07-24 19:56:08

标签: go nlp

或者问题也可能是: Go获得单词袋的方式是什么?

例如,如果输入为

"This is a big apple tree. I love big big apple! 42"

然后如何获取每个单词计数的map输出(并且,如果方便的话,可以沿途进行一些简单的字符串解析,例如仅保留字母并降低它们):

{this=1, is=1, a=1, big=3, apple=2, tree=1, i=1, love=1}


一些Kotlin代码的简单版本如下:

fun main(args: Array<String>) {
    val inputText = "This is a big apple tree. I love big big apple! 42"

    val map = inputText.replace("[^a-zA-Z]+".toRegex(), " ") // only keep letters
            .trim()
            .toLowerCase()
            .split(" ")
            .groupingBy { it }
            .eachCount()

    println(map)
}

给出输出{this=1, is=1, a=1, big=3, apple=2, tree=1, i=1, love=1}

我想知道Golang做这种事情的等效方式是什么。 希望它既快速又易于阅读。

1 个答案:

答案 0 :(得分:3)

例如,

package main

import (
    "fmt"
    "strings"
)

func main() {
    text := "This is a big apple tree. I love big big apple! 42"
    fields := strings.FieldsFunc(text, func(r rune) bool {
        return !('a' <= r && r <= 'z' || 'A' <= r && r <= 'Z')
    })
    words := make(map[string]int)
    for _, field := range fields {
        words[strings.ToLower(field)]++
    }
    fmt.Println(words)
}

游乐场:https://play.golang.org/p/6J-ptfCoJ8r

输出:

map[tree:1 i:1 love:1 this:1 is:1 a:1 big:3 apple:2]