使用行号查找重复单词拼写错误的命令行方法

cla*_*123 6 command-line bash awk text-processing aspell

更新:澄清行号要求,减少一些冗长

从命令行,有没有办法:

  • 检查英文文本文件
  • 查找重复的拼写错误,
  • 连同找到它们的行号,

为了帮助纠正它们?

示例 1

目前,帮助完成一篇文章或其他英文写作,aspell -c text.txt有助于发现拼写错误。但是,当错误是无意中连续重复一个单词时,这没有帮助。

highlander_typo.txt

There can be only one one.
Run Code Online (Sandbox Code Playgroud)

运行aspell

$ aspell -c highlander_typo.txt
Run Code Online (Sandbox Code Playgroud)

可能因为aspell是拼写检查器,而不是语法检查器,所以重复的单词拼写错误超出了其预期的功能范围。因此,结果是该文件通过了aspell检查,因为就单个单词的拼写而言,没有任何“错误”。

正确的句子是There can be only one.,第二个one是无意的重复单词错字。

示例 2

但另一种情况是,例如kylie_minogue.txt

La la la
Run Code Online (Sandbox Code Playgroud)

这里的重复不是错字,因为这些是艺术家歌词的一部分

所以解决方案不应该自己假设和“修复”任何东西,否则它可能会覆盖故意重复的单词。

示例 3:多行

jefferson_typo.txt

He has has refused his Assent to Laws, the most wholesome and necessary
for the public good.
He has forbidden his Governors to pass Laws of immediate and
and pressing importance, unless suspended in their operation till his
Assent should be be obtained; and when so suspended, he has utterly
neglected to attend to them.
Run Code Online (Sandbox Code Playgroud)

改编自《独立宣言》

在以上六行中,

  • 1:He has has refused应该是He has refused,第二个has是重复的单词拼写错误
  • 5:should be be obtained应该是should be obtained,第二个be是重复单词拼写错误

但是,您是否注意到第三个重复的单词拼写错误?

  • 3: ... immediate and
  • 4: and pressing ...

这也是一个重复的单词拼写错误,因为尽管它们在不同的行上,但它们仍然是同一个英语句子的一部分,上面一行的尾端有一个单词被意外添加到下一行的开头。由于重复出现在一段文本的两侧,肉眼很难检测到。

预期输出

  • 具有类似于aspell -c但能够检测重复词的过程的交互式程序,或者,

  • 能够提取行号和可疑重复词的脚本或命令组合。此信息使使用编辑器更容易,例如vim跳转到重复词并在适当的地方进行修复。

使用上面的 multi-line jefferson_typo.txt,所需的输出将类似于:

1: has has
3: and
4: and
5: be be
Run Code Online (Sandbox Code Playgroud)

或者:

1: He [has has] refused his Assent to Laws, the most wholesome and necessary
3: He has forbidden his Governors to pass Laws of immediate [and]
4: [and] pressing importance, unless suspended in their operation till his
5: Assent should [be be] obtained; and when so suspended, he has utterly
Run Code Online (Sandbox Code Playgroud)

我实际上并不完全确定如何显示行间或跨行重复单词的困难情况,例如and上面的重复,因此如果您的解决方案与此不完全相似,请不要担心。

但我希望,像上面一样,它表明:

  • 相关原始输入的行号
  • 某种方法来引起人们对重复内容的注意,如果文本行也很长,则特别有用。
  • 如果显示整行以提供上下文(信用:@Wildcard),那么需要有一种方法以某种方式以某种方式使重复的单词或单词与众不同。此处显示的示例通过将重复包含在 ASCII 字符中来标记重复[ ]。或者,也许模仿grep --colors=always为线条的匹配着色以在彩色终端中显示

其他注意事项

  • 文本,应保留为纯文本文件
  • 请不要GUI解决方案,只是文本。ssh -XX11 转发不可靠,需要编辑ssh

不成功的尝试

