Skip to content

Commit

Permalink
Add a spellchecker. Not finished, still needs support for user and ot…
Browse files Browse the repository at this point in the history
…her language dictionaries. Also a settings gui
  • Loading branch information
mattmess1221 committed Sep 25, 2015
1 parent e961e31 commit c808a3c
Show file tree
Hide file tree
Showing 9 changed files with 162,740 additions and 6 deletions.
2 changes: 1 addition & 1 deletion MnM-Utils
1 change: 1 addition & 0 deletions TabbyChat-Common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ dependencies {
compile utils
compile api
compile ':liteloader:1.8-SNAPSHOT-mcpnames'
compile 'net.sf.jazzy:jazzy:0.5.2-rtext-1.4.1-2'

testCompile 'junit:junit:4.12'
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
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;
import mnm.mods.tabbychat.settings.TabbySettings;
Expand All @@ -49,6 +51,7 @@ public abstract class TabbyChat extends TabbyAPI {

private AddonManager addonManager;
private TabbyEvents events;
private Spellcheck spellcheck;

public TabbySettings settings;
@Nullable
Expand Down Expand Up @@ -84,6 +87,10 @@ public TabbyEvents getEventManager() {
return this.events;
}

public Spellcheck getSpellcheck() {
return spellcheck;
}

@Override
public ChatGui getGui() {
return GuiNewChatTC.getInstance().getChatManager().getChatBox();
Expand Down Expand Up @@ -120,6 +127,7 @@ protected void init() {

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

// Set global settings
settings = new TabbySettings();
Expand Down
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);
}
}
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);
}
}
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();
}

}
23 changes: 18 additions & 5 deletions TabbyChat-Common/src/main/java/mnm/mods/tabbychat/gui/TextBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,38 @@
import java.awt.Dimension;
import java.util.List;

import com.google.common.base.Function;
import com.google.common.collect.Lists;

import mnm.mods.tabbychat.TabbyChat;
import mnm.mods.tabbychat.api.Channel;
import mnm.mods.tabbychat.api.TabbyAPI;
import mnm.mods.tabbychat.api.gui.ChatInput;
import mnm.mods.tabbychat.core.GuiChatTC;
import mnm.mods.tabbychat.extra.spell.SpellingFormatter;
import mnm.mods.util.Color;
import mnm.mods.util.gui.GuiComponent;
import mnm.mods.util.gui.GuiText;
import mnm.mods.util.text.FancyFontRenderer;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.Gui;
import net.minecraft.client.gui.GuiTextField;
import net.minecraft.util.IChatComponent;

public class TextBox extends ChatGui implements ChatInput {

private FontRenderer fr = mc.fontRendererObj;
// Dummy textField
private GuiText textField = new GuiText();
private int cursorCounter;
private Function<String, IChatComponent> formatter;

public TextBox() {
super();
textField.getTextField().setMaxStringLength(300);
textField.setFocused(true);
textField.getTextField().setCanLoseFocus(false);
formatter = new SpellingFormatter(TabbyChat.getInstance().getSpellcheck());
}

@Override
Expand Down Expand Up @@ -71,8 +80,7 @@ private void drawCursor() {
yPos += fr.FONT_HEIGHT + 2;
}

if (textField.getCursorPosition() + size < this.textField
.getValue().length()) {
if (textField.getCursorPosition() + size < this.textField.getValue().length()) {
marker = '|';
} else {
marker = '_';
Expand All @@ -83,17 +91,18 @@ private void drawCursor() {
}

private void drawText() {
FancyFontRenderer ffr = new FancyFontRenderer(mc.fontRendererObj);
// selection
boolean started = false;
boolean ended = false;
GuiTextField textField = this.textField.getTextField();

int yPos = 2;
int pos = 0;
for (String line : getWrappedLines()) {
for (IChatComponent line : getFormattedLines()) {
ffr.drawChat(line, 1, yPos, false);
int xPos = 1;
for (Character c : line.toCharArray()) {
fr.drawString(c.toString(), xPos, yPos, getForeColor());
for (Character c : line.getUnformattedText().toCharArray()) {
int width = fr.getCharWidth(c);
int cursorPos = textField.getCursorPosition();
int selectDist = textField.getSelectedText().length();
Expand Down Expand Up @@ -157,6 +166,10 @@ public List<String> getWrappedLines() {
return fr.listFormattedStringToWidth(textField.getValue(), getBounds().width);
}

public List<IChatComponent> getFormattedLines() {
return Lists.transform(getWrappedLines(), formatter);
}

@Override
public Dimension getMinimumSize() {
return new Dimension(100, (fr.FONT_HEIGHT + 2) * getWrappedLines().size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,12 @@ public class GeneralSettings extends SettingObject<GeneralSettings> {
public SettingValue<Boolean> unreadFlashing = value(true);
@Setting
public SettingValue<Boolean> checkUpdates = value(true);
@Setting
public Spelling spelling = new Spelling();

public class Spelling extends SettingObject<Spelling> {

@Setting
public SettingValue<Boolean> enabled = value(true);
}
}
Loading

0 comments on commit c808a3c

Please sign in to comment.