How to convert strings to lower case in GO?

huangapple go评论69阅读模式
英文:

How to convert strings to lower case in GO?

问题

我是你的中文翻译助手,以下是你要翻译的内容:

我对GO语言还不熟悉,正在处理一个任务,需要编写一段代码来返回文本中单词的频率。然而,我知道单词'Hello'、'HELLO'和'hello'都被计算为'hello',所以我需要将所有字符串转换为小写。

我知道应该使用strings.ToLower(),但我不知道应该在类中的哪个位置包含它。有人可以帮帮我吗?

package main

import (
	"fmt"
	"io/ioutil"
	"log"
	"strings"
	"time"
)

const DataFile = "loremipsum.txt"

// 返回文本中单词的频率
func WordCount(text string) map[string]int {
	fregs := make(map[string]int)
	words := strings.Fields(text)
	for _, word := range words {
		fregs[strings.ToLower(word)] += 1
	}
	return fregs
}

// 对文本进行多次运行以计算单词频率所需的时间
//
// 返回总共经过的时间
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
}

// 打印基准测试结果
func printResults(runtimeMillis int64, numRuns int) {
	fmt.Printf("运行次数:%d\n", numRuns)
	fmt.Printf("总时间:%d 毫秒\n", runtimeMillis)
	average := float64(runtimeMillis) / float64(numRuns)
	fmt.Printf("平均时间/次:%0.2f 毫秒\n", average)
}

func main() {
	// 读取DataFile文件内容并存储为字符串data
	data, err := ioutil.ReadFile("loremipsum.txt")
	if err != nil {
		log.Fatal(err)
	}

	// 将[]byte转换为字符串并打印到屏幕上
	text := string(data)
	fmt.Println(text)

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

	numRuns := 100
	runtimeMillis := benchmark(string(data), numRuns)
	printResults(runtimeMillis, numRuns)
}

希望对你有帮助!

英文:

I am new to the language GO and working on an assignment where i should write a code that return the word frequencies of the text. However I know that the words 'Hello', 'HELLO' and 'hello' are all counted as 'hello', so I need to convert all strings to lower case.

I know that I should use strings.ToLower(), however I dont know where I should Included that in the class. Can someone please help me?

package main
import (
&quot;fmt&quot;
&quot;io/ioutil&quot;
&quot;log&quot;
&quot;strings&quot;
&quot;time&quot;
)
const DataFile = &quot;loremipsum.txt&quot;
// 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 &lt; 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(&quot;amount of runs: %d\n&quot;, numRuns)
fmt.Printf(&quot;total time: %d ms\n&quot;, runtimeMillis)
average := float64(runtimeMillis) / float64(numRuns)
fmt.Printf(&quot;average time/run: %.2f ms\n&quot;, average)
}
func main() {
// read in DataFile as a string called data
data, err:= ioutil.ReadFile(&quot;loremipsum.txt&quot;)
if err != nil {
log.Fatal(err)
}
// Convert []byte to string and print to screen
text := string(data)
fmt.Println(text)
fmt.Printf(&quot;%#v&quot;,WordCount(string(data)))
numRuns := 100
runtimeMillis := benchmark(string(data), numRuns)
printResults(runtimeMillis, numRuns)
}

答案1

得分: 2

当你将单词用作映射键时,应将它们转换为小写。

for _, word := range words {
    fregs[strings.ToLower(word)] += 1
}
英文:

You should convert words to lowercase when you are using them as map key

for _, word := range words {
fregs[strings.ToLower(word)] += 1
}

答案2

得分: 0

你需要仔细定义一个单词。例如,将连续的字母和数字转换为小写。

func WordCount(s string) map[string]int {
    wordFunc := func(r rune) bool {
        return !unicode.IsLetter(r) && !unicode.IsNumber(r)
    }
    counts := make(map[string]int)
    for _, word := range strings.FieldsFunc(s, wordFunc) {
        counts[strings.ToLower(word)]++
    }
    return counts
}

请注意,这是一个示例代码,用于计算字符串中每个单词出现的次数,并将其转换为小写。你可以根据自己的需求进行修改。

英文:

> I get [a:822 a.:110 I want all a in the same. How do i a change the code so that a and a. is the same? – hello123


You need to carefully define a word. For example, a string of consecutive letters and numbers converted to lowercase.

func WordCount(s string) map[string]int {
wordFunc := func(r rune) bool {
return !unicode.IsLetter(r) &amp;&amp; !unicode.IsNumber(r)
}
counts := make(map[string]int)
for _, word := range strings.FieldsFunc(s, wordFunc) {
counts[strings.ToLower(word)]++
}
return counts
}

答案3

得分: 0

要删除所有非单词字符,你可以使用正则表达式:

package main

import (
	"bufio"
	"fmt"
	"log"
	"regexp"
	"strings"
)

func main() {
	str1 := "This is some text! I want to count each word. Is it cool?"

	re, err := regexp.Compile(`[^\w]`)
	if err != nil {
		log.Fatal(err)
	}
	str1 = re.ReplaceAllString(str1, " ")

	scanner := bufio.NewScanner(strings.NewReader(str1))
	scanner.Split(bufio.ScanWords)
	for scanner.Scan() {
		fmt.Println(strings.ToLower(scanner.Text()))
	}
}

这段代码会将字符串中的非单词字符替换为空格,并逐个打印出每个单词(转换为小写)。

英文:

to remove all non-word characters you could use a regular expression:

package main
import (
&quot;bufio&quot;
&quot;fmt&quot;
&quot;log&quot;
&quot;regexp&quot;
&quot;strings&quot;
)
func main() {
str1 := &quot;This is some text! I want to count each word. Is it cool?&quot;
re, err := regexp.Compile(`[^\w]`)
if err != nil {
log.Fatal(err)
}
str1 = re.ReplaceAllString(str1, &quot; &quot;)
scanner := bufio.NewScanner(strings.NewReader(str1))
scanner.Split(bufio.ScanWords)
for scanner.Scan() {
fmt.Println(strings.ToLower(scanner.Text()))
}
}

答案4

得分: -1

请问您需要将这段内容翻译成中文吗?

英文:

See strings.EqualFold.

Here is an example.

huangapple
  • 本文由 发表于 2022年4月4日 19:38:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/71736628.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定