เชื่อมต่อกับ API: วิเคราะห์ความรู้สึกของความคิดเห็น

ระดับการเขียนโค้ด: ปานกลาง
ระยะเวลา: 20 นาที
ประเภทโปรเจ็กต์: การทำงานอัตโนมัติที่มีเมนูที่กำหนดเอง

วัตถุประสงค์

  • ทำความเข้าใจสิ่งที่โซลูชันใช้
  • ทำความเข้าใจสิ่งที่บริการ Apps Script ทำภายในโซลูชัน
  • ตั้งค่าสภาพแวดล้อมของคุณ
  • ตั้งค่าสคริปต์
  • เรียกใช้สคริปต์

เกี่ยวกับโซลูชันนี้

คุณจะวิเคราะห์ข้อมูลแบบข้อความจำนวนมากได้ เช่น ความคิดเห็นปลายเปิด โซลูชันนี้ใช้ บริการ UrlFetch ในการเชื่อมต่อกับ Google Cloud Natural Language API ทำการวิเคราะห์เอนทิตีและความเห็นจากภายใน Google ชีต

แผนภาพวิธีการทำงานของการวิเคราะห์ความเห็น

วิธีการทำงาน

สคริปต์จะรวบรวมข้อความจากสเปรดชีตและเชื่อมต่อกับ API ภาษาธรรมชาติของ Google Cloud เพื่อวิเคราะห์เอนทิตีและความเห็นที่แสดงในสตริง ตาราง Pivot จะสรุปคะแนนความพึงพอใจเฉลี่ยของเอนทิตีแต่ละรายการที่กล่าวถึงในข้อมูลข้อความทั้งหมดในแถว

บริการ Apps Script

โซลูชันนี้ใช้บริการต่อไปนี้

  • บริการสเปรดชีต - ส่งข้อมูลข้อความไปยัง Google Cloud Natural Language API และทำเครื่องหมายแต่ละแถวว่า "เสร็จสมบูรณ์" เมื่อวิเคราะห์ความรู้สึกแล้ว
  • บริการ UrlFetch - เชื่อมต่อกับ Google Cloud Natural Language API เพื่อดำเนินการวิเคราะห์เอนทิตีและความเห็นเกี่ยวกับข้อความ

ข้อกำหนดเบื้องต้น

หากต้องการใช้ตัวอย่างนี้ คุณต้องมีข้อกำหนดเบื้องต้นต่อไปนี้

  • บัญชี Google (บัญชี Google Workspace อาจ ต้องได้รับการอนุมัติจากผู้ดูแลระบบ)
  • เว็บเบราว์เซอร์ที่เข้าถึงอินเทอร์เน็ตได้

  • โปรเจ็กต์ Google Cloud ที่มีบัญชีสำหรับการเรียกเก็บเงินที่เชื่อมโยง โปรดดูหัวข้อเปิดใช้การเรียกเก็บเงินสำหรับโปรเจ็กต์

ตั้งค่าสภาพแวดล้อมของคุณ

เปิดโปรเจ็กต์ที่อยู่ในระบบคลาวด์ในคอนโซล Google Cloud

หากยังไม่ได้เปิด ให้เปิดโปรเจ็กต์ที่อยู่ในระบบคลาวด์ที่ต้องการใช้สำหรับตัวอย่างนี้

  1. ในคอนโซล Google Cloud ให้ไปที่หน้าเลือกโปรเจ็กต์

    เลือกโปรเจ็กต์ที่อยู่ในระบบคลาวด์

  2. เลือกโปรเจ็กต์ Google Cloud ที่ต้องการใช้ หรือคลิกสร้างโปรเจ็กต์ แล้วทำตามวิธีการบนหน้าจอ หากสร้างโปรเจ็กต์ Google Cloud คุณอาจต้องเปิดการเรียกเก็บเงินสำหรับโปรเจ็กต์

เปิด Google Cloud Natural Language API

โซลูชันนี้เชื่อมต่อกับ Google Cloud Natural Language API ก่อนใช้ Google APIs คุณต้องเปิดใช้ API ในโปรเจ็กต์ Google Cloud คุณเปิด API ได้มากกว่า 1 รายการในโปรเจ็กต์ Google Cloud เดียว

  • เปิด Google Cloud Natural Language API ในโปรเจ็กต์ที่อยู่ในระบบคลาวด์

    เปิด API

