The necessary steps include:
- Add the following string declarations to the main method:
ArrayList<String> textList = new ArrayList<>();
textList.add("Language is a system that consists of the development, "
+ "acquisition, maintenance and use of complex systems of "
+ "communication, particularly the human ability to do so; "
+ "and a language is any specific example of such a system.");
textList.add("Language ist ein Lied des US-amerikanischen DJs und "
+ "Musikproduzenten Porter Robinson, das von Heather Bright "
+ "gesungen und am 10. April 2012 auf Beatport veröffentlicht"
+ " wurde. Language kann dem Genre Electro House zugeordnet "
+ "werden und hat 128 bpm. Die Vollversion war bereits ab "
+ "dem 26. März 2012 bei YouTube anhörbar. Der Track ist "
+ "unter anderem auch auf dem Soundtrack von Forza Horizon enthalten.");
- Insert a try block, which loads the model:
try {
LMClassifier lmClassifier = (LMClassifier) AbstractExternalizable
.readObject(new File("langid-leipzig.classifier"));
…
} catch (IOException | ClassNotFoundException ex) {
ex.printStackTrace();
}
- Classify and display the language used for each text with the following for loop:
for (String text : textList) {
Classification classification = lmClassifier.classify(text);
System.out.println("Best Language: "
+ classification.bestCategory());
}
- Execute the program. You will get the following output:
Best Language: en
Best Language: de