拿 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/


<<:  Python学习:Markdown语法

>>:  Day37. 原型模式

Day10:Emit Direct Message II(Render 私人讯息到 HTML)

全文同步於个人 Docusaurus Blog 继承前一章的内容,现在需要将 server-sid...

基本面 VS 技术面

接触股市的人一定常听到「基本面」与「技术面」这两种面向。 很多人都有相同疑问:我该研究「基本面」还是...

【没钱买ps,PyQt自己写】Day 16 - 在 PyQt5 中取得图片座标 (滑鼠位置) mousePressEvent,观察图片在 Qt 中产生的方式,对原图进行座标换算处理

看完这篇文章你会得到的成果图 前言 这一篇我们会继续拿现有的 day 15 成品来改, 接下来我们要...

【Day 7】Dockerfile x 连千毅:从一排汉堡认识容器映像档

tags: 铁人赛 Docker Dockerfile 汉堡 概述 碎念时间 【以皮毛技术与 AWS...

【设计+切版30天实作】|Day3 - 参考Bootstrap画出理想的header(上集)

设计大纲 今天来设计Landing page的header。这次想要做的是一个满版的header,在...