Melakukan analisis semantik dengan fungsi AI terkelola

Tutorial ini menunjukkan cara menggunakan fungsi AI terkelola BigQuery ML untuk melakukan analisis semantik pada masukan pelanggan.

Tujuan

Dalam tutorial ini, Anda telah:

  • Membuat set data dan memuat data sentimen ke dalam tabel
  • Gunakan fungsi AI berikut untuk melakukan analisis semantik:
    • AI.IF: untuk memfilter data Anda dengan kondisi bahasa alami
    • AI.SCORE: untuk memberi rating input berdasarkan sentimen
    • AI.CLASSIFY: untuk mengklasifikasikan input ke dalam kategori yang ditentukan pengguna

Biaya

Tutorial ini menggunakan komponen Google Cloudyang dapat ditagih, termasuk:

  • BigQuery
  • BigQuery ML

Untuk informasi selengkapnya tentang biaya BigQuery, lihat halaman harga BigQuery.

Untuk informasi selengkapnya tentang biaya BigQuery ML, lihat harga BigQuery ML.

Sebelum memulai

  1. Login ke akun Google Cloud Anda. Jika Anda baru menggunakan Google Cloud, buat akun untuk mengevaluasi performa produk kami dalam skenario dunia nyata. Pelanggan baru juga mendapatkan kredit gratis senilai $300 untuk menjalankan, menguji, dan men-deploy workload.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.

  4. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  5. If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.

  6. Aktifkan BigQuery API.

    Peran yang diperlukan untuk mengaktifkan API

    Untuk mengaktifkan API, Anda memerlukan peran IAM Service Usage Admin (roles/serviceusage.serviceUsageAdmin), yang berisi izin serviceusage.services.enable. Pelajari cara memberikan peran.

    Mengaktifkan API

    Untuk project baru, BigQuery API diaktifkan secara otomatis.

  7. Opsional: Aktifkan penagihan untuk project. Jika Anda tidak ingin mengaktifkan penagihan atau memberikan kartu kredit, langkah-langkah dalam dokumen ini tetap berfungsi. BigQuery menyediakan sandbox untuk melakukan langkah-langkah tersebut. Untuk mengetahui informasi selengkapnya, lihat Mengaktifkan sandbox BigQuery.

Peran yang diperlukan

Untuk mendapatkan izin yang Anda perlukan untuk menggunakan fungsi AI, minta administrator Anda untuk memberi Anda peran IAM berikut pada project:

  • Menjalankan tugas kueri dan tugas pemuatan: BigQuery Job User (roles/bigquery.jobUser)
  • Buat set data, buat tabel, muat data ke dalam tabel, dan kueri tabel: Editor Data BigQuery (roles/bigquery.dataEditor)

Untuk mengetahui informasi selengkapnya tentang pemberian peran, lihat Mengelola akses ke project, folder, dan organisasi.

Anda mungkin juga bisa mendapatkan izin yang diperlukan melalui peran khusus atau peran bawaan lainnya.

Membuat data sampel

Untuk membuat set data bernama my_dataset untuk tutorial ini, jalankan kueri berikut.

CREATE SCHEMA my_dataset OPTIONS (location = 'LOCATION');

Selanjutnya, buat tabel bernama customer_feedback yang berisi contoh ulasan pelanggan untuk sebuah perangkat:

CREATE TABLE my_dataset.customer_feedback AS (
  SELECT
    *
  FROM
    UNNEST( [STRUCT<review_id INT64, review_text STRING> 
      (1, "The battery life is incredible, and the screen is gorgeous! Best phone I've ever had. Totally worth the price."),
      (2, "Customer support was a nightmare. It took three weeks for my order to arrive, and when it did, the box was damaged. Very frustrating!"),
      (3, "The product does exactly what it says on the box. No complaints, but not exciting either."),
      (4, "I'm so happy with this purchase! It arrived early and exceeded all my expectations. The quality is top-notch, although the setup was a bit tricky."),
      (5, "The price is a bit too high for what you get. The material feels cheap and I'm worried it won't last. Service was okay."),
      (6, "Absolutely furious! The item arrived broken, and getting a refund is proving impossible. I will never buy from them again."),
      (7, "This new feature for account access is confusing. I can't find where to update my profile. Please fix this bug!"),
      (8, "The shipping was delayed, but the support team was very helpful and kept me informed. The product itself is great, especially for the price.") 
      ])
);

Mengategorikan sentimen keseluruhan

