📄 datareader.java
字号:
if(!addedModules.containsKey(module)) { distribGroup.add(module); addedModules.put(module, module); } else { printerr("module " + module + " is already in a distribution group"); createDistributionGroup = false; break; } } else { printerr(module + " is not a specified module"); createDistributionGroup = false; break; } } if(createDistributionGroup) { modelmaker.addDistributionGroup(distribGroup); j++; } else { /* do not add group, let user try again */ for(ListIterator i = (ListIterator)distribGroup.iterator();i.hasNext();) { String n = (String)(i.next()); addedModules.remove(n); } } } } /******************************************************************************************** ********************** methods for getting transition ties **************************** ********************************************************************************************/ private void specifyTransitionTies() { int nrModules = modelmaker.getNrModules(); String[] theNames = new String[nrModules]; Hashtable addedModules = new Hashtable(); println("Specify transition-tie groups"); print("(Possible modules are:"); int j = 0; for(ListIterator i = modelmaker.getModules();i.hasNext();) { String name = ((Module)i.next()).getName(); print(" " + name); theNames[j] = name; j++; } print(")"); newln(); boolean done = false; boolean createTransTieGroup; j = 1; while(!done) { LinkedList transTieGroup = new LinkedList(); print("Transition-tie group " + j + " (separate by ';'):"); String s = readln(); if(s.equals("") || s.equals("d") || s.equals("done")) { done = true; continue; } s = s.trim(); StringTokenizer st = new StringTokenizer(s,";"); createTransTieGroup = true; while(st.hasMoreTokens()) { String module = st.nextToken(); module = module.trim(); if(isModuleName(module, theNames)) { if(!addedModules.containsKey(module)) { transTieGroup.add(module); addedModules.put(module, module); if(!modelmaker.identicalModules(((String)transTieGroup.get(0)), module)) { printerr("module " + module + " is of incorrect type"); createTransTieGroup = false; break; } } else { printerr("module " + module + " is already in a distribution group"); createTransTieGroup = false; break; } } else { printerr(module + " is not a specified module"); createTransTieGroup = false; break; } } if(createTransTieGroup) { modelmaker.addTransTieGroup(transTieGroup); j++; } else { /* do not add group, let user try again */ for(ListIterator i = (ListIterator)transTieGroup.iterator();i.hasNext();) { String n = (String)(i.next()); addedModules.remove(n); } } } } /******************************************************************************************** ********************** methods for getting initial values **************************** ********************************************************************************************/ private void specifyInitvalues() { LinkedList restModules = new LinkedList(); for(ListIterator i = modelmaker.getModules();i.hasNext();) { restModules.add(i.next()); } println("Specify initial emission probabilities and prior distributions for each " + "module/distribution group"); for(ListIterator i = modelmaker.getDistributionGroups();i.hasNext();) { print("[ "); LinkedList distribGroup = (LinkedList)i.next(); for(ListIterator j = (ListIterator)distribGroup.iterator(); j.hasNext();) { Module m = modelmaker.getModule(((String)j.next())); print(m.getName() + " "); restModules.remove(m); } print("] "); boolean done = false; while(!done) { print("(initprobtype, priorfile, emisspriorscaler):"); String s = readln(); StringTokenizer st = new StringTokenizer(s,","); int initDistribType = HMM.EVEN; boolean locked = false; double[] initDistrib = null; if(st.countTokens() > 3) { printerr("illegal choice"); continue; } else if(st.countTokens() == 0) { s = null; } else { /* read init distribution instruction */ s = st.nextToken(); s = s.trim(); initDistribType = parseDistribType(s); initDistrib = null; if(initDistribType == HMM.NOTYPE) { continue; } else if(initDistribType == HMM.MANUAL || initDistribType == HMM.LOCKED_MANUAL) { initDistrib = getInitDistribution(); if(initDistrib == null) { continue; } if(initDistribType == HMM.LOCKED_MANUAL) { locked = true; } } else if(initDistribType == HMM.RANDOM) { initDistrib = getRandomDistrib(); } else if(initDistribType == HMM.LOCKED_EVEN) { locked = true; } else { } } double priScaleValue = 1.0; if(st.countTokens() == 1) { /* read prior file */ s = st.nextToken(); s = s.trim(); } else if(st.countTokens() == 2) { /* read prior file */ s = st.nextToken(); s = s.trim(); /* read prior scaler */ String priScale = st.nextToken(); priScale = priScale.trim(); priScaleValue = 1.0; try { priScaleValue = Double.parseDouble(priScale); if(priScaleValue < 0.0) { printerr("illegal value, setting priorscaler to 1.0"); priScaleValue = 1.0; } } catch(NumberFormatException e){ printerr("illegal value, setting priorscaler to 1.0"); } } else if(st.countTokens() == 0) { s = null; } /* give info to modules in distribution group */ for(ListIterator k = (ListIterator)distribGroup.iterator(); k.hasNext();) { Module m = modelmaker.getModule((String) k.next()); m.setPriorfile(s); m.setEmissPriorScaler(priScaleValue); modelmaker.addPriorfile(s); if(initDistribType == HMM.MANUAL || initDistribType == HMM.RANDOM) { m.setDistribType(initDistribType, initDistrib); } else { m.setDistribType(initDistribType); } if(locked) { m.lockVertexEmissions(); } } done = true; } } for(ListIterator i = (ListIterator)restModules.iterator();i.hasNext();) { Module m = (Module)i.next(); if(m.getVertexType() == HMM.START || m.getVertexType() == HMM.END) { continue; } print(m.getName() + " "); boolean done = false; while(!done) { print("(initprobtype, priorfile, emisspriorscaler):"); String s = readln(); StringTokenizer st = new StringTokenizer(s,","); int initDistribType = HMM.EVEN; boolean locked = false; double[] initDistrib = null; if(st.countTokens() > 3 ) { printerr("illegal choice"); continue; } else if(st.countTokens() == 0) { s = null; } else { /* read init distribution instruction */ s = st.nextToken(); s = s.trim(); initDistribType = parseDistribType(s); initDistrib = null; if(initDistribType == HMM.NOTYPE) { continue; } else if(initDistribType == HMM.MANUAL || initDistribType == HMM.LOCKED_MANUAL) { initDistrib = getInitDistribution(); if(initDistrib == null) { continue; } if(initDistribType == HMM.LOCKED_MANUAL) { locked = true; } } else if(initDistribType == HMM.RANDOM) { initDistrib = getRandomDistrib(); } else if(initDistribType == HMM.LOCKED_EVEN) { locked = true; } else { } } double priScaleValue = 1.0; if(st.countTokens() == 1) { /* read prior file */ s = st.nextToken(); s = s.trim(); } else if(st.countTokens() == 2) { /* read prior file */ s = st.nextToken(); s = s.trim(); /* read prior scaler */ String priScale = st.nextToken(); priScale = priScale.trim(); priScaleValue = 1.0; try { priScaleValue = Double.parseDouble(priScale); if(priScaleValue < 0.0) { printerr("illegal value, setting priorscaler to 1.0"); priScaleValue = 1.0; } } catch(NumberFormatException e){ printerr("illegal value, setting priorscaler to 1.0"); } } else if(st.countTokens() == 0) { s = null; } /* give info to module */ m.setPriorfile(s); m.setEmissPriorScaler(priScaleValue); modelmaker.addPriorfile(s); if(initDistribType == HMM.MANUAL) { m.setDistribType(initDistribType, initDistrib); } else { m.setDistribType(initDistribType); } if(locked) { m.lockVertexEmissions(); } done = true; } } } /******************************************************************************************** ********************** methods for getting initial values **************************** ********************************************************************************************/ private void specifyTransitionInitvalues() { LinkedList restModules = new LinkedList(); for(ListIterator i = modelmaker.getModules();i.hasNext();) { restModules.add(i.next()); } println("Specify initial transition probabilities and prior distributions for each " + "module/transition distribution group"); for(ListIterator i = modelmaker.getTransTieGroups();i.hasNext();) { print("[ "); LinkedList distribGroup = (LinkedList)i.next(); for(ListIterator j = (ListIterator)distribGroup.iterator(); j.hasNext();) { Module m = modelmaker.getModule(((String)j.next())); print(m.getName() + " "); restModules.remove(m); } print("] "); boolean done = false; while(!done) { print("(priorfile, transpriorscaler):"); String s = readln(); StringTokenizer st = new StringTokenizer(s,","); int initDistribType = HMM.EVEN; double[] initDistrib = null; double priScaleValue = 1.0; if(st.countTokens() > 2) { printerr("illegal choice, try again"); continue; } else if(st.countTokens() == 0) { s = null; } else { if(st.countTokens() == 1) { /* read prior file */ s = st.nextToken(); s = s.trim(); } else if(st.countTokens() == 2) { /* read prior file */ s = st.nextToken(); s = s.trim(); /* read prior scaler */ String priScale = st.nextToken(); priScale = priScale.trim(); priScaleValue = 1.0; try { priScaleValue = Double.parseDouble(priScale); if(priScaleValue < 0.0) { printerr("illegal value, setting priorscaler to 1.0"); priScaleValue = 1.0; } } catch(NumberFormatException e){ printerr("illegal value, setting priorscaler to 1.0"); } } } /* give info to modules in distribution group */ for(ListIterator k = (ListIterator)distribGroup.iterator(); k.hasNext();) { Module m = modelmaker.getModule((String) k.next()); m.setTransPriorfile(s); m.setTransPriorScaler(priScaleValue); modelmaker.addTransPriorfile(s); } done = true; } } for(ListIterator i = (ListIterator)restModules.iterator();i.hasNext();) { Module m = (Module)i.next(); if(m.getVertexType() == HMM.START || m.getVertexType() == HMM.END) { continue; } print(m.getName() + " "); boolean done = false; while(!done) { print("(priorfile, transpriorscaler):"); String s = readln(); StringTokenizer st = new StringTokenizer(s,","); int initDistribType = HMM.EVEN; double[] initDistrib = null; double priScaleValue = 1.0; if(st.countTokens() > 3 ) { printerr("illegal choice"); continue; } else if(st.countTokens() == 0) { s = null; } else { if(st.countTokens() == 1) { /* read prior file */ s = st.nextToken(); s = s.trim(); } else if(st.countTokens() == 2) { /* read prior file */ s = st.nextToken(); s = s.trim(); /* read prior scaler */ String priScale = st.nextToken(); priScale = priScale.trim(); priScaleValue = 1.0; try { priScaleValue = Double.parseDouble(priScale); if(priScaleValue < 0.0) { printerr("illegal value, setting priorscaler to 1.0"); priScaleValue = 1.0; } } catch(NumberFormatException e){ printerr("illegal value, setting priorscaler to 1.0"); } } } /* give info to module */ m.setPriorfile(s); m.setTransPriorScaler(priScaleValue); modelmaker.addPriorfile(s); done = true; } } /* m鰆ligg鰎 specificering av transitionssannolikheter inom modul, och fr錸 modul, specifikt f鰎 varje modul */ } private int parseDistribType(String t) { t = t.toLowerCase(); if(t.equals("u")){ return HMM.EVEN; } else if(t.equals("uniform")) { return HMM.EVEN; } else if(t.equals("z")) { return HMM.ZERO; } else if(t.equals("zero")) { return HMM.ZERO; } else if(t.equals("r")) { return HMM.RANDOM; } else if(t.equals("random")) { return HMM.RANDOM; } else if(t.equals("m")) { return HMM.MANUAL; } else if(t.equals("man")) { return HMM.MANUAL; } else if(t.equals("lm")) { return HMM.LOCKED_MANUAL; } else if(t.equals("locked_man")) { return HMM.LOCKED_MANUAL; } else if(t.equals("lu")) { return HMM.LOCKED_EVEN; } else if(t.equals("locked_uniform")) { return HMM.LOCKED_EVEN; } /* Free to define more valid types here */ else { /* type not defined */ return HMM.NOTYPE; } } private double[] getInitDistribution() { print("Specify initial emission probabilities file ('k' to write on keyboard): "); String s = readln(); if(s.equals("k")) { print("Enter probabilities (separate by blank): "); s = readln(); return makeProbArray(s); } else { /* read probs from file */ try { BufferedReader probReader = new BufferedReader(new FileReader(s)); String probs = probReader.readLine(); probs = probs.trim(); return makeProbArray(probs); } catch(IOException e) { P.MESSAGE("I/O error: Couldn't read from file '" + s + "'"); return null; } } } private double[] getRandomDistrib() { double[] probs = new double[modelmaker.getAlphabetSize()]; int nrEmissions = modelmaker.getAlphabetSize(); double sum = 0; for(int i = 0; i < nrEmissions; i++) { double initProb = Math.random(); probs[i] = initProb; sum = sum + initProb; } for(int i = 0; i < nrEmissions; i++) { probs[i] = probs[i] / sum; } return probs; } private double[] makeProbArray(String s) { StringTokenizer st = new StringTokenizer(s, " "); double[] probs = new double[modelmaker.getAlphabetSize()]; if(st.countTokens() != modelmaker.getAlphabetSize()) { P.MESSAGE("Distribution has incorrect format for this alphabet"); return null; } else { try { double sum = 0; for(int i = 0; i < modelmaker.getAlphabetSize();i++) { String p = st.nextToken(); double prob = Double.parseDouble(p); sum = sum + prob; probs[i] = prob; } if(sum != 1.0) { P.MESSAGE("Warning: sum of probabilities not equal" + " to 1.0: autocorrecting"); for(int i = 0; i < modelmaker.getAlphabetSize();i++) { probs[i] = probs[i] / sum; } } } catch(NumberFormatException e) { P.MESSAGE("Some distribution object is not a number"); return null; } } return probs; } private void cleanUp() { } /******************************************************************************************** ********************** method for saving HMM **************************** ********************************************************************************************/ private void saveHMM(String outdir) { int res = modelmaker.saveHMM(outdir); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -