/** * Command Line Interface * @param args command line arguments */ public static void main(String[] args) { Command command = new Command(); command.addOpt("-d", "directory", "./"); command.addOpt("-a", "alpha", "" + DEFAULT_ALPHA); command.addOpt("-s", "seed", null); command.addOpt("-l", "lang", null); command.parse(args); if (command.hasOpt("--genprofile")) { command.generateProfile(); } else if (command.hasOpt("--genprofile-text")) { command.generateProfileFromText(); } else if (command.hasOpt("--detectlang")) { command.detectLang(); } else if (command.hasOpt("--batchtest")) { command.batchTest(); } }
/** * Language detection test for each file (--detectlang option) * * <pre> * usage: --detectlang -d [profile directory] -a [alpha] -s [seed] [test file(s)] * </pre> * */ public void detectLang() { if (loadProfile()) return; for (String filename: arglist) { BufferedReader is = null; try { is = new BufferedReader(new InputStreamReader(new FileInputStream(filename), "utf-8")); Detector detector = DetectorFactory.create(getDouble("alpha", DEFAULT_ALPHA)); if (hasOpt("--debug")) detector.setVerbose(); detector.append(is); System.out.println(filename + ":" + detector.getProbabilities()); } catch (IOException e) { e.printStackTrace(); } catch (LangDetectException e) { e.printStackTrace(); } finally { try { if (is!=null) is.close(); } catch (IOException e) {} } } }
/** * load profiles * @return false if load success */ private boolean loadProfile() { String profileDirectory = get("directory") + "/"; try { DetectorFactory.loadProfile(profileDirectory); Long seed = getLong("seed"); if (seed != null) DetectorFactory.setSeed(seed); return false; } catch (LangDetectException e) { System.err.println("ERROR: " + e.getMessage()); return true; } }
File directory = new File(get("directory")); for (String lang: arglist) { File file = searchFile(directory, lang + "wiki-.*-abstract\\.xml.*"); if (file == null) { System.err.println("Not Found abstract xml : lang = " + lang); profile.omitLessFreq(); File profile_path = new File(get("directory") + "/profiles/" + lang); os = new FileOutputStream(profile_path); JSON.encode(profile, os);
String lang = get("lang"); if (lang == null) { System.err.println("Need to specify langage code(-l)");
if (loadProfile()) return; HashMap<String, ArrayList<String>> result = new HashMap<String, ArrayList<String>>(); for (String filename: arglist) { String text = line.substring(idx + 1); Detector detector = DetectorFactory.create(getDouble("alpha", DEFAULT_ALPHA)); detector.append(text); String lang = ""; if (hasOpt("--debug")) System.out.println(correctLang + "," + lang + "," + (text.length()>100?text.substring(0, 100):text));