uniq想到要尝试查找重复项,因此计划首先确定如何使重复词识别首先在一行上工作。

为了使用,uniq我们需要首先将一行上的单词转换为每行一个单词。

$ tr ' ' '\n' < highlander_typo.txt
There
can
be
only
one
one.
Run Code Online (Sandbox Code Playgroud)

很遗憾:

$ tr ' ' '\n' < highlander_typo.txt | uniq -D
Run Code Online (Sandbox Code Playgroud)

没有。

这是因为对于-D通常会显示重复项的选项,输入必须是完全重复的行。不幸的.是,重复词末尾的句号one否定了这一点。它只是看起来像一条不同的线。不确定我将如何解决诸如此句号之类的任意标点符号,并在tr处理后以某种方式将其添加回来。

这是不成功的。但是如果它成功了,接下来需要有一种方法来包含这一行的行号,因为输入文件可能有数百行,这将有助于指出输入文件的哪一行,重复词被检测到在。

这种单行代码处理可能是父循环的一部分,以便进行某种逐行多行处理,从而能够处理文件中的所有行,但不幸的是甚至超过了单行重复词识别一直存在问题。

jim*_*mij 5

编辑:添加安装和演示

您至少需要处理一些边缘情况,例如

  • 在行尾(和开头)重复的单词。
  • 搜索应该不区分大小写,因为像The the apple.
  • 可能您只想将搜索限制为单词组成部分以不匹配类似( ( a + b) + c )(重复的左括号。
  • 只有完整的单词应该匹配以消除 the thesis
  • 当涉及到人类语言中的 Unicode 字符时,应正确解释单词

总而言之,我推荐pcregrep解决方案:

pcregrep -Min --color=auto '\b([^[:space:]]+)[[:space:]]+\1\b' file
Run Code Online (Sandbox Code Playgroud)

显然颜色和行号(n选项)是可选的,但通常很好。

安装

在基于 Debian 的发行版上,您可以通过以下方式安装:

$ sudo apt-get install pcregrep
Run Code Online (Sandbox Code Playgroud)

例子

运行命令jefferson_typo.txt查看:

pcregrep -Min --color=auto '\b([^[:space:]]+)[[:space:]]+\1\b' file
Run Code Online (Sandbox Code Playgroud)

以上只是一个文本捕获,但在支持颜色的终端上,匹配是彩色的:

  • 成为


von*_*and 5

你应该看看尊者diction(1)style(1)命令。他们听到了各种各样的嘘声。有新版本(Fedora 23 上的 GPLv3)。

安装

例如,在基于 Debian 的发行版上,安装包diction,其中包括style

$ sudo apt-get install diction
Run Code Online (Sandbox Code Playgroud)

至少在 Fedora 中是这样的:

$ dnf install diction
Run Code Online (Sandbox Code Playgroud)

红帽企业(和克隆)可能需要:

$ yum install diction
Run Code Online (Sandbox Code Playgroud)

在任何情况下,这都来自一个名为 的上游 GNU 包diction,因此它几乎在所有地方都应该被称为相同的。

例子

$ diction jefferson_typo.txt
jefferson_typo.txt:1: He has [has] refused his Assent to Laws, the [most] wholesome and necessary for the public good.

jefferson_typo.txt:3: He has forbidden his Governors to pass Laws of immediate and [and] pressing importance, unless suspended in their operation till his Assent should be [be] obtained; and when [so] suspended, he has utterly neglected to attend to them.

2 phrases in 2 sentences found.
Run Code Online (Sandbox Code Playgroud)

优点

  • 捕捉重复的话,除其他外

缺点

  • []为与重复单词无关的项目引入标记。例如[so],可能被标记是因为它可以被Strunk视为无关的风格元素。看man diction
  • 显示的数字并不总是原始输入的行号,而是句子开始的行号。所以例如[be]是原始输入的第 5 行,但这里显示它3只是因为[be]是从第 5 行开始的句子的一部分3。所以这与你想要的略有不同