Skip to content

Commit

Permalink
Spelling fixes towards #34
Browse files Browse the repository at this point in the history
- first word is checked
- words with punctuation is checked
- words with numbers are not checked
- all caps are not checked
  • Loading branch information
mattmess1221 committed Oct 1, 2015
1 parent a3df3c1 commit cf4718e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
2 changes: 1 addition & 1 deletion MnM-Utils
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import mnm.mods.tabbychat.extra.ChatAddonAntiSpam;
import mnm.mods.tabbychat.extra.ChatLogging;
import mnm.mods.tabbychat.extra.filters.FilterAddon;
import mnm.mods.tabbychat.extra.spell.LangDict;
import mnm.mods.tabbychat.extra.spell.Spellcheck;
import mnm.mods.tabbychat.gui.settings.GuiSettingsScreen;
import mnm.mods.tabbychat.settings.ServerSettings;
Expand Down Expand Up @@ -127,7 +126,7 @@ protected void init() {

addonManager = new TabbyAddonManager();
events = new TabbyEvents(addonManager);
spellcheck = new Spellcheck(LangDict.ENGLISH);
spellcheck = new Spellcheck();

// Set global settings
settings = new TabbySettings();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,40 @@
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.List;
import java.util.Set;

import com.google.common.collect.Sets;
import com.google.common.io.Closeables;
import com.swabunga.spell.engine.SpellDictionary;
import com.swabunga.spell.engine.SpellDictionaryHashMap;
import com.swabunga.spell.event.SpellCheckEvent;
import com.swabunga.spell.event.SpellCheckListener;
import com.swabunga.spell.event.SpellChecker;
import com.swabunga.spell.event.StringWordTokenizer;

import mnm.mods.tabbychat.TabbyChat;

public class Spellcheck {

private SpellChecker spellCheck;
private Set<String> errors = Sets.newHashSet();

public Spellcheck(LangDict lang) {
public Spellcheck() {
InputStream in = null;
Reader read = null;
try {
in = lang.openStream();
in = LangDict.ENGLISH.openStream();
read = new InputStreamReader(in);
SpellDictionary dictionary = new SpellDictionaryHashMap(read);
spellCheck = new SpellChecker(dictionary);
spellCheck.addSpellCheckListener(new SpellCheckListener() {
@Override
public synchronized void spellingError(SpellCheckEvent event) {
errors.add(event.getInvalidWord());
// System.out.println(event.getWordContextPosition() + " " +
// event.getInvalidWord());
}
});
} catch (IOException e) {
TabbyChat.getLogger().warn("Error whie loading dictionary.", e);
} finally {
Expand All @@ -37,7 +51,13 @@ public List<String> getSuggestions(String word, int threshold) {
return this.spellCheck.getSuggestions(word, threshold);
}

public void checkSpelling(String string) {
this.errors.clear();
this.spellCheck.checkSpelling(new StringWordTokenizer(string));
}

public boolean isCorrect(String word) {
return this.spellCheck.isCorrect(word);
return !errors.contains(word);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

public class SpellingFormatter implements Function<String, IChatComponent> {

private Spellcheck spelling;
private final Spellcheck spelling;

public SpellingFormatter(Spellcheck sp) {
spelling = sp;
Expand All @@ -20,15 +20,15 @@ public SpellingFormatter(Spellcheck sp) {
public IChatComponent apply(String text) {
if (!TabbyChat.getInstance().settings.general.spelling.enabled.getValue())
return new ChatComponentText(text);
spelling.checkSpelling(text);
String[] split = text.split(" ");
ChatBuilder b = new ChatBuilder();
for (String word : split) {
if (b.size() > 0)
b.text(" ");
b.text(word);
if (!spelling.isCorrect(word)) {
b.underline(Color.RED);
}
b.text(" ");
}
return b.build();
}
Expand Down

0 comments on commit cf4718e

Please sign in to comment.