Java 中的拼写检查和/或拼写更正

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10731645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 02:17:42  来源:igfitidea点击:

Spell check and/or spell correction in Java

javanlpspell-checkinglanguagetool

提问by Ronaldinho Learn Coding

How can I do spell checking and/or spell correction in a Java application?

如何在 Java 应用程序中进行拼写检查和/或拼写更正?

采纳答案by Nishant

Google's Spell Checker http://code.google.com/p/google-api-spelling-java/

Google 的拼写检查器http://code.google.com/p/google-api-spelling-java/

 SpellChecker checker = new SpellChecker();

 SpellResponse spellResponse = checker.check( "helloo worlrd" );

 for( SpellCorrection sc : spellResponse.getCorrections() )
    System.out.println( sc.getValue() );

It's much like when you use Gmail or Google services (like translate.google.com or search) that gives you alternate suggestion if you have a typo.

这很像当您使用 Gmail 或 Google 服务(如 translate.google.com 或搜索)时,如果您有错字,它会为您提供备用建议。

What happens in the background?

The SpellChecker class transforms the request into XML and sends it to the Google's spell checker service. The response is also in XML, which is then deserialized into simple POJOs.

The request to the first example above looks like:

  <?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
  <spellrequest textalreadyclipped="0" ignoredigits="1" 
                          ignoreallcaps="1" ignoredups="0">
    <text>helloo worlrd</text>  
  </spellrequest>

And the response XML looks like:

  <?xml version="1.0" encoding="UTF-8"?>  
  <spellresult error="0" clipped="0" charschecked="13">
     <c o="0" l="6" s="1">hello  Helli   hell    hallo   hullo</c>
     <c o="7" l="6" s="1">world  whorled wold    warlord would</c>  
  </spellresult>

后台会发生什么?

SpellChecker 类将请求转换为 XML 并将其发送到 Google 的拼写检查服务。响应也是 XML 格式,然后将其反序列化为简单的 POJO。

对上面第一个示例的请求如下所示:

  <?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
  <spellrequest textalreadyclipped="0" ignoredigits="1" 
                          ignoreallcaps="1" ignoredups="0">
    <text>helloo worlrd</text>  
  </spellrequest>

响应 XML 如下所示:

  <?xml version="1.0" encoding="UTF-8"?>  
  <spellresult error="0" clipped="0" charschecked="13">
     <c o="0" l="6" s="1">hello  Helli   hell    hallo   hullo</c>
     <c o="7" l="6" s="1">world  whorled wold    warlord would</c>  
  </spellresult>

Haven't tried though.

不过没试过。



UPDATE:
Google might have started charging for this. I do not have time to code to check this. Someone can confirm. As far as Google is concerned, it seems that they have deprecated the old API for new and paid one.

更新:
谷歌可能已经开始为此收费。我没有时间编写代码来检查这一点。有人可以确认。就谷歌而言,似乎他们已经弃用了新的和付费的旧 API。

Refer: Google Translate API FAQ

参考:谷歌翻译 API 常见问题

What happened to earlier free versions of Translate API?
Google Translate API v1 is no longer available as of December 1, 2011 and has been replaced by Google Translate API v2. Google Translate API v1 was officially deprecated on May 26, 2011. The decision to deprecate the API and replace it with the paid service was made due to the substantial economic burden caused by extensive abuse.

早期免费版本的 Translate API 发生了什么变化?
自 2011 年 12 月 1 日起,Google Translate API v1 不再可用,并已被 Google Translate API v2 取代。Google Translate API v1 于 2011 年 5 月 26 日正式弃用。由于广泛滥用造成的巨大经济负担,决定弃用该 API 并将其替换为付费服务。

回答by Rakesh

You can use JOrtho. I have used it earlier in one of the swing app.

您可以使用JOrtho。我之前在其中一款 Swing 应用程序中使用过它。

回答by user2601995

A good offline solution is Jazzy. Try this exampleand download the dictionary.

一个很好的离线解决方案是Jazzy。试试这个例子并下载字典

Here's the Maven dependencyfor library:

这是库的Maven 依赖项

<dependency>
    <groupId>net.sf.jazzy</groupId>
    <artifactId>jazzy</artifactId>
    <version>0.5.2-rtext-1.4.1-2</version>
</dependency>

回答by Wolfgang Fahl

Languagetool is Java bases spell checking and proofreading software that might fit. See

Languagetool 是可能适合的基于 Java 的拼写检查和校对软件。看

回答by Rael Gugelmin Cunha

If you want a simple and offline solution, based on Peter Norvig explanationof Google spell corrector, take a look here: http://raelcunha.com/spell-correct.php

如果您想要一个简单的离线解决方案,基于Peter Norvig 对Google 拼写校正器的解释,请看这里:http: //raelcunha.com/spell-correct.php

回答by user1294764

Try Hunspell. It is a standard for spell check. You can use Java port of Hunspellwhich is Hunspell-c+ JNA

试试 Hunspell。它是拼写检查的标准。您可以使用Hunspell 的 Java 端口,即 Hunspell-c+ JNA