Search in sources :

Example 1 with CoreferenceChain

use of de.tudarmstadt.ukp.dkpro.core.api.coref.type.CoreferenceChain in project webanno by webanno.

the class TcfReader method convertCoreference.

/**
 * Correferences in CAS should be represented {@link CoreferenceChain} and
 * {@link CoreferenceLink}. The TCF representation Uses <b> rel </b> and
 * <b>target </b> to build chains. Example: </br>
 * <i> {@literal  <entity><reference ID="rc_0" tokenIDs="t_0" mintokIDs=
 * "t_0" type="nam"/> } </br>
 * {@literal <reference ID="rc_1" tokenIDs="t_6" mintokIDs="t_6" type=
 * "pro.per3" rel=
 * "anaphoric" target="rc_0"/></entity>
 * }</i> </br>
 * The first phase of conversion is getting all <b>references</b> and
 * <b>targets</b> alongside the <b>type</b> and <b>relations in different
 * maps</b> <br>
 * Second, an iteration is made through all the maps and the
 * {@link CoreferenceChain} and {@link CoreferenceLink} annotations are
 * constructed.
 */
private void convertCoreference(JCas aJCas, TextCorpus aCorpusData, Map<String, Token> aTokens) {
    if (aCorpusData.getReferencesLayer() == null) {
        // No layer to read from.
        return;
    }
    for (int i = 0; i < aCorpusData.getReferencesLayer().size(); i++) {
        eu.clarin.weblicht.wlfxb.tc.api.ReferencedEntity entity = aCorpusData.getReferencesLayer().getReferencedEntity(i);
        Map<Integer, CoreferenceLink> referencesMap = new TreeMap<>();
        storeReferencesAndTargetsInMap(referencesMap, entity, aCorpusData, aTokens, aJCas);
        CoreferenceChain chain = new CoreferenceChain(aJCas);
        CoreferenceLink link = null;
        for (Integer address : referencesMap.keySet()) {
            if (chain.getFirst() == null) {
                chain.setFirst(referencesMap.get(address));
                link = chain.getFirst();
                chain.addToIndexes();
            } else {
                link.setNext(referencesMap.get(address));
                if (link.getReferenceRelation() == null) {
                    link.setReferenceRelation(referencesMap.get(address).getReferenceRelation());
                }
                link = link.getNext();
                link.addToIndexes();
            }
        }
    }
}
Also used : CoreferenceChain(de.tudarmstadt.ukp.dkpro.core.api.coref.type.CoreferenceChain) CoreferenceLink(de.tudarmstadt.ukp.dkpro.core.api.coref.type.CoreferenceLink) TreeMap(java.util.TreeMap)

Example 2 with CoreferenceChain

use of de.tudarmstadt.ukp.dkpro.core.api.coref.type.CoreferenceChain in project webanno by webanno.

the class TcfWriter method writeCoreference.

private void writeCoreference(JCas aJCas, TextCorpus aTextCorpus, Map<Integer, eu.clarin.weblicht.wlfxb.tc.api.Token> aTokensBeginPositionMap) {
    if (!JCasUtil.exists(aJCas, CoreferenceChain.class)) {
        // Do nothing if there are no coreference chains in the CAS
        getLogger().debug("Layer [" + TextCorpusLayerTag.REFERENCES.getXmlName() + "]: empty");
        return;
    }
    String tagSetName = "TueBaDz";
    for (TagsetDescription tagSet : select(aJCas, TagsetDescription.class)) {
        if (tagSet.getLayer().equals(CoreferenceLink.class.getName())) {
            tagSetName = tagSet.getName();
            break;
        }
    }
    ReferencesLayer coreferencesLayer = aTextCorpus.createReferencesLayer(null, tagSetName, null);
    getLogger().debug("Layer [" + TextCorpusLayerTag.REFERENCES.getXmlName() + "]: created");
    for (CoreferenceChain chain : select(aJCas, CoreferenceChain.class)) {
        CoreferenceLink prevLink = null;
        Reference prevRef = null;
        List<Reference> refs = new ArrayList<>();
        for (CoreferenceLink link : chain.links()) {
            // Get covered tokens
            List<eu.clarin.weblicht.wlfxb.tc.api.Token> tokens = new ArrayList<>();
            for (Token token : selectCovered(Token.class, link)) {
                tokens.add(aTokensBeginPositionMap.get(token.getBegin()));
            }
            // Create current reference
            Reference ref = coreferencesLayer.createReference(link.getReferenceType(), tokens, null);
            // Special handling for expletive relations
            if (REL_TYPE_EXPLETIVE.equals(link.getReferenceRelation())) {
                coreferencesLayer.addRelation(ref, REL_TYPE_EXPLETIVE);
                // chain, so we bail out here.
                continue;
            }
            // Create relation between previous and current reference
            if (prevLink != null) {
                coreferencesLayer.addRelation(prevRef, prevLink.getReferenceRelation(), ref);
            }
            prevLink = link;
            prevRef = ref;
            refs.add(ref);
        }
        coreferencesLayer.addReferent(refs);
    }
}
Also used : CoreferenceChain(de.tudarmstadt.ukp.dkpro.core.api.coref.type.CoreferenceChain) Reference(eu.clarin.weblicht.wlfxb.tc.api.Reference) ArrayList(java.util.ArrayList) Token(de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token) TagsetDescription(de.tudarmstadt.ukp.dkpro.core.api.metadata.type.TagsetDescription) CoreferenceLink(de.tudarmstadt.ukp.dkpro.core.api.coref.type.CoreferenceLink) ReferencesLayer(eu.clarin.weblicht.wlfxb.tc.api.ReferencesLayer)

Example 3 with CoreferenceChain

use of de.tudarmstadt.ukp.dkpro.core.api.coref.type.CoreferenceChain in project webanno by webanno.

the class WebAnnoSemanticGraphReader method convertToCas.

public void convertToCas(JCas aJCas, InputStream aIs, String aEncoding) throws IOException {
    StringBuilder text = new StringBuilder();
    LineIterator lineIterator = IOUtils.lineIterator(aIs, aEncoding);
    int tokenBeginPosition = 0;
    while (lineIterator.hasNext()) {
        String line = lineIterator.next();
        String[] contents = line.split("\t>\t|\tX\t");
        int sentenceBegin = tokenBeginPosition;
        int chainBegin = tokenBeginPosition;
        int chainEnd = 0;
        StringTokenizer st = new StringTokenizer(contents[0]);
        while (st.hasMoreTokens()) {
            String content = st.nextToken();
            Token outToken = new Token(aJCas, tokenBeginPosition, tokenBeginPosition + content.length());
            outToken.addToIndexes();
            tokenBeginPosition = outToken.getEnd() + 1;
            chainEnd = tokenBeginPosition;
            text.append(content).append(" ");
        }
        CoreferenceChain chain = new CoreferenceChain(aJCas);
        CoreferenceLink link = new CoreferenceLink(aJCas, chainBegin, chainEnd - 1);
        link.setReferenceType("text");
        link.addToIndexes();
        chain.setFirst(link);
        if (line.contains("\t>\t")) {
            link.setReferenceRelation("entails");
            Token outToken = new Token(aJCas, tokenBeginPosition, tokenBeginPosition + 1);
            outToken.addToIndexes();
            tokenBeginPosition = outToken.getEnd() + 1;
            text.append("> ");
        } else {
            link.setReferenceRelation("do not entails");
            Token outToken = new Token(aJCas, tokenBeginPosition, tokenBeginPosition + 1);
            outToken.addToIndexes();
            tokenBeginPosition = outToken.getEnd() + 1;
            text.append("X ");
        }
        chainBegin = tokenBeginPosition;
        st = new StringTokenizer(contents[0]);
        while (st.hasMoreTokens()) {
            String content = st.nextToken();
            Token outToken = new Token(aJCas, tokenBeginPosition, tokenBeginPosition + content.length());
            outToken.addToIndexes();
            tokenBeginPosition = outToken.getEnd() + 1;
            chainEnd = tokenBeginPosition;
            text.append(content).append(" ");
        }
        CoreferenceLink nextLink = new CoreferenceLink(aJCas, chainBegin, chainEnd - 1);
        nextLink.setReferenceType("hypothesis");
        nextLink.addToIndexes();
        link.setNext(nextLink);
        chain.addToIndexes();
        text.append("\n");
        Sentence outSentence = new Sentence(aJCas);
        outSentence.setBegin(sentenceBegin);
        outSentence.setEnd(tokenBeginPosition);
        outSentence.addToIndexes();
        tokenBeginPosition = tokenBeginPosition + 1;
        sentenceBegin = tokenBeginPosition;
    }
    aJCas.setDocumentText(text.toString());
}
Also used : StringTokenizer(java.util.StringTokenizer) CoreferenceChain(de.tudarmstadt.ukp.dkpro.core.api.coref.type.CoreferenceChain) CoreferenceLink(de.tudarmstadt.ukp.dkpro.core.api.coref.type.CoreferenceLink) Token(de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token) LineIterator(org.apache.commons.io.LineIterator) Sentence(de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence)

Aggregations

CoreferenceChain (de.tudarmstadt.ukp.dkpro.core.api.coref.type.CoreferenceChain)3 CoreferenceLink (de.tudarmstadt.ukp.dkpro.core.api.coref.type.CoreferenceLink)3 Token (de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token)2 TagsetDescription (de.tudarmstadt.ukp.dkpro.core.api.metadata.type.TagsetDescription)1 Sentence (de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence)1 Reference (eu.clarin.weblicht.wlfxb.tc.api.Reference)1 ReferencesLayer (eu.clarin.weblicht.wlfxb.tc.api.ReferencesLayer)1 ArrayList (java.util.ArrayList)1 StringTokenizer (java.util.StringTokenizer)1 TreeMap (java.util.TreeMap)1 LineIterator (org.apache.commons.io.LineIterator)1