/** * Load profiles from specified directory. * This method must be called once before language detection. * * @param profileDirectory profile directory path * @throws LangDetectException Can't open profiles(error code = {@link ErrorCode#FileLoadError}) * or profile's format is wrong (error code = {@link ErrorCode#FormatError}) */ public static void loadProfile(String profileDirectory) throws LangDetectException { loadProfile(new File(profileDirectory)); }
public LanguageIdentifier() throws LangDetectException { DetectorFactory.clear(); try { DetectorFactory.loadProfile(loadProfiles("profiles","profiles.cfg")); } catch (Exception e) { throw new LangDetectException(null, "Error in Initialization: "+e.getMessage()); } } /**
public static synchronized void loadData() { if (loaded) { return; } loaded = true; List<String> profileData = new ArrayList<String>(); try { Charset encoding = Charset.forName("UTF-8"); for (YLanguage language : detectableLanguages) { try (InputStream stream = new ClassPathResource("langdetect-profiles/" + language.getShortCode()).getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(stream, encoding));) { profileData.add(new String(IOUtils.toCharArray(reader))); } } DetectorFactory.loadProfile(profileData); DetectorFactory.setSeed(System.currentTimeMillis()); } catch (IOException | LangDetectException e) { throw new GeneralBusinessException(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; } }
/** * Initializes the language detection.<p> */ private void initLanguageDetection() { try { // use a seed for initializing the language detection for making sure the // same probabilities are detected for the same document contents DetectorFactory.clear(); DetectorFactory.setSeed(42L); DetectorFactory.loadProfile(loadProfiles(getAvailableLocales())); } catch (Exception e) { LOG.error(Messages.get().getBundle().key(Messages.INIT_I18N_LANG_DETECT_FAILED_0), e); } }