Languagetool in a simple java application

Hello,
I’m trying to use languagetool in a simple java application (using Netbeans). I successfully used the languagetool in a maven project as it is described in your site but I wonder if it is possible to use languagetool without maven.
What are the steps to implement the simple example of Embedding LanguageTool in Java applications?
I cant figure out what jars I should include.

Thank you in advance

Without Maven, download the *.zip from our homepage (“Download LanguageTool for stand-alone use”). You will need to include all the JARs in the “lib” directory and create a JAR of everything in the “org” directory.

First of all thank you for immediate answer.

I did the following:
- added all jars ( from the directory : \LanguageTool-2.3\LanguageTool-2.3\libs )
-and my program looks like this:

                 ...
                  import org.languagetool.JLanguageTool;
                  import org.languagetool.language.BritishEnglish;
                  import org.languagetool.rules.RuleMatch;
                  ..
                  
                  // the simple code example of your site:

                   JLanguageTool langTool = new JLanguageTool(new BritishEnglish());
                   for (Rule rule : langTool.getAllRules()) {
                   if (!rule.isSpellingRule()) {
                   langTool.disableRule(rule.getId());
                      }
                   }
                  List<RuleMatch> matches = langTool.check("A speling error");
                  for (RuleMatch match : matches) {
                  System.out.println("Potential typo at line " +
                  match.getLine() + ", column " +
                  match.getColumn() + ": " + match.getMessage());
                  System.out.println("Suggested correction(s): " +
                  match.getSuggestedReplacements());
                  }
                  ...

but there’s a problem with “import org.languagetool.language.BritishEnglish;” it says cannot find symbol " class BritishEnglish " in the package org.languagetool.language.

I apologize for insisting to get a specific answer for something probably very easy but I m not familiar with this and I hope this thread will be helpful for others to use Languagetool

BritishEnglish is in the “org” directory and not in the JARs, but you can put in a JAR like this: “zip -r languages.jar org/”, then add languages.jar to your classpath like the other JARs.

My long term advice would be to use Maven (or a similar system like Gradle). It’s quite a complex tool but it pays off in the long run.

Thank you very much for your reply.I’ve tried many useless ways in order to make it work till I decided to use the forum. Finally I am able to use it in my program.

I’ll keep in mind your advice about maven.

Thanks again for your time

Hello again,

using languageTool in a simple java application it’s ok! (a function with parameter the phrase I want to do spelling correction)

But calling the same function in a servlet throws exception. I’ve read that probably it has to do with the concurency and threads, is that the problem ? Can you give me any hint ?( how can I find out what is the exact problem ? )

Thank you again in advance

ConFer

What is the exception?

Actually I wasn’t very clear: I m getting a general IOexception calling the initial function that was working fine(I didn’t change a single line).
The problem is located just before the creation of the LanguageTool object , I just call my function in a servlet , everything else works fine in my project,the appropriate jar files are included , I just can’t use the specific function for the spelling correction and I don’t know how to catch a more detailed exception in order to solve it due to my poor java skills.

Thank you again for your time

ConFer

Well, you’ll need the full exception with a stacktrace to debug this. It might be written to a log file or printed to the console.

This is the error :

" Caused by: java.lang.RuntimeException: Problems with loading disambiguation file: /org/languagetool /resource/en/disambiguation.xml
at org.languagetool.tagging.disambiguation.rules.XmlRuleDisambiguator.disambiguate(XmlRuleDisambiguator.java:61)
at org.languagetool.JLanguageTool.getAnalyzedSentence(JLanguageTool.java:732) "

also:
" Caused by: java.lang.IllegalArgumentException: ‘en’ is not a language code known to LanguageTool. Supported language codes are: xx-XX "

using the same jars in the library I don’t face this problem in my initial program.

Thank you in advance for any help

ConFer

You need to have in your classpath:

  • All language resources, like org/languagetool/resource/en/disambiguation.xml
  • All language classes you want to use, like org/languagetool/language/English.java
  • META-INF/org/languagetool/language-module.properties (see ‘Supported Languages’ at Java API - LanguageTool Wiki)

If you have downloaded the LanguageTool ZIP, just add everything under “org” and “META-INF” to your classpath.

actually i am also facing same error.
but go through this site i can’t understand.
“zip -r languages.jar org/”—how do i use?.
can you please explain me clearly?

After “zip -r languages.jar META-INF/ org/” you need to add languages.jar to your classpath. How to do that exactly depends on your local environment or IDE.

zip -r languages.jar org/ when used on command mode it shows error as
“zip is not recognized as an internal or external command”.
Is that to be used in command mode. Please explain.

Also, If i zip the Org folder and create it as a jar, i cannot import it in my java code.
Please assist.

Thanks in advance.

I was having the same exact problem with my IDE not being able to find the BritishEnglish class. I have made the the language folder into a jar and added it the libraries the same way I did with the other JAR files. I also made the language-module under META-INF/org a jar file and added that as well.

I was able to get Language Tools to run as a standalone so I should have everything necessary and I believe I have put everything from the language tools program in my project classpath.

Sorry for being so specific but I have been trying for the past three days to get LT to work, I even tried using maven but was getting many more errors that I was having trouble figuring out the meaning of.

Thank you in advance,
Jacob

You’d need to make the top-level org directory a JAR file, not the one below META-INF. But even that isn’t needed, as long as you simply add the top-level org to the classpath (together with all the JARs in the libs directory).

I have added the top level org as well as all the JARS in the libs and the program still won’t compile. The only error i am getting is with the British English. I’ve tried JAR ing the language folder as well as the BritishEnglish.class and adding that but still I am having no luck. I do not understand what I could be doing wrong. Please let me know if you have any ideas.

The import

import org.languagetool.language.*;

Is also coming up saying it is an unused import, so it seems obvious that the program cannot find the BritishEnglish.class in the languages folder but I don’t know why.

I would say managing jars manually in Java projects is not recommended exactly for this reason: it’s really time-consuming to do it right and it’s really easy to break it if something changes.
Maven and Gradle are excellent build systems that make things so much easier and they will make sure your build is correct and all the necessary jars with correct versions are pulled in.

I was originally trying to use Maven but have no experience with it and was getting multiple error messages. I started with a simple Hello World Maven app and then tried adding the dependency and code that the API suggests but still couldn’t get the program to compile. The last error it gives is MojoFailureException, which when you look up, tells me the problem is something with the plugin I am trying to use. I tried scrolling through errors to see the source of the problem but cannot even get all of them because the command prompt cuts off some of them due to how many there are.