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


有個人已經將自己讀文章時,所產生的情緒,讓機器來學習,並產生一個已經訓練好的 model,
我們只要透過 ml5,就可以很簡單地,拿一些現在的文章,測試以他當時的情緒,會有怎樣的表現。(



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

sentiment.html 的程式碼如下—

    <title>Sentiment Analysis Demo</title>
    <script src=""></script>
    <script src=""></script>
    <script src="" type="text/javascript"></script>
    <h1>Sentiment Analysis Demo</h1>
    <script src="sketch_sentiment.js"></script>

sketch_sentiment.js 的程式碼如下—

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

function setup() {
  // 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()

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


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,可知這樣的文章,對當時被複製者而言,會產生極接近負面的情緒。