jeudi 29 novembre 2018

Advice if DRY could be applied for the following Java Code

public static HashMap<Language, Double> getBigramResult(ArrayList<Character> textCharList) {
        HashMap<Language, Double> totalProbabilities = new HashMap<Language, Double>();
        for (int j = 0; j < textCharList.size() - 1; j++) {
            if (textCharList.get(j) != '+' && textCharList.get(j + 1) != '+') {
                FileHandler.writeSentences("BIGRAM :"+textCharList.get(j)+""+textCharList.get(j + 1));
                for (int k = 0; k < biGramList.size(); k++) {
                    BiGramV2 temp = biGramList.get(k);
                    double conditionalProbability = Math.log10(temp.getConditionalProbabilty(textCharList.get(j),
                            textCharList.get(j + 1)));
                    updateTotalProbabilities(totalProbabilities,temp.getLanguage(),conditionalProbability);
                    FileHandler.writeSentences(temp.getLanguage().toString()+ ": p("+textCharList.get(j+1)+"|"+textCharList.get(j) +") ="+conditionalProbability+"==> log prob of sentence so far: " +totalProbabilities.get(temp.getLanguage()));
                }
                FileHandler.writeSentences("");
            }
        }
        return totalProbabilities;
    }

    public static HashMap<Language, Double> getUnigramResult(ArrayList<Character> textCharList) {
        HashMap<Language, Double> totalProbabilities = new HashMap<Language, Double>();
        for (int j = 0; j < textCharList.size(); j++) {
            if (textCharList.get(j) != '+') {
                FileHandler.writeSentences("UNIGRAM :"+textCharList.get(j));
                for (int k = 0; k < uniGramList.size(); k++) {
                    Unigram temp = uniGramList.get(k);
                    double conditionalProbability = Math.log10(temp.getProbabilty(textCharList.get(j)));
                    updateTotalProbabilities(totalProbabilities,temp.getLanguage(),conditionalProbability);
                    FileHandler.writeSentences(temp.getLanguage().toString()+ ": p("+textCharList.get(j)+") ="+conditionalProbability+"==> log prob of sentence so far: " +totalProbabilities.get(temp.getLanguage()));

                }
                FileHandler.writeSentences("");
            }
        }
        return totalProbabilities;
    }

public static void updateTotalProbabilities(HashMap<Language, Double> totalProbabilities,Language l,double conditionalProbability)
{

    Double oldValue = totalProbabilities.get( l);
    totalProbabilities.put( l,oldValue != null ? oldValue + conditionalProbability : conditionalProbability);

}

In the above code ArrayList textCharList => List of lowercase characters including '+'.

BiGramList is a list of "BiGramV2" class which has 2 methods "getLanguage()" that returns either Germany, English or French (as enum) and "getConditionalProbabilty()" that takes 2 characters and return a double.

Now almost similar is UniGramList is a list of UniGram class has 2 methods "getLangauge()" which is same as Bigram and "getProbabilty()" that takes 1 character and returns a double.

Both the above methods are very similar, and I feel like its not design efficient, but I am not able to refactor them because of the different outer for-loop, if block and different probability calculating methods. Any suggestion on my code would be appreciated.

Aucun commentaire:

Enregistrer un commentaire