package lemmaextractor; import org.ini4j.Ini; import org.ini4j.Profile; import java.io.File; import java.io.FileNotFoundException; import java.util.*; /** * Created by michal on 3/25/14. */ public class ProcessorFactory { public static LanguageProcessor initializeProcessor(String lang, Ini config, String iniDir, boolean useNER) throws FileNotFoundException { Profile.Section section = config.get("lang-" + lang); if(lang.equals("pl")){ String WCRFTModel = !section.entrySet().equals("null") ? checkPath(section.get("wcrftModel"), iniDir) : null; if(useNER){ String linerModel = checkPath(section.get("linerModel"), iniDir); return new PolishProcessor(section.get("wcrftConfig"), WCRFTModel, linerModel); } else{ return new PolishProcessor(section.get("wcrftConfig"), section.get("wcrftModel")); } } else if(lang.equals("en")){ String taggerModel = checkPath(section.get("taggerModel"), iniDir); if(useNER){ return new EnglishProcessor(taggerModel, checkPath(section.get("nerModel"), iniDir)); } else{ return new EnglishProcessor(taggerModel); } } else{ return new DummyProcessor(lang); } } public static HashMap initializeAll(Ini config, boolean useNER) throws FileNotFoundException { HashMap processors = new HashMap(); Collection langSections = config.values(); langSections.remove(config.get("main")); for(Ini.Section section: langSections){ String lang =section.getName().substring(5); processors.put(lang, initializeProcessor(lang, config, config.getFile().getParent(), useNER)); } processors.put("unknown", new DummyProcessor("unknown")); return processors; } private static String checkPath(String path, String iniDir) throws FileNotFoundException { File f = new File(path); if(!f.exists()){ File absF = new File(iniDir, path); if(absF.exists()){ return absF.getPath(); } else{ throw new FileNotFoundException("File "+path+" does not exist, provide an absolute path or relative to Extractor config file."); } } else{ return path; } } }