Skip to content

Commit

Permalink
some code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
danielnaber committed Aug 9, 2018
1 parent efa2ec7 commit 2725336
Showing 1 changed file with 23 additions and 26 deletions.
Expand Up @@ -44,7 +44,7 @@ public class SuggestionsOrderer {

private boolean mlAvailable = true;

private static SuggestionsOrderer.NGramUtil nGramUtil = null;
private static NGramUtil nGramUtil = null;
private static Predictor predictor;

public boolean isMlAvailable() {
Expand All @@ -53,9 +53,9 @@ public boolean isMlAvailable() {

public SuggestionsOrderer(Language language, String ruleId) {
try {
nGramUtil = new SuggestionsOrderer.NGramUtil(language);
String ngramBasedModelFilename = SuggestionsOrderer.XGBOOST_MODEL_BASE_PATH + ruleId + "/" + SuggestionsOrderer.SPC_NGRAM_BASED_MODEL_FILENAME;
String nonNgramModelFilename = SuggestionsOrderer.XGBOOST_MODEL_BASE_PATH + ruleId + "/" + SuggestionsOrderer.NO_NGRAM_BASED_MODEL_FILENAME;
nGramUtil = new NGramUtil(language);
String ngramBasedModelFilename = XGBOOST_MODEL_BASE_PATH + ruleId + "/" + SPC_NGRAM_BASED_MODEL_FILENAME;
String nonNgramModelFilename = XGBOOST_MODEL_BASE_PATH + ruleId + "/" + NO_NGRAM_BASED_MODEL_FILENAME;

String languageModelFileName;
if (nGramUtil.isMockLanguageModel()) {
Expand All @@ -64,11 +64,11 @@ public SuggestionsOrderer(Language language, String ruleId) {
languageModelFileName = ngramBasedModelFilename;
}

try (InputStream models_path = this.getClass().getClassLoader().getResourceAsStream(languageModelFileName)) {
predictor = new Predictor(models_path);
try (InputStream modelsPath = this.getClass().getClassLoader().getResourceAsStream(languageModelFileName)) {
predictor = new Predictor(modelsPath);
} catch (IOException | NullPointerException e) {
try (InputStream models_path = this.getClass().getClassLoader().getResourceAsStream(COMMON_DEFAULT_MODEL_PATH)) {
predictor = new Predictor(models_path);
try (InputStream modelsPath = this.getClass().getClassLoader().getResourceAsStream(COMMON_DEFAULT_MODEL_PATH)) {
predictor = new Predictor(modelsPath);
} catch (IOException | NullPointerException e1) {
mlAvailable = false;
}
Expand Down Expand Up @@ -160,14 +160,13 @@ public List<String> orderSuggestionsUsingModel(List<String> suggestions, String
return result;
}

@SuppressWarnings("WeakerAccess")
static class NGramUtil {
private static class NGramUtil {

private Language language;
private LanguageModel languageModel;
private boolean mockLanguageModel = false;

public NGramUtil(Language language) {
private NGramUtil(Language language) {
this.language = language;
try {
String ngramPath = SuggestionsOrdererConfig.getNgramsPath();
Expand All @@ -185,38 +184,37 @@ public NGramUtil(Language language) {
}
}

public List<String> tokenizeString(String s) {
private List<String> tokenizeString(String s) {
return GoogleTokenUtil.getGoogleTokensForString(s, false, language);
}

public Double stringProbability(List<String> sTokenized, int length) {
private Double stringProbability(List<String> sTokenized, int length) {
if (sTokenized.size() > length) {
sTokenized = sTokenized.subList(sTokenized.size() - length, sTokenized.size());
}
return sTokenized.isEmpty() ? null : languageModel.getPseudoProbability(sTokenized).getProb();
}

public boolean isMockLanguageModel() {
private boolean isMockLanguageModel() {
return mockLanguageModel;
}
}

@SuppressWarnings("WeakerAccess")
static class ContextUtils {
private static class ContextUtils {

public static String leftContext(String originalSentence, int errorStartIdx, String errorString, int contextLength) {
private static String leftContext(String originalSentence, int errorStartIdx, String errorString, int contextLength) {
String regex = repeat(contextLength, "\\w+\\W+") + errorString + "$";
String stringToSearch = originalSentence.substring(0, errorStartIdx + errorString.length());
return findFirstRegexMatch(regex, stringToSearch);
}

public static String rightContext(String originalSentence, int errorStartIdx, String errorString, int contextLength) {
private static String rightContext(String originalSentence, int errorStartIdx, String errorString, int contextLength) {
String regex = "^" + errorString + repeat(contextLength, "\\W+\\w+");
String stringToSearch = originalSentence.substring(errorStartIdx);
return findFirstRegexMatch(regex, stringToSearch);
}

public static int firstDifferencePosition(String sentence1, String sentence2) {
private static int firstDifferencePosition(String sentence1, String sentence2) {
int result = -1;
for (int i = 0; i < sentence1.length(); i++) {
if (i >= sentence2.length() || sentence1.charAt(i) != sentence2.charAt(i)) {
Expand All @@ -227,7 +225,7 @@ public static int firstDifferencePosition(String sentence1, String sentence2) {
return result;
}

public static int startOfErrorString(String sentence, String errorString, int sentencesDifferenceCharIdx) {
private static int startOfErrorString(String sentence, String errorString, int sentencesDifferenceCharIdx) {
int result = -1;
List<Integer> possibleIntersections = allIndexesOf(sentence.charAt(sentencesDifferenceCharIdx), errorString);
for (int i : possibleIntersections) {
Expand All @@ -244,7 +242,7 @@ public static int startOfErrorString(String sentence, String errorString, int se
return result;
}

public static String getMaximalPossibleRightContext(String sentence, int errorStartIdx, String errorString,
private static String getMaximalPossibleRightContext(String sentence, int errorStartIdx, String errorString,
int startingContextLength) {
String rightContext = "";
for (int contextLength = startingContextLength; contextLength > 0; contextLength--) {
Expand All @@ -256,7 +254,7 @@ public static String getMaximalPossibleRightContext(String sentence, int errorSt
return rightContext;
}

public static String getMaximalPossibleLeftContext(String sentence, int errorStartIdx, String errorString,
private static String getMaximalPossibleLeftContext(String sentence, int errorStartIdx, String errorString,
int startingContextLength) {
String leftContext = "";
for (int contextLength = startingContextLength; contextLength > 0; contextLength--) {
Expand All @@ -268,7 +266,7 @@ public static String getMaximalPossibleLeftContext(String sentence, int errorSta
return leftContext;
}

public static Pair<String, String> extractContext(String sentence, String covered, int errorStartIdx, int contextLength) {
private static Pair<String, String> extractContext(String sentence, String covered, int errorStartIdx, int contextLength) {
int errorEndIdx = errorStartIdx + covered.length();
String errorString = sentence.substring(errorStartIdx, errorEndIdx);

Expand All @@ -278,8 +276,7 @@ public static Pair<String, String> extractContext(String sentence, String covere
return Pair.of(leftContext, rightContext);
}


public static String longestCommonPrefix(String[] strs) {
private static String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length == 0) {
return "";
}
Expand Down Expand Up @@ -309,7 +306,7 @@ public static String longestCommonPrefix(String[] strs) {
return strs[0].substring(0, minLen);
}

public static int editDistance(String x, String y) {
private static int editDistance(String x, String y) {
int[][] dp = new int[x.length() + 1][y.length() + 1];

for (int i = 0; i <= x.length(); i++) {
Expand Down

0 comments on commit 2725336

Please sign in to comment.