โซลูชันนี้ต้องใช้โปรเจ็กต์ Cloud ที่มีหน้าจอคำยินยอมที่กำหนดค่าไว้ การกำหนดค่าหน้าจอคำยินยอม OAuth จะกำหนดสิ่งที่ Google แสดงต่อผู้ใช้และลงทะเบียนแอปเพื่อให้คุณเผยแพร่แอปได้ในภายหลัง

  1. ในคอนโซล Google Cloud ให้ไปที่เมนู > API และบริการ > หน้าจอขอความยินยอม OAuth

    ไปที่หน้าจอขอความยินยอม OAuth

  2. สำหรับประเภทผู้ใช้ ให้เลือกภายใน แล้วคลิกสร้าง
  3. กรอกแบบฟอร์มการลงทะเบียนแอปให้เสร็จสมบูรณ์ แล้วคลิกบันทึกและดำเนินการต่อ
  4. สำหรับตอนนี้ คุณอาจข้ามการเพิ่มขอบเขต แล้วคลิกบันทึกและต่อไป ในอนาคต เมื่อสร้างแอปเพื่อใช้นอกองค์กร Google Workspace คุณจะต้องเปลี่ยนประเภทผู้ใช้เป็นภายนอก จากนั้นจึงเพิ่มขอบเขตการให้สิทธิ์ที่แอปต้องการ

  5. ดูสรุปการลงทะเบียนแอป หากต้องการเปลี่ยนแปลง ให้คลิกแก้ไข หากการลงทะเบียนแอปถูกต้องแล้ว ให้คลิกกลับไปยังหน้าแดชบอร์ด

รับคีย์ API สำหรับ Google Cloud Natural Language API

  1. ไปที่คอนโซล Google Cloud ตรวจสอบว่าโปรเจ็กต์ที่เปิดใช้การเรียกเก็บเงินเปิดอยู่
  2. ในคอนโซล Google Cloud ให้ไปที่เมนู > API และบริการ > ข้อมูลเข้าสู่ระบบ

    ไปที่ข้อมูลเข้าสู่ระบบ

  3. คลิกสร้างข้อมูลเข้าสู่ระบบ > คีย์ API

  4. จดคีย์ API ไว้ใช้ในภายหลัง

ตั้งค่าสคริปต์

สร้างโปรเจ็กต์ Apps Script

  1. คลิกปุ่มด้านล่างเพื่อทำสำเนาสเปรดชีตตัวอย่างการวิเคราะห์ความเห็นสำหรับความคิดเห็น โครงการ Apps Script สำหรับโซลูชันนี้แนบอยู่กับสเปรดชีต
    ทำสำเนา
  2. คลิกส่วนขยาย > Apps Script
  3. อัปเดตตัวแปรต่อไปนี้ในไฟล์สคริปต์ด้วยคีย์ API ของคุณ
    const myApiKey = 'YOUR_API_KEY'; // Replace with your API key.
  4. คลิกบันทึก ไอคอนบันทึก

เพิ่มข้อมูลข้อความ

  1. กลับไปที่สเปรดชีต
  2. เพิ่มข้อมูลข้อความลงในคอลัมน์รหัสและความคิดเห็น คุณสามารถใช้ตัวอย่างรีวิวที่พักสำหรับวันหยุดจาก Kaggle หรือใช้ข้อมูลของคุณเอง คุณเพิ่มคอลัมน์ได้หากจำเป็น แต่สคริปต์จะต้องมีข้อมูลในคอลัมน์ id และ comments เพื่อให้ทำงานได้

เรียกใช้สคริปต์

  1. ที่ด้านบนของสเปรดชีต ให้คลิกเครื่องมือความเห็น > ทำเครื่องหมายเอนทิตีและความเห็น คุณอาจต้องรีเฟรชหน้าเว็บเพื่อให้เมนูที่กำหนดเองนี้ปรากฏขึ้น
  2. เมื่อได้รับข้อความแจ้ง ให้ให้สิทธิ์สคริปต์ หากหน้าจอความยินยอมของ OAuth แสดงคำเตือน แอปนี้ไม่ได้รับการยืนยัน ให้ดำเนินการต่อโดยเลือกขั้นสูง > ไปที่ {Project Name} (ไม่ปลอดภัย)

  3. คลิกเครื่องมือแสดงความเห็น > ทำเครื่องหมายเอนทิตีและความเห็นอีกครั้ง

  4. เมื่อสคริปต์ดำเนินการเสร็จ ให้เปลี่ยนไปใช้ชีตตาราง Pivot เพื่อดูผลลัพธ์

ตรวจสอบโค้ด

หากต้องการตรวจสอบโค้ด Apps Script สำหรับโซลูชันนี้ ให้คลิกดูซอร์สโค้ดด้านล่าง

ดูซอร์สโค้ด

Code.gs

solutions/automations/feedback-sentiment-analysis/code.js
// To learn how to use this script, refer to the documentation:
// https://developers.google.com/apps-script/samples/automations/feedback-sentiment-analysis

/*
Copyright 2022 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.
*/

// Sets API key for accessing Cloud Natural Language API.
const myApiKey = 'YOUR_API_KEY'; // Replace with your API key.

// Matches column names in Review Data sheet to variables.
let COLUMN_NAME = {
  COMMENTS: 'comments',
  ENTITY: 'entity_sentiment',
  ID: 'id'
};

/**
 * Creates a Demo menu in Google Spreadsheets.
 */
function onOpen() {
  SpreadsheetApp.getUi()
    .createMenu('Sentiment Tools')
    .addItem('Mark entities and sentiment', 'markEntitySentiment')
    .addToUi();
};

/**
* Analyzes entities and sentiment for each comment in  
* Review Data sheet and copies results into the 
* Entity Sentiment Data sheet.
*/
function markEntitySentiment() {
  // Sets variables for "Review Data" sheet
  let ss = SpreadsheetApp.getActiveSpreadsheet();
  let dataSheet = ss.getSheetByName('Review Data');
  let rows = dataSheet.getDataRange();
  let numRows = rows.getNumRows();
  let values = rows.getValues();
  let headerRow = values[0];

  // Checks to see if "Entity Sentiment Data" sheet is present, and
  // if not, creates a new sheet and sets the header row.
  let entitySheet = ss.getSheetByName('Entity Sentiment Data');
  if (entitySheet == null) {
   ss.insertSheet('Entity Sentiment Data');
   let entitySheet = ss.getSheetByName('Entity Sentiment Data');
   let esHeaderRange = entitySheet.getRange(1,1,1,6);
   let esHeader = [['Review ID','Entity','Salience','Sentiment Score',
                    'Sentiment Magnitude','Number of mentions']];
   esHeaderRange.setValues(esHeader);
  };

  // Finds the column index for comments, language_detected, 
  // and comments_english columns.
  let textColumnIdx = headerRow.indexOf(COLUMN_NAME.COMMENTS);
  let entityColumnIdx = headerRow.indexOf(COLUMN_NAME.ENTITY);
  let idColumnIdx = headerRow.indexOf(COLUMN_NAME.ID);
  if (entityColumnIdx == -1) {
    Browser.msgBox("Error: Could not find the column named " + COLUMN_NAME.ENTITY + 
                   ". Please create an empty column with header \"entity_sentiment\" on the Review Data tab.");
    return; // bail
  };

  ss.toast("Analyzing entities and sentiment...");
  for (let i = 0; i < numRows; ++i) {
    let value = values[i];
    let commentEnCellVal = value[textColumnIdx];
    let entityCellVal = value[entityColumnIdx];
    let reviewId = value[idColumnIdx];

    // Calls retrieveEntitySentiment function for each row that has a comment 
    // and also an empty entity_sentiment cell value.
    if(commentEnCellVal && !entityCellVal) {
        let nlData = retrieveEntitySentiment(commentEnCellVal);
        // Pastes each entity and sentiment score into Entity Sentiment Data sheet.
        let newValues = []
        for (let entity in nlData.entities) {
          entity = nlData.entities [entity];
          let row = [reviewId, entity.name, entity.salience, entity.sentiment.score, 
                     entity.sentiment.magnitude, entity.mentions.length
                    ];
          newValues.push(row);
        }
      if(newValues.length) {
        entitySheet.getRange(entitySheet.getLastRow() + 1, 1, newValues.length, newValues[0].length).setValues(newValues);
      }
        // Pastes "complete" into entity_sentiment column to denote completion of NL API call.
        dataSheet.getRange(i+1, entityColumnIdx+1).setValue("complete");
     }
   }
};

/**
 * Calls the Cloud Natural Language API with a string of text to analyze
 * entities and sentiment present in the string.
 * @param {String} the string for entity sentiment analysis
 * @return {Object} the entities and related sentiment present in the string
 */
function retrieveEntitySentiment (line) {
  let apiKey = myApiKey;
  let apiEndpoint = 'https://language.googleapis.com/v1/documents:analyzeEntitySentiment?key=' + apiKey;
  // Creates a JSON request, with text string, language, type and encoding
  let nlData = {
    document: {
      language: 'en-us',
      type: 'PLAIN_TEXT',
      content: line
    },
    encodingType: 'UTF8'
  };
  // Packages all of the options and the data together for the API call.
  let nlOptions = {
    method : 'post',
    contentType: 'application/json',  
    payload : JSON.stringify(nlData)
  };
  // Makes the API call.
  let response = UrlFetchApp.fetch(apiEndpoint, nlOptions);
  return JSON.parse(response);
};

ผู้ร่วมให้ข้อมูล

ตัวอย่างนี้ดูแลโดย Google ด้วยความช่วยเหลือจากผู้เชี่ยวชาญด้านนักพัฒนาซอฟต์แวร์ของ Google

ขั้นตอนถัดไป