iT邦幫忙

0

拿 ml5 來練習 如何載入別人的情緒給自己的電腦 (四)

介紹

首先介紹什麼是載入別人的情緒給自己的電腦
有個人已經將自己讀文章時,所產生的情緒,讓機器來學習,並產生一個已經訓練好的 model,
我們只要透過 ml5,就可以很簡單地,拿一些現在的文章,測試以他當時的情緒,會有怎樣的表現。(https://examples.ml5js.org/p5js/sentiment/sentiment_interactive/)

備料

接著備料,

  1. 在 hello-ml5 裡新增兩個檔案,一檔名為 sentiment.html,另一檔名為 sketch_sentiment.js,在 sentiment.html 與 sketch_sentiment.js 分別輸入以下程式碼。

sentiment.html 的程式碼如下—

<html>
  <head>
    <title>Sentiment Analysis Demo</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.9.0/p5.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.9.0/addons/p5.dom.min.js"></script>
    <script src="https://unpkg.com/ml5@latest/dist/ml5.min.js" type="text/javascript"></script>
  </head>
  <body>
    <h1>Sentiment Analysis Demo</h1>
    <script src="sketch_sentiment.js"></script>
  </body>
  </html>

sketch_sentiment.js 的程式碼如下—

let sentiment;
let statusEl;
let submitBtn;
let inputBox;
let sentimentResult;

function setup() {
  noCanvas();
  // initialize sentiment
  sentiment = ml5.sentiment('movieReviews', modelReady);

  // setup the html environment
  statusEl = createP('Loading Model...');
  inputBox = createInput();
  inputBox.attribute('size', '75');
  submitBtn = createButton('submit');
  sentimentResult = createP('sentiment score:');

  // predicting the sentiment on mousePressed()
  submitBtn.mousePressed(getSentiment);
}

function getSentiment() {
  // get the values from the input
  const text = inputBox.value();

  // make the prediction
  const prediction = sentiment.predict(text);

  // display sentiment result on html page
  sentimentResult.html('Sentiment score: ' + prediction.score);
}

function modelReady() {
  // model is ready
  statusEl.html('model loaded');
}

執行

備料完成後,就可啟動 Live Server,
在 VS Code 裡的 sentiment.html 程式碼按右鍵,在顯示的內容選單裡,點選 Open with Live Server
就可顯示如下畫面。
https://ithelp.ithome.com.tw/upload/images/20201031/201321565TEO5hA3TB.png

測試

先拿以下文字測試,
die hard mario fan and i loved this game br br this game starts slightly boring but trust me it's worth it as soon as you start your hooked the levels are fun and exiting they will hook you OOV your mind turns to mush i'm not kidding this game is also orchestrated and is beautifully done br br to keep this spoiler free i have to keep my mouth shut about details but please try this game it'll be worth it br br story 9 9 action 10 1 it's that good OOV 10 attention OOV 10 average 10
貼入輸入框內,按下 submit
可得情緒分數 (Sentiment score) 為0.9999998807907104 (0分為100%負面情緒,1分為100%正面情緒)。
再拿以下文字測試,
the mother in this movie is reckless with her children to the point of neglect i wish i wasn't so angry about her and her actions because i would have otherwise enjoyed the flick what a number she was take my advise and fast forward through everything you see her do until the end also is anyone else getting sick of watching movies that are filmed so dark anymore one can hardly see what is being filmed as an audience we are impossibly involved with the actions on the screen so then why the hell can't we have night vision
貼入輸入框內,按下 submit
可得情緒分數 (Sentiment score) 為0.002577823121100664,可知這樣的文章,對當時被複製者而言,會產生極接近負面的情緒。

所以,透過機械學習,我們能用過去的自己來處理現在的問題。

參考資料

https://ml5js.org/reference/api-Sentiment/


尚未有邦友留言

立即登入留言