关于github:中英文拼写检测纠正开源项目使用入门-wordchecker-110

0次阅读

共计 4347 个字符,预计需要花费 11 分钟才能阅读完成。

我的项目简介

word-checker 本我的项目用于单词拼写查看。反对英文单词拼写检测,和中文拼写检测。

个性阐明

  • 能够迅速判断以后单词是否拼写错误
  • 能够返回最佳匹配后果
  • 能够返回纠正匹配列表,反对指定返回列表的大小
  • 谬误提醒反对 i18n
  • 反对大小写、全角半角格式化解决
  • 反对自定义词库
  • 内置 27W+ 的英文词库
  • 反对指定英文的编辑间隔
  • 反对根本的中文拼写检测

变更日志

变更日志

疾速开始

JDK 版本

Jdk 1.7+

maven 引入

<dependency>
     <groupId>com.github.houbb</groupId>
     <artifactId>word-checker</artifactId>
    <version>1.1.0</version>
</dependency>

测试案例

会依据输出,主动返回最佳纠正后果。

final String speling = "speling";
Assert.assertEquals("spelling", WordCheckerHelper.correct(speling));

外围 api 介绍

外围 api 在 WordCheckerHelper 工具类下。

WordCheckers 工具类提供了长文本中英文混合的主动纠正性能,当然也反对单个单词。

性能 办法 参数 返回值 备注
文本拼写是否正确 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

英文测试例子

参见 EnWordCheckerTest.java

是否拼写正确

final String hello = "hello";
final String speling = "speling";
Assert.assertTrue(WordCheckerHelper.isCorrect(hello));
Assert.assertFalse(WordCheckerHelper.isCorrect(speling));

返回最佳匹配后果

final String hello = "hello";
final String speling = "speling";
Assert.assertEquals("hello", WordCheckerHelper.correct(hello));
Assert.assertEquals("spelling", WordCheckerHelper.correct(speling));

默认纠正匹配列表

final String word = "goox";
List<String> stringList = WordCheckerHelper.correctList(word);
Assert.assertEquals("[good, goo, goon, goof, gook, goop, goos, gox, goog, gool, goor]", stringList.toString());

指定纠正匹配列表大小

final String word = "goox";
final int limit = 2;
List<String> stringList = WordCheckerHelper.correctList(word, limit);
Assert.assertEquals("[good, goo]", stringList.toString());

中文拼写纠正

是否拼写正确

final String right = "正确";
final String error = "万变不离其中";

Assert.assertTrue(WordCheckerHelper.isCorrect(right));
Assert.assertFalse(WordCheckerHelper.isCorrect(error));

返回最佳匹配后果

final String right = "正确";
final String error = "万变不离其中";

Assert.assertEquals("正确", WordCheckerHelper.correct(right));
Assert.assertEquals("万变不离其宗", WordCheckerHelper.correct(error));

默认纠正匹配列表

final String word = "万变不离其中";

List<String> stringList = WordCheckerHelper.correctList(word);
Assert.assertEquals("[ 万变不离其宗]", stringList.toString());

指定纠正匹配列表大小

final String word = "万变不离其中";
final int limit = 1;

List<String> stringList = WordCheckerHelper.correctList(word, limit);
Assert.assertEquals("[ 万变不离其宗]", stringList.toString());

长文本中英文混合

情景

理论拼写纠正的话,最佳的应用体验是用户输出一个长文本,并且可能是中英文混合的。

而后实现上述对应的性能。

拼写是否正确

final String hello = "hello 你好";
final String speling = "speling 你好 以毒功毒";
Assert.assertTrue(WordCheckers.isCorrect(hello));
Assert.assertFalse(WordCheckers.isCorrect(speling));

返回最佳纠正后果

final String hello = "hello 你好";
final String speling = "speling 你好以毒功毒";
Assert.assertEquals("hello 你好", WordCheckers.correct(hello));
Assert.assertEquals("spelling 你好以毒攻毒", WordCheckers.correct(speling));

判断文本拼写是否正确

每一个词,对应的纠正后果。

final String hello = "hello 你好";
final String speling = "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());

判断文本拼写是否正确

同上,指定最多返回的个数。

final String hello = "hello 你好";
final String speling = "speling 你好以毒功毒";

Assert.assertEquals("{hello=[hello],  =[], 你 =[ 你], 好 =[好]}", WordCheckers.correctMap(hello, 2).toString());
Assert.assertEquals("{ =[], speling=[spelling, spewing], 你 =[你], 好 =[好], 以毒功毒 =[以毒攻毒]}", WordCheckers.correctMap(speling, 2).toString());

格式化解决

有时候用户的输出是各式各样的,本工具反对对于格式化的解决。

大小写

大写会被对立格式化为小写。

final String word = "stRing";

Assert.assertTrue(WordCheckerHelper.isCorrect(word));

全角半角

全角会被对立格式化为半角。

final String word = "string";

Assert.assertTrue(WordCheckerHelper.isCorrect(word));

自定义英文词库

文件配置

你能够在我的项目资源目录创立文件 resources/data/define_word_checker_en.txt

内容如下:

my-long-long-define-word,2
my-long-long-define-word-two

不同的词独立一行。

每一行第一列代表单词,第二列代表呈现的次数,二者用逗号 , 隔开。

次数越大,在纠正的时候返回优先级就越高,默认值为 1。

用户自定义的词库优先级高于零碎内置词库。

测试代码

咱们在指定了对应的单词之后,拼写检测的时候就会失效。

final String word = "my-long-long-define-word";
final String word2 = "my-long-long-define-word-two";

Assert.assertTrue(WordCheckerHelper.isCorrect(word));
Assert.assertTrue(WordCheckerHelper.isCorrect(word2));

自定义中文词库

文件配置

你能够在我的项目资源目录创立文件 resources/data/define_word_checker_zh.txt

内容如下:

 默守成规 按部就班 

应用英文空格分隔,后面是谬误,前面是正确。

前期 Road-Map

  • [x] 反对英文分词,解决整个英文句子
  • 反对中文分词拼写检测
  • 引入中文纠错算法,同音字和形近字解决。
  • 反对中英文混合拼写检测

技术鸣谢

Words 提供的原始英语单词数据。

开源地址

https://github.com/houbb/word-checker/ 欢送大家 fork+star ~~~

正文完
 0