public static void main(String[] args) {
    try {
      BufferedReader br = new BufferedReader(new FileReader(args[0]));
      int tIndex = 0;
      int pIndex = 0;

      // This will probably change soon (name and implementation)
      NgramParser tnp = new NgramParser(args[1]);

      ArrayList<String> triplet = tnp.getTriplet();
      ArrayList<String> polarity = tnp.getPolarity();

      FileWriter sw = new FileWriter(args[2]);
      String line = null;

      while (((line = br.readLine()) != null)
          && (tIndex < triplet.size())
          && (pIndex < polarity.size())) {
        if (line.matches("^[\\d]*:")) {
          // System.out.println(line);
          sw.write(line + "\n");
        } else {
          Scanner sc = new Scanner(line);
          String trip = sc.findInLine(Pattern.compile("[a-zA-Z]+#[a-z]+[#]?[0-9]*"));
          // if (trip != null && trip.equals(triplet.get(tIndex))) {
          System.out.println(trip);
          if (trip != null && !trip.toLowerCase().contains("no#cl")) {
            // System.out.println(triplet.get(tIndex) + ":" +polarity.get(pIndex));
            String pol = polarity.get(pIndex);
            sw.write(line + " " + pol + "\n");
            sc.close();
            tIndex++;
            pIndex++;
          } else {
            String pol = "neg";
            sw.write("no#a#1" + " " + pol + "\n");
            sc.close();
          }
        }

        // sw.flush();
      }

      sw.close();

    } catch (IOException e) {
      e.printStackTrace();
    }
  }
  /**
   * This function evaluates current Lisp expression in inputExpr It return result of the expression
   *
   * <p>The algorithm:
   *
   * <p>Step 1 Scan the tokens in the string. Step 2 If you see an operand, push operand object onto
   * the thisExprStack Step 3 If you see "(", next token should be an operator Step 4 If you see an
   * operator, push operator object onto the thisExprStack Step 5 If you see ")" // steps in
   * evaluateCurrentOperation() : Step 6 Pop operands and push them onto thisOpStack until you find
   * an operator Step 7 Apply the operator to the operands on thisOpStack Step 8 Push the result
   * into thisExprStack Step 9 If you run out of tokens, the value on the top of thisExprStack is is
   * the result of the expression.
   */
  public double evaluate() {
    Scanner inputExprScanner = new Scanner(inputExpr);

    // Use zero or more white space as delimiter,
    // which breaks the string into single character tokens
    inputExprScanner = inputExprScanner.useDelimiter("\\s*");

    while (inputExprScanner.hasNext()) {

      if (inputExprScanner.hasNextInt()) {
        String dataString = inputExprScanner.findInLine("\\d+");
        thisExprStack.push(new Double(dataString));
      } else {
        String aToken = inputExprScanner.next();
        char item = aToken.charAt(0);
        String nextToken;
        char nextItem;

        switch (item) {
          case '(':
            nextToken = inputExprScanner.next();
            nextItem = nextToken.charAt(0);
            if (nextItem == '+' || nextItem == '-' || nextItem == '*' || nextItem == '/') {
              thisExprStack.push(nextItem);
            }
            break;

          case ')':
            try {
              evaluateCurrentOperation();
            } catch (LispExprEvaluatorException e) {
              System.out.println("Error: " + e);
            } catch (ArithmeticException ae) {
              System.out.println("Error: " + ae);
            }
            break;

          default: // error
            throw new LispExprEvaluatorException(item + " is not a legal expression operator");
        } // end switch
      } // end else
    } // end while

    double result = (Double) thisExprStack.pop();

    return result;
  }
Exemple #3
0
  public static void main(String[] args) {
    int[] arr = new int[N];
    int[] dp = new int[Q];
    int a, b, c, q, k, m, n, i, j, max;
    boolean g;

    Scanner sc = new Scanner(System.in);
    while (true) {
      q = (int) (sc.nextDouble() * G);
      if (q >= Q) {
        while (true) ;
      }
      n = sc.nextInt();
      if (n == 0) {
        break;
      }
      k = 0;
      for (i = 0; i < n; ++i) {
        m = sc.nextInt();
        a = b = c = 0;
        g = true;
        for (j = 0; j < m; ++j) {
          switch (sc.findInLine("[A-Z]:").charAt(0)) {
            case 'A':
              a += (int) (sc.nextDouble() * G);
              if (a > S) {
                g = false;
              }
              break;
            case 'B':
              b += (int) (sc.nextDouble() * G);
              if (b > S) {
                g = false;
              }
              break;
            case 'C':
              c += (int) (sc.nextDouble() * G);
              if (c > S) {
                g = false;
              }
              break;
            default:
              sc.nextDouble();
              g = false;
              break;
          }
        }
        if (g && a + b + c <= T) {
          arr[k++] = a + b + c;
        }
      }
      for (i = 1; i <= q; ++i) {
        dp[i] = 0;
      }
      dp[0] = 1;
      max = 0;
      for (i = 0; i < k; ++i) {
        for (j = q; j >= arr[i]; --j) {
          if (dp[j - arr[i]] == 1) {
            dp[j] = 1;
            max = Math.max(max, j);
          }
        }
      }
      System.out.printf("%.2f%n", (double) max / G);
    }
  }
  public MedlineGenerator(String seed) {
    String[] fields = seed.split("\\r?\\n");

    for (String fieldData : fields) {
      if (fieldData.length() < 4) {
        continue;
      }
      final Scanner scanner = new Scanner(fieldData);
      scanner.useDelimiter("\\t");
      String fieldName = scanner.next();
      scanner.useDelimiter("; ");
      scanner.findInLine("\t");

      MedlineFieldDefinition defn = MedlineFieldDefinitions.getDefinition(fieldName);

      MedlineFieldDefinition.FieldType fieldType =
          defn != null ? defn.type : MedlineFieldDefinition.FieldType.SINGLE_TEXT_VALUE;

      BaseFieldModel fieldModel = null;

      Iterable<Pair<Long, String>> scannerIterator =
          new Iterable<Pair<Long, String>>() {
            @Override
            public Iterator<Pair<Long, String>> iterator() {
              return new Iterator<Pair<Long, String>>() {
                @Override
                public boolean hasNext() {
                  return scanner.hasNext();
                }

                @Override
                public Pair<Long, String> next() {
                  String value = "";
                  String[] data;
                  do {
                    String next = scanner.next();
                    data = next.split("\\t");
                    if (data.length > 2) {
                      throw new IllegalStateException(
                          String.format("Cannot parse word: '%s'", value + next));
                    }
                    value += data[0];
                  } while (data.length < 2);
                  return new Pair<>(Long.parseLong(data[1]), value);
                }

                @Override
                public void remove() {
                  throw new NotImplementedException();
                }
              };
            }
          };

      switch (fieldType) {
        case ARRAY_TEXT_VALUES:
        case SINGLE_TEXT_VALUE:
        case WORDS:
          SimpleFieldModel model = new SimpleFieldModel(fieldName, fieldType);
          for (Pair<Long, String> pair : scannerIterator) {
            model.addValue(pair.getKey(), pair.getValue());
          }
          fieldModel = model;
          break;
        case SINGLE_OBJECT_VALUE:
          ObjectFieldModel objectModel = new ObjectFieldModel(fieldName);
          for (Pair<Long, String> pair : scannerIterator) {
            long weight = pair.getKey();
            String propertyData = pair.getValue();
            int firstColonIndex = propertyData.indexOf(':');
            String propertyName = propertyData.substring(0, firstColonIndex);
            String propertyValue = propertyData.substring(firstColonIndex);
            objectModel.addValue(propertyName, weight, propertyValue);
          }
          fieldModel = objectModel;
          break;
      }

      fieldModels.put(fieldName, fieldModel);
    }
  }