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

edu.stanford.nlp.ling.tokensregex.types.Tags Maven / Gradle / Ivy

Go to download

Stanford CoreNLP provides a set of natural language analysis tools which can take raw English language text input and give the base forms of words, their parts of speech, whether they are names of companies, people, etc., normalize dates, times, and numeric quantities, mark up the structure of sentences in terms of phrases and word dependencies, and indicate which noun phrases refer to the same entities. It provides the foundational building blocks for higher level text understanding applications.

There is a newer version: 4.5.7
Show newest version
package edu.stanford.nlp.ling.tokensregex.types;

import edu.stanford.nlp.ling.CoreAnnotation;
import edu.stanford.nlp.util.ErasureUtils;
import edu.stanford.nlp.util.Generics;

import java.io.Serializable;
import java.util.*;

/**
 * Tags that can be added to values or annotations
 */
public class Tags implements Serializable {

  public static class TagsAnnotation implements CoreAnnotation {
    public Class getType() {
      return Tags.class;
    }
  }

  Map tags;

  public Tags(String... tags) {
    if (tags != null) {
      this.tags = new HashMap<>();// Generics.newHashMap();
      for (String tag:tags) {
        this.tags.put(tag, null);
      }
    }
  }

  public Collection getTags() {
    return tags.keySet();
  }

  public boolean hasTag(String tag) {
    return (tags != null)? tags.containsKey(tag): false;
  }

  public void setTag(String tag, Value v) {
    if (tags == null) { tags = new HashMap<>(1);//Generics.newHashMap(1);
    }
    tags.put(tag, v);
  }

  public void addTag(String tag, Value v) {
    if (tags == null) { tags = new HashMap<>(1);//Generics.newHashMap(1);
    }
    // Adds v as a tag into a list of tags...
    List tagList = null;
    if (tags.containsKey(tag)) {
      Value oldValue = tags.get(tag);
      if (Expressions.TYPE_LIST.equals(oldValue.getType())) {
        tagList = ErasureUtils.uncheckedCast(oldValue.get());
      } else {
        // Put the oldValue into a new array
        tagList = new ArrayList<>();
        tagList.add(oldValue);
        tags.put(tag, Expressions.createValue(Expressions.TYPE_LIST, tagList));
      }
    } else {
      tagList = new ArrayList<>();
      tags.put(tag, Expressions.createValue(Expressions.TYPE_LIST, tagList));
    }
    tagList.add(v);
  }

  public void removeTag(String tag) {
    if (tags != null) { tags.remove(tag); }
  }

  public Value getTag(String tag) {
    return (tags != null)? tags.get(tag): null;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (!(o instanceof Tags)) return false;

    Tags tags1 = (Tags) o;

    if (tags != null ? !tags.equals(tags1.tags) : tags1.tags != null) return false;

    return true;
  }

  @Override
  public int hashCode() {
    return tags != null ? tags.hashCode() : 0;
  }

  private static final long serialVersionUID = 2;
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy