Exemplo n.º 1
0
	public void entityProcessComplete(CAS aCas, EntityProcessStatus aStatus) {
		if (aStatus != null) {
			if (aStatus.isException()) {
				System.err.println("Error on process CAS call to remote service:");
				List<Exception> exceptions = aStatus.getExceptions();
				for (int i = 0; i < exceptions.size(); i++) {
					((Throwable) exceptions.get(i)).printStackTrace();
				}
			}
			
			try {
				JCas cas = aCas.getJCas();

				for(Token token : JCasUtil.select(cas, Token.class)) {
					System.out.println(token.getCoveredText() + " " + token.getPos().getPosValue());
				}

			} catch (CASException e) {
				e.printStackTrace();
			}
		}
	}
  /**
   * CasConsumer would use tags and features to write output file, evaluate and print precision,
   * recall and F-1 measure.
   *
   * @param arg0
   * @throws ResourceProcessException
   */
  @Override
  public void processCas(CAS arg0) throws ResourceProcessException {
    /** convert type of arg0 */
    JCas jcas = null;
    try {
      jcas = arg0.getJCas();
    } catch (CASException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    }
    // TODO Auto-generated method stub
    FSIterator<Annotation> ite = jcas.getAnnotationIndex(WordTag.type).iterator();

    while (ite.hasNext()) {
      /** collect features */
      String id = ((WordTag) ite.get()).getId();
      int begin = ((WordTag) ite.get()).getBegin0();
      int end = ((WordTag) ite.get()).getEnd0();
      String name = ((WordTag) ite.get()).getName();

      /** organize string for output */
      report.append(id);
      report.append("|");
      report.append(begin);
      report.append(" ");
      report.append(end);
      report.append("|");
      report.append(name);
      report.append("\n");

      /** count the length of output string */
      count++;
      ite.next();
    }

    result = report.toString();
    File sampleOut = new File("src/main/resources/data/sample.out");
    try {
      testRecall = FileUtils.file2String(sampleOut);
    } catch (IOException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    }

    /** split strings from file into sentences */
    String[] resultSplit = result.split("\n");
    String[] recallSplit = testRecall.split("\n");
    PrecisionRecallCalculator(recallSplit, resultSplit);

    /** write the output file to the project root */
    String path = "hw1-longh.out";
    File dirFile = new File(path);

    /** make sure no conflict */
    if (dirFile.exists()) {
      dirFile.delete();
    }

    try {
      /** write file */
      BufferedWriter bw1 = new BufferedWriter(new FileWriter(path, true));
      bw1.write(report.toString());
      bw1.flush();
      bw1.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }