// Copyright 2019 Google LLC
//
// Licensed 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
//
//     https://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 com.google.sps.webcrawler;

import com.google.sps.data.NewsArticle;

/** 
 * Provides a tool for checking the relevancy of content to a candidate.
 */
public class RelevancyChecker {

  // @TODO
  public static boolean isRelevant(NewsArticle newsArticle, String candidateName) {
    return true;
  }
}

@@ -618,16 +618,499 @@
 awler;%0A%0A
+import com.google.cloud.language.v1.AnalyzeEntitiesRequest;%0Aimport com.google.cloud.language.v1.AnalyzeEntitiesResponse;%0Aimport com.google.cloud.language.v1.Document;%0Aimport com.google.cloud.language.v1.Document.Type;%0Aimport com.google.cloud.language.v1.EncodingType;%0Aimport com.google.cloud.language.v1.Entity;%0Aimport com.google.cloud.language.v1.EntityMention;%0Aimport com.google.cloud.language.v1.LanguageServiceClient;%0Aimport com.google.cloud.language.v1.LanguageServiceSettings;%0A
 import c
@@ -1256,17 +1256,16 @@
 ecker %7B%0A
-%0A
   // @TO
@@ -1270,109 +1270,1898 @@
 TODO
-%0A  public static boolean isRelevant(NewsArticle newsArticle, String candidateName) %7B%0A    return true;
+ %5BCalculate a meaningful salience threshold.%5D%0A  private static final double salienceThreshold = 0.5;%0A%0A  /** %0A   * Checks whether the %7B@code newsArticle%7D is relevant to the %7B@code candidateName%7D of%0A   * interest. Defines relevancy as the salience of %7B@code candidateName%7D in the content,%0A   * and defines sufficient relevancy with %7B@salienceThreshold%7D.%0A   */%0A  public static boolean isRelevant(NewsArticle newsArticle, String candidateName) %7B%0A    double totalSalience = 0;%0A    int salienceCount = 0;%0A    for (String content : newsArticle.getContent()) %7B%0A      try %7B%0A        totalSalience += computeSalienceOfName(content, candidateName);%0A        salienceCount++;%0A      %7D catch (Exception e) %7B%0A        continue;%0A      %7D%0A    %7D%0A    double averageSalience = salienceCount != 0 ? totalSalience / salienceCount : 0;%0A    return averageSalience %3E= salienceThreshold ? true : false;%0A  %7D%0A%0A  /** %0A   * Performs entity analysis, and computes the salience score of %7B@code candidateName%7D in the%0A   * %7B@code content%7D. Salience has range %5B0, 1%5D, with higher salience indicating higher%0A   * relevance of %7B@code candidateName%7D to %7B@code content%7D overall.%0A   */%0A  private static double computeSalienceOfName(String content, String candidateName)%0A      throws Exception %7B%0A    try (LanguageServiceClient language = LanguageServiceClient.create()) %7B%0A      Document doc = Document.newBuilder().setContent(content).setType(Type.PLAIN_TEXT).build();%0A      AnalyzeEntitiesRequest request =%0A          AnalyzeEntitiesRequest.newBuilder()%0A              .setDocument(doc)%0A              .setEncodingType(EncodingType.UTF8)%0A              .build();%0A      AnalyzeEntitiesResponse response = language.analyzeEntities(request);%0A      for (Entity entity : response.getEntitiesList()) %7B%0A        if (candidateName.equalsIgnoreCase(entity.getName())) %7B%0A          return entity.getSalience();%0A        %7D%0A      %7D%0A      return 0;%0A    %7D
 %0A  %7D

Implement RelevancyChecker: Compute the salience of the candidate's name in the content using entity analysis with the Natural Language API.