Ekstraksi sentimen keseluruhan yang dinyatakan dalam teks dapat membantu mendukung kasus penggunaan seperti berikut:

  • Ukur kepuasan pelanggan dari ulasan.
  • Pantau persepsi merek di media sosial.
  • Prioritaskan tiket dukungan berdasarkan seberapa kesal pengguna.

Kueri berikut menunjukkan cara menggunakan fungsi AI.CLASSIFY untuk mengklasifikasikan ulasan dari tabel customer_feedback sebagai positif, negatif, atau netral:

SELECT
  review_id,
  review_text,
  AI.CLASSIFY(
    review_text,
    categories => ['positive', 'negative', 'neutral']) AS sentiment
FROM
  my_dataset.customer_feedback;

Hasilnya akan terlihat mirip dengan berikut ini:

+-----------+------------------------------------------+-----------+
| review_id | review_text                              | sentiment |
+-----------+------------------------------------------+-----------+
| 7         | This new feature for account access is   | negative  |
|           | confusing. I can't find where to update  |           |
|           | my profile. Please fix this bug!         |           |
+-----------+------------------------------------------+-----------+
| 4         | "I'm so happy with this purchase! It     | positive  |
|           | arrived early and exceeded all my        |           |
|           | expectations. The quality is top-notch,  |           |
|           | although the setup was a bit tricky."    |           |
+-----------+------------------------------------------+-----------+
| 2         | "Customer support was a nightmare. It    | negative  |
|           | took three weeks for my order to         |           |
|           | arrive, and when it did, the box was     |           |
|           | damaged. Very frustrating!"              |           |
+-----------+------------------------------------------+-----------+
| 1         | "The battery life is incredible, and     | positive  |
|           | the screen is gorgeous! Best phone I've  |           |
|           | ever had. Totally worth the price."      |           |
+-----------+------------------------------------------+-----------+
| 8         | "The shipping was delayed, but the       | positive  |
|           | support team was very helpful and kept   |           |
|           | me informed. The product itself is       |           |
|           | great, especially for the price."        |           |
+-----------+------------------------------------------+-----------+
| 5         | The price is a bit too high for what     | negative  |
|           | you get. The material feels cheap and    |           |
|           | I'm worried it won't last. Service was   |           |
|           | okay.                                    |           |
+-----------+------------------------------------------+-----------+
| 3         | "The product does exactly what it says   | neutral   |
|           | on the box. No complaints, but not       |           |
|           | exciting either."                        |           |
+-----------+------------------------------------------+-----------+
| 6         | "Absolutely furious! The item arrived    | negative  |
|           | broken, and getting a refund is proving  |           |
|           | impossible. I will never buy from them   |           |
|           | again."                                  |           |
+-----------+------------------------------------------+-----------+

Menganalisis sentimen berbasis aspek

Jika sentimen keseluruhan seperti positif atau negatif tidak cukup untuk kasus penggunaan Anda, Anda dapat menganalisis aspek tertentu dari makna teks. Misalnya, Anda mungkin ingin memahami sikap pengguna terhadap kualitas produk, tanpa mempertimbangkan pendapat mereka tentang harganya. Anda bahkan dapat meminta nilai kustom untuk menunjukkan bahwa aspek tertentu tidak berlaku.

Contoh berikut menunjukkan cara menggunakan fungsi AI.SCORE untuk memberi peringkat sentimen pengguna dari 1 hingga 10 berdasarkan seberapa menguntungkan setiap ulasan dalam tabel customer_feedback terhadap harga, layanan pelanggan, dan kualitas. Fungsi ini menampilkan nilai kustom -1 jika aspek tidak disebutkan dalam ulasan sehingga Anda dapat memfilternya nanti.

SELECT
  review_id,
  review_text,
  AI.SCORE(
    ("Score 0.0 to 10 on positive sentiment about PRICE for review: ", review_text,
    "If price is not mentioned, return -1.0")) AS price_score,
  AI.SCORE(
    ("Score 0.0 to 10 on positive sentiment about CUSTOMER SERVICE for review: ", review_text,
    "If customer service is not mentioned, return -1.0")) AS service_score,
  AI.SCORE(
    ("Score 0.0 to 10 on positive sentiment about QUALITY for review: ", review_text,
    "If quality is not mentioned, return -1.0")) AS quality_score
FROM
  my_dataset.customer_feedback
LIMIT 3;

Hasilnya akan terlihat mirip dengan berikut ini:

+-----------+------------------------------------------+--------------+---------------+---------------+
| review_id | review_text                              |  price_score | service_score | quality_score |
+-----------+------------------------------------------+--------------+---------------+---------------+
| 4         | "I'm so happy with this purchase! It     | -1.0         | -1.0          | 9.5           |
|           | arrived early and exceeded all my        |              |               |               |
|           | expectations. The quality is top-notch,  |              |               |               |
|           | although the setup was a bit tricky."    |              |               |               |
+-----------+------------------------------------------+--------------+---------------+---------------+
| 8         | "The shipping was delayed, but the       |  9.0         |  8.5          | 9.0           |
|           | support team was very helpful and kept   |              |               |               |
|           | me informed. The product itself is       |              |               |               |
|           | great, especially for the price."        |              |               |               |
+-----------+------------------------------------------+--------------+---------------+---------------+
| 6         | "Absolutely furious! The item arrived    | -1.0         |  1.0          | 0.0           |
|           | broken, and getting a refund is proving  |              |               |               |
|           | impossible. I will never buy from them   |              |               |               |
|           | again."                                  |              |               |               |
+-----------+------------------------------------------+--------------+---------------+---------------+

Mendeteksi emosi

Selain sentimen positif atau negatif, Anda dapat mengklasifikasikan teks berdasarkan emosi tertentu yang Anda pilih. Hal ini berguna jika Anda ingin lebih memahami respons pengguna, atau menandai masukan yang sangat emosional untuk ditinjau.

SELECT
  review_id,
  review_text,
  AI.CLASSIFY(
    review_text,
    categories => ['joy', 'anger', 'sadness', 'surprise', 'fear', 'disgust', 'neutral', 'other']
  ) AS emotion
FROM
  my_dataset.customer_feedback;

Hasilnya akan terlihat mirip dengan berikut ini:

+-----------+------------------------------------------+---------+
| review_id | review_text                              | emotion |
+-----------+------------------------------------------+---------+
| 2         | "Customer support was a nightmare. It    | anger   |
|           | took three weeks for my order to         |         |
|           | arrive, and when it did, the box was     |         |
|           | damaged. Very frustrating!"              |         |
+-----------+------------------------------------------+---------+
| 7         | This new feature for account access is   | anger   |
|           | confusing. I can't find where to update  |         |
|           | my profile. Please fix this bug!         |         |
+-----------+------------------------------------------+---------+
| 4         | "I'm so happy with this purchase! It     | joy     |
|           | arrived early and exceeded all my        |         |
|           | expectations. The quality is top-notch,  |         |
|           | although the setup was a bit tricky."    |         |
+-----------+------------------------------------------+---------+
| 1         | "The battery life is incredible, and     | joy     |
|           | the screen is gorgeous! Best phone I've  |         |
|           | ever had. Totally worth the price."      |         |
+-----------+------------------------------------------+---------+
| 8         | "The shipping was delayed, but the       | joy     |
|           | support team was very helpful and kept   |         |
|           | me informed. The product itself is       |         |
|           | great, especially for the price."        |         |
+-----------+------------------------------------------+---------+
| 5         | The price is a bit too high for what     | sadness |
|           | you get. The material feels cheap and    |         |
|           | I'm worried it won't last. Service was   |         |
|           | okay.                                    |         |
+-----------+------------------------------------------+---------+
| 3         | "The product does exactly what it says   | neutral |
|           | on the box. No complaints, but not       |         |
|           | exciting either."                        |         |
+-----------+------------------------------------------+---------+
| 6         | "Absolutely furious! The item arrived    | anger   |
|           | broken, and getting a refund is proving  |         |
|           | impossible. I will never buy from them   |         |
|           | again."                                  |         |
+-----------+------------------------------------------+---------+

Mengategorikan ulasan menurut topik

Anda dapat menggunakan fungsi AI.CLASSIFY untuk mengelompokkan ulasan ke dalam topik yang telah ditentukan. Misalnya, Anda dapat melakukan hal berikut:

  • Temukan tema umum dalam masukan pelanggan.
  • Mengatur dokumen berdasarkan pokok bahasan.
  • Merutekan tiket dukungan berdasarkan topik.

Contoh berikut menunjukkan cara mengklasifikasikan masukan pelanggan ke dalam berbagai jenis seperti masalah penagihan atau akses akun, lalu menghitung jumlah ulasan yang termasuk dalam setiap kategori:

SELECT
  AI.CLASSIFY(
    review_text,
    categories => ['Billing Issue', 'Account Access',
                   'Product Bug', 'Feature Request',
                   'Shipping Delay', 'Other']) AS topic,
    COUNT(*) AS number_of_reviews,
FROM
  my_dataset.customer_feedback
GROUP BY topic
ORDER BY number_of_reviews DESC;

Hasilnya akan terlihat mirip dengan berikut ini:

+----------------+-------------------+
| topic          | number_of_reviews |
+----------------+-------------------+
| Other          | 5                 |
| Shipping Delay | 2                 |
| Product Bug    | 1                 |
+----------------+-------------------+

Mengidentifikasi ulasan yang mirip secara semantik

Anda dapat menggunakan fungsi AI.SCORE untuk menilai seberapa mirip dua teks secara semantik dengan memintanya untuk menilai kesamaan makna. Cara ini dapat membantu Anda melakukan tugas seperti berikut:

  • Menemukan entri duplikat atau hampir duplikat.
  • Kelompokkan masukan yang serupa.
  • Mendukung aplikasi penelusuran semantik.

Kueri berikut menemukan ulasan yang membahas kesulitan dalam menyiapkan produk:

SELECT
  review_id,
  review_text,
  AI.SCORE(
    (
      """How similar is the review to the concept of 'difficulty in setting up the product'?
         A higher score indicates more similarity. Review: """,
      review_text)) AS setup_difficulty
FROM my_dataset.customer_feedback
ORDER BY setup_difficulty DESC
LIMIT 2;

Hasilnya akan terlihat mirip dengan berikut ini:

+-----------+------------------------------------------+------------------+
| review_id | review_text                              | setup_difficulty |
+-----------+------------------------------------------+------------------+
| 4         | "I'm so happy with this purchase! It     | 3                |
|           | arrived early and exceeded all my        |                  |
|           | expectations. The quality is top-notch,  |                  |
|           | although the setup was a bit tricky."    |                  |
+-----------+------------------------------------------+------------------+
| 7         | This new feature for account access is   | 1                |
|           | confusing. I can't find where to update  |                  |
|           | my profile. Please fix this bug!         |                  |
+-----------+------------------------------------------+------------------+

Anda juga dapat menggunakan fungsi AI.IF untuk menemukan ulasan yang terkait dengan teks:

SELECT
  review_id,
  review_text
FROM my_dataset.customer_feedback
WHERE
  AI.IF(
    (
      "Does this review discuss difficulty setting up the product? Review: ",
      review_text));

Menggabungkan fungsi

Sebaiknya gabungkan fungsi ini dalam satu kueri. Misalnya, kueri berikut memfilter ulasan untuk sentimen negatif terlebih dahulu, lalu mengklasifikasikannya berdasarkan jenis kekecewaan:

SELECT
  review_id,
  review_text,
  AI.CLASSIFY(
    review_text,
    categories => [
      'Poor Quality', 'Bad Customer Service', 'High Price', 'Other Negative']) AS negative_topic
FROM my_dataset.customer_feedback
WHERE
  AI.IF(
    ("Does this review express a negative sentiment? Review: ", review_text));

Membuat UDF perintah yang dapat digunakan kembali

Agar kueri Anda tetap mudah dibaca, Anda dapat menggunakan kembali logika perintah dengan membuat fungsi yang ditentukan pengguna. Kueri berikut membuat fungsi untuk mendeteksi sentimen negatif dengan memanggil AI.IF dengan perintah kustom. Kemudian, fungsi tersebut memanggil fungsi itu untuk memfilter berdasarkan ulasan negatif.

CREATE OR REPLACE FUNCTION my_dataset.is_negative_sentiment(review_text STRING)
RETURNS BOOL
AS (
    AI.IF(
      ("Does this review express a negative sentiment? Review: ", review_text))
);

SELECT
  review_id,
  review_text
FROM my_dataset.customer_feedback
WHERE my_dataset.is_negative_sentiment(review_text);

Pembersihan

Agar tidak dikenai biaya, Anda dapat menghapus project yang berisi resource yang Anda buat, atau menyimpan project dan menghapus setiap resource.

Menghapus project Anda

Untuk menghapus project:

  1. Di Konsol Google Cloud , buka halaman Manage resources.

    Buka Kelola resource

  2. Pada daftar project, pilih project yang ingin Anda hapus, lalu klik Delete.
  3. Pada dialog, ketik project ID, lalu klik Shut down untuk menghapus project.

Menghapus set data

Untuk menghapus set data dan semua resource yang ada di dalamnya, termasuk semua tabel dan fungsi, jalankan kueri berikut:

DROP SCHEMA my_dataset CASCADE;