package com.google.sps.servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Given a tweet or sentence, the servlet will extract salient terms
 * and store them in a list.
 */
@WebServlet("/keyword")
public class KeywordServlet extends HttpServlet {

  private static final List<String> tweets = new ArrayList<>();
  private static final List<String> terms = Arrays.asList("Black Lives Matter", "COVID-19");

  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    // TODO: Extract saliency.
    String json = jsonBuilder(terms);
    response.getWriter().println(json);
  }

  @Override
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
    final String tweet = request.getParameter("keyword-sentence");
    if (tweet != null && !tweet.equals("")) {
      tweets.add(request.getParameter("keyword-sentence"));
    }
    response.sendRedirect("/index.html");
  }

  /**
   * Generates and returns a JSON usable by Mustache.
   */
  private String jsonBuilder(Collection<String> toJson) {
    List<String> terms = new ArrayList<>();
    for (String term : toJson) {
      terms.add(keyValueJson("term", term));
    }

    // Remove extra comma at the end.
    String keywordPrefix = "{\"keywords\": [";
    return String.format(keywordPrefix + "%s]}", String.join(",", terms));
  }

  /**
   * Given a key-value pair, this function returns "{key: value}".
   */
  private String keyValueJson(String key, String value) {
    return String.format("{\"%s\": \"%s\"}", key, value);
  }
}


@@ -51,24 +51,344 @@
 OException;%0A
+import com.google.cloud.language.v1.Document;%0Aimport com.google.cloud.language.v1.LanguageServiceClient;%0Aimport com.google.cloud.language.v1.AnalyzeEntitiesRequest;%0Aimport com.google.cloud.language.v1.AnalyzeEntitiesResponse;%0Aimport com.google.cloud.language.v1.EncodingType;%0Aimport com.google.cloud.language.v1.Entity;%0A
 import java.
@@ -476,24 +476,72 @@
 .util.List;%0A
+import java.util.Map;%0Aimport java.util.TreeMap;%0A
 import javax
@@ -888,39 +888,32 @@
 et %7B%0A%0A  private 
-static 
 final List%3CStrin
@@ -957,233 +957,988 @@
 ate 
-static final List%3CString%3E terms = Arrays.asList(%22Black Lives Matter%22, %22COVID-19%22);%0A%0A  @Override%0A  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException %7B%0A    // TODO: Extract saliency.
+final TreeMap%3CEntity, Float%3E terms = new TreeMap%3C%3E((term1, term2) -%3E %7B%0A    if (term1.getSalience() %3E term2.getSalience()) %7B%0A      return 1;%0A    %7D else if (term1.getSalience() %3C term2.getSalience()) %7B%0A      return -1;%0A    %7D else %7B%0A      return 0;%0A    %7D%0A  %7D);%0A%0A  @Override%0A  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException %7B%0A    for (String tweet : tweets) %7B%0A      AnalyzeEntitiesResponse entityResponse = analyzeEntity(tweet);%0A      List%3CEntity%3E entities = entityResponse.getEntitiesList();%0A      for (Entity entity : entities) %7B%0A        if (entity.getSalience() == 0) %7B%0A          continue;%0A        %7D%0A        %0A        if (terms.size() %3C 10) %7B%0A          terms.put(entity, entity.getSalience());%0A        %7D else if (!terms.containsKey(entity) && entity.getSalience() %3E terms.get(terms.pollFirstEntry())) %7B%0A          terms.remove(terms.pollFirstEntry());%0A          terms.put(entity, entity.getSalience());%0A        %7D else %7B%7D%0A      %7D%0A    %7D%0A
 %0A   
@@ -2454,25 +2454,25 @@
 der(
-Collection%3CString
+Map%3CEntity, Float
 %3E to
@@ -2532,22 +2532,22 @@
    for (
-String
+Entity
  term : 
@@ -2548,24 +2548,33 @@
 erm : toJson
+.keySet()
 ) %7B%0A      te
@@ -2606,16 +2606,26 @@
 m%22, term
+.getName()
 ));%0A    
@@ -2990,12 +2990,650 @@
 ue);%0A  %7D
+%0A%0A  /**%0A   * @return a response to the analyze-entity request%0A   */ %0A  private AnalyzeEntitiesResponse analyzeEntity(String message) throws IOException %7B%0A    try (LanguageServiceClient language = LanguageServiceClient.create()) %7B%0A      Document doc = Document.newBuilder().setContent(message).setType(Document.Type.PLAIN_TEXT).build();%0A      AnalyzeEntitiesRequest request = AnalyzeEntitiesRequest.newBuilder()%0A          .setDocument(doc)%0A          .setEncodingType(EncodingType.UTF16)%0A          .build();%0A      AnalyzeEntitiesResponse response = language.analyzeEntities(request);%0A      language.close();%0A      return response;%0A    %7D%0A  %7D
 %0A%7D%0A%0A

Add extraction of saliency to servlet