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

edu.jhu.hlt.concrete.validation.ValidatableTokenRefSequence Maven / Gradle / Ivy

There is a newer version: 4.15.0
Show newest version
/*
 * Copyright 2012-2015 Johns Hopkins University HLTCOE. All rights reserved.
 * See LICENSE in the project root directory.
 */
package edu.jhu.hlt.concrete.validation;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import edu.jhu.hlt.concrete.Communication;
import edu.jhu.hlt.concrete.TokenRefSequence;
import edu.jhu.hlt.concrete.UUID;
import edu.jhu.hlt.concrete.miscommunication.MiscommunicationException;
import edu.jhu.hlt.concrete.miscommunication.tokenized.CachedTokenizationCommunication;

/**
 *
 */
public class ValidatableTokenRefSequence extends AbstractAnnotation {

  private static final Logger LOGGER = LoggerFactory.getLogger(ValidatableTokenRefSequence.class);

  /**
   * @param annotation
   */
  public ValidatableTokenRefSequence(TokenRefSequence annotation) {
    super(annotation);
  }

  /*
   * (non-Javadoc)
   *
   * @see edu.jhu.hlt.concrete.validation.AbstractAnnotation#isValidWithComm(edu.jhu.hlt.concrete.Communication)
   */
  @Override
  protected boolean isValidWithComm(Communication c) {
    UUID tokUuid = this.annotation.getTokenizationId();
    List tokenIdxIds = this.annotation.getTokenIndexList();

    try {
      CachedTokenizationCommunication cc = new CachedTokenizationCommunication(c);
      if (this.printStatus("Tokenization UUID must be an existing tokenization.", cc.getUuidToTokenizationMap().keySet().contains(tokUuid))) {
        Set tokIdxSet = cc.getUuidToTokenIdxToTokenMap().get(tokUuid).keySet();
        boolean allInTokenization = tokIdxSet.containsAll(tokenIdxIds);
        if (!allInTokenization) {
          Set notInTokenization = new HashSet<>(tokIdxSet);
          notInTokenization.removeAll(tokenIdxIds);
          LOGGER.info("Some of the integers in this TokenRefSequence [{}] do not exist in the tokenization's token indices. They are:", tokUuid.getUuidString());
          notInTokenization.forEach(idx -> LOGGER.info("{}", idx));
          return false;
        }

        return true;
      } else {
        return false;
      }
    } catch (MiscommunicationException e) {
      LOGGER.error("Caught an exception creating the convenience wrapper.", e);
      return false;
    }
  }

  /*
   * (non-Javadoc)
   *
   * @see edu.jhu.hlt.concrete.validation.AbstractAnnotation#isValid()
   */
  @Override
  public boolean isValid() {
    UUID tokUuid = this.annotation.getTokenizationId();
    return this.validateUUID(tokUuid) && this.printStatus("TokenIndexList must be set", this.annotation.isSetTokenIndexList())
        && this.printStatus("TokenIndexList must have >0 items", this.annotation.getTokenIndexListSize() > 0);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy