xxxxxxxxxx
176
// what if instead of syn. i do the def. + ex. too
// https://dictionaryapi.dev/
// Variable to store user input words
let inputWords = [];
// Flag to toggle color mode
let colored = true;
// Flag to track if keyboard controls are active
let pressed = false;
// Array to store synonyms fetched from API
let wordList = [];
// Object to store word synonyms
let wordDict = {};
function setup() {
createCanvas(windowHeight, windowWidth);
background (0);
textFont('Verdana')
input = createInput();
input.position(20,20);
}
function draw() {
background(20);
translate(width/2, height/2);
// Process user input
inputWords = input.value().split(" ");
//let textSizeVar;
if (!pressed) {
textSizeVar = map(mouseY, 0, height, 10, 30);
mappedMouseX = map(mouseX, width/2, 0, 0, 300)
}
for (i = 0; i < 400; i++) {
let ang = i/4.6 + frameCount/110;
let r = i + noise(i) * 2 * mappedMouseX;
if (colored) {
// Calculate a percentage for the color gradient
let percent = map(i, 0, 400, 0, 10);
// Interpolate between two colors using lerpColor
if (percent < 0.5) {
col = lerpColor(color('#FDF48A'), color('#FF5631'), map(percent, 0, 0.5, 0, 1));
} else {
col = lerpColor(color('#FF5631'), color('#634FFF'), map(percent, 0.5, 1, 0, 1));
}
} else {
col = 255;
}
fill(col);
textSize(textSizeVar);
// Draw text at calculated position
let word = inputWords[i];
if (word) {
for (let j = 0; j < word.length; j++) {
let charIndex = j % word.length;
text(word.charAt(charIndex), cos(ang) * r, sin(ang) * r);
ang += 0.1; // Increment angle for each character to avoid overlapping
}
}
// Display synonyms
let synonyms = wordDict[word];
if (synonyms) {
for (let synonym of synonyms) {
r += 10; // Increase radius for synonyms to separate them
for (let j = 0; j < synonym.length; j++) {
let charIndex = j % synonym.length;
text(synonym.charAt(charIndex), cos(ang) * r, sin(ang) * r);
ang += 0.1; // Increment angle for each character to avoid overlapping
}
}
}
}
}
function keyPressed() {
// Toggle color mode with the up arrow
if (keyCode === UP_ARROW){
colored = !colored;
pressed = !pressed;
}
else if (keyCode === RETURN){
// Fetch synonyms when enter key is pressed
startup();
}
}
// Fetch synonyms of a given word from API
async function getSynonyms(word) {
if (wordList.indexOf(word) == -1) {
wordList.push(word);
let synonyms = [];
try {
let response = await fetch('https://api.dictionaryapi.dev/api/v2/entries/en/' + word);
let data = await response.json();
findSynonyms(data, synonyms);
let completed = removeDuplicates(synonyms);
let cleanedSynonyms = [];
//check for any potentially offensive terms
for (let item of completed) {
response = await fetch('https://www.purgomalum.com/service/containsprofanity?text='+item);
data = await response.json();
if (!(data)) {
cleanedSynonyms.push(item);
}
}
if (cleanedSynonyms.length > 20) {
cleanedSynonyms.splice(20, cleanedSynonyms.length - 20);
}
wordDict[word] = cleanedSynonyms;
console.log(cleanedSynonyms);
return cleanedSynonyms;
} catch (error) {
console.error('Error fetching synonyms:', error);
return [];
}
} else {
return [];
}
}
// Function to extract synonyms from API response data
function findSynonyms(data,synList) {
let type = typeof data;
if (type == 'object') {
for (const [key, value] of Object.entries(data)) {
findSynonyms(value, synList);
if (key == 'synonyms') {
for (let synonym of value){
synList.push(synonym);
}
}
}
}
}
// Function to remove duplicate words from an array
function removeDuplicates(arr) {
let tempList = [];
for (let i = 0; i<arr.length;i++) {
if (arr.indexOf(arr[i]) == i) {
tempList.push(arr[i]);
}
}
return tempList
}
// Function to handle fetching synonyms when user inputs a new word
function startup() {
console.log("startup");
let startWord = inputWords[inputWords.length - 1];
console.log(startWord)
getSynonyms(startWord)
.then(result => next_list = result)
.catch(err => console.log(err));
inputWords.push(new Word(startWord, 0, 0, random(-10, 10)));
prev_list = [startWord];
}