Hi, I’m trying to write a sketch that replaces all the nouns in the user input content with the dictionary definition of the noun.
I’m using RiTa.js to detect the nouns (that part is successful), and Wordnik API for the definitions.
For each noun detected, I created a url that links to the Wordnik JSON file in relation to the noun, and I put all the nouns in nounArray, all the urls in urlArray–both of which print correctly in the console so far.
Now the question is: how can I extract the [1].text value in each of the JSON files, and put them into the definitionArray?
An example of the Wordnik JSON file:
var input;
var button;
var api1 = 'https://api.wordnik.com/v4/word.json/';
var api2 = '/definitions?limit=200&includeRelated=true&useCanonical=false&includeTags=false&api_key=wmw2c7bi8zih4peeugrgr595b02vem2h1y5jkvm29ghohhpuk';
var dictionary;
let nounArray = []
let urlArray = []
let definitionArray = []
function setup() {
opts = {
ignoreCase: true,
ignoreStopWords: true
};
noCanvas;
input = createInput("There is a tree in front of my house");
button = createButton("submit");
input.changed(processRita);
button.mousePressed(processRita);
input.size(300);
}
function processRita() {
var s = input.value();
var words = RiTa.tokens(s);
var pos = RiTa.pos(s);
console.log(words);
console.log(pos);
var output = '';
for (var i = 0; i < words.length; i++) {
if (/nn.*/.test(pos[i])) {
nounArray.push(words[i]);
urlArray.push(api1 + words[i] + api2);
console.log('nouns: ', nounArray);
console.log('urls: ', urlArray);
// up until this point, nounArray and urlArray print out correctly
definitionArray = [];
getDefinition();
function getDefinition() {
for (var i = 0; i < urlArray.length; i++) {
loadJSON(urlArray[i], gotData);
}
}
function gotData(data) {
definitionArray.push(data[1].text);
}
console.log('definitions: ', definitionArray);
output += definitionArray[i];
} else {
output += words[i];
}
output += " ";
}
createP(output);
}
use menu Edit | Tidy to get better indents that show you this
(I don’t know the language, so it’s just a remark, maybe it’s valid to place a function inside a function)
Remark 2
I can’t run your code, it says Rita is not defined.
Remark 3
from your screenshot (which is too short) you can maybe use the ID and modify it to get from the text JSON object (the first section) to the 2nd section
OR when it’s just an array, go to next element and retrieve text from there?
to get real help please post your screenshot as text and longer
I have a sketch that does work with the Wordnik API & RiTa.js:
The function gotData(data) is inside the function processRita and it works so maybe we can ignore that for now.
The problem with the initial sketch is that it only recognizes one definition, so I created the second sketch adding some arrays in case there are multiple nouns, which is the content in my original post.
and here is the full sketch, the complete JSON file can be found in the urls printed in the console:
Yeah the second link would say undefined, and not printing out anything in the definitionArray–which is the one I’m asking for help on.
The first link works if there’s only one noun (might need to click the button more than once)
I’m posting the first link again here:
Just <script src=https://Unpkg.com/rita></script> is enough for grabbing library RiTa.js.
The last statement output += definitionArray[i]; is attempting to read from array definitionArray[] before it’s fully populated by loadJSON() + gotData()!
We should never immediately access data that’s asynchronously acquired.
Hmmm the reason I didn’t write loadJSON() in the preload was that before there are urls in the urlArray, there’s no link to JSON files to be loaded, and all of those are determined by the nouns in the content that would be changing in real time depending on user input
The 2nd sketch was my attempt to fix the 1st one, by putting everything in arrays in case there are multiple of them, and I’m stuck at not being able to extract all the [1].text in those JSON files.