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

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

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

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

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

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

แผนภาพแสดงวิธีการทำงานของการวิเคราะห์ความรู้สึก

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

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

บริการ Apps Script

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

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

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

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

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

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

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

เปิดโปรเจ็กต์ Cloud ใน Google Cloud Console

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

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

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

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

เปิด Google Cloud Natural Language API

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

  • เปิด Google Cloud Natural Language API ในโปรเจ็กต์ Cloud

    เปิด API

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

  1. ในคอนโซล Google Cloud ให้ไปที่เมนู > Google Auth platform > การสร้างแบรนด์

    ไปที่การสร้างแบรนด์

  2. หากกำหนดค่า Google Auth platformแล้ว คุณจะกำหนดค่าการตั้งค่าหน้าจอขอความยินยอม OAuth ต่อไปนี้ได้ในการสร้างแบรนด์ กลุ่มเป้าหมาย และการเข้าถึงข้อมูล หากเห็นข้อความที่ระบุว่าGoogle Auth platform ยังไม่ได้กำหนดค่า ให้คลิกเริ่มต้นใช้งาน
    1. ในส่วนข้อมูลแอป ให้ป้อนชื่อแอปในชื่อแอป
    2. ในอีเมลสนับสนุนสำหรับผู้ใช้ ให้เลือกอีเมลสนับสนุนที่ผู้ใช้สามารถติดต่อคุณได้หากมีข้อสงสัยเกี่ยวกับการยินยอม
    3. คลิกถัดไป
    4. เลือกภายในในส่วนผู้ชม
    5. คลิกถัดไป
    6. ในส่วนข้อมูลติดต่อ ให้ป้อนอีเมลที่คุณต้องการรับการแจ้งเตือนเกี่ยวกับการเปลี่ยนแปลงในโปรเจ็กต์
    7. คลิกถัดไป
    8. ในส่วนเสร็จสิ้น ให้อ่านนโยบายข้อมูลผู้ใช้ของบริการ Google API และหากยอมรับ ให้เลือกฉันยอมรับนโยบายข้อมูลผู้ใช้ของบริการ Google API
    9. คลิกต่อไป
    10. คลิกสร้าง
  3. ในตอนนี้ คุณข้ามการเพิ่มขอบเขตได้ ในอนาคต เมื่อสร้างแอปเพื่อใช้ภายนอกองค์กร Google Workspace คุณจะต้องเปลี่ยนประเภทผู้ใช้เป็นภายนอก จากนั้น เพิ่มขอบเขตการให้สิทธิ์ที่แอปของคุณต้องการ ดูข้อมูลเพิ่มเติมได้ที่คู่มือกำหนดค่าความยินยอม OAuth ฉบับเต็ม

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

  1. ไปที่ Google Cloud Console ตรวจสอบว่าโปรเจ็กต์ที่เปิดใช้การเรียกเก็บเงินเปิดอยู่
  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. เพิ่มข้อมูลข้อความลงในคอลัมน์ id และ comments คุณสามารถใช้รีวิวที่พักตากอากาศตัวอย่างจาก Kaggle หรือใช้ข้อมูลของคุณเองก็ได้ คุณเพิ่มคอลัมน์ได้หากต้องการ แต่สคริปต์ต้องมีข้อมูลในคอลัมน์ id และความคิดเห็น จึงจะทำงานได้สำเร็จ

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

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

  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 Developer Expert

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