Sentiment analysis with AWS machine learning

weerachit wong
5 min readDec 29, 2020
How do you know he is thinking or feeling with the product on hand?

ทีมา และความสำคัญ

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

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

การนำข้อมูลที่เป็นข้อความมาใช้วิเคราะห์เพื่อหา insight ของลูกค้า เพื่อนำมาใช้ให้เกิดประโยชน์ และประกอบการรตัดสินใจในการดำเนินการทางธุรกิจ เช่น ข้อความ feedback จากลูกค้า ,การreview สินค้า ในระบบ online หรือ offline ,review application , Survey เป็นต้น

Sentiment Analysis คือ การวิเคราะห์ข้อความเพื่อหาถึงความความเชื่อมั่นหรือ ความรู้สึก ของผู้เขียนข้อความนั้น โดยแนวทางที่มีอยู่ในการวิเคราะห์ความเชื่อมั่น สามารถแบ่งออกเป็นสามประเภทหลัก ๆ ได้แก่

1เทคนิคที่ใช้ความรู้ 2.วิธีการทางสถิต 3.แนวทางแบบผสมผสาน

ในปัจจุบันมีการนำ machine learning ใช้ในการวิเคราะห์ข้อความ ซึ่งสำหรับผู้ที่ต้องการใช้งานต้องศึกษา machine learning เพื่อทำการสร้าง model ในปัจจุบัน AWS มีเครื่องมือสำเร็จรูปที่ง่าย สะดวก และรวดเร็ว ต่อการใช้งานโดยไม่ต้องเสียเวลาในการสร้าง model machine learning และการทำ coding ทั้งในการฝึกเรียนรู้ บุคคลกร หรือ ทำการจ้างผู้เชี่ยวชาญมาสร้างระบบ ทาง AWS มี service ที่เรียกว่า Perform sentiment analysis with Amazon Aurora ML integration

เครื่องมือที่ใช้Perform sentiment analysis

Amazon Aurora เป็นRDMSที่สามารถเลือกใช้ได้กับ MySQL และ PostgreSQL ที่สร้างขึ้นสำหรับระบบคลาวด์ซึ่งรวมประสิทธิภาพและความพร้อมใช้งานของฐานข้อมูลองค์กรแบบดั้งเดิมเข้ากับความเรียบง่ายและ เป็นฐานข้อมูลโอเพนซอร์ส

Amazon Aurora machine learning คุณสามารถเปิดระบบ ML prediction ให้กับแอปพลิเคชันผ่านภาษาการเขียนโปรแกรม SQL ดังนั้นคุณจึงไม่จำเป็นต้องเรียนรู้เครื่องมือแยกต่างหากหรือมีประสบการณ์ด้าน machine learning การสั่งงานผ่านภาษาการเขียนโปรแกรม SQL คือการเรียกใช้ Amazon SageMaker หรือ Amazon Comprehend โดยไม่ต้องเข้าไปใช้งานโดยตรง Aurora machine learning เหมาะกับงานประเภท low latency

Amazon sageMaker คือ ผลิตภัณฑ์ machine learning ของทาง Amazon

Amazon Comprehend คือ machine learning ประเภท natural language processing (NLP) service ใช้เพื่อค้นหาข้อมูลเชิงลึกและความสัมพันธ์ในข้อความ

ก่อนที่จะเริ่มใช้งาน Sentiment analysis AWS ทาง AWS มีระบบ Well-Architected Framework เพื่อพิจารณาการออกแบบระบบ cloudให้มีการใช้งานอย่างคุ้มค่า และมีประสิทธิภาพสูงสุดให้เหมาะสมกับงาน Sentiment analysis

พิจารณา Well-Architected Framework ดังนี้

1 Operational Excellence (การปฎิบัติงานอย่างมีประสิทธิภาพ)

Sentiment analysis ของ AWS มีAmazon Comprehend เป็น service ที่สามารถเรียกใช้งานได้เลย,ง่าย และสะดวก โดยทาง AWSมีการ update serviceอย่างต่อเนื่อง หากเปรียบเทียบกับ การที่ต้องดำเนินการเพื่อเพิ่มประประสิทธิภาพของmodel และระบบถือว่าคุ้มค่ากว่า

2. Security (ความปลอดภัย)

Sentiment analysis ของ AWS นำ IAM (Identity and Access Management)เพื่อกำหนดการเข้าถึง RDS ให้เป็นเพียง read-only ในส่วนเรื่องระบบเชื่อมต่อ cloud AWS กับ local database มีการกำหนด IP address ที่ in bound ruleไว้ชัดเจน

3. Performance Efficiency (ประสิทธิภาพการทำงาน)

Sentiment analysis ของ AWS เป็น Democratize advanced technologies คือมีการพัฒนาให้ง่ายต่อการใช้งาน

4. Cost Optimization (การปรับค่าใช้จ่ายให้เหมาะสม)

Sentiment analysis ของ AWS เป็น Adopt a consumption model จ่ายเฉพาะที่ใช้งาน โดยระบบจะคิดค่าใช้จ่ายเป็น units ต่อการใช้งาน เราสามารถปิดระบบเมื่อไม่ต้องการใช้งานได้ สามารถหาข้อมูลได้จาก

4. Reliability (ความน่าเชื่อถือ)

Sentiment analysis ของ AWS เป็นระบบว่าสามารถจัดการเรื่องการเปลี่ยนแปลงของระบบได้ เนื่องจากเป็น Cloud ทีความเป็นไปได้น้อยที่ระบบจะล่ม หรือ ระบบ fail จริงพร้อมกับมีการใช้ S3 ทำการsnapshot ไว้สำหรับ back upระบบให้กลับมาใช้งานได้

https://aws.amazon.com/comprehend/pricing/

หากต้องการใช้งานสามารถทำตามขั้นตอนใน link โดยขั้นตอนมีดังนี้

  1. Create the IAM roles required to allow Amazon Aurora access to Amazon Comprehend and Amazon S3.
  2. Create the Amazon Aurora PostgreSQL database instance
  3. Download and install a PostgreSQL client
  4. Connect to the Aurora DB instance with the PostgreSQL client
  5. Install AWS ML and S3 extensions so you can query the database and analyze the sentiment of a sample table and customer reviews dataset

https://aws.amazon.com/getting-started/hands-on/sentiment-analysis-amazon-aurora-ml-integration/?trk=gs_card

  1. Create the IAM roles ที่ทำงานร่วมกับ RDS (relational database service) กำหนดขึ้นมาIAM 2 role คือ

1.1TutorialAuroraS3Role=> AmazonS3ReadOnlyAccess

1.2 TutorialAuroraComprehendRole =>ComprehendReadOnly

2. Create the Amazon Aurora PostgreSQL database instance คือการสร้าง relational database Postgres บนระบบของ AWS และ Setting เพื่อการเชื่อมต่อ กับ local Database

3. Download and install a PostgreSQL client เป็นการติดตั้ง local database เพื่อเตรียมการเชื่อมต่อ database ของผู้ใช้กับ AWS cloud service

4.Connect to the Aurora DB instance with the PostgreSQL client

5. Install AWS ML and S3 extensions so you can query the database and analyze the sentiment of a sample table and customer reviews dataset

ในขั้นตอนนี ้เราจะทำการสร้างDatabase เพื่อเชื่อมต่อ และทดสอบการใช้งานระบบ Sentiment analysis โดยติดตั้งAmazon ML services extension

CREATE EXTENSION IF NOT EXISTS aws_ml CASCADE; 
CREATE EXTENSION IF NOT EXISTS aws_ml CASCADE;

ในการทดสอบการใช้งานมีด้วยกัน 2 ตัวอย่าง คือ table comments และ table review_simple

ตัวอย่างที่1 table comments:ทำการสร้าง table comments และ insert ข้อมูลลง table comments

CREATE TABLE IF NOT EXISTS comments (comment_id serial PRIMARY KEY,comment_text VARCHAR(255) NOT NULL);INSERT INTO comments (comment_text)VALUES (‘I do not like how this was implemented.’);INSERT INTO comments (comment_text)VALUES (‘ดีมาก.’);INSERT INTO comments (comment_text)VALUES (‘แย่สุดสุด’);
table comments

นำ AWScomprehend มาทำการวิเคราห์ table comments และสร้าง columns sentiment คือ บอกผลลัพธ์ว่าเป็น positive หรือ negative และ columns confidence คือ ค่าความเชื่อมั่นของผลวิเคราะห์ หากเข้าใกล้ 1 แสดงว่า sentiment analysis มั่นใจว่าสิ่งที่วิเคราะห์ถูกต้อง detect_sentiment จะรองรับ language code เพียง 12 languagesเท่านั้น คือ ar (Arabic), hi(Hindi), ko(Korean),zh-TW (Chinese (T)),ja(japenese) ,zh (Chinese) ,de (German) ,pt(Portuguese),en(english),it (Italian),fr(French),es (Spanish)ไม่ได้รองรับภาษาไทย

reference lang. code :http://www.lingoes.net/en/translator/langcode.htm

SELECT * FROM comments, aws_comprehend.detect_sentiment(comments.comment_text, ‘en’) as s; 

จากการทดลองระบบได้ทำการวิเคราะห์ columns comment_text แสดง sentiment เป็นแบบ POSITIVE หรือ NEGATIVE และได้ทำการ insert ภาษาไทยเข้าไปเพื่อทดสอบ พบว่า สามารถวิเคราะห์ได้ จึงต้องศึกษาเพิ่มเติมว่าสามารถใช้งานภาษาไทยได้จริงหรือไม่

result applied sentiment analysis at table comments

ตัวอย่างที่2 table review_simple.

เพื่อให้สามารถ load dataจาก Amzon S3 ไปที่ Aurora DB instance

CREATE EXTENSION IF NOT EXISTS aws_s3 CASCADE; ติดตั้งAmazon S3 service extension 

สร้าง Table review_simple

create table review_simple ( marketplace char(2), customer_id varchar(20), review_id varchar(20) primary key, product_id varchar(20), product_parent varchar(20), product_title text, product_category varchar(20), star_rating int, helpful_votes int, total_votes int, vine char, verified_purchase char, review_headline varchar(255), review_body text, review_date date, scored_sentiment varchar(20), scored_confidence float4 )

Load ข้อมูล จาก Aurora PostgreSQL มาที่ local database table review_simple

select aws_s3.table_import_from_s3( ‘review_simple’, ‘marketplace, customer_id,review_id,product_id, product_parent, product_title, product_category, star_rating, helpful_votes, total_votes, vine, verified_purchase,review_headline, review_body, review_date’, ‘(FORMAT CSV, HEADER true, DELIMITER E’’\t’’, QUOTE ‘’|’’)’, ‘amazon-reviews-pds’, ‘tsv/sample_us.tsv’, ‘us-east-1’ )

ทำการวิเคราะห์ข้อมูล SQL languages ได้ทำการวิเคราะห์ columns review_body
ใน SQL language จะมีกำหนดเงื่อนไขว่าถ้าข้อคววามไหนวิเคราะห์ไปแล้ว จะไม่วิเคราะห์ใหม่ เพื่อลดค่าใช้จ่ายจากการวิเคราะห์ข้อชุดเดิม
<src.scored_sentiment is null>

update review_simple set scored_sentiment = s.sentiment, scored_confidence = s.confidence from review_simple as src, aws_comprehend.detect_sentiment( src.review_body, ‘en’) as s where src.review_id = review_simple.review_id and src.scored_sentiment is null
result applied sentiment analysis at table review_simple

จากผลการวิเคราะห์สามารถสรุปโดยใช้ SQL language แบ่ง sentiment ได้ 4 กลุ่ม

MIXED <ข้อความ positive และ negative> ,POSITIVE ,NEGATIVE และ NEUTRAL

select scored_sentiment,count(*) as nReviews from review_simple group by scored_sentiment

ทำการขัดกรองข้อมูลด้วยการ SQL language ที่มี confidence >0.9

select scored_sentiment,count(*) as nReviews from review_simple where scored_confidence > .9 group by scored_sentiment

หากพิจารณาจาก table สามารถหา insight จากข้อมูลได้ ยกตัวอย่างเช่น สามารถรู้ได้ว่า สินค้าชิ้นไหนได้รับความนิยม และลูกค้าชื่นชอบ โดยพิจารณาได้จาก product_id คู่กับ scored_sentiment เพื่อตัดสินใจเพิ่มปริมาณสินค้า และหากสินค้าไหนม ีscored_sentiment negativeมาก เราสามารถหาinsight ของสินค้านั้น เพื่อปรับปรุง สินค้านั้นให้ดียิ่งขึ้น หรือลดการนำสินค้านั้นมาจำหน่วย ทำให้ธุรกิจ มีกำไรจากการขายสินค้าที่ลูกค้าชี่นชอบ ยังช่วยในการบริหาร stockสินค้าในการจัดเก็บได้

ข้อดีของการใช้ Sentiment analysis ของAWS

  • ไม่รู้จำเป็นต้องรู้จัก NLP ก็สามารถใช้งาน ผ่าน SQL languageได้
  • ไม่ต้องพัฒนา หรือสรรหาบุคลากร เพื่อให้มาดูแลงานด้าน sentiment analysis

ข้อเสีย ของการใช้ Sentiment analysis ของ AWS

  • ต้องมีความรู้พื้นฐานการใช้งาน cloud AWS เพื่อติดตั้ง หรือ หยุดระบบ
  • ค่าใช้จ่ายอื่นในการ snapshot S3 ต้องมีความรู้ในการตั้งค่าที่เหมาะสม
  • ไม่รองรับภาษไทย

Reference :

https://www.lexalytics.com/technology/sentiment-analysis

https://en.wikipedia.org/wiki/Sentiment_analysis

https://www.youtube.com/watch?v=qDRS_vGFjsw&ab_channel=AmazonWebServices

--

--