⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 datareadermultialpha.java

📁 马尔科夫模型的c语言实现
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
		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(nr,s);		    m.setEmissPriorScaler(nr,priScaleValue);		    modelmaker.addPriorfile(nr,s);		    if(initDistribType == HMM.MANUAL || initDistribType == HMM.RANDOM) {			m.setDistribType(nr, initDistribType, initDistrib);		    }		    else {			m.setDistribType(nr, 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(" (alphabet nr " + nr + ") (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(nr);			if(initDistrib == null) {			    continue;			}			if(initDistribType == HMM.LOCKED_MANUAL) {			    locked = true;			}		    }		    else if(initDistribType == HMM.RANDOM) {			initDistrib = getRandomDistrib(nr);		    }		    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(nr,s);		m.setEmissPriorScaler(nr,priScaleValue);		modelmaker.addPriorfile(nr,s);		if(initDistribType == HMM.MANUAL) {		    m.setDistribType(nr,initDistribType, initDistrib);		}		else {		    m.setDistribType(nr,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.setTransPriorfile(s);		m.setTransPriorScaler(priScaleValue);		modelmaker.addTransPriorfile(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[] getInitDistribution(int nr)    {	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(nr, s);	}	else {	    /* read probs from file */	    try {		BufferedReader probReader = new BufferedReader(new FileReader(s));		String probs = probReader.readLine();		probs = probs.trim();		return makeProbArray(nr, 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[] getRandomDistrib(int nr)    {	double[] probs = new double[modelmaker.getAlphabetSize(nr)];	int nrEmissions = modelmaker.getAlphabetSize(nr);	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 double[] makeProbArray(int nr, String s)    {	StringTokenizer st = new StringTokenizer(s, " ");	double[] probs = new double[modelmaker.getAlphabetSize(nr)];	if(st.countTokens() != modelmaker.getAlphabetSize(nr)) {	    P.MESSAGE("Distribution has incorrect format for this alphabet");	    return null;	}	else {	    try {		double sum = 0;		for(int i = 0; i < modelmaker.getAlphabetSize(nr);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(nr);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 + -