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

com.basistech.rosette.dm.ResolvedEntity Maven / Gradle / Ivy

There is a newer version: 3.0.3
Show newest version
/*
* Copyright 2014 Basis Technology Corp.
*
* 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 com.basistech.rosette.dm;

import com.google.common.base.Objects;

import java.io.Serializable;
import java.util.Map;

/**
 * A reference to a "real world" entity.  For example, an {@code EntityMention} of
 * "George" could refer to many different Georges.  A {@code ResolvedEntity}
 * associates the mention with an id from some external knowledge base, e.g.
 * Q23 from Wikidata.
 * An entity may optionally have a sentiment category associated with it
 * (e.g. "positive", "negative", "neutral").
 * @deprecated replaced by {@link Entity}.
 */
@Deprecated
public class ResolvedEntity extends Attribute implements Serializable {
    private static final long serialVersionUID = 222L;
    private final String entityId;
    //I prefer 'chainId' over 'coreferenceChainId' but picked the latter to make it consistent with EntityMention
    private final Integer coreferenceChainId;
    private final Double confidence;
    private final CategorizerResult sentiment;

    // for json compatibility on ADM without sentiment fields
    protected ResolvedEntity(int startOffset, int endOffset, String entityId,
                             Integer coreferenceChainId, Double confidence,
                             Map extendedProperties) {
        this(startOffset, endOffset, entityId, coreferenceChainId, confidence,
            null, extendedProperties);
    }

    protected ResolvedEntity(int startOffset, int endOffset, String entityId,
                             Integer coreferenceChainId, Double confidence,
                             CategorizerResult sentiment,
                             Map extendedProperties) {
        super(startOffset, endOffset, extendedProperties);
        this.entityId = entityId;
        this.coreferenceChainId = coreferenceChainId;
        this.confidence = confidence;
        this.sentiment = sentiment;
    }

    /**
     * Returns the unique identifier of this entity.
     *
     * @return the unique identifier of this entity
     */
    public String getEntityId() {
        return entityId;
    }

    /**
     * Returns the in-document coreference chain ID for this entity, or null if there is none.
     *
     * @return the in-document coreference chain ID for this entity, or null if there is none
     */

    public Integer getCoreferenceChainId() {
        return coreferenceChainId;
    }

    /**
     * Returns the confidence for this resolved entity, or null if there is none.
     *
     * @return the confidence for this resolved entity, or null if there is none
     */
    public Double getConfidence() {
        return confidence;
    }

    /**
     * Returns the sentiment of this entity, or null if not computed.
     * Only the top-ranked sentiment result is returned.  Use
     * {@link Entity#getSentiment()} to obtain all results.
     *
     * @return the sentiment of this entity, or null if not computed.
     */
    public CategorizerResult getSentiment() {
        return sentiment;
    }

    // hmm, what *really* belongs in equals and hashCode?  maybe just entityId?
    // for now, I'm following suit by using all fields.
    @Override
    public boolean equals(Object o) {
        // generated by intellij
        //CHECKSTYLE:OFF
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        if (!super.equals(o)) return false;

        ResolvedEntity that = (ResolvedEntity) o;

        if (entityId != null ? !entityId.equals(that.entityId) : that.entityId != null) return false;
        if (coreferenceChainId != null ? !coreferenceChainId.equals(that.coreferenceChainId) : that.coreferenceChainId != null)
            return false;
        if (confidence != null ? !confidence.equals(that.confidence) : that.confidence != null) return false;
        return sentiment != null ? sentiment.equals(that.sentiment) : that.sentiment == null;
        //CHECKSTYLE:ON
    }

    @Override
    public int hashCode() {
        // generated by intellij
        //CHECKSTYLE:OFF
        int result = super.hashCode();
        result = 31 * result + (entityId != null ? entityId.hashCode() : 0);
        result = 31 * result + (coreferenceChainId != null ? coreferenceChainId.hashCode() : 0);
        result = 31 * result + (confidence != null ? confidence.hashCode() : 0);
        result = 31 * result + (sentiment != null ? sentiment.hashCode() : 0);
        return result;
        //CHECKSTYLE:ON
    }

    @Override
    protected Objects.ToStringHelper toStringHelper() {
        return super.toStringHelper()
                .add("entityId", entityId)
                .add("confidence", confidence)
                .add("coreferenceChainId", coreferenceChainId)
                .add("sentiment", sentiment);
    }

    /**
     * A builder for resolved entities.
     */
    public static class Builder extends Attribute.Builder {
        private String entityId;
        private Double confidence; // NAN?
        private Integer coreferenceChainId;
        private CategorizerResult sentiment;

        /**
         * Constructs a builder from the required values.
         *
         * @param startOffset the start offset in characters
         * @param endOffset the end offset in character
         * @param entityId the entity ID
         */
        public Builder(int startOffset, int endOffset, String entityId) {
            super(startOffset, endOffset);
            this.entityId = entityId;
        }

        /**
         * Constructs a builder by copying values from an existing resolved entity.
         *
         * @param toCopy the object to create
         * @adm.ignore
         */
        public Builder(ResolvedEntity toCopy) {
            super(toCopy);
            this.entityId = toCopy.entityId;
            this.confidence = toCopy.confidence;
            this.coreferenceChainId = toCopy.coreferenceChainId;
            this.sentiment = toCopy.sentiment;
        }

        /**
         * Specifies the entity unique ID.
         *
         * @param entityId the ID
         * @return this
         */
        public Builder entityId(String entityId) {
            this.entityId = entityId;
            return this;
        }

        /**
         * Specifies the confidence value.
         *
         * @param confidence the confidence value
         * @return this
         */
        public Builder confidence(double confidence) {
            this.confidence = confidence;
            return this;
        }

        /**
         * Specifies the coreference chain id.
         *
         * @param coreferenceChainId the chain id
         * @return this
         */
        public Builder coreferenceChainId(int coreferenceChainId) {
            this.coreferenceChainId = coreferenceChainId;
            return this;
        }

        /**
         * Specifies the sentiment.
         *
         * @param sentiment the sentiment
         * @return this
         */
        public Builder sentiment(CategorizerResult sentiment) {
            this.sentiment = sentiment;
            return this;
        }

        /**
         * Returns an immutable resolved entity from the current state of the builder.
         *
         * @return the new resolved entity
         */
        public ResolvedEntity build() {
            return new ResolvedEntity(startOffset, endOffset, entityId, coreferenceChainId, confidence,
                sentiment, buildExtendedProperties());
        }

        @Override
        protected Builder getThis() {
            return this;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy