001: /*
002: * $Revision: 1.1 $
003: * $Date: 2006/06/10 13:32:32 $
004: * $Author: fdietz $
005: *
006: * Copyright (C) 2001 C. Scott Willy
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: */
022: package org.columba.mail.spellcheck.cswilly;
023:
024: //import java.io.*;
025: import java.util.HashMap;
026: import java.util.HashSet;
027: import java.util.List;
028:
029: /**
030: * A validator of a spell check results
031: *<p>
032: * After a spell check engine runs, its results must be validated (normally by
033: * a user). The {@link Validator} class provides this service.
034: */
035: public class Validator {
036: private final HashMap _changeAllMap = new HashMap();
037: private final HashSet _ignoreAllSet = new HashSet();
038:
039: /**
040: * Validate a line of words that have the <code>results</code> of a spell
041: * check.
042: *<p>
043: * @param line String with a line of words that are to be corrected
044: * @param results List of {@link Result} of a spell check
045: * @return new line with all corrected words validated
046: */
047: public String validate(String line, List results) {
048: String checkedLine = line;
049:
050: for (int ii = results.size() - 1; ii >= 0; ii--) {
051: Result result = (Result) results.get(ii);
052:
053: if (result.getType() != Result.OK) {
054: String replacementWord;
055:
056: if (_changeAllMap.containsKey(result.getOriginalWord())) {
057: replacementWord = (String) _changeAllMap.get(result
058: .getOriginalWord());
059: } else if (_ignoreAllSet.contains(result
060: .getOriginalWord())) {
061: replacementWord = result.getOriginalWord();
062: } else {
063: replacementWord = validate(result);
064:
065: if (replacementWord == null) {
066: checkedLine = null;
067:
068: break;
069: }
070: }
071:
072: if (replacementWord != null) {
073: checkedLine = replaceWord(checkedLine, result
074: .getOriginalWord(), result.getOffset(),
075: replacementWord);
076: }
077: }
078: }
079:
080: return checkedLine;
081: }
082:
083: /**
084: * Validates a single correction
085: *<p>
086: *
087: *<p>
088: * @param result A {@link Result} of spell checking one word
089: * @return validated correction (this is the replacement word). <i>null</i>
090: * is returned if the operation is cancelled. The replacement word
091: * maybe the same or different from the original word in
092: * <code>result</code>.
093: */
094: public String validate(Result result) {
095: String replacementWord = null;
096:
097: ValidationDialog validationDialog;
098: validationDialog = new ValidationDialog(result
099: .getOriginalWord(), result.getSuggestions());
100: validationDialog.setVisible(true);
101:
102: ValidationDialog.UserAction userAction = validationDialog
103: .getUserAction();
104:
105: if (userAction == ValidationDialog.CANCEL) {
106: replacementWord = null;
107: } else if (userAction == ValidationDialog.CHANGE_ALL) {
108: if (_changeAllMap.containsKey(result.getOriginalWord())) {
109: System.err
110: .println("Validator error: Change all twice same word: "
111: + result.getOriginalWord());
112: }
113:
114: _changeAllMap.put(result.getOriginalWord(),
115: validationDialog.getSelectedWord());
116: replacementWord = validationDialog.getSelectedWord();
117: } else if (userAction == ValidationDialog.CHANGE) {
118: replacementWord = validationDialog.getSelectedWord();
119: } else if (userAction == ValidationDialog.IGNORE_ALL) {
120: if (_ignoreAllSet.contains(result.getOriginalWord())) {
121: System.err
122: .println("Validator error: Ignore all twice same word: "
123: + result.getOriginalWord());
124: }
125:
126: _ignoreAllSet.add(result.getOriginalWord());
127: replacementWord = result.getOriginalWord();
128: } else if (userAction == ValidationDialog.IGNORE) {
129: replacementWord = result.getOriginalWord();
130: }
131:
132: return replacementWord;
133: }
134:
135: /**
136: * Helper method to replace the original word with the correction in the line
137: */
138: protected String replaceWord(String originalLine,
139: String originalWord, int originalIndex,
140: String replacementWord) {
141: String leftText = originalLine.substring(0, originalIndex - 1);
142: String rightText = originalLine
143: .substring((originalIndex + originalWord.length()) - 1);
144:
145: StringBuffer buf = new StringBuffer();
146: buf.append(leftText);
147: buf.append(replacementWord);
148: buf.append(rightText);
149:
150: return buf.toString();
151: }
152: }
|