-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a spellchecker. Not finished, still needs support for user and ot…
…her language dictionaries. Also a settings gui
- Loading branch information
1 parent
e961e31
commit c808a3c
Showing
9 changed files
with
162,740 additions
and
6 deletions.
There are no files selected for viewing
Submodule MnM-Utils
updated
from f1006c to ec61e7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
TabbyChat-Common/src/main/java/mnm/mods/tabbychat/extra/spell/LangDict.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package mnm.mods.tabbychat.extra.spell; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
import mnm.mods.tabbychat.TabbyChat; | ||
|
||
public class LangDict { | ||
|
||
public static final LangDict ENGLISH = new LangDict("en_US"); | ||
|
||
private String path; | ||
|
||
public LangDict(String s) { | ||
path = s; | ||
} | ||
|
||
public boolean isClasspath() { | ||
return getClass().getClassLoader().getResource("dicts/" + path + "x.dic") != null; | ||
} | ||
|
||
public boolean isConfig() { | ||
File lang = new File(TabbyChat.getInstance().getDataFolder(), getPath()); | ||
return lang.exists() && lang.isFile(); | ||
} | ||
|
||
public InputStream openStream() throws IOException { | ||
InputStream in; | ||
if (isClasspath()) { | ||
in = getClass().getClassLoader().getResourceAsStream(getPath()); | ||
} else if (isConfig()) { | ||
File f = new File(TabbyChat.getInstance().getDataFolder(), getPath()); | ||
in = new FileInputStream(f); | ||
} else { | ||
// it doesn't exist. | ||
in = ENGLISH.openStream(); | ||
} | ||
return in; | ||
} | ||
|
||
private String getPath() { | ||
return String.format("dicts/%sx.dic", path); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
TabbyChat-Common/src/main/java/mnm/mods/tabbychat/extra/spell/Spellcheck.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package mnm.mods.tabbychat.extra.spell; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.io.Reader; | ||
import java.util.List; | ||
|
||
import com.google.common.io.Closeables; | ||
import com.swabunga.spell.engine.SpellDictionary; | ||
import com.swabunga.spell.engine.SpellDictionaryHashMap; | ||
import com.swabunga.spell.event.SpellChecker; | ||
|
||
import mnm.mods.tabbychat.TabbyChat; | ||
|
||
public class Spellcheck { | ||
|
||
private SpellChecker spellCheck; | ||
|
||
public Spellcheck(LangDict lang) { | ||
InputStream in = null; | ||
Reader read = null; | ||
try { | ||
in = lang.openStream(); | ||
read = new InputStreamReader(in); | ||
SpellDictionary dictionary = new SpellDictionaryHashMap(read); | ||
spellCheck = new SpellChecker(dictionary); | ||
} catch (IOException e) { | ||
TabbyChat.getLogger().warn("Error whie loading dictionary.", e); | ||
} finally { | ||
Closeables.closeQuietly(in); | ||
Closeables.closeQuietly(read); | ||
} | ||
} | ||
|
||
public List<String> getSuggestions(String word, int threshold) { | ||
return this.spellCheck.getSuggestions(word, threshold); | ||
} | ||
|
||
public boolean isCorrect(String word) { | ||
return this.spellCheck.isCorrect(word); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
TabbyChat-Common/src/main/java/mnm/mods/tabbychat/extra/spell/SpellingFormatter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package mnm.mods.tabbychat.extra.spell; | ||
|
||
import com.google.common.base.Function; | ||
|
||
import mnm.mods.tabbychat.TabbyChat; | ||
import mnm.mods.util.ChatBuilder; | ||
import mnm.mods.util.Color; | ||
import net.minecraft.util.ChatComponentText; | ||
import net.minecraft.util.IChatComponent; | ||
|
||
public class SpellingFormatter implements Function<String, IChatComponent> { | ||
|
||
private Spellcheck spelling; | ||
|
||
public SpellingFormatter(Spellcheck sp) { | ||
spelling = sp; | ||
} | ||
|
||
@Override | ||
public IChatComponent apply(String text) { | ||
if (!TabbyChat.getInstance().settings.general.spelling.enabled.getValue()) | ||
return new ChatComponentText(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); | ||
} | ||
} | ||
return b.build(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.