如何在 GO 中将字符串转换为小写?

我是 GO 语言的新手,正在做一项作业,我应该编写一个代码来返回文本的词频。但是我知道单词“Hello”、“HELLO”和“hello”都算作“hello”,所以我需要将所有字符串转换为小写。


我知道我应该使用 strings.ToLower(),但是我不知道我应该把它包括在课堂上的什么地方。有人可以帮帮我吗?


package main


import (

    "fmt"

    "io/ioutil"

    "log"

    "strings"

    "time"

)


const DataFile = "loremipsum.txt"


// Return the word frequencies of the text argument.

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

    fregs := make(map[string]int)

    words := strings.Fields(text)

    for _, word := range words {

        fregs[word] += 1

    }

    return fregs

}


// Benchmark how long it takes to count word frequencies in text numRuns times.

//

// Return the total time elapsed.

func benchmark(text string, numRuns int) int64 {

    start := time.Now()

    for i := 0; i < numRuns; i++ {

        WordCount(text)

    }

    runtimeMillis := time.Since(start).Nanoseconds() / 1e6


    return runtimeMillis

}


// Print the results of a benchmark

func printResults(runtimeMillis int64, numRuns int) {

    fmt.Printf("amount of runs: %d\n", numRuns)

    fmt.Printf("total time: %d ms\n", runtimeMillis)

    average := float64(runtimeMillis) / float64(numRuns)

    fmt.Printf("average time/run: %.2f ms\n", average)

}


func main() {

    // read in DataFile as a string called data

    data, err:= ioutil.ReadFile("loremipsum.txt")

      if err != nil {

        log.Fatal(err)

        }


        // Convert []byte to string and print to screen


    text := string(data)

    fmt.Println(text)

    


    fmt.Printf("%#v",WordCount(string(data)))


    numRuns := 100

    runtimeMillis := benchmark(string(data), numRuns)

    printResults(runtimeMillis, numRuns)

}


aluckdog
浏览 112回答 3
3回答

弑天下

当您将单词用作地图键时,您应该将它们转换为小写for _, word := range words {&nbsp; &nbsp; &nbsp; &nbsp; fregs[strings.ToLower(word)] += 1&nbsp; &nbsp; }

POPMUISE

我得到 [a:822 a.:110 我想要所有的 a 都一样。ia如何更改代码以便a和a。是一样的吗?– 你好123您需要仔细定义一个词。例如,将一串连续的字母和数字转换为小写。func WordCount(s string) map[string]int {&nbsp; &nbsp; wordFunc := func(r rune) bool {&nbsp; &nbsp; &nbsp; &nbsp; return !unicode.IsLetter(r) && !unicode.IsNumber(r)&nbsp; &nbsp; }&nbsp; &nbsp; counts := make(map[string]int)&nbsp; &nbsp; for _, word := range strings.FieldsFunc(s, wordFunc) {&nbsp; &nbsp; &nbsp; &nbsp; counts[strings.ToLower(word)]++&nbsp; &nbsp; }&nbsp; &nbsp; return counts}

德玛西亚99

要删除所有非单词字符,您可以使用正则表达式:package mainimport (&nbsp; &nbsp; "bufio"&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "log"&nbsp; &nbsp; "regexp"&nbsp; &nbsp; "strings")func main() {&nbsp; &nbsp; str1 := "This is some text! I want to count each word. Is it cool?"&nbsp; &nbsp; re, err := regexp.Compile(`[^\w]`)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; log.Fatal(err)&nbsp; &nbsp; }&nbsp; &nbsp; str1 = re.ReplaceAllString(str1, " ")&nbsp; &nbsp; scanner := bufio.NewScanner(strings.NewReader(str1))&nbsp; &nbsp; scanner.Split(bufio.ScanWords)&nbsp; &nbsp; for scanner.Scan() {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(strings.ToLower(scanner.Text()))&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go