Package lipstone.joshua.parser.util

Examples of lipstone.joshua.parser.util.ConsCell


    for (int i = 0; i < items.length; i++)
      items[i] = new BigDec(parser.run(list.get(i)).toString());
    NumberSet set = new NumberSet(items);
    try {
      if (operation.equals("mean"))
        return new ConsCell(set.getMean(), ConsType.NUMBER);
      if (operation.equals("median"))
        return new ConsCell(set.getMedian(), ConsType.NUMBER);
      if (operation.equals("mode"))
        return new ConsCell(set.getMode(), ConsType.NUMBER);
      if (operation.equals("stdev"))
        return new ConsCell(set.getSTDEV(1), ConsType.NUMBER);
      if (operation.equals("zScore"))
        return new ConsCell(set.getzScore(extra), ConsType.NUMBER);
      if (operation.equals("iqr"))
        return new ConsCell(set.getIQR(), ConsType.NUMBER);
    }
    catch (ParserException e) {
      e.setThrower(this);
      throw e;
    }
View Full Code Here


  }
 
  public ConsCell plusTax(ConsCell input) {
    if (input.length() < 3)
      return input;
    ConsCell current = input;
    int i = 0;
    do {
      if (current.getCar().equals("plus") && current.getNextConsCell().getCar().equals("tax")) {
        current.getNextConsCell().remove();
        ConsCell insert = input.clone();
        insert.getNextConsCell(i).removeAll();
        current.replaceCar(insert);
        current.append(new ConsCell('*', ConsType.OPERATOR, new ConsCell(tax, ConsType.NUMBER, current.getNextConsCell())));
      }
      i++;
    } while (!(current = current.getNextConsCell()).isNull() && !current.getNextConsCell().isNull()); //This and the one after it are not null
    return input;
  }
View Full Code Here

   * @throws ParserException
   * @throws NumberFormatException
   */
  public ConsCell findUnitConversion(ConsCell input) throws ParserException {
    ArrayList<String> oldVars = parser.getVars(); //Save the variables for later
    ConsCell current = input;
    do {
      if (current.getCarType() == ConsType.CONS_CELL)
        current.replaceCar(findUnitConversion((ConsCell) current.getCar()));
      if (current.getCarType() == ConsType.IDENTIFIER && ((String) current.getCar()).equals("in")) {
        ConsCell leftUnits = new ConsCell(), rightUnits = new ConsCell();
        ConsCell head = current.getPreviousConsCell();
        boolean power = true;
        do {
          head.singular().append(leftUnits);
          leftUnits = leftUnits.getPreviousConsCell();
          ConsCell previous = head.getPreviousConsCell();
          power = head.getCarType() != ConsType.NUMBER || (!previous.isNull() && (previous.getCarType() == ConsType.OPERATOR && ((char) previous.getCar() == '^' ||
              ((char) previous.getCar() == '+' || (char) previous.getCar() == '-' && previous.getPreviousConsCell().getCarType() == ConsType.OPERATOR &&
                  (char) previous.getPreviousConsCell().getCar() == '^'))));
        } while (power && !(head = head.remove().getPreviousConsCell()).isNull() &&
            (head.getCarType() == ConsType.OPERATOR || (head.getCarType() == ConsType.IDENTIFIER && units.getUnitNames().contains((String) head.getCar())) || head.getCarType() == ConsType.CONS_CELL));
        head = current.getNextConsCell();
        ConsCell temp = rightUnits;
        power = false;
        do {
          temp = temp.append(head);
          power = head.getCarType() == ConsType.OPERATOR && ((char) head.getCar() == '^' || (power && ((char) head.getCar() == '-' || (char) head.getCar() == '+')));
        } while (!(head = head.remove()).isNull() &&
            ((head.getCarType() == ConsType.OPERATOR && ((char) head.getCar() == '^' || (power && ((char) head.getCar() == '-' || (char) head.getCar() == '+')))) ||
                (head.getCarType() == ConsType.IDENTIFIER && units.getUnitNames().contains((String) head.getCar())) || (power && (head.getCarType() == ConsType.CONS_CELL ||
            head.getCarType() == ConsType.NUMBER))));
        //In theory, leftUnits and rightUnits are composed of units, the signs between them and powers
        current.replaceCar(new ConsCell('*', ConsType.OPERATOR));
        current.insert(new ConsCell(units.convertUnits(leftUnits, rightUnits), ConsType.NUMBER));
      }
    } while (!(current = current.getNextConsCell()).isNull());
    parser.setVars(oldVars); //Restore the variables
    return input;
  }
View Full Code Here

      naturalMath = new HashMap<ConsCell, ConsCell>();
      naturalNumbers = new HashMap<String, Integer>();
      abbreviations = new HashMap<String, String>();
      abbreviationPrefixes = new HashMap<String, String>();
      units = new Units(parser, this);
      naturalMath.put(new ConsCell("plus", ConsType.IDENTIFIER), new ConsCell('+', ConsType.OPERATOR));
      naturalMath.put(new ConsCell("minus", ConsType.IDENTIFIER), new ConsCell('-', ConsType.OPERATOR));
      naturalMath.put(new ConsCell("negative", ConsType.IDENTIFIER), new ConsCell('-', ConsType.OPERATOR));
      naturalMath.put(new ConsCell("times", ConsType.IDENTIFIER), new ConsCell('*', ConsType.OPERATOR));
      naturalMath.put(new ConsCell("divided", ConsType.IDENTIFIER, new ConsCell("by", ConsType.IDENTIFIER)), new ConsCell('/', ConsType.OPERATOR));
      naturalMath.put(new ConsCell("over", ConsType.IDENTIFIER), new ConsCell('/', ConsType.OPERATOR));
      naturalMath.put(new ConsCell("%", ConsType.OPERATOR, new ConsCell("of", ConsType.IDENTIFIER)), new ConsCell('/', ConsType.OPERATOR,
          new ConsCell(new BigDec(100), ConsType.NUMBER, new ConsCell('*', ConsType.OPERATOR))));
      naturalMath.put(new ConsCell("to", ConsType.IDENTIFIER, new ConsCell("the", ConsType.IDENTIFIER)), new ConsCell('^', ConsType.OPERATOR));
      naturalMath.put(new ConsCell("squared", ConsType.IDENTIFIER), new ConsCell('^', ConsType.OPERATOR, new ConsCell(new BigDec(2), ConsType.NUMBER)));
      naturalMath.put(new ConsCell("cubed", ConsType.IDENTIFIER), new ConsCell('^', ConsType.OPERATOR, new ConsCell(new BigDec(3), ConsType.NUMBER)));
      Object[] nNumbers = {"zero", 0, "one", 1, "two", 2, "three", 3, "four", 4, "five", 5, "six", 6, "seven", 7, "eight", 8, "nine", 9,
          "ten", 10, "eleven", 11, "twelve", 12, "thirteen", 13, "fourteen", 14, "fifteen", 15, "sixteen", 16, "seventeen", 17, "eightteen", 18, "nineteen", 19,
          "twenty", 20, "thirty", 30, "forty", 40, "fifty", 50, "sixty", 60, "seventy", 70, "eighty", 80, "ninety", 90};
      String[] kys = {"thousand", "million", "billion", "trillion", "quadrillion", "sextillion", "septillion", "octillion", "nonillion", "decillion", "undecillion",
          "duodecillion", "tredecillion", "quattuordecillion", "quindecillion", "sedecillion", "septendecillion", "octodecillion", "novemdecillion", "viginillion", "centillion"};
View Full Code Here

    }
  }
 
  @Override
  public ConsCell preProcess(ConsCell input) throws ParserException {
    ConsCell backup = input.clone();
    try {
      input = convertMaths(input);
      input = convertNumbers(input);
      //input = convertInlineVariables(input);
      //input = findUnitConversion(input);
View Full Code Here

        units.addUnit(arguments.get(0).toString(), unit, (BigDec) arguments.get(2).getCar(), abbreviations);
        //Add the remaining information to unit
        for (int i = 2; i < arguments.size(); i++)
          unit = unit + ", " + arguments.get(i);
        setData(arguments.get(0).toString(), Tokenizer.tokenizeString(unit));
        return new ConsCell("Successfully saved " + arguments.get(0) + " to the unit list.", ConsType.IDENTIFIER);
      }
    }
    if (command.equals("removeUnit") && arguments.size() >= 1) {
      String arg = arguments.get(0).toString();
      if (getData(arg) != null) {
        removeData(arg);
        units.removeUnit(arg);
        return new ConsCell("Successfully removed " + arg + " from the unit list.", ConsType.IDENTIFIER);
      }
      else
        return new ConsCell(arg + " cannot be removed.", ConsType.IDENTIFIER);
    }
    return null;
  }
View Full Code Here

  public BigDec convertUnits(ConsCell leftUnits, ConsCell rightUnits) throws ParserException {
    return getFactor(leftUnits).divide(getFactor(rightUnits));
  }
 
  private BigDec getFactor(ConsCell units) throws ParserException {
    ConsCell current = units;
    BigDec factor = BigDec.ONE;
    do {
      boolean divide = current.getPreviousConsCell().getCarType() == ConsType.OPERATOR && (char) current.getPreviousConsCell().getCar() == '/';
      BigDec partial;
      if (current.getCarType() == ConsType.IDENTIFIER) {
        String[] unit = stripPrefix((String) current.getCar());
        partial = this.units.get(unit[1]).getFactor();
        if (partial == null)
          throw new InvalidUnitException(current.getCar() + " is not a valid unit.", nl);
        if (unit[0].length() > 0)
          partial = partial.multiply(prefixes.get(unit[0]));
      }
      else if (current.getCarType() == ConsType.CONS_CELL) {
        ConsCell fact = parser.run(parser.preProcess((ConsCell) current.getCar()));
        if (fact.getCarType() != ConsType.NUMBER || fact.length() != 1)
          throw new InvalidUnitException(current.getCar() + " is not a valid unit set.", nl);
        partial = (BigDec) fact.getCar();
      }
      else
        continue;
      if (current.getNextConsCell().getCarType() == ConsType.OPERATOR && (char) current.getNextConsCell().getCar() == '^') {
        current.getNextConsCell().remove();
        ConsCell power = current.getNextConsCell().singular();
        if (power.getCarType() == ConsType.OPERATOR)
          power.append(current.getNextConsCell().remove().singular());
        else if (power.getCarType() == ConsType.CONS_CELL)
          power = (ConsCell) power.getCar();
        current.getNextConsCell().remove();
        power = parser.run(parser.preProcess(power));
        if (power.getCarType() != ConsType.NUMBER || power.length() != 1)
          throw new InvalidUnitException(current.getCar() + " is raised to an invalid power.", nl);
        partial = partial.pow((BigDec) power.getCar());
      }
      factor = divide ? factor.divide(partial) : factor.multiply(partial);
    } while (!(current = current.getNextConsCell()).isNull());
    return factor;
  }
View Full Code Here

    BigDec mean = getMean(), sigma = BigDec.ZERO;
    if (set.size() <= 1)
      return BigDec.ZERO;
    //Construct sigma
    for (BigDec d : set) {
      ConsCell temp = parser.run(new ConsCell(new ConsCell(d, ConsType.NUMBER, new ConsCell('-', ConsType.OPERATOR, new ConsCell(mean, ConsType.NUMBER), ConsType.CONS_CELL), ConsType.CONS_CELL), ConsType.CONS_CELL));
      sigma = sigma.add(new BigDec(temp.toString()));
    }
    //Run the root
    return new BigDec(parser.run(new ConsCell(new ConsCell(sigma, ConsType.NUMBER, new ConsCell('/', ConsType.OPERATOR, new ConsCell(new BigDec(set.size() - subtracter), ConsType.NUMBER), ConsType.CONS_CELL), ConsType.CONS_CELL), ConsType.CONS_CELL, new ConsCell('^', ConsType.OPERATOR, new ConsCell(new BigDec(0.5), ConsType.NUMBER), ConsType.CONS_CELL), ConsType.CONS_CELL)).toString());
  }
View Full Code Here

    for (BigDec d : set)
      if (d.eq(item))
        isInList = true;
    if (!isInList)
      set.add(item);
    ConsCell result = parser.run(new ConsCell(new ConsCell(item, ConsType.NUMBER, new ConsCell('-', ConsType.OPERATOR, new ConsCell(getMean(), ConsType.NUMBER), ConsType.CONS_CELL), ConsType.CONS_CELL),
        ConsType.CONS_CELL, new ConsCell('/', ConsType.OPERATOR, new ConsCell(getSTDEV(1), ConsType.NUMBER), ConsType.CONS_CELL), ConsType.CONS_CELL));
    set.remove(item);
    if (result.length() == 1 && result.getCarType() == ConsType.NUMBER)
      return (BigDec) result.getCar();
    else
      throw new ParserException("Unable to find a numeric result to getzScore.", null);
  }
View Full Code Here

    ArrayList<ConsCell> parts = input.splitOnSeparator();
    if (parts.size() < 3)
      return BigDec.ZERO;
    if (!parser.containsVariables(parts.get(0)))
      var = "[xyz]";
    ConsCell temp = parts.get(0);
    Pattern p = Pattern.compile("d[xyz" + vars.get(0) + "]");
    do {
      if (temp.getCarType() != ConsType.IDENTIFIER)
        continue;
      Matcher m = p.matcher((String) temp.getCar());
      if (m.find())
        temp = temp.remove();
      if (temp == null)
        break;
    } while (!(temp = temp.getNextConsCell()).isNull());
   
    BigDec limits[] = {BigDec.ZERO, BigDec.ZERO};
    temp = parser.run(parts.get(1));
    if (temp.getCarType() != ConsType.NUMBER)
      throw new InvalidOperationException("The second and third parameters for the integrate operation must both evaluate to a number", this, "integrate", parts);
    limits[0] = (BigDec) temp.getCar();
    temp = parser.run(parts.get(2));
    if (temp.getCarType() != ConsType.NUMBER)
      throw new InvalidOperationException("The second and third parameters for the integrate operation must both evaluate to a number", this, "integrate", parts);
    limits[1] = (BigDec) temp.getCar();
    if (limits[1] == limits[0])
      return BigDec.ZERO;
    if (limits[1].lt(limits[0])) {
      BigDec tempLim = limits[0];
      limits[0] = limits[1];
View Full Code Here

TOP

Related Classes of lipstone.joshua.parser.util.ConsCell

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.