拼写检查程序Shell脚本 [英] Spell checker shell script

查看:52
本文介绍了拼写检查程序Shell脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些问题.我遇到了一个脚本问题,该脚本应该是一个简单的拼写检查器.

I have some questions. I'm having an issue with a script that's supposed to be a simple spell checker.

这是要执行的操作,当找到错误的单词时,它将提示用户输入单词的正确拼写,如果用户输入了正确的拼写,它将随后显示正确的单词以及不正确的单词向下(经过所有单词之后).但是,如果用户只是按下Enter键,那么它将把该单词作为正确的拼写并将其放置在〜./memory中,因此,如果再次运行,它将忽略该单词.

What it is meant to do is, when a wrong word is found, it will prompt the user to enter the correct spelling of the word, if the user enters the correct spelling it will then then display the corrected word along with the incorrect word down below (after going through all the words). But, if the user just hits Enter, then it will take the word as a correct spelling and place it in ~./memory, so that if ran again, it will overlook this word.

截至目前,单词的正确/错误拼写尚未显示,〜./内存中也没有记住"任何内容.老实说,我不确定为什么.

As of right now, the correct/incorrect spellings of the words are not being displayed and nothing is being "remembered" in ~./memory. And to be honest, I'm not really sure why.

#! /bin/bash
wrongWords=`aspell list < $1`
touch ~/.memory

for wrongWord in $wrongWords
    do
            echo  $wrongWord "is mispelled."
            read -p "Press ""Enter"" to keep this spelling, or type a correction here: " newWord
            if [ "$newWord" = "" ]
                    then
                            echo "$newWord" >> ~/.memory
            fi
    done

printf "%-20s %-20s \n Mispelled: Corrections:"
printf "\n $wrongWord $newWord"

推荐答案

我编辑了脚本的敏感部分,这对于尝试查找实际问题是一个很好的猜测.我稍微结合了自己的逻辑,但并没有太在意.我将编辑后的脚本附加到该问题上,希望这将是找到问题的好一步,我建议您进一步阅读以下主题:

I edited sensitive parts of your script, which are a good guess in trying to find the actual problem. I slightly combined my own logic into this, but without much care. I attach the edited script to this question with a hope that it will be a good step in finding your problem, and I suggest you to read further about the following subjects:

  • Do not read lines with for.
  • Process Substitution.
  • Test and Conditionals.

以上三个链接摘自本Bash指南,它是主要的指南之一,也是唯一的资源从那里学习Bash.

The three links above are taken from this Bash Guide which is one of the main, and only sources to learn Bash from out there.

我还建议您以后在需要时使用 ShellCheck 来检查脚本.

I would like to also recommend you to use ShellCheck to check your scripts when needed in the future.

已编辑脚本.不一定正确.

#! /bin/bash
# This script should be checked before use.
# It is not necessarily correct.

wrong_words=()
new_words=()

while read -r ww; do

    printf '%s is mispelled.\n' "$ww"

    wrong_words+=("$ww")

    read -rp "Press \"Enter\" to keep this spelling, or type a correction here: " nw

    # User provided a correction to $ww
    if [[ $nw ]]; then
        printf 'User corrected %s to %s\n' "$ww" "$nw"
        new_words+=("$nw")
    else
        printf 'User decided to keep the spelling of %s even though it was detected to be wrong.\n' "$ww"
    fi

done < <(aspell list < "$1")

# Saving new words to ~/.memory_words
printf '%s\n' "${new_words[@]}" >> ~/.memory_words

# Displaying info. Not necessarily useful.
printf 'New word: %s\n' "${new_words[@]}"
printf 'Wrong word: %s\n' "${wrong_words[@]}"

新单词将保存到 new_words 数组中.错误的单词将保存到 wrong_words 数组中.在脚本末尾,将 new_words 数组追加到文件〜/.. memory_words 中.

New words are saved to new_words array. Wrong words are saved to wrong_words array. In the end of the script new_words array is appended to the file ~/.memory_words.

这篇关于拼写检查程序Shell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