Skip to content

Instantly share code, notes, and snippets.

@JoshuaCaputo
Created January 27, 2019 20:37
Show Gist options
  • Save JoshuaCaputo/80de498ecea160f9ab75d028c26a069d to your computer and use it in GitHub Desktop.
Save JoshuaCaputo/80de498ecea160f9ab75d028c26a069d to your computer and use it in GitHub Desktop.
Allows to call functions outside of scope and make inferred decision. v0,1
/**
* These groupings give positive or negative inflection to the words
*/
const cognition = {
positive: ['bright', 'pleased', 'kind', 'amused', 'up', 'exposed'],
negative: ['disapproval', 'displeasure', 'down']
}
const Sun = {
definitions: {
shine: "give out a bright light",
move: "go in a specified direction or manner; change position."
},
shine: function(){
console.log("The Sun is Shining");
},
move: function(){
console.log('The Sun is Moving');
}
}
const FacialExpression = {
definitions: {
smile: "form one's features into a pleased, kind, or amused expression, typically with the corners of the mouth turned up and the front teeth exposed",
frown: "form an expression of disapproval, displeasure, or concentration, typically by turning down the corners of the mouth."
},
smile: function() {
console.log("The FacialExpression is Smiling");
},
frown: function() {
console.log("The FacialExpression is Frowning");
}
}
const ByzantineObject = {
// smile: function() {
// this.definition = "a pleased, kind, or amused expression, typically with the corners of the mouth turned up and the front teeth exposed"
// console.log("The ByzantineFunuction has taken over");
// },
}
// Sun.smile();
// Doesn't work obviously
// So...
const MyObjects = [{"Sun": Sun}, {"FacialExpression": FacialExpression},{"ByzantineObject": ByzantineObject}];
const MyMorphFunction = (input, output) => {
// Check to see if input has the requested output
function getInheritedCommand(input, output){
for (everything in input){
const name = everything.toString();
if (name == output) {
return input[output];
}
}
}
let InheritedCommand = getInheritedCommand(input, output);
console.log('The InheritedCommand is :', InheritedCommand);
if (InheritedCommand) { InheritedCommand() }
else {
// Check MyObjects for a similar function
let MatchedCommands = [];
MyObjects.forEach(element => {
let MatchedCommand = undefined;
const object_name = (Object.keys(element)[0])
const object = element[object_name];
for (everything in object){
const name = everything.toString();
if (name == output) {
MatchedCommand = [object, output];
MatchedCommands.push(MatchedCommand);
console.log('The '+object_name+" Object has the command :", name);
}
}
});
// Check if there is a similar function
if (MatchedCommands.length > 0){
if (MatchedCommands.length == 1){
// Weight the match
let matched_command_name = MatchedCommands[0][1];
let matched_command_definition = MatchedCommands[0][0]['definitions'][matched_command_name]
let matched_command_weight = getDefinitionWeight(matched_command_definition)
console.log(matched_command_weight)
// Weight functions in
let weights = [];
for (everything in input){
const definition = input['definitions'][everything];
if (definition) {
console.log([everything, getDefinitionWeight(definition)])
weights.push([everything, getDefinitionWeight(definition)])
}
}
// Find which function in input output maps to
let distances = [];
let names = [];
for (i in weights){
let w1 = weights[i][1];
let w2 = matched_command_weight;
console.log(weights[i][0] ,Math.getDistance(w2[0],w2[1],w1[0],w1[1]))
distances.push(Math.getDistance(w2[0],w2[1],w1[0],w1[1]))
names.push(weights[i][0])
}
console.log(distances)
let a = distances.indexOf(Math.min(...distances));
var min = Math.min(...distances)
console.log(min,a)
console.log(input[names[a]])
input[names[a]]();
}
else {
}
}
}
}
function getDefinitionWeight(definition){
let weight = [0,0]; // positive influence, negative influence
const words = definition.split(' ');
for (index in words){
let word = words[index];
if (cognition.positive.includes(word)){
weight[0]++;
}
if (cognition.negative.includes(word)){
weight[1]++;
}
}
return weight;
}
/**
* return the distance between two points.
*
* @param {number} x1 x position of first point
* @param {number} y1 y position of first point
* @param {number} x2 x position of second point
* @param {number} y2 y position of second point
* @return {number} distance between given points
*/
Math.getDistance = function( x1, y1, x2, y2 ) {
var xs = x2 - x1,
ys = y2 - y1;
xs *= xs;
ys *= ys;
return Math.sqrt( xs + ys );
};
MyMorphFunction(Sun, 'smile');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment