Because of a lapse in government funding, the information on this website may not be up to date, transactions submitted via the website may not be processed, and the agency may not be able to respond to inquiries until appropriations are enacted. The NIH Clinical Center (the research hospital of NIH) is open. For more details about its operating status, please visit cc.nih.gov. Updates regarding government operating status and resumption of normal operations can be found at OPM.gov
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