All Downloads are FREE. Search and download functionalities are using the official Maven repository.

eu.danieldk.nlp.conllx.CONLLToken Maven / Gradle / Ivy

The newest version!
// Copyright 2008, 2013 Daniel de Kok
//
// This class is part of the Jitar tagger.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package eu.danieldk.nlp.conllx;

import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import org.apache.commons.lang3.StringUtils;

/**
 * Instances of CONLLToken represent a token from a CONLL corpus.
 * At the bare minimum a token contains a position/identifier and the
 * actual token. The token can optionally be annotated with: a lemma,
 * a course-grained POS tag, a fine-grained POS tag, features, the head
 * of the token, the dependency relation to the head, the projective head
 * of the token, and the dependency relation to the projective head.
 */
public class CONLLToken implements Token {
    private final int position;

    private final Optional form;

    private final Optional lemma;

    private final Optional coarsePOSTag;

    private final Optional posTag;

    private final Optional features;

    private final Optional head;

    private final Optional depRel;

    private final Optional pHead;

    private final Optional pDepRel;

    public CONLLToken(int position, Optional form, Optional lemma, Optional coarsePOSTag,
                      Optional posTag, Optional features, Optional head,
                      Optional depRel, Optional pHead, Optional pDepRel) {
        Preconditions.checkNotNull(form);
        Preconditions.checkNotNull(lemma);
        Preconditions.checkNotNull(coarsePOSTag);
        Preconditions.checkNotNull(posTag);
        Preconditions.checkNotNull(features);
        Preconditions.checkNotNull(head);
        Preconditions.checkNotNull(depRel);
        Preconditions.checkNotNull(pHead);
        Preconditions.checkNotNull(pDepRel);

        this.position = position;
        this.form = form;
        this.lemma = lemma;
        this.coarsePOSTag = coarsePOSTag;
        this.posTag = posTag;
        this.features = features;
        this.head = head;
        this.depRel = depRel;
        this.pHead = pHead;
        this.pDepRel = pDepRel;
    }

    @Override
    public int getID() {
        return position;
    }

    @Override
    public Optional getForm() {
        return form;
    }

    @Override
    public Optional getLemma() {
        return lemma;
    }

    @Override
    public Optional getCoarsePOSTag() {
        return coarsePOSTag;
    }

    @Override
    public Optional getPosTag() {
        return posTag;
    }

    @Override
    public Optional getFeatures() {
        return features;
    }

    @Override
    public Optional getHead() {
        return head;
    }

    @Override
    public Optional getDepRel() {
        return depRel;
    }

    @Override
    public Optional getPHead() {
        return pHead;
    }

    @Override
    public Optional getPDepRel() {
        return pDepRel;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        final CONLLToken that = (CONLLToken) o;

        if (position != that.position) return false;
        if (!coarsePOSTag.equals(that.coarsePOSTag)) return false;
        if (!depRel.equals(that.depRel)) return false;
        if (!features.equals(that.features)) return false;
        if (!form.equals(that.form)) return false;
        if (!head.equals(that.head)) return false;
        if (!lemma.equals(that.lemma)) return false;
        if (!pDepRel.equals(that.pDepRel)) return false;
        if (!pHead.equals(that.pHead)) return false;
        if (!posTag.equals(that.posTag)) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = position;
        result = 31 * result + form.hashCode();
        result = 31 * result + lemma.hashCode();
        result = 31 * result + coarsePOSTag.hashCode();
        result = 31 * result + posTag.hashCode();
        result = 31 * result + features.hashCode();
        result = 31 * result + head.hashCode();
        result = 31 * result + depRel.hashCode();
        result = 31 * result + pHead.hashCode();
        result = 31 * result + pDepRel.hashCode();
        return result;
    }

    @Override
    public String toString() {
        String fields[] = {Integer.toString(position), form.or("_"), lemma.or("_"), coarsePOSTag.or("_"), posTag.or("_"),
                features.or("_"), intToStringOr(head, "_"), depRel.or("_"), intToStringOr(pHead, "_"), pDepRel.or("_")};
        return StringUtils.join(fields, "\t");
    }

    private String intToStringOr(Optional v, String or) {
        return v.isPresent() ? Integer.toString(v.get()) : or;

    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy