public static byte[] hexStringToByteArray(String s) { int len = s.length(); byte[] data = new byte[len / 2]; for (int i = 0; i < len; i += 2) { data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16)); } return data; }
/** Loads custom hyphenation rules. You probably want to use loadDefault() instead. */ public void load(BufferedReader input) throws IOException { String line; while ((line = input.readLine()) != null) { if (StringUtils.matches(line, "\\s*(%.*)?")) { // comment or blank line System.err.println("Skipping: " + line); continue; } char[] linechars = line.toCharArray(); int[] pattern = new int[linechars.length]; char[] chars = new char[linechars.length]; int c = 0; for (int i = 0; i < linechars.length; ++i) { if (Character.isDigit(linechars[i])) { pattern[c] = Character.digit(linechars[i], 10); } else { chars[c++] = linechars[i]; } } char[] shortchars = new char[c]; int[] shortpattern = new int[c + 1]; System.arraycopy(chars, 0, shortchars, 0, c); System.arraycopy(pattern, 0, shortpattern, 0, c + 1); insertHyphPattern(shortchars, shortpattern); } }
private char uniEscape() throws java.io.IOException, jasError { int res = 0; for (int i = 0; i < 4; i++) { advance(); if (next_char == -1) return 0; int tmp = Character.digit((char) next_char, 16); if (tmp == -1) throw new jasError("Bad '\\u' escape sequence"); res = (res << 4) | tmp; } return (char) res; }
static int nextInt() throws IOException { int ch = -1; do { ch = System.in.read(); } while (!Character.isDigit(ch)); int ret = 0; while (Character.isDigit(ch)) { ret *= 10; ret += Character.digit(ch, 10); ch = System.in.read(); } return ret; }
public String nextToken() { if (putBack != null) { String s = putBack; putBack = null; return s; } int start = current; if (isDelim(current)) { /* This is whitespace */ while (current < string.length && isDelim(current)) current++; if (returnTokens) return new String(string, start, current - start); } boolean quoted = false; boolean escaped = false; StringBuffer sb = new StringBuffer(); while (true) { if (current == string.length) break; if (escaped) { if (Character.digit(string[current], 10) >= 0) { String s = new String(string, current, 3); int i = Integer.parseInt(s); sb.append((char) i); current += 2; } else sb.append(string[current]); escaped = false; } else if (quoted) { if (string[current] == '"') { current++; break; } else sb.append(string[current]); } else { if (string[current] == '"') quoted = true; else if (string[current] == '\\') escaped = true; else if (isDelim(current)) { break; } else sb.append(string[current]); } current++; } return sb.toString(); }
public static Vector<Double> trainNode( String filename, int outputRepresentation, String inputRepresentation, int filter, double learningRate, int epochs) { Vector<Double> node = null; // initialize node to null double error = 1; // initialize error double output = 0; // initialize output Vector<Double> trainError = new Vector<Double>(); double meansq = 0; String binTarget = ""; String binaryRep = ""; int target; openFile(filename); // open our file for training firstRead = true; int targetCounter = 0; while ((target = readFileIntoInputVector(inputRepresentation)) != -2) { // while we have not reached out end of file error if (outputRepresentation == 4) { binTarget = Integer.toBinaryString(target); // convert target to binary int[] targetArray = new int[4]; // array to hold binary ints for (int h = 0; h < targetArray.length; h++) { // add binary elements to int array try { targetArray[targetArray.length - h - 1] = Character.digit(binTarget.charAt(binTarget.length() - h - 1), 10); } catch (IndexOutOfBoundsException e) { // if there is empty spaces, fill with zeros targetArray[targetArray.length - h - 1] = 0; } } target = targetArray[filter]; binTarget = ""; for (int i = 0; i < targetArray.length; i++) { binTarget = binTarget + targetArray[i]; } } if ((filter == target && outputRepresentation == 10) || outputRepresentation != 10) { if (node == null) { node = new Vector<Double>(); for (int i = 0; i < inputs.size(); i++) { // for each input double randWeight = 1; // initialize random weight while (randWeight > 0.15) // while out random isn't less than 0.15 randWeight = random.nextDouble(); // get a new random double if (random.nextBoolean()) randWeight = randWeight * -1; // randomly set to negative node.addElement(new Double(randWeight)); // store random weight } } targetCounter++; // train for number of epochs for (int i = 0; i < epochs; i++) { // for each epoch double weightedSum = 0; // set/reset the weighted sum to 0 for (int j = 0; j < inputs.size(); j++) { weightedSum += (inputs.elementAt(j) * node.elementAt(j)); // calculate weighted sum } output = activation(weightedSum); // retrieve output if (outputRepresentation == 1 || outputRepresentation == 10) output = output * 10; else if (outputRepresentation == 4) { // each node must have a whole number if (output > 0.5) output = 1; else output = 0; } error = target - output; // calculate error if (i == epochs - 1) { if (outputRepresentation == 4) { if (filter == 0) { allOutputs.ensureCapacity(targetCounter); allOutputs.add(Integer.toString((int) output)); } else { allOutputs.set( targetCounter - 1, allOutputs.get(targetCounter - 1) + ((int) output)); } } trainError.add(error); if (Math.round(error) == 0 && outputRepresentation != 4) { trainCountCorrect++; } else if (outputRepresentation == 4 && filter == 3) { if (allOutputs.get(targetCounter - 1).equals(binTarget)) { trainCountCorrect++; } } } for (int k = 0; k < inputs.size(); k++) { // for each input double derivative = (1.64872 * Math.exp(weightedSum)) / (Math.pow(1.64872 + Math.exp(weightedSum), 2)); // calculate new weights double newWeight = node.elementAt(k) + (learningRate * inputs.elementAt(k) * error * derivative); // cont. node.setElementAt(newWeight, k); // update weights } } } inputs.clear(); } errorVect.addElement((Vector<Double>) trainError.clone()); trainError.clear(); if (outputRepresentation == 4 && filter == 3) System.out.println( "Training Percent: " + (100 * (double) trainCountCorrect / (double) targetCounter)); if (outputRepresentation == 10 && filter == 9) System.out.println( "Training Percent: " + (10 * (double) trainCountCorrect / (double) targetCounter)); if (outputRepresentation == 1) System.out.println( "Training Percent: " + (100 * (double) trainCountCorrect / (double) targetCounter)); closeFile(); return node; // node is now trained for an epoch }