/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 opennlp.tools.ngram;

import java.util.ArrayList;
import java.util.List;

/**
 * Generates an nGram, with optional separator, and returns the grams as a list
 * of strings
 */
public class NGramGenerator {


  /**
   * Creates an ngram separated
   * by the separator param value i.e. a,b,c,d with n = 3 and separator = "-"
   * would return a-b-c,b-c-d
   *
   * @param input     the input tokens the output ngrams will be derived from
   * @param n         the number of tokens as the sliding window
   * @param separator each string in each gram will be separated by this value if desired.
   *                  Pass in empty string if no separator is desired
   * @return
   */
  public static List<String> generate(List<String> input, int n, String separator) {

    List<String> outGrams = new ArrayList<>();
    for (int i = 0; i < input.size() - (n - 2); i++) {
      final StringBuilder sb = new StringBuilder();
      if ((i + n) <= input.size()) {
        for (int x = i; x < (n + i); x++) {
          sb.append(input.get(x));
          sb.append(separator);
        }
        String gram = sb.toString();
        gram = gram.substring(0, gram.lastIndexOf(separator));
        outGrams.add(gram);
      }
    }
    return outGrams;
  }

  /**
   *Generates an nGram based on a char[] input
   * @param input the array of chars to convert to nGram
   * @param n The number of grams (chars) that each output gram will consist of
   * @param separator each char in each gram will be separated by this value if desired.
   *                  Pass in empty string if no separator is desired
   * @return
   */
  public static List<String> generate(char[] input, int n, String separator) {

    List<String> outGrams = new ArrayList<>();
    for (int i = 0; i < input.length - (n - 2); i++) {
      final StringBuilder sb = new StringBuilder();
      if ((i + n) <= input.length) {
        for (int x = i; x < (n + i); x++) {
          sb.append(input[x]);
          sb.append(separator);
        }
        String gram = sb.toString();
        gram = gram.substring(0, gram.lastIndexOf(separator));
        outGrams.add(gram);
      }
    }
    return outGrams;
  }
}

@@ -1654,33 +1654,33 @@
 t.size() - (n - 
-2
+1
 ); i++) %7B%0A      
@@ -1735,41 +1735,9 @@
     
-if ((i + n) %3C= input.size()) %7B%0A  
+%0A
     
@@ -1766,34 +1766,32 @@
 (n + i); x++) %7B%0A
-  
         sb.appen
@@ -1807,34 +1807,32 @@
 et(x));%0A        
-  
 sb.append(separa
@@ -1835,38 +1835,34 @@
 parator);%0A      
-  %7D%0A  
+%7D%0A
       String gra
@@ -1878,34 +1878,32 @@
 String();%0A      
-  
 gram = gram.subs
@@ -1939,34 +1939,32 @@
 arator));%0A      
-  
 outGrams.add(gra
@@ -1965,33 +1965,32 @@
 dd(gram);%0A      
-%7D
 %0A    %7D%0A    retur
@@ -2545,17 +2545,17 @@
  - (n - 
-2
+1
 ); i++) 
@@ -2617,42 +2617,9 @@
     
- if ((i + n) %3C= input.length) %7B%0A  
+%0A
     
@@ -2656,34 +2656,32 @@
  x++) %7B%0A        
-  
 sb.append(input%5B
@@ -2685,18 +2685,16 @@
 ut%5Bx%5D);%0A
-  
         
@@ -2721,26 +2721,24 @@
 ;%0A      
-  
 %7D%0A      
   String
@@ -2725,26 +2725,24 @@
     %7D%0A      
- 
- 
 String gram 
@@ -2754,26 +2754,24 @@
 toString();%0A
-  
       gram =
@@ -2821,26 +2821,24 @@
 or));%0A      
-  
 outGrams.add
@@ -2845,24 +2845,16 @@
 (gram);%0A
-      %7D%0A
     %7D%0A  

Correct loop bounds NGramGenerator.generate