如何高效查找源代码文件中的小错别字?

dja*_*aut 5 python lint spell-checking aspell

我想递归搜索大型代码库(主要是 python、HTML 和 javascript)以查找注释、字符串以及变量/方法/类名称中的拼写错误。对在终端中运行的东西的强烈偏好。

问题是拼写检查器喜欢aspell或scspell几乎只发现误报(例如编程术语,驼峰式术语),而如果它可以帮助我主要找到简单的拼写错误,例如打乱或丢失的字母,例如维护与维护,限制与限制,我会很高兴,部署与部署。

到目前为止我在玩的是:

for f in **/*.py ; do echo $f ; aspell list < $f |  uniq -c ; done
Run Code Online (Sandbox Code Playgroud)

但它会找到类似的东西: assertEqual, MyTestCase, lifecycle

dja*_*aut 3

我自己的这个解决方案主要关注python文件,但最终也在html和js中找到了它们。它仍然需要手动排除误报,但这只花了几分钟的时间,它就识别出了评论中大约 150 个拼写错误,这些错误也可以在非评论部分找到。

将其保存为可执行文件,例如extractcomments:

#!/usr/bin/env python3
import argparse
import io
import tokenize


if __name__ == "__main__":
    parser = argparse.ArgumentParser(add_help=False)
    parser.add_argument('filename')
    args = parser.parse_args()

    with io.open(args.filename, "r", encoding="utf-8") as sourcefile:
        for t in tokenize.generate_tokens(sourcefile.readline):
            if t.type == tokenize.COMMENT:
                print(t.string.lstrip("#").strip())
Run Code Online (Sandbox Code Playgroud)

收集所有评论以供进一步处理:

for f in **/*.py ; do  ~/extractcomments $f >> ~/comments.txt ; done
Run Code Online (Sandbox Code Playgroud)

使用一个或多个字典在您的代码库上递归运行它aspell,并收集所有识别为拼写错误的内容并计算它们的出现次数:

aspell <~/comments.txt --lang=en list|aspell --lang=de list | sort | uniq -c | sort -n > ~/typos.txt
Run Code Online (Sandbox Code Playgroud)

产生类似的东西:

10 availabe
 8 assignement
 7 hardwird
Run Code Online (Sandbox Code Playgroud)

获取没有前导数字的列表,清除误报,将其复制到第二个文件correct.txt并对其运行 aspell 以获得每个拼写错误的所需替换:aspell -c correct.txt

typo;correction现在粘贴这两个文件以获得with的格式paste -d";" typos.txt correct.txt > known_typos.csv

现在我们要递归地替换代码库中的那些:

#!/bin/bash

root_dir=$(git rev-parse --show-toplevel)

while IFS=";" read -r typo fix ; do
    git grep -l -z -w "${typo}" -- "*.py" "*.html"  | xargs -r --null sed -i "s/\b${typo}\b/${fix}/g"
done < $root_dir/known_typos.csv
Run Code Online (Sandbox Code Playgroud)

我的 bash 技能很差,所以肯定还有改进的空间。

更新:通过运行以下命令,我可以在方法名称中找到更多拼写错误:

grep -r def --include \*.py . | cut -d ":" -f 2- |tr "_" " " | aspell --lang=en list | sort -u
Run Code Online (Sandbox Code Playgroud)

Update2:设法修复了下划线名称或没有单词边界的字符串内的拼写错误,例如i_am_a_typpo3:

#!/bin/bash                                                                                                                         

root_dir=$(git rev-parse --show-toplevel)                                                                                           
while IFS=";" read -r typo fix ; do                                                                                                 
    echo ${typo}                                                                                                                    
    find $root_dir  \( -name '*.py' -or -name '*.html' \) -print0 | xargs -0 perl -pi -e "s/(?<![a-zA-Z])${typo}(?![a-zA-Z])/${fix}/g"                                                                                                                    
done < $root_dir/known_typos.csv 
Run Code Online (Sandbox Code Playgroud)