-
Notifications
You must be signed in to change notification settings - Fork 1
/
DetectionExtLib.java
38 lines (33 loc) · 1.29 KB
/
DetectionExtLib.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.jleth.andorid.langdetect;
import android.os.AsyncTask;
import android.os.SystemClock;
import android.util.Log;
import com.cybozu.labs.langdetect.Detector;
import com.cybozu.labs.langdetect.Language;
import com.cybozu.labs.langdetect.DetectorFactory;
import java.util.ArrayList;
/**
* Detection of language using {@link Detector}
*/
public class DetectionExtLib extends AsyncTask<String, Void, DetectionResult> {
@Override
protected DetectionResult doInBackground(String... params) {
long startTime = SystemClock.elapsedRealtime();
DetectionResult result = new DetectionResult();
try {
Detector detector = DetectorFactory.create();
detector.append(params[0]);
// detector.setVerbose(); // Will add a significant overhead of ~500ms
ArrayList<Language> tre = detector.getProbabilities();
result.list = new ArrayList<>(tre.size());
for (Language language : tre) {
result.list.add(new com.jleth.andorid.langdetect.Language(language.lang, language.prob));
}
result.executionTime = SystemClock.elapsedRealtime() - startTime;
} catch (Exception e) {
Log.d("MAIN", "Error", e);
result.e = e;
}
return result;
}
}