- Notifications
You must be signed in to change notification settings - Fork58
🇨🇳🇬🇧Chinese and English word spelling corrector.(中文易错别字检测,中文拼写检测纠正。英文单词拼写校验工具)
License
houbb/word-checker
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
word-checker 本项目用于单词拼写检查。支持英文单词拼写检测,和中文拼写检测。
附加编辑距离等常用算法。
任意两个单词间的最短编辑距离
任意两个单词间最短编辑距离推导过程
可以迅速判断当前单词是否拼写错误
可以返回最佳匹配结果
可以返回纠正匹配列表,支持指定返回列表的大小
错误提示支持 i18n
支持大小写、全角半角格式化处理
支持自定义词库
内置 27W+ 的英文词库
支持指定英文的编辑距离
Jdk 1.7+
<dependency> <groupId>com.github.houbb</groupId> <artifactId>word-checker</artifactId> <version>1.2.0</version></dependency>
方法在工具类EditDistanceHelper
EditDistanceHelper.minDistance(source, target) 任意两个单词间的最短编辑距离
Assert.assertEquals(3,EditDistanceHelper.minDistance("horse","ros"));Assert.assertEquals(5,EditDistanceHelper.minDistance("intention","execution"));
EditDistanceHelper.minDistanceList(source, target) 任意两个单词间的最短编辑距离推导过程
Assert.assertEquals("[horse, hors, hos, ros]",EditDistanceHelper.minDistanceList("horse","ros").toString());Assert.assertEquals("[intention, intenution, intecution, inecution, ixecution, execution]",EditDistanceHelper.minDistanceList("intention","execution").toString());
会根据输入,自动返回最佳纠正结果。
finalStringspeling ="speling";Assert.assertEquals("spelling",WordCheckerHelper.correct(speling));
核心 api 在WordCheckerHelper 工具类下。
WordCheckerHelper 工具类提供了长文本中英文混合的自动纠正功能,当然也支持单个单词。
| 功能 | 方法 | 参数 | 返回值 | 备注 |
|---|---|---|---|---|
| 文本拼写是否正确 | isCorrect(string) | 待检测的文本 | boolean | 全部正确,才会返回 true |
| 返回最佳纠正结果 | correct(string) | 待检测的单词 | String | 如果没有找到可以纠正的文本,则返回其本身 |
| 判断文本拼写是否正确 | correctMap(string) | 待检测的单词 | Map<String, List<String>> | 返回所有匹配的纠正列表 MAP |
| 判断文本拼写是否正确 | correctMap(string, int limit) | 待检测的文本, 返回列表的大小 | 返回指定大小的的纠正列表 MAP | 列表大小 <= limit |
| 判断文本拼写是否正确 | correctList(string) | 待检测的单词 | List<String> | 返回所有匹配的纠正列表 |
| 判断文本拼写是否正确 | correctList(string, int limit) | 待检测的文本, 返回列表的大小 | 返回指定大小的的纠正列表 | 列表大小 <= limit |
finalStringhello ="hello";finalStringspeling ="speling";Assert.assertTrue(WordCheckerHelper.isCorrect(hello));Assert.assertFalse(WordCheckerHelper.isCorrect(speling));
finalStringhello ="hello";finalStringspeling ="speling";Assert.assertEquals("hello",WordCheckerHelper.correct(hello));Assert.assertEquals("spelling",WordCheckerHelper.correct(speling));
finalStringword ="goox";List<String>stringList =WordCheckerHelper.correctList(word);Assert.assertEquals("[good, goo, goon, goof, gook, goop, goos, gox, goog, gool, goor]",stringList.toString());
finalStringword ="goox";finalintlimit =2;List<String>stringList =WordCheckerHelper.correctList(word,limit);Assert.assertEquals("[good, goo]",stringList.toString());
finalStringright ="正确";finalStringerror ="万变不离其中";Assert.assertTrue(WordCheckerHelper.isCorrect(right));Assert.assertFalse(WordCheckerHelper.isCorrect(error));
finalStringright ="正确";finalStringerror ="万变不离其中";Assert.assertEquals("正确",WordCheckerHelper.correct(right));Assert.assertEquals("万变不离其宗",WordCheckerHelper.correct(error));
finalStringword ="万变不离其中";List<String>stringList =WordCheckerHelper.correctList(word);Assert.assertEquals("[万变不离其宗]",stringList.toString());
finalStringword ="万变不离其中";finalintlimit =1;List<String>stringList =WordCheckerHelper.correctList(word,limit);Assert.assertEquals("[万变不离其宗]",stringList.toString());
实际拼写纠正的话,最佳的使用体验是用户输入一个长文本,并且可能是中英文混合的。
然后实现上述对应的功能。
finalStringhello ="hello 你好";finalStringspeling ="speling 你好 以毒功毒";Assert.assertTrue(WordCheckers.isCorrect(hello));Assert.assertFalse(WordCheckers.isCorrect(speling));
finalStringhello ="hello 你好";finalStringspeling ="speling 你好以毒功毒";Assert.assertEquals("hello 你好",WordCheckers.correct(hello));Assert.assertEquals("spelling 你好以毒攻毒",WordCheckers.correct(speling));
每一个词,对应的纠正结果。
finalStringhello ="hello 你好";finalStringspeling ="speling 你好以毒功毒";Assert.assertEquals("{hello=[hello], =[ ], 你=[你], 好=[好]}",WordCheckers.correctMap(hello).toString());Assert.assertEquals("{ =[ ], speling=[spelling, spewing, sperling, seeling, spieling, spiling, speeling, speiling, spelding], 你=[你], 好=[好], 以毒功毒=[以毒攻毒]}",WordCheckers.correctMap(speling).toString());
同上,指定最多返回的个数。
finalStringhello ="hello 你好";finalStringspeling ="speling 你好以毒功毒";Assert.assertEquals("{hello=[hello], =[ ], 你=[你], 好=[好]}",WordCheckers.correctMap(hello,2).toString());Assert.assertEquals("{ =[ ], speling=[spelling, spewing], 你=[你], 好=[好], 以毒功毒=[以毒攻毒]}",WordCheckers.correctMap(speling,2).toString());
有时候用户的输入是各式各样的,本工具支持对于格式化的处理。
大写会被统一格式化为小写。
finalStringword ="stRing";Assert.assertTrue(WordCheckerHelper.isCorrect(word));
全角会被统一格式化为半角。
finalStringword ="string";Assert.assertTrue(WordCheckerHelper.isCorrect(word));
你可以在项目资源目录创建文件resources/data/define_word_checker_en.txt
内容如下:
my-long-long-define-word,2my-long-long-define-word-two不同的词独立一行。
每一行第一列代表单词,第二列代表出现的次数,二者用逗号, 隔开。
次数越大,在纠正的时候返回优先级就越高,默认值为 1。
用户自定义的词库优先级高于系统内置词库。
我们在指定了对应的单词之后,拼写检测的时候就会生效。
finalStringword ="my-long-long-define-word";finalStringword2 ="my-long-long-define-word-two";Assert.assertTrue(WordCheckerHelper.isCorrect(word));Assert.assertTrue(WordCheckerHelper.isCorrect(word2));
你可以在项目资源目录创建文件resources/data/define_word_checker_zh.txt
内容如下:
默守成规 墨守成规使用英文空格分隔,前面是错误,后面是正确。
java 实现中英文拼写检查和错误纠正?可我只会写 CRUD 啊!
单词拼写纠正-03-leetcode edit-distance 72.力扣编辑距离
支持英文分词,处理整个英文句子
支持中文分词拼写检测
引入中文纠错算法,同音字和形近字处理。
支持中英文混合拼写检测
其他常用的编辑距离算法
Words 提供的原始英语单词数据。
About
🇨🇳🇬🇧Chinese and English word spelling corrector.(中文易错别字检测,中文拼写检测纠正。英文单词拼写校验工具)
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors3
Uh oh!
There was an error while loading.Please reload this page.