|
|
|
@ -29,6 +29,7 @@ import java.util.Iterator;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.Vector;
|
|
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This class can try to identify the language a text is written in.
|
|
|
|
@ -37,14 +38,14 @@ public final class Identificator {
|
|
|
|
|
|
|
|
|
|
private static final LanguageStatisticsHolder languages = LanguageStatisticsHolder.getInstance();
|
|
|
|
|
|
|
|
|
|
private final Map<Character, Integer> letter;
|
|
|
|
|
private final Map<Character, AtomicInteger> letter;
|
|
|
|
|
private int letters;
|
|
|
|
|
private String language;
|
|
|
|
|
|
|
|
|
|
public Identificator() {
|
|
|
|
|
letter = new HashMap<Character, Integer>();
|
|
|
|
|
letters = 0;
|
|
|
|
|
language = null;
|
|
|
|
|
this.letter = new HashMap<Character, AtomicInteger>();
|
|
|
|
|
this.letters = 0;
|
|
|
|
|
this.language = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -73,7 +74,7 @@ public final class Identificator {
|
|
|
|
|
upperLimit = limit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Identificator id = new Identificator();
|
|
|
|
|
final Identificator id = new Identificator();
|
|
|
|
|
|
|
|
|
|
// count number of characters in text
|
|
|
|
|
for (int i = 0; i < upperLimit; i++) id.inc(text.charAt(i));
|
|
|
|
@ -83,25 +84,25 @@ public final class Identificator {
|
|
|
|
|
|
|
|
|
|
public void inc(final char c) {
|
|
|
|
|
if (!Character.isLetter(c)) return;
|
|
|
|
|
Character cc = Character.toLowerCase(c);
|
|
|
|
|
Integer i = letter.get(cc);
|
|
|
|
|
final Character cc = Character.toLowerCase(c);
|
|
|
|
|
final AtomicInteger i = this.letter.get(cc);
|
|
|
|
|
if (i == null) {
|
|
|
|
|
letter.put(cc, 1);
|
|
|
|
|
this.letter.put(cc, new AtomicInteger(1));
|
|
|
|
|
} else {
|
|
|
|
|
letter.put(cc, i.intValue() + 1);
|
|
|
|
|
i.incrementAndGet();
|
|
|
|
|
}
|
|
|
|
|
letters++;
|
|
|
|
|
this.letters++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void add(String word) {
|
|
|
|
|
public void add(final String word) {
|
|
|
|
|
if (word == null) return;
|
|
|
|
|
for (int i = 0; i < word.length(); i++) inc(word.charAt(i));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String getLanguage() {
|
|
|
|
|
|
|
|
|
|
if (language != null) return language; // don't compute that twice
|
|
|
|
|
if (letters == 0) return null; // not enough information available
|
|
|
|
|
if (this.language != null) return this.language; // don't compute that twice
|
|
|
|
|
if (this.letters == 0) return null; // not enough information available
|
|
|
|
|
|
|
|
|
|
final LanguageStatistics testStat = new LanguageStatistics("text");
|
|
|
|
|
|
|
|
|
@ -110,14 +111,14 @@ public final class Identificator {
|
|
|
|
|
Character maxChar = null;
|
|
|
|
|
int count = 0;
|
|
|
|
|
int max = 0;
|
|
|
|
|
for (Map.Entry<Character, Integer> e: letter.entrySet()) {
|
|
|
|
|
for (final Map.Entry<Character, AtomicInteger> e: this.letter.entrySet()) {
|
|
|
|
|
character = e.getKey();
|
|
|
|
|
count = e.getValue().intValue();
|
|
|
|
|
if (count > max) {
|
|
|
|
|
maxChar = character;
|
|
|
|
|
max = count;
|
|
|
|
|
}
|
|
|
|
|
testStat.put(character, ((float) 100) * ((float) count) / ((float) letters));
|
|
|
|
|
testStat.put(character, ((float) 100) * ((float) count) / (this.letters));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// create list with relevant languages
|
|
|
|
@ -136,7 +137,7 @@ public final class Identificator {
|
|
|
|
|
final float[] offsetList = new float[relevantLanguages.size()];
|
|
|
|
|
final int[] votesList = new int[relevantLanguages.size()];
|
|
|
|
|
|
|
|
|
|
Iterator<Character> iter = testStat.keySet().iterator();
|
|
|
|
|
final Iterator<Character> iter = testStat.keySet().iterator();
|
|
|
|
|
float minimum;
|
|
|
|
|
float offset = 0;
|
|
|
|
|
float valueCharacter;
|
|
|
|
@ -175,8 +176,8 @@ public final class Identificator {
|
|
|
|
|
// prevents a language beeing reported that has won the voting, but
|
|
|
|
|
// is still not the right language.
|
|
|
|
|
if (offset < 20) {
|
|
|
|
|
language = languages.get(relevantLanguages.get(bestLanguage)).getName();
|
|
|
|
|
return language;
|
|
|
|
|
this.language = languages.get(relevantLanguages.get(bestLanguage)).getName();
|
|
|
|
|
return this.language;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|