package com.google.sps.utilities;

import com.google.cloud.language.v1.AnalyzeEntitiesRequest;
import com.google.cloud.language.v1.AnalyzeEntitiesResponse;
import com.google.cloud.language.v1.Document;
import com.google.cloud.language.v1.Document.Type;
import com.google.cloud.language.v1.EncodingType;
import com.google.cloud.language.v1.Entity;
import com.google.cloud.language.v1.LanguageServiceClient;
import com.google.sps.data.Keyword;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/* Class that abstracts away getting keywords for specific content to enable testing. */
public class KeywordHelper {
  
  /** Singleton instance of KeywordHelper. */
  private static KeywordHelper instance;

  /** Returns the singleton instance of KeywordHelper. */
  public static KeywordHelper getInstance() {
    if (instance == null) {
      instance = new KeywordHelper();
    }
    return instance;
  }

  private String content;

  public void setContent(String content) {
    this.content = content;
  }

  /**
   * Finds the keywords in the current instance's content.
   *
   * @return list of Keyword objects representing the keywords and their relevances for the current
   *     instances's content.
   */
  public List<Keyword> getKeywords() throws IOException {
    List<Keyword> keywords = new ArrayList<Keyword>();
    try (LanguageServiceClient language = LanguageServiceClient.create()) {
      Document doc = Document.newBuilder().setContent(content).setType(Type.PLAIN_TEXT).build();
      AnalyzeEntitiesRequest request =
          AnalyzeEntitiesRequest.newBuilder()
              .setDocument(doc)
              .setEncodingType(EncodingType.UTF16)
              .build();

      AnalyzeEntitiesResponse response = language.analyzeEntities(request);
      for (Entity entity : response.getEntitiesList()) {
        keywords.add(new Keyword(entity.getName(), entity.getSalience()));
      }
    }
    return keywords;
  }
}

@@ -149,16 +149,133 @@
 sponse;%0A
+import com.google.cloud.language.v1.AnalyzeSyntaxRequest;%0Aimport com.google.cloud.language.v1.AnalyzeSyntaxResponse;%0A
 import c
@@ -512,24 +512,67 @@
 viceClient;%0A
+import com.google.cloud.language.v1.Token;%0A
 import com.g
@@ -673,16 +673,64 @@
 il.List;
+%0Aimport java.util.HashMap;%0Aimport java.util.Map;
 %0A%0A/* Cla
@@ -1483,27 +1483,8 @@
 ds()
- throws IOException
  %7B%0A 
@@ -2128,28 +2128,1135 @@
    %7D
-%0A    return keywords
+ catch (IOException e) %7B%0A      System.err.println(%22IO Exception due to NLP library.%22);%0A    %7D%0A    return keywords;%0A  %7D%0A%0A  /**%0A   * Finds the tokens with their basic forms in the current instance's content.%0A   *%0A   * @return mapping of each token in current instance's content to its basic form.%0A   */%0A  public Map%3CString, String%3E getTokensWithBasicForm() %7B%0A    Map%3CString, String%3E tokenToBasicForm = new HashMap%3CString, String%3E();%0A    try (LanguageServiceClient language = LanguageServiceClient.create()) %7B%0A      Document doc = Document.newBuilder().setContent(content).setType(Type.PLAIN_TEXT).build();%0A      AnalyzeSyntaxRequest request =%0A          AnalyzeSyntaxRequest.newBuilder()%0A              .setDocument(doc)%0A              .setEncodingType(EncodingType.UTF16)%0A              .build();%0A      AnalyzeSyntaxResponse response = language.analyzeSyntax(request);%0A      for (Token token : response.getTokensList()) %7B%0A        tokenToBasicForm.put(token.getText().getContent(), token.getLemma());%0A      %7D%0A    %7D catch (IOException e) %7B%0A      System.err.println(%22IO Exception due to NLP library.%22);%0A    %7D%0A    return tokenToBasicForm
 ;%0A  

Add functionality to helper to find tokens.