/**
   * Add an alignment link from T to H, based on the rule t->h in which t is a phrase in T from
   * index textStart to textEnd of the tokens, and h is a phrase in H from index hypoStart to
   * hypoEnd of the tokens,
   *
   * @param textToken Token in TextView to annotate
   * @param hypoToken Token in HypoView to annotate
   * @param confidence The confidence of the rule
   * @param linkDirection The direction of the link (t to h, h to t or bidirectional).
   * @param linkInfo The relation of the rule (Wordnet synonym, Wikipedia redirect etc).
   * @param linkGroupLabel
   * @throws CASException
   */
  private void addAlignmentAnnotations(
      Token textToken,
      Token hypoToken,
      double confidence,
      Direction linkDirection,
      String linkInfo,
      StringList linkGroupLabel)
      throws CASException {

    // Prepare the Target instances
    Target textTarget = new Target(textView);
    Target hypoTarget = new Target(hypoView);

    // Prepare an FSArray instance and put the target annotations in it
    FSArray textAnnots = new FSArray(textView, 1);
    FSArray hypoAnnots = new FSArray(hypoView, 1);

    textAnnots.set(0, textToken);
    hypoAnnots.set(0, hypoToken);

    textTarget.setTargetAnnotations(textAnnots);
    hypoTarget.setTargetAnnotations(hypoAnnots);

    // Set begin and end value of the Target annotations
    textTarget.setBegin(textToken.getBegin());
    textTarget.setEnd(textToken.getEnd());
    hypoTarget.setBegin(hypoToken.getBegin());
    hypoTarget.setEnd(hypoToken.getEnd());

    // Add the targets to the indices
    textTarget.addToIndexes();
    hypoTarget.addToIndexes();

    // Mark an alignment.Link and add it to the hypothesis view
    Link link = new Link(hypoView);
    link.setTSideTarget(textTarget);
    link.setHSideTarget(hypoTarget);

    // Set the link direction
    link.setDirection(linkDirection);

    // Set strength
    link.setStrength(confidence);

    // Set Group label
    link.setGroupLabel(linkGroupLabel);

    // Add the link information
    link.setAlignerID(ALIGNER_ID);
    link.setAlignerVersion(ALIGNER_VERSION);
    link.setLinkInfo(linkInfo);

    // Mark begin and end according to the hypothesis target
    link.setBegin(hypoTarget.getBegin());
    link.setEnd(hypoTarget.getEnd());

    // Add to index
    link.addToIndexes();
  }
  /**
   * Add an alignment link from T to H, based on the rule t->h in which t is a phrase in T from
   * index textStart to textEnd of the tokens, and h is a phrase in H from index hypoStart to
   * hypoEnd of the tokens,
   *
   * @param aJCas The JCas object
   * @param textStart The index of the first token in T in this alignment link
   * @param textEnd The index of the last token in T in this alignment link
   * @param hypoStart The index of the first token in H in this alignment link
   * @param hypoEnd The index of the last token in H in this alignment link
   * @param resourceName The lexical resource that this rule came from
   * @param lexicalResourceVersion The version of the lexical resource
   * @param confidence The confidence of the rule
   * @param linkDirection The direction of the link (t to h, h to t or bidirectional).
   * @param linkInfo The relation of the rule (Wordnet synonym, Wikipedia redirect etc).
   * @throws CASException
   */
  private void addAlignmentAnnotations(
      JCas aJCas,
      int textStart,
      int textEnd,
      int hypoStart,
      int hypoEnd,
      String resourceName,
      String lexicalResourceVersion,
      double confidence,
      Direction linkDirection,
      String linkInfo)
      throws CASException {

    // Get the text and hypothesis views
    JCas textView = aJCas.getView(LAP_ImplBase.TEXTVIEW);
    JCas hypoView = aJCas.getView(LAP_ImplBase.HYPOTHESISVIEW);

    // Prepare the Target instances
    Target textTarget = new Target(textView);
    Target hypoTarget = new Target(hypoView);

    // Prepare an FSArray instance and put the target annotations in it
    FSArray textAnnots = new FSArray(textView, textEnd - textStart + 1);
    FSArray hypoAnnots = new FSArray(hypoView, hypoEnd - hypoStart + 1);

    int tokenIndex = 0;

    for (Token token : textTokens.subList(textStart, textEnd + 1)) {
      textAnnots.set(tokenIndex++, token);
    }

    tokenIndex = 0;

    for (Token token : hypoTokens.subList(hypoStart, hypoEnd + 1)) {
      hypoAnnots.set(tokenIndex++, token);
    }

    textTarget.setTargetAnnotations(textAnnots);
    hypoTarget.setTargetAnnotations(hypoAnnots);

    // Set begin and end value of the Target annotations
    textTarget.setBegin(textTokens.get(textStart).getBegin());
    textTarget.setEnd(textTokens.get(textEnd).getEnd());
    hypoTarget.setBegin(hypoTokens.get(hypoStart).getBegin());
    hypoTarget.setEnd(hypoTokens.get(hypoEnd).getEnd());

    // Add the targets to the indices
    textTarget.addToIndexes();
    hypoTarget.addToIndexes();

    // Mark an alignment.Link and add it to the hypothesis view
    Link link = new Link(hypoView);
    link.setTSideTarget(textTarget);
    link.setHSideTarget(hypoTarget);

    // Set the link direction
    link.setDirection(linkDirection);

    // Set strength according to the rule data
    link.setStrength(confidence);

    // Add the link information
    link.setAlignerID(resourceName);
    link.setAlignerVersion(lexicalResourceVersion);
    link.setLinkInfo(linkInfo);

    // Mark begin and end according to the hypothesis target
    link.setBegin(hypoTarget.getBegin());
    link.setEnd(hypoTarget.getEnd());

    // Add to index
    link.addToIndexes();
  }