Ejemplo n.º 1
0
  public boolean add(StateSet set) {
    
    if (DEBUG) Out.dump("StateSet.add("+set+") start");

    if (set == null) return false;

    if (bits.length < set.bits.length) {
      long newbits[] = new long[set.bits.length];
      System.arraycopy(bits, 0, newbits, 0, bits.length);
      
      bits = newbits;
    }
    
    boolean changed = false;

    for (int i = 0; i < set.bits.length; i++) {
      long n = bits[i] | set.bits[i];
      if ( n != bits[i] ) changed = true;
      bits[i] = n;
    }
    
    if (DEBUG) {
      Out.dump("StateSet.add("+set+") end");
      Out.dump("Set is : "+this);
    }

    return changed;
  }
Ejemplo n.º 2
0
 @Test
 public void OnlyCoffee() {
   Out.print("only coffee");
   CentralPerk house = new HouseBlend();
   Out.print(house.cost());
   // assertThat(actual, matcher)
   assertEquals(28.0, house.cost(), DELTA);
   CentralPerk espresso = new Espresso();
   assertEquals(32.0, espresso.cost(), DELTA);
 }
 /** Test Client */
 public static void main(String[] args) {
   Integer N = new Integer(args[0]);
   Integer T = new Integer(args[1]);
   Out out = new Out();
   PercolationStats stats = new PercolationStats(N, T);
   out.println("mean                    = " + stats.mean());
   out.println("stddev                  = " + stats.stddev());
   out.println("95% confidence interval = " + stats.confidenceLo() + ", " + stats.confidenceHi());
   out.close();
 }
  /**
   * Initialise the ANNIE system. This creates a "corpus pipeline" application that can be used to
   * run sets of documents through the extraction system.
   */
  public void initAnnie() throws GateException, IOException {
    Out.prln("Initialising ANNIE...");

    // load the ANNIE application from the saved state in plugins/ANNIE
    File pluginsHome = Gate.getPluginsHome();
    File anniePlugin = new File(pluginsHome, "ANNIE");
    File annieGapp = new File(anniePlugin, "ANNIE_with_defaults.gapp");
    annieController = (CorpusController) PersistenceManager.loadObjectFromFile(annieGapp);

    Out.prln("...ANNIE loaded");
  } // initAnnie()
Ejemplo n.º 5
0
 public void addState(int state) {
   if (DEBUG) {
     Out.dump("StateSet.addState("+state+") start");
     Out.dump("Set is : "+this);
   }
   
   resize(state);
   bits[state >> BITS] |= (1L << (state & MASK));
   
   if (DEBUG) {
     Out.dump("StateSet.addState("+state+") end");
     Out.dump("Set is : "+this);
   }
 }
Ejemplo n.º 6
0
  /**
   * Prints one Unicode property value per line, along with its aliases, if any, for the given
   * unicodeVersion.
   *
   * @param unicodeVersion The Unicode version to print property values and aliases for
   * @throws UnicodeProperties.UnsupportedUnicodeVersionException if unicodeVersion is not supported
   */
  private static void printUnicodePropertyValuesAndAliases(String unicodeVersion)
      throws UnicodeProperties.UnsupportedUnicodeVersionException {
    Pattern versionPattern = Pattern.compile("(\\d+)(?:\\.(\\d+))?(?:\\.\\d+)?");
    Matcher matcher = versionPattern.matcher(unicodeVersion);
    if (!matcher.matches()) {
      throw new UnicodeProperties.UnsupportedUnicodeVersionException();
    }
    String underscoreVersion =
        matcher.group(1) + (null == matcher.group(2) ? "_0" : "_" + matcher.group(2));

    String[] propertyValues;
    String[] propertyValueAliases;
    try {
      Class<?> clazz = Class.forName("jflex.unicode.data.Unicode_" + underscoreVersion);
      Field field = clazz.getField("propertyValues");
      propertyValues = (String[]) field.get(null);
      field = clazz.getField("propertyValueAliases");
      propertyValueAliases = (String[]) field.get(null);
    } catch (Exception e) {
      throw new UnicodeProperties.UnsupportedUnicodeVersionException();
    }
    SortedMap<String, SortedSet<String>> propertyValuesToAliases =
        new TreeMap<String, SortedSet<String>>();
    for (String value : propertyValues) {
      propertyValuesToAliases.put(value, new TreeSet<String>());
    }
    for (int i = 0; i < propertyValueAliases.length; i += 2) {
      String alias = propertyValueAliases[i];
      String value = propertyValueAliases[i + 1];
      SortedSet<String> aliases = propertyValuesToAliases.get(value);
      if (null == aliases) {
        aliases = new TreeSet<String>();
        propertyValuesToAliases.put(value, aliases);
      }
      aliases.add(alias);
    }
    for (Map.Entry<String, SortedSet<String>> entry : propertyValuesToAliases.entrySet()) {
      String value = entry.getKey();
      SortedSet<String> aliases = entry.getValue();
      Out.print(value);
      if (aliases.size() > 0) {
        for (String alias : aliases) {
          Out.print(", " + alias);
        }
      }
      Out.println("");
    }
  }
 private static void saveWords(String fileName, String[] words) {
   int MAX_LENGTH = 70;
   Out out = new Out(fileName);
   int length = 0;
   for (String word : words) {
     length += word.length();
     if (length > MAX_LENGTH) {
       out.println();
       length = word.length();
     }
     out.print(word);
     out.print(" ");
     length++;
   }
   out.close();
 }
Ejemplo n.º 8
0
  /**
   * @throws ClassCastException if b is not a StateSet
   * @throws NullPointerException if b is null
   */
  public boolean equals(Object b) {

    int i = 0;
    int l1,l2;
    StateSet set = (StateSet) b;

    if (DEBUG) Out.dump("StateSet.equals("+set+"), this="+this);

    l1 = bits.length;
    l2 = set.bits.length;

    if (l1 <= l2) {      
      while (i < l1) {
        if (bits[i] != set.bits[i]) return false;
        i++;
      }
      
      while (i < l2) 
        if (set.bits[i++] != 0) return false;
    }
    else {
      while (i < l2) {
        if (bits[i] != set.bits[i]) return false;
        i++;
      }
      
      while (i < l1) 
        if (bits[i++] != 0) return false;
    }

    return true;
  }
Ejemplo n.º 9
0
 public int nextElement() {
   if (DEBUG)
     Out.dump(
         "nextElement, index = " + index + ", offset = " + offset); // $NON-NLS-1$ //$NON-NLS-2$
   int x = (index << StateSet.BITS) + offset;
   advance();
   return x;
 }
Ejemplo n.º 10
0
 public boolean hasMoreElements() {
   if (DEBUG)
     Out.dump(
         "hasMoreElements, index = "
             + index
             + ", offset = "
             + offset); //$NON-NLS-1$ //$NON-NLS-2$
   return index < bits.length;
 }
Ejemplo n.º 11
0
 @Test
 public void withMilk() {
   Out.print("only coffee");
   CentralPerk house = new HouseBlend();
   house = new Milk(house);
   house = new Milk(house);
   house = new Mocha(house);
   assertEquals(28 + 5 + 5 + 7, house.cost(), DELTA);
 }
Ejemplo n.º 12
0
 @Test
 public void testConstructor() {
   byte[] rawTx =
       Utils.hexStringToByteArray(
           "0100000001bdc0141fe3e5c2223a6d26a95acbf791042d93f9d9b8b38f133bf7adb5c1e293010000006a47304402202214770c0f5a9261190337273219a108132a4bc987c745db8dd6daded34b0dcb0220573de1d973166024b8342d6b6fef2a864a06cceee6aee13a910e5d8df465ed2a01210382b259804ad8d88b96a23222e24dd5a130d39588e78960c9e9b48a5b49943649ffffffff02a0860100000000001976a91479a7bf0bba8359561d4dab457042d7b632d5e64188ac605b0300000000001976a914b036c529faeca8040232cc4bd5918e709e90c4ff88ac00000000");
   Tx tx = new Tx(rawTx);
   byte[] txBytes = tx.bitcoinSerialize();
   assertTrue(Arrays.equals(rawTx, txBytes));
   byte[] exceptTxHash =
       Utils.reverseBytes(
           Utils.hexStringToByteArray(
               "584985ca8a9ed57987da36ea3d13fe05a7c498f2098ddeb6c8d0f3214067640c"));
   byte[] txHash = tx.getTxHash();
   for (Out out : tx.getOuts()) {
     String outAddress = out.getOutAddress();
   }
   assertTrue(Arrays.equals(exceptTxHash, txHash));
 }
Ejemplo n.º 13
0
 /**
  * Starts the generation process with the files in <code>argv</code> or pops up a window to choose
  * a file, when <code>argv</code> doesn't have any file entries.
  *
  * @param argv the commandline.
  */
 public static void main(String argv[]) {
   try {
     generate(argv);
   } catch (GeneratorException e) {
     Out.statistics();
     System.exit(1);
   } catch (SilentExit e) {
     System.exit(1);
   }
 }
Ejemplo n.º 14
0
 public void close() {
   System.err.println("Closing connection with client");
   out.close();
   in.close();
   try {
     server.close();
   } catch (IOException ioe) {
     System.out.println("IOException on socket listen: " + ioe);
     ioe.printStackTrace();
   }
 }
Ejemplo n.º 15
0
  /**
   * Stores a new macro and its definition.
   *
   * @param name the name of the new macro
   * @param definition the definition of the new macro
   * @return <code>true</code>, iff the macro name has not been stored before.
   */
  public boolean insert(String name, RegExp definition) {

    if (Options.DEBUG)
      Out.debug(
          "inserting macro "
              + name
              + " with definition :"
              + Out.NL
              + definition); //$NON-NLS-1$ //$NON-NLS-2$

    used.put(name, Boolean.FALSE);
    return macros.put(name, definition) == null;
  }
  private void increaseSize(int length) {
    length = Math.max(length + 1, 4 * p.length);
    Out.debug("increasing length to " + length); // $NON-NLS-1$

    int pn[] = new int[length];
    int qn[] = new int[length];

    System.arraycopy(p, 0, pn, 0, p.length);
    System.arraycopy(q, 0, qn, 0, q.length);

    p = pn;
    q = qn;
  }
Ejemplo n.º 17
0
  private void advance() {

    if (DEBUG)
      Out.dump(
          "Advancing, at start, index = "
              + index
              + ", offset = "
              + offset); //$NON-NLS-1$ //$NON-NLS-2$

    // cache fields in local variable for faster access
    int _index = this.index;
    int _offset = this.offset;
    long _mask = this.mask;
    long[] _bits = this.bits;

    long bi = _bits[_index];

    do {
      _offset++;
      _mask <<= 1;
    } while (_offset <= StateSet.MASK && ((bi & _mask) == 0));

    if (_offset > StateSet.MASK) {
      int length = _bits.length;

      do _index++;
      while (_index < length && _bits[_index] == 0);

      if (_index >= length) {
        this.index = length; // indicates "no more elements"
        return;
      }

      _offset = 0;
      _mask = 1;
      bi = _bits[_index];

      // terminates, because bi != 0
      while ((bi & _mask) == 0) {
        _mask <<= 1;
        _offset++;
      }
    }

    // write back cached values
    this.index = _index;
    this.mask = _mask;
    this.offset = _offset;
  }
Ejemplo n.º 18
0
  public boolean containsSet(StateSet set) {

    if (DEBUG)
      Out.dump("StateSet.containsSet("+set+"), this="+this);

    int i;
    int min = Math.min(bits.length, set.bits.length);
    
    for (i = 0; i < min; i++) 
      if ( (bits[i] & set.bits[i]) != set.bits[i] ) return false;
    
    for (i = min; i < set.bits.length; i++)
      if ( set.bits[i] != 0 ) return false;
    
    return true;
  }
  // write the concordance to a file
  private static void serialize(String fileName, ST<String, SET<Integer>> concordance) {
    Out out = new Out(fileName);
    out.println(concordance.size());
    for (String s : concordance.keys()) {
      SET<Integer> set = concordance.get(s);
      if (set == null) set = new SET<Integer>();

      out.println(s);
      out.println(set.size());
      for (int k : set) out.println(k);
    }
    out.close();
  }
Ejemplo n.º 20
0
 /**
  * Returns the set of elements that contained are in the specified set
  * but are not contained in this set.
  */
 public StateSet complement(StateSet set) {
   
   if (set == null) return null;
   
   StateSet result = new StateSet();
   
   result.bits = new long[set.bits.length];
   
   int i;
   int m = Math.min(bits.length, set.bits.length);
   
   for (i = 0; i < m; i++) {
     result.bits[i] = ~bits[i] & set.bits[i];
   }
   
   if (bits.length < set.bits.length) 
     System.arraycopy(set.bits, m, result.bits, m, result.bits.length-m);
   
   if (DEBUG) 
     Out.dump("Complement of "+this+Out.NL+"and "+set+Out.NL+" is :"+result);
   
   return result;
 }
Ejemplo n.º 21
0
  @Test
  public void testDb() {
    Tx tx = new Tx();
    byte[] txHash =
        Utils.reverseBytes(
            Utils.hexStringToByteArray(
                "f8a8335594d4c883f367e003cb3832015640f24714b48bd21cf6fbe84a617dfe"));
    tx.setTxHash(
        Utils.reverseBytes(
            Utils.hexStringToByteArray(
                "f8a8335594d4c883f367e003cb3832015640f24714b48bd21cf6fbe84a617dfe")));
    tx.setBlockNo(304942);
    tx.setTxTime((int) new Date().getTime() / 1000);
    tx.setTxVer(1);
    In inPut = new In();
    inPut.setPrevTxHash(
        Utils.reverseBytes(
            Utils.hexStringToByteArray(
                "d7f4efff7aeaffc1630dd3653e923a233fd463f9dc7dd4f97bb5cbf0cf99e56a")));
    inPut.setInSn(0);
    inPut.setTxHash(txHash);
    inPut.setInSequence(1);
    inPut.setInSignature(txHash);

    tx.addInput(inPut);
    Out out = new Out();
    out.setTxHash(txHash);
    out.setOutSn(0);
    out.setOutValue(3400);
    out.setOutScript(
        Utils.hexStringToByteArray("76a914abceaddc7d791f749671c17dfa36e9b17a4b055588ac"));
    out.setOutStatus(Out.OutStatus.spent);
    out.setOutAddress("test");
    tx.addOutput(out);
    AbstractDb.txProvider.add(tx);
    Tx testTx = AbstractDb.txProvider.getTxDetailByTxHash(txHash);
    assertEquals(
        Utils.bytesToHexString(tx.getTxHash()), Utils.bytesToHexString(testTx.getTxHash()));
  }
Ejemplo n.º 22
0
  /**
   * Generates a scanner for the specified input file.
   *
   * @param inputFile a file containing a lexical specification to generate a scanner for.
   */
  public static void generate(File inputFile) {

    Out.resetCounters();

    Timer totalTime = new Timer();
    Timer time = new Timer();

    LexScan scanner = null;
    LexParse parser = null;
    FileReader inputReader = null;

    totalTime.start();

    try {
      Out.println(ErrorMessages.READING, inputFile.toString());
      inputReader = new FileReader(inputFile);
      scanner = new LexScan(inputReader);
      scanner.setFile(inputFile);
      parser = new LexParse(scanner);
    } catch (FileNotFoundException e) {
      Out.error(ErrorMessages.CANNOT_OPEN, inputFile.toString());
      throw new GeneratorException();
    }

    try {
      NFA nfa = (NFA) parser.parse().value;

      Out.checkErrors();

      if (Options.dump) Out.dump(ErrorMessages.get(ErrorMessages.NFA_IS) + Out.NL + nfa + Out.NL);

      if (Options.dot) nfa.writeDot(Emitter.normalize("nfa.dot", null)); // $NON-NLS-1$

      Out.println(ErrorMessages.NFA_STATES, nfa.numStates);

      time.start();
      DFA dfa = nfa.getDFA();
      time.stop();
      Out.time(ErrorMessages.DFA_TOOK, time);

      dfa.checkActions(scanner, parser);

      nfa = null;

      if (Options.dump) Out.dump(ErrorMessages.get(ErrorMessages.DFA_IS) + Out.NL + dfa + Out.NL);

      if (Options.dot) dfa.writeDot(Emitter.normalize("dfa-big.dot", null)); // $NON-NLS-1$

      Out.checkErrors();

      time.start();
      dfa.minimize();
      time.stop();

      Out.time(ErrorMessages.MIN_TOOK, time);

      if (Options.dump) Out.dump(ErrorMessages.get(ErrorMessages.MIN_DFA_IS) + Out.NL + dfa);

      if (Options.dot) dfa.writeDot(Emitter.normalize("dfa-min.dot", null)); // $NON-NLS-1$

      time.start();

      Emitter e = new Emitter(inputFile, parser, dfa);
      e.emit();

      time.stop();

      Out.time(ErrorMessages.WRITE_TOOK, time);

      totalTime.stop();

      Out.time(ErrorMessages.TOTAL_TIME, totalTime);
    } catch (ScannerException e) {
      Out.error(e.file, e.message, e.line, e.column);
      throw new GeneratorException();
    } catch (MacroException e) {
      Out.error(e.getMessage());
      throw new GeneratorException();
    } catch (IOException e) {
      Out.error(ErrorMessages.IO_ERROR, e.toString());
      throw new GeneratorException();
    } catch (OutOfMemoryError e) {
      Out.error(ErrorMessages.OUT_OF_MEMORY);
      throw new GeneratorException();
    } catch (GeneratorException e) {
      throw new GeneratorException();
    } catch (Exception e) {
      e.printStackTrace();
      throw new GeneratorException();
    }
  }
Ejemplo n.º 23
0
 public static void printUsage() {
   Out.println(""); // $NON-NLS-1$
   Out.println("Usage: jflex <options> <input-files>");
   Out.println("");
   Out.println("Where <options> can be one or more of");
   Out.println("-d <directory>    write generated file to <directory>");
   Out.println("--skel <file>     use external skeleton <file>");
   Out.println("--switch             (DEPRECATED - will be removed in JFlex 1.6)");
   Out.println("--table              (DEPRECATED - will be removed in JFlex 1.6)");
   Out.println("--pack            set default code generation method (default)");
   Out.println("--jlex            strict JLex compatibility");
   Out.println("--legacydot       dot (.) metachar matches [^\\n] instead of");
   Out.println("                  [^\\n\\r\\u000B\\u000C\\u0085\\u2028\\u2029]");
   Out.println("--inputstreamctor    include a scanner constructor taking InputStream (default)");
   Out.println("--noinputstreamctor  don't include a scanner constructor taking InputStream");
   Out.println("--nomin           skip minimization step");
   Out.println("--nobak           don't create backup files");
   Out.println("--dump            display transition tables");
   Out.println("--dot             write graphviz .dot files for the generated automata (alpha)");
   Out.println("--verbose");
   Out.println("-v                display generation progress messages (default)");
   Out.println("--quiet");
   Out.println("-q                display errors only");
   Out.println("--time            display generation time statistics");
   Out.println("--version         print the version number of this copy of jflex");
   Out.println("--info            print system + JDK information");
   Out.println("--uniprops <ver>  print all supported properties for Unicode version <ver>");
   Out.println("--help");
   Out.println("-h                print this message");
   Out.println("");
   Out.println(ErrorMessages.THIS_IS_JFLEX, version);
   Out.println("Have a nice day!");
 }
Ejemplo n.º 24
0
  @Override
  public void harvest() {

    Out.print("Pear is harvested");
  }
Ejemplo n.º 25
0
  public static List<File> parseOptions(String argv[]) throws SilentExit {
    List<File> files = new ArrayList<File>();

    for (int i = 0; i < argv.length; i++) {

      if (argv[i].equals("-d") || argv[i].equals("--outdir")) { // $NON-NLS-1$ //$NON-NLS-2$
        if (++i >= argv.length) {
          Out.error(ErrorMessages.NO_DIRECTORY);
          throw new GeneratorException();
        }
        Options.setDir(argv[i]);
        continue;
      }

      if (argv[i].equals("--skel") || argv[i].equals("-skel")) { // $NON-NLS-1$ //$NON-NLS-2$
        if (++i >= argv.length) {
          Out.error(ErrorMessages.NO_SKEL_FILE);
          throw new GeneratorException();
        }

        Options.setSkeleton(new File(argv[i]));
        continue;
      }

      if (argv[i].equals("-jlex") || argv[i].equals("--jlex")) { // $NON-NLS-1$ //$NON-NLS-2$
        Options.jlex = true;
        continue;
      }

      if (argv[i].equals("-v")
          || argv[i].equals("--verbose")
          || argv[i].equals("-verbose")) { // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        Options.verbose = true;
        Options.progress = true;
        continue;
      }

      if (argv[i].equals("-q")
          || argv[i].equals("--quiet")
          || argv[i].equals("-quiet")) { // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        Options.verbose = false;
        Options.progress = false;
        continue;
      }

      if (argv[i].equals("--dump") || argv[i].equals("-dump")) { // $NON-NLS-1$ //$NON-NLS-2$
        Options.dump = true;
        continue;
      }

      if (argv[i].equals("--time") || argv[i].equals("-time")) { // $NON-NLS-1$ //$NON-NLS-2$
        Options.time = true;
        continue;
      }

      if (argv[i].equals("--version") || argv[i].equals("-version")) { // $NON-NLS-1$ //$NON-NLS-2$
        Out.println(ErrorMessages.THIS_IS_JFLEX, version);
        throw new SilentExit();
      }

      if (argv[i].equals("--dot") || argv[i].equals("-dot")) { // $NON-NLS-1$ //$NON-NLS-2$
        Options.dot = true;
        continue;
      }

      if (argv[i].equals("--help")
          || argv[i].equals("-h")
          || argv[i].equals("/h")) { // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        printUsage();
        throw new SilentExit();
      }

      if (argv[i].equals("--info") || argv[i].equals("-info")) { // $NON-NLS-1$ //$NON-NLS-2$
        Out.printSystemInfo();
        throw new SilentExit();
      }

      if (argv[i].equals("--nomin") || argv[i].equals("-nomin")) { // $NON-NLS-1$ //$NON-NLS-2$
        Options.no_minimize = true;
        continue;
      }

      if (argv[i].equals("--pack") || argv[i].equals("-pack")) { // $NON-NLS-1$ //$NON-NLS-2$
        Options.gen_method = Options.PACK;
        continue;
      }

      if (argv[i].equals("--table") || argv[i].equals("-table")) { // $NON-NLS-1$ //$NON-NLS-2$
        Options.gen_method = Options.TABLE;
        continue;
      }

      if (argv[i].equals("--switch") || argv[i].equals("-switch")) { // $NON-NLS-1$ //$NON-NLS-2$
        Options.gen_method = Options.SWITCH;
        continue;
      }

      if (argv[i].equals("--nobak") || argv[i].equals("-nobak")) { // $NON-NLS-1$ //$NON-NLS-2$
        Options.no_backup = true;
        continue;
      }

      if (argv[i].equals("--legacydot")
          || argv[i].equals("-legacydot")) { // $NON-NLS-1$ //$NON-NLS-2$
        Options.legacy_dot = true;
        continue;
      }

      // TODO: In the JFlex version after 1.6, --inputstreamctor will be removed.
      if (argv[i].equals("--inputstreamctor")
          || argv[i].equals("-inputstreamctor")) { // $NON-NLS-1$ //$NON-NLS-2$
        Options.emitInputStreamCtor = true;
        continue;
      }

      // TODO: In the JFlex version after 1.6, --noinputstreamctor will be removed.
      if (argv[i].equals("--noinputstreamctor")
          || argv[i].equals("-noinputstreamctor")) { // $NON-NLS-1$ //$NON-NLS-2$
        Options.emitInputStreamCtor = false;
        continue;
      }

      if (argv[i].equals("--uniprops")
          || argv[i].equals("-uniprops")) { // $NON-NLS-1$ //$NON-NLS-2$
        if (++i >= argv.length) {
          Out.error(
              ErrorMessages.PROPS_ARG_REQUIRES_UNICODE_VERSION, UnicodeProperties.UNICODE_VERSIONS);
          throw new GeneratorException();
        }
        String unicodeVersion = argv[i];
        try {
          printUnicodePropertyValuesAndAliases(unicodeVersion);
        } catch (UnicodeProperties.UnsupportedUnicodeVersionException e) {
          Out.error(
              ErrorMessages.UNSUPPORTED_UNICODE_VERSION_SUPPORTED_ARE,
              UnicodeProperties.UNICODE_VERSIONS);
          throw new GeneratorException();
        }
        throw new SilentExit();
      }

      if (argv[i].startsWith("-")) { // $NON-NLS-1$
        Out.error(ErrorMessages.UNKNOWN_COMMANDLINE, argv[i]);
        printUsage();
        throw new SilentExit();
      }

      // if argv[i] is not an option, try to read it as file
      File f = new File(argv[i]);
      if (f.isFile() && f.canRead()) files.add(f);
      else {
        Out.error("Sorry, couldn't open \"" + f + "\""); // $NON-NLS-2$
        throw new GeneratorException();
      }
    }

    return files;
  }
Ejemplo n.º 26
0
 @Override
 public void plant() {
   Out.print("Pear is plantling");
 }
Ejemplo n.º 27
0
 /**
  * Formats JSON Object and sends it through the connection
  *
  * @param json Instance of net.sf.json.JSONObject;
  */
 public void send(JSONObject json) {
   System.err.println("Sending Message: " + json.toString());
   out.println(json.toString());
 }
  /**
   * Run from the command-line, with a list of URLs as argument.
   *
   * <p><B>NOTE:</B><br>
   * This code will run with all the documents in memory - if you want to unload each from memory
   * after use, add code to store the corpus in a DataStore.
   */
  public static void main(String args[]) throws GateException, IOException {
    // initialise the GATE library
    Out.prln("Initialising GATE...");
    Gate.init();
    Out.prln("...GATE initialised");

    // initialise ANNIE (this may take several minutes)
    StandAloneAnnie annie = new StandAloneAnnie();
    annie.initAnnie();

    // create a GATE corpus and add a document for each command-line
    // argument
    Corpus corpus = Factory.newCorpus("StandAloneAnnie corpus");
    for (int i = 0; i < args.length; i++) {
      URL u = new URL(args[i]);
      FeatureMap params = Factory.newFeatureMap();
      params.put("sourceUrl", u);
      params.put("preserveOriginalContent", new Boolean(true));
      params.put("collectRepositioningInfo", new Boolean(true));
      Out.prln("Creating doc for " + u);
      Document doc = (Document) Factory.createResource("gate.corpora.DocumentImpl", params);
      corpus.add(doc);
    } // for each of args

    // tell the pipeline about the corpus and run it
    annie.setCorpus(corpus);
    annie.execute();

    // for each document, get an XML document with the
    // person and location names added
    Iterator iter = corpus.iterator();
    int count = 0;
    String startTagPart_1 = "<span GateID=\"";
    String startTagPart_2 = "\" title=\"";
    String startTagPart_3 = "\" style=\"background:Red;\">";
    String endTag = "</span>";

    while (iter.hasNext()) {
      Document doc = (Document) iter.next();
      AnnotationSet defaultAnnotSet = doc.getAnnotations();
      Set annotTypesRequired = new HashSet();
      annotTypesRequired.add("Person");
      annotTypesRequired.add("Location");
      Set<Annotation> peopleAndPlaces =
          new HashSet<Annotation>(defaultAnnotSet.get(annotTypesRequired));

      FeatureMap features = doc.getFeatures();
      String originalContent =
          (String) features.get(GateConstants.ORIGINAL_DOCUMENT_CONTENT_FEATURE_NAME);
      RepositioningInfo info =
          (RepositioningInfo) features.get(GateConstants.DOCUMENT_REPOSITIONING_INFO_FEATURE_NAME);

      ++count;
      File file = new File("StANNIE_" + count + ".HTML");
      Out.prln("File name: '" + file.getAbsolutePath() + "'");
      if (originalContent != null && info != null) {
        Out.prln("OrigContent and reposInfo existing. Generate file...");

        Iterator it = peopleAndPlaces.iterator();
        Annotation currAnnot;
        SortedAnnotationList sortedAnnotations = new SortedAnnotationList();

        while (it.hasNext()) {
          currAnnot = (Annotation) it.next();
          sortedAnnotations.addSortedExclusive(currAnnot);
        } // while

        StringBuffer editableContent = new StringBuffer(originalContent);
        long insertPositionEnd;
        long insertPositionStart;
        // insert anotation tags backward
        Out.prln("Unsorted annotations count: " + peopleAndPlaces.size());
        Out.prln("Sorted annotations count: " + sortedAnnotations.size());
        for (int i = sortedAnnotations.size() - 1; i >= 0; --i) {
          currAnnot = (Annotation) sortedAnnotations.get(i);
          insertPositionStart = currAnnot.getStartNode().getOffset().longValue();
          insertPositionStart = info.getOriginalPos(insertPositionStart);
          insertPositionEnd = currAnnot.getEndNode().getOffset().longValue();
          insertPositionEnd = info.getOriginalPos(insertPositionEnd, true);
          if (insertPositionEnd != -1 && insertPositionStart != -1) {
            editableContent.insert((int) insertPositionEnd, endTag);
            editableContent.insert((int) insertPositionStart, startTagPart_3);
            editableContent.insert((int) insertPositionStart, currAnnot.getType());
            editableContent.insert((int) insertPositionStart, startTagPart_2);
            editableContent.insert((int) insertPositionStart, currAnnot.getId().toString());
            editableContent.insert((int) insertPositionStart, startTagPart_1);
          } // if
        } // for

        FileWriter writer = new FileWriter(file);
        writer.write(editableContent.toString());
        writer.close();
      } // if - should generate
      else if (originalContent != null) {
        Out.prln("OrigContent existing. Generate file...");

        Iterator it = peopleAndPlaces.iterator();
        Annotation currAnnot;
        SortedAnnotationList sortedAnnotations = new SortedAnnotationList();

        while (it.hasNext()) {
          currAnnot = (Annotation) it.next();
          sortedAnnotations.addSortedExclusive(currAnnot);
        } // while

        StringBuffer editableContent = new StringBuffer(originalContent);
        long insertPositionEnd;
        long insertPositionStart;
        // insert anotation tags backward
        Out.prln("Unsorted annotations count: " + peopleAndPlaces.size());
        Out.prln("Sorted annotations count: " + sortedAnnotations.size());
        for (int i = sortedAnnotations.size() - 1; i >= 0; --i) {
          currAnnot = (Annotation) sortedAnnotations.get(i);
          insertPositionStart = currAnnot.getStartNode().getOffset().longValue();
          insertPositionEnd = currAnnot.getEndNode().getOffset().longValue();
          if (insertPositionEnd != -1 && insertPositionStart != -1) {
            editableContent.insert((int) insertPositionEnd, endTag);
            editableContent.insert((int) insertPositionStart, startTagPart_3);
            editableContent.insert((int) insertPositionStart, currAnnot.getType());
            editableContent.insert((int) insertPositionStart, startTagPart_2);
            editableContent.insert((int) insertPositionStart, currAnnot.getId().toString());
            editableContent.insert((int) insertPositionStart, startTagPart_1);
          } // if
        } // for

        FileWriter writer = new FileWriter(file);
        writer.write(editableContent.toString());
        writer.close();
      } else {
        Out.prln("Content : " + originalContent);
        Out.prln("Repositioning: " + info);
      }

      String xmlDocument = doc.toXml(peopleAndPlaces, false);
      String fileName = new String("StANNIE_toXML_" + count + ".HTML");
      FileWriter writer = new FileWriter(fileName);
      writer.write(xmlDocument);
      writer.close();
    } // for each doc
  } // main
Ejemplo n.º 29
0
 public void badRequest() {
   out.println("bad request");
 }
 /** Run ANNIE */
 public void execute() throws GateException {
   Out.prln("Running ANNIE...");
   annieController.execute();
   Out.prln("...ANNIE complete");
 } // execute()