xxxxxxxxxx
95
//This isn't going to work. The resulting vectors won't reveal a categorical pattern for the machine to recognize and "learn." The machine will never know what to return here.
//https://github.com/Programming-from-A-to-Z/A2Z-F21/tree/main/08-ml#universal-sentence-encoder-and-embeddings
//https://www.youtube.com/watch?v=8HEgeAbYphA
let model;
let targetLabel;
//------
let myJSON;
let sentenceEncoder;
let sentences = [];
let okToDraw = false;
let results = [];
let newSentences = [];
let sentenceObjects = []
function preload() {
myJSON = loadJSON("free-britney-flag.json", parseData);
// myJSON = loadJSON("trump-flag.json", parseData);
// myJSON = loadJSON("biden-flag.json", parseData);
// myJSON = loadJSON("blm-flag.json", parseData);
// myJSON = loadJSON("blue-lives-flag.json", parseData);
}
function parseData() {
let data = myJSON;
for (let i = 0; i < myJSON.data.length; i++) {
let thisReview = myJSON.data[i];
sentences.push(thisReview.reviewTitle);
sentences.push(thisReview.review);
}
}
//--------------------------------------------------------------
function setup() {
createCanvas(700, 700);
let options = {
inputs: ["vector"],
outputs: ["sentence"],
task: "classification",
debug: true,
};
model = ml5.neuralNetwork(options);
sentenceEncoder = ml5.universalSentenceEncoder(modelLoaded);
}
function modelLoaded() {
sentenceEncoder.predict(sentences, gotResults);
}
function gotResults(err, result) {
for (let i = 0; i < result.length; i++) {
// each result is an 512-object array specific to each sentence
let object = {
sentence: sentences[i],
vector: result[i][0]
}
console.log(object);
sentenceObjects.push(object);
}
//console.log(sentenceObjects);
}
function keyPressed(){
if (key == "c"){
addDataToModel();
}
}
function addDataToModel(){
console.log("adding data");
for (let i = 0; i < sentenceObjects.length; i++){
let inputs = sentenceObjects[i].vector;
let target = sentenceObjects[i].sentence
model.addData(inputs, target);
}
}