Application: CSpellFile
I. Objective
To use CSpell APIs to correct spelling errors from text in an input file.
In NLP applications, we often want to correct spelling errors in the pre-process before further core process (such as name entity recognition, concept mapping, question answering, etc.). CSpell API can be implemented in Java for this purpose. This example illustrates how to use CSpell APIs to correct spelling errors in the text of an input file.
II. Pre-Requirements
install cSpell.${YEAR} package to "/Projects/cSpell${YEAR}"
III. Source Code
package cSpellExample; import java.io.*; import java.nio.file.*; import java.nio.charset.*; import gov.nih.nlm.nls.cSpell.Util.*; import gov.nih.nlm.nls.cSpell.Api.*; /***************************************************************************** * This class is to use CSpellApi to process a file. * * @author chlu * * @version V-2018 *****************************************************************************/ public class CSpellFile { // test driver public static void main(String[] args) { String configFile = "../../data/Config/cSpell.properties"; String inFile = "../data/inData/testNonWord.txt"; String outFile = "../data/outData/testNonWord.txt.out"; if(args.length == 3) { configFile = args[0]; inFile = args[1]; outFile = args[2]; } else if(args.length > 0) { System.out.println("Usage: java CSpellFile <configFile> <inFile> <outFile>"); System.exit(0); } // init, set fucntion to non-word correction CSpellApi cSpellApi = new CSpellApi(configFile); // test System.out.println("----- Sample Code: CSpellFile( ): -----"); System.out.println("- inFile: [" + inFile + "]"); System.out.println("- outFile: [" + outFile + "]"); try { BufferedWriter outWriter = Files.newBufferedWriter( Paths.get(outFile), Charset.forName("UTF-8")); // read in file and process String inText = FileIo.GetStrFromFileAddNewLineAtTheEnd(inFile); String outText = cSpellApi.ProcessToStr(inText); // print out outWriter.write(outText); // close outWriter.close(); cSpellApi.Close(); } catch(Exception e) { System.err.println("*ERR@CSpellFile: " + e.toString()); } } }
IV. Compile
shell>javac -classpath ../lib/cSpell2018dist.jar CSpellFile.java
V. Run & Results
shell>java -classpath ./:../lib/cSpell2018dist.jar:/Projects/cSpell2018/ CSpellFile