// (see HTTP specification section 3.4.1)
logger.info("changing to default encoding");
request.setCharacterEncoding( encoding );
}
StringBuffer correction = new StringBuffer();
String query = request.getParameter("query");
String lang = request.getParameter("l");
if (lang == null) {
lang = "pt_PT";
} else if ( lang.equals("en") ) {
lang = "en_US";
} else {
lang = "pt_PT";
}
if (query != null && lang != null) {
Matcher matcher = pattern.matcher( query );
logger.info("checking query: "+ query);
while ( matcher.find() ) {
String[] allSuggestions = null;
String match = matcher.group(1).toLowerCase();
logger.info("match: "+ match);
if ( !isOperator( match ) ) {
try {
allSuggestions = SpellChecker.suggestSimilarHunspell(match, lang, 1, reader, FIELD, minFreq, timesFreq, dictPath);
if ( allSuggestions.length > 0 ) {
// only add word to suggestion if it is different
if ( !match.equals( allSuggestions[0] ) ) {
logger.info("suggestion: "+ allSuggestions[0]);
matcher.appendReplacement( correction, "<em>"+ allSuggestions[0] +"</em>");
}
}
} catch (InterruptedException e) {
throw new IOException(e);
}
}
}
matcher.appendTail(correction);
}
response.setContentType("text/html; charset=UTF-8");
response.setCharacterEncoding("UTF-8");
PrintWriter out=response.getWriter();
out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">");
out.println("<html>");
out.println("<head>");
out.println("<title>Query Spellchecker</title>");
out.println("</head>");
out.println("<body>");
out.println("<h3>Query Spellchecker</h3>");
out.println("<h5>Query:</h5>");
out.println("<div id=\"query\">");
out.println( query );
out.println("</div>");
out.println("<h5>Correction:</h5>");
out.println("<div id=\"correction\">");
out.println( correction.toString() );
out.println("</div>");
out.println("</body>");
out.println("</html>");
out.close();
}