package com.google.sps.Objects;

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

/**
 * A class containing String manipulation helper methods, used when calculating TF-IDF.
 */
public class TFIDFStringHelper {
  
  /**
   * "Sanitizes" a String so it can be analyzed.
   * Remove all punctuation and convert String to lowercase.
   */
  public static String sanitize(String input) {
    if (input ==  null || input.length() == 0) {
      return input;
    }

    input = input.toLowerCase();
    // Regex identifies punctuation except apostrophes.
    input = input.replaceAll("[\\p{P}&&[^']]", "");
    return input;  
  }

  /**
   * Splits up a given string into n-grams, counting their frequency along the way.
   * Returns a LinkedHashMap containing single words (unigrams), bigrams, and trigrams,
   * mapped with each term's freqency count.
   */
  public static LinkedHashMap<String, Integer> ngramTokenizer(String input) {
    LinkedHashMap<String, Integer> ngrams = new LinkedHashMap<String, Integer>();

    if (input ==  null || input.length() == 0) {
      return ngrams;
    }

    input = sanitize(input);
    String[] words = input.split(" ");

    int nLength = (words.length >= 3) ? 3 : words.length;

    for (int n = 1; n <= nLength; n++) {
      for (int i = 0; i < words.length - n + 1; i++) {
        String ngram = concat(words, i, n);
        Integer count = ngrams.get(ngram);
        if (count == null) {
          ngrams.put(ngram, 1);
        } else {
          ngrams.put(ngram, count + 1);
        }
      }
    }

    return ngrams;
  }

  /**
   * Helper method for {@code ngramTokenizer()}. Concatenates words to form appropriate n-gram.
   */
  public static String concat(String[] words, int startIndex, int n) {
    StringBuilder sb = new StringBuilder();
    for (int i = startIndex; i < startIndex + n; i++) {
      if (i > startIndex) {
        sb.append(" ");
      }
      sb.append(words[i]);
    }
    return sb.toString();
  }
}
@@ -58,16 +58,41 @@
 ashMap;%0A
+import java.util.Arrays;%0A
 import j
@@ -1452,65 +1452,16 @@
 .get
-(ngram);%0A        if (count == null) %7B%0A          ngrams.pu
+OrDefaul
 t(ng
@@ -1469,31 +1469,12 @@
 am, 
-1
+0
 );%0A
-        %7D else %7B%0A  
     
@@ -1511,18 +1511,8 @@
 1);%0A
-        %7D%0A
     
@@ -1739,58 +1739,45 @@
 ring
-Builder sb = new StringBuilder();%0A    for (int i =
+%5B%5D nWords = Arrays.copyOfRange(words,
  sta
@@ -1783,21 +1783,17 @@
 artIndex
-; i %3C
+,
  startIn
@@ -1803,133 +1803,45 @@
  + n
-; i++) %7B%0A      if (i %3E startIndex) %7B%0A        sb.append(%22 %22);%0A      %7D%0A      sb.append(words%5Bi%5D);%0A    %7D%0A    return sb.toString(
+);%0A    return String.join(%22 %22, nWords
 );%0A 

Make code more concise