Example #1
0
 /**
  * Read an optional signed integer. If there is no integer in the input stream, return 1. For
  * excessively large exponents, return Integer.MIN_VALUE or Integer.MAX_VALUE.
  */
 public int readOptionalExponent() throws java.io.IOException {
   int sign = read();
   boolean overflow = false;
   int c;
   if (sign == '+' || sign == '-') c = read();
   else {
     c = sign;
     sign = 0;
   }
   int value;
   if (c < 0 || (value = Character.digit((char) c, 10)) < 0) {
     if (sign != 0) error("exponent sign not followed by digit");
     value = 1;
   } else {
     int max = (Integer.MAX_VALUE - 9) / 10;
     for (; ; ) {
       c = read();
       int d = Character.digit((char) c, 10);
       if (d < 0) break;
       if (value > max) overflow = true;
       value = 10 * value + d;
     }
   }
   if (c >= 0) unread(c);
   if (sign == '-') value = -value;
   if (overflow) return sign == '-' ? Integer.MIN_VALUE : Integer.MAX_VALUE;
   return value;
 }
Example #2
0
 Term createIntegerOrLong(String x) throws ParseException {
   try {
     Term retval = null;
     if (x.endsWith("L") || x.endsWith("l")) {
       try {
         retval =
             TermWare.getInstance()
                 .getTermFactory()
                 .createLong(Long.decode(x.substring(0, x.length() - 1)));
       } catch (NumberFormatException ex) {
         // it can be just too big, becouse literals can be unsigned, while decode does not handle
         // unsogned,
         //  bigger then MAX
         // Here we will handle one case, which exists in JDK sources. (java/lang/Long.java)
         if (x.length() > 2) {
           char last = x.charAt(x.length() - 2);
           long l = Long.decode(x.substring(0, x.length() - 2));
           char x0 = x.charAt(0);
           char x1 = x.charAt(1);
           if (x0 == '0') {
             if (x1 == 'x' || x1 == 'X') {
               // hex
               int l1 = Character.digit(last, 16);
               l = ((l << 8) + l1);
             } else {
               // oct
               int l1 = Character.digit(last, 8);
               l = ((l << 4) + l1);
             }
           }
           retval = TermWare.getInstance().getTermFactory().createLong(l);
         } else {
           throw ex;
         }
       }
     } else {
       long l = Long.decode(x);
       retval = TermWare.getInstance().getTermFactory().createInt((int) l);
     }
     return retval;
   } catch (NumberFormatException ex) {
     throw new ParseException(
         "Can't read IntegerLiteral "
             + ex.getMessage()
             + "(s="
             + x
             + ") "
             + " in file "
             + inFname);
   }
 }
Example #3
0
 /** 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);
   }
 }
Example #4
0
 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;
 }
Example #5
0
  public static void debugger() {
    //Debug.println('m', "Debugger not implemented");

    int num;
    char inputChar;  // char read from input stream
    

    Interrupt.dumpState();
    dumpState();
    System.out.print(Nachos.stats.totalTicks + "> ");
    System.out.flush();

    // get user input, character at a time.

    try {
      inputChar = (char)(System.in.read());
    } catch (IOException _) {
      inputChar = 'x';
    }

    if (inputChar == 'c')
      singleStep = false;
    else if (inputChar == '?') {
      System.out.println("Machine commands:");
      System.out.println("    <return>  execute one instruction");
      System.out.println("    <number>  run until the given timer tick");
      System.out.println("    c         run until completion");
      System.out.println("    ?         print this help message");
    }
    else if (Character.isDigit(inputChar)) {
      int tempNum = 0;

      while (Character.isDigit(inputChar)) {
	tempNum *= 10;
	tempNum += Character.digit(inputChar, 10);
	try {
	  inputChar = (char)(System.in.read());
	} catch (IOException _) {
	  inputChar = 'x';
	  break;
	}
	runUntilTime = tempNum;
      }
    }
      
  }
Example #6
0
 /**
  * Read digits, up to the first non-digit or the buffer limit
  *
  * @param ival previously-seen digits or -2 if no digits seen
  * @return the digits seen as a non-negative long, or -1 on overflow, or -2 if no digits seen
  */
 public static long readDigitsInBuffer(InPort port, long ival, int radix) {
   int i = port.pos;
   if (i >= port.limit) return ival;
   for (; ; ) {
     char c = port.buffer[i];
     int dval = Character.digit(c, radix);
     if (dval < 0) break;
     if (ival == -2) // initial digits
     ival = dval;
     else if (ival == -1) ;
     else if (ival > (Long.MAX_VALUE - dval) / radix) ival = -1;
     else ival = ival * radix + dval;
     if (++i >= port.limit) break;
   }
   port.pos = i;
   return ival;
 }
Example #7
0
 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();
 }
Example #8
0
  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
  }
Example #9
0
  public void write(char ch) throws IOException {
    boolean ok;

    switch (state) {
      case S_text:
        if (ch == '\n' || ch == '\r') {
          break; // unadorned newlines are ignored
        } else if (ch == '{') {
          if (currentCharacters.length() > 0) {
            handleText(currentCharacters.toString());
            currentCharacters = new StringBuffer();
          }
          level++;
          begingroup();
        } else if (ch == '}') {
          if (currentCharacters.length() > 0) {
            handleText(currentCharacters.toString());
            currentCharacters = new StringBuffer();
          }
          if (level == 0) throw new IOException("Too many close-groups in RTF text");
          endgroup();
          level--;
        } else if (ch == '\\') {
          if (currentCharacters.length() > 0) {
            handleText(currentCharacters.toString());
            currentCharacters = new StringBuffer();
          }
          state = S_backslashed;
        } else {
          currentCharacters.append(ch);
        }
        break;
      case S_backslashed:
        if (ch == '\'') {
          state = S_aftertick;
          break;
        }
        if (!Character.isLetter(ch)) {
          char newstring[] = new char[1];
          newstring[0] = ch;
          if (!handleKeyword(new String(newstring))) {
            warning("Unknown keyword: " + newstring + " (" + (byte) ch + ")");
          }
          state = S_text;
          pendingKeyword = null;
          /* currentCharacters is already an empty stringBuffer */
          break;
        }

        state = S_token;
        /* FALL THROUGH */
      case S_token:
        if (Character.isLetter(ch)) {
          currentCharacters.append(ch);
        } else {
          pendingKeyword = currentCharacters.toString();
          currentCharacters = new StringBuffer();

          // Parameter following?
          if (Character.isDigit(ch) || (ch == '-')) {
            state = S_parameter;
            currentCharacters.append(ch);
          } else {
            ok = handleKeyword(pendingKeyword);
            if (!ok) warning("Unknown keyword: " + pendingKeyword);
            pendingKeyword = null;
            state = S_text;

            // Non-space delimiters get included in the text
            if (!Character.isWhitespace(ch)) write(ch);
          }
        }
        break;
      case S_parameter:
        if (Character.isDigit(ch)) {
          currentCharacters.append(ch);
        } else {
          /* TODO: Test correct behavior of \bin keyword */
          if (pendingKeyword.equals("bin")) {
              /* magic layer-breaking kwd */
            long parameter = Long.parseLong(currentCharacters.toString());
            pendingKeyword = null;
            state = S_inblob;
            binaryBytesLeft = parameter;
            if (binaryBytesLeft > Integer.MAX_VALUE)
              binaryBuf = new ByteArrayOutputStream(Integer.MAX_VALUE);
            else binaryBuf = new ByteArrayOutputStream((int) binaryBytesLeft);
            savedSpecials = specialsTable;
            specialsTable = allSpecialsTable;
            break;
          }

          int parameter = Integer.parseInt(currentCharacters.toString());
          ok = handleKeyword(pendingKeyword, parameter);
          if (!ok)
            warning("Unknown keyword: " + pendingKeyword + " (param " + currentCharacters + ")");
          pendingKeyword = null;
          currentCharacters = new StringBuffer();
          state = S_text;

          // Delimiters here are interpreted as text too
          if (!Character.isWhitespace(ch)) write(ch);
        }
        break;
      case S_aftertick:
        if (Character.digit(ch, 16) == -1) state = S_text;
        else {
          pendingCharacter = Character.digit(ch, 16);
          state = S_aftertickc;
        }
        break;
      case S_aftertickc:
        state = S_text;
        if (Character.digit(ch, 16) != -1) {
          pendingCharacter = pendingCharacter * 16 + Character.digit(ch, 16);
          ch = translationTable[pendingCharacter];
          if (ch != 0) handleText(ch);
        }
        break;
      case S_inblob:
        binaryBuf.write(ch);
        binaryBytesLeft--;
        if (binaryBytesLeft == 0) {
          state = S_text;
          specialsTable = savedSpecials;
          savedSpecials = null;
          handleBinaryBlob(binaryBuf.toByteArray());
          binaryBuf = null;
        }
    }
  }