如何从文件中读取一个单词的大写和小写形式?

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

How to read a word in upper and lower case from file? Go

问题

我有一个任务,需要读取一个文件,然后扫描是否包含"不良词汇",最后输出文件中是否包含"不良词汇"的结果,如果包含则输出"True",否则输出"False"。

我写了这个函数:

func readFile(fileName string, tabooName string) {
    dataFile, err := os.Open(fileName)
    if err != nil {
        log.Fatal(err)
    }
    defer dataFile.Close()

    scanner := bufio.NewScanner(dataFile)

    for scanner.Scan() {
        scanner.Text()
        if strings.Contains(scanner.Text(), strings.ToLower(tabooName)) || strings.Contains(scanner.Text(), strings.ToUpper(tabooName)) {
            fmt.Println("True")
            break
        } else {
            fmt.Println("False")
            break
        }
    }

    if err := scanner.Err(); err != nil {
        log.Fatal(err)
    }
}

但是,如果"不良词汇"以大写形式写入,它无法正常工作。例如:

测试 #2 中的错误答案

这个词属于不良词汇

请查看您的程序在此失败测试期间的输出
注意">" 字符表示输入行的开头

---

forbidden_words.txt
HARSH
False

我应该如何正确处理这个问题?

更新:我不知道为什么,但这个函数现在可以正常工作了...
首先,我只是将整个函数写在了main函数中,它可以工作,现在将函数封装成了func,它也可以工作。我不知道为什么,哈哈。

英文:

I have a task to read a file and, then scan for "bad word", and finally output "True" or "False" if "bad word" contains in file or not.
I wrote this function:

func readFile(fileName string, tabooName string) {
	dataFile, err := os.Open(fileName)
	if err != nil {
		log.Fatal(err)
	}
	defer dataFile.Close()

	scanner := bufio.NewScanner(dataFile)

	for scanner.Scan() {
		scanner.Text()
		if strings.Contains(scanner.Text(), strings.ToLower(tabooName)) || strings.Contains(scanner.Text(), strings.ToUpper(tabooName)) {
			fmt.Println("True")
			break
		} else {
			fmt.Println("False")
			break
		}
	}

	if err := scanner.Err(); err != nil {
		log.Fatal(err)
	}
}

But it does not work if the "bad word" is written in UPPERCASE
For example:

Wrong answer in test #2

This word belongs to bad words

Please find below the output of your program during this failed test.
Note that the '>' character indicates the beginning of the input line.

---

forbidden_words.txt
HARSH
False

How can i do it corectly?

UPDATE: I don't know why but this function is working now...
First, i justed wrote this whole func in main function, and it's working, now with func it's working too. I don't know why lol

答案1

得分: 1

你可以稍微不同地使用strings.Containsstrings.ToLower

func readFile(fileName string, tabooName string) {
    file, err := os.Open(fileName)
    if err != nil {
        log.Fatal(err)
    }
    defer file.Close()

    tabooNameLowered := strings.ToLower(tabooName)

    scanner := bufio.NewScanner(file)
    for scanner.Scan() {
        loweredLine := strings.ToLower(scanner.Text())
        if strings.Contains(loweredLine, tabooNameLowered) {
            fmt.Println("True")
            return
        }
    }
        
    fmt.Println("False")

    if err := scanner.Err(); err != nil {
        log.Fatal(err)
    }
}
英文:

You could use <code>strings.<b>Contains</b></code> and <code>strings.<b>ToLower</b></code> slightly differently:

func readFile(fileName string, tabooName string) {
	file, err := os.Open(fileName)
	if err != nil {
		log.Fatal(err)
	}
	defer file.Close()

	tabooNameLowered := strings.ToLower(tabooName)

	scanner := bufio.NewScanner(file)
	for scanner.Scan() {
		loweredLine := strings.ToLower(scanner.Text())
		if strings.Contains(loweredLine, tabooNameLowered) {
			fmt.Println(&quot;True&quot;)
			return
		}
	}
        
	fmt.Println(&quot;False&quot;)

	if err := scanner.Err(); err != nil {
		log.Fatal(err)
	}
}

huangapple
  • 本文由 发表于 2022年7月17日 19:26:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/73011353.html
匿名

发表评论

匿名网友

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

确定