Search in sources :

Example 16 with TextClassificationOutcome

use of org.dkpro.tc.api.type.TextClassificationOutcome in project dkpro-tc by dkpro.

the class SequenceOutcomeReaderTest method testSkipLineReader.

@Test
public void testSkipLineReader() throws Exception {
    CollectionReader reader = CollectionReaderFactory.createReader(SequenceOutcomeReader.class, SequenceOutcomeReader.PARAM_SOURCE_LOCATION, "src/test/resources/sequence/posDummy.txt", SequenceOutcomeReader.PARAM_SKIP_LINES_START_WITH_STRING, "#");
    List<List<String>> readSequences = new ArrayList<>();
    List<List<String>> readOutcomes = new ArrayList<>();
    while (reader.hasNext()) {
        JCas theJCas = JCasFactory.createJCas();
        reader.getNext(theJCas.getCas());
        Collection<TextClassificationSequence> sequence = JCasUtil.select(theJCas, TextClassificationSequence.class);
        for (TextClassificationSequence s : sequence) {
            List<TextClassificationTarget> targets = JCasUtil.selectCovered(theJCas, TextClassificationTarget.class, s);
            List<String> tokens = new ArrayList<>();
            for (TextClassificationTarget target : targets) {
                tokens.add(target.getCoveredText());
            }
            readSequences.add(tokens);
        }
        Collection<TextClassificationOutcome> outcomeAnnotations = JCasUtil.select(theJCas, TextClassificationOutcome.class);
        List<String> outcomes = new ArrayList<>();
        for (TextClassificationOutcome o : outcomeAnnotations) {
            outcomes.add(o.getOutcome());
        }
        readOutcomes.add(outcomes);
    }
    assertEquals(4, readSequences.get(1).size());
    // 2 - tokens
    assertEquals("This2", readSequences.get(1).get(0));
    assertEquals("is2", readSequences.get(1).get(1));
    assertEquals("a2", readSequences.get(1).get(2));
    assertEquals("!", readSequences.get(1).get(3));
}
Also used : CollectionReader(org.apache.uima.collection.CollectionReader) ArrayList(java.util.ArrayList) TextClassificationTarget(org.dkpro.tc.api.type.TextClassificationTarget) JCas(org.apache.uima.jcas.JCas) TextClassificationOutcome(org.dkpro.tc.api.type.TextClassificationOutcome) ArrayList(java.util.ArrayList) List(java.util.List) TextClassificationSequence(org.dkpro.tc.api.type.TextClassificationSequence) Test(org.junit.Test)

Example 17 with TextClassificationOutcome

use of org.dkpro.tc.api.type.TextClassificationOutcome in project dkpro-tc by dkpro.

the class LibsvmDataFormatLoadModelConnector method process.

@Override
public void process(JCas jcas) throws AnalysisEngineProcessException {
    try {
        File tempFile = createInputFile(jcas);
        File prediction = runPrediction(tempFile);
        List<TextClassificationOutcome> outcomes = getOutcomeAnnotations(jcas);
        List<String> writtenPredictions = FileUtils.readLines(prediction, "utf-8");
        checkErrorConditionNumberOfOutcomesEqualsNumberOfPredictions(outcomes, writtenPredictions);
        for (int i = 0; i < outcomes.size(); i++) {
            if (isRegression()) {
                String val = writtenPredictions.get(i);
                outcomes.get(i).setOutcome(val);
            } else {
                String val = writtenPredictions.get(i).replaceAll("\\.0", "");
                String pred = integer2OutcomeMapping.get(val);
                outcomes.get(i).setOutcome(pred);
            }
        }
    } catch (Exception e) {
        throw new AnalysisEngineProcessException(e);
    }
}
Also used : TextClassificationOutcome(org.dkpro.tc.api.type.TextClassificationOutcome) File(java.io.File) AnalysisEngineProcessException(org.apache.uima.analysis_engine.AnalysisEngineProcessException) ResourceInitializationException(org.apache.uima.resource.ResourceInitializationException) IOException(java.io.IOException) AnalysisEngineProcessException(org.apache.uima.analysis_engine.AnalysisEngineProcessException)

Example 18 with TextClassificationOutcome

use of org.dkpro.tc.api.type.TextClassificationOutcome in project dkpro-tc by dkpro.

the class FolderwiseDataReader method setTextClassificationOutcome.

protected void setTextClassificationOutcome(JCas aJCas, Resource currentFile, int begin, int end) throws IOException {
    TextClassificationOutcome tco = new TextClassificationOutcome(aJCas, begin, end);
    tco.setOutcome(currentFile.getResource().getFile().getParentFile().getName());
    tco.addToIndexes();
}
Also used : TextClassificationOutcome(org.dkpro.tc.api.type.TextClassificationOutcome)

Example 19 with TextClassificationOutcome

use of org.dkpro.tc.api.type.TextClassificationOutcome in project dkpro-tc by dkpro.

the class CrfSuiteLoadModelConnector method setPredictedOutcome.

private void setPredictedOutcome(JCas jcas, String aLabels) {
    List<TextClassificationOutcome> outcomes = new ArrayList<TextClassificationOutcome>(JCasUtil.select(jcas, TextClassificationOutcome.class));
    String[] labels = aLabels.split("\n");
    for (int i = 0, labelIdx = 0; i < outcomes.size(); i++) {
        if (labels[labelIdx].isEmpty()) {
            // empty lines mark end of sequence
            // shift label index +1 to begin of next sequence
            labelIdx++;
        }
        TextClassificationOutcome o = outcomes.get(i);
        o.setOutcome(labels[labelIdx++]);
    }
}
Also used : TextClassificationOutcome(org.dkpro.tc.api.type.TextClassificationOutcome) ArrayList(java.util.ArrayList)

Example 20 with TextClassificationOutcome

use of org.dkpro.tc.api.type.TextClassificationOutcome in project dkpro-tc by dkpro.

the class MappingAnnotator method mapOutcomes.

private void mapOutcomes(JCas aJCas) {
    Collection<TextClassificationOutcome> tcos = JCasUtil.select(aJCas, TextClassificationOutcome.class);
    for (TextClassificationOutcome o : tcos) {
        String outcome = o.getOutcome();
        if (outcomeMap.containsKey(outcome)) {
            continue;
        }
        outcomeMap.put(outcome, outcomeIdx++);
    }
}
Also used : TextClassificationOutcome(org.dkpro.tc.api.type.TextClassificationOutcome)

Aggregations

TextClassificationOutcome (org.dkpro.tc.api.type.TextClassificationOutcome)59 JCas (org.apache.uima.jcas.JCas)29 ArrayList (java.util.ArrayList)27 AnalysisEngine (org.apache.uima.analysis_engine.AnalysisEngine)19 TextClassificationTarget (org.dkpro.tc.api.type.TextClassificationTarget)18 Token (de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token)16 CollectionReader (org.apache.uima.collection.CollectionReader)15 CollectionException (org.apache.uima.collection.CollectionException)9 CASException (org.apache.uima.cas.CASException)8 JCasId (org.dkpro.tc.api.type.JCasId)8 TextClassificationSequence (org.dkpro.tc.api.type.TextClassificationSequence)7 AnalysisEngineProcessException (org.apache.uima.analysis_engine.AnalysisEngineProcessException)6 Test (org.junit.Test)6 DocumentMetaData (de.tudarmstadt.ukp.dkpro.core.api.metadata.type.DocumentMetaData)5 Sentence (de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence)5 File (java.io.File)5 IOException (java.io.IOException)4 ResourceInitializationException (org.apache.uima.resource.ResourceInitializationException)4 TextClassificationException (org.dkpro.tc.api.exception.TextClassificationException)4 HashSet (java.util.HashSet)3