Jannis (0.1preAlpha) | ||
Frames | No Frames |
1: /* PatternConverter.java - Copyright (c) 2005 by Stefan Thesing 2: <p>This file is part of Jannis.</p> 3: <p>Jannis is free software; you can redistribute it and/or modify 4: it under the terms of the GNU General Public License as published by 5: the Free Software Foundation; either version 2 of the License, or 6: (at your option) any later version.</p> 7: <p>Jannis is distributed in the hope that it will be useful, 8: but WITHOUT ANY WARRANTY; without even the implied warranty of 9: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10: GNU General Public License for more details.</p> 11: <p>You should have received a copy of the GNU General Public License 12: along with Jannis; if not, write to the<br> 13: Free Software Foundation, Inc.,<br> 14: 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA<br> 15: */ 16: package de.webdings.jannis.neuralnet; 17: 18: import de.webdings.jannis.exceptions.PatternCreateException; 19: 20: /** 21: * PatternConverter is used to convert 22: * {@link java.lang.String}s to arrays of {@link Pattern}s 23: * and vice versa. The character "0" corresponds to an 24: * pattern entry of <code>false</code>, "1" to <code>true 25: * </code>. 26: * 27: * @author Stefan Thesing<br> 28: * Website: <a href="http://www.webdings.de">http://www.webdings.de</a> 29: * @version 0.1 01.08.2005 30: */ 31: public class PatternConverter { 32: //methods 33: /** 34: * @param s The {@link java.lang.String} containing a 35: * representation of the pattern 36: * @param patternSize The size of the pattern (usually 37: * the number of neurons contained in the input or 38: * output layer) 39: * @return an array of {@link Pattern}s 40: * @throws PatternCreateException if the string contains characters 41: * other than <code>0</code> and <code>1</code> 42: */ 43: public static Pattern[] strToPattern(String s, int patternSize) throws PatternCreateException { 44: int numberOfPatterns = s.length()/patternSize; 45: Pattern[] pattern = new Pattern[numberOfPatterns]; 46: char[] ablage = s.toCharArray(); 47: int i,j; 48: int k=0; 49: for(i=0;i<numberOfPatterns;++i){ 50: pattern[i] = new Pattern(new boolean[patternSize]); 51: for(j=0;j<patternSize;++j) { 52: if((ablage[k]=='1')||(ablage[k]=='0')) { 53: pattern[i].entries[j] = (ablage[k]== '1'); 54: ++k; 55: } else { 56: pattern = null; 57: throw new PatternCreateException("Can't " + 58: "create pattern. Data contains " + 59: "characters other than 0 and 1"); 60: } 61: } 62: } 63: return pattern; 64: } 65: 66: /** 67: * @param pattern an array of {@link Pattern}s 68: * @param patternSize The size of the pattern (usually 69: * the number of neurons contained in the input or 70: * output layer) 71: * @return a {@link java.lang.String} representing 72: * the patterns. 73: */ 74: public static String patternToStr(Pattern[] pattern, int patternSize) { 75: String s = new String(); 76: int i,j; 77: for(i=0;i<pattern.length; ++i){ 78: for(j=0;j<patternSize;++j){ 79: if(pattern[i].entries[j]){ 80: s = s + "1"; 81: } else { 82: s = s + "0"; 83: } 84: } 85: } 86: return s; 87: } 88: }
Jannis (0.1preAlpha) |
© 2005 by Stefan Thesing;
Verbatim copying and redistribution of this entire page are permitted provided this notice is preserved.