Skip to content

Instantly share code, notes, and snippets.

@mem-memov
Last active August 22, 2018 20:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mem-memov/4419427 to your computer and use it in GitHub Desktop.
Save mem-memov/4419427 to your computer and use it in GitHub Desktop.
Semantic JavaScript is a new way of inteacting in the web. In this simple example two men give information about birds using cybe language. Semantic graph is created authomatically. The machine asks the second man questions about birds.
var ai = (function() {
var ai = {
phrases: {},
phraseIds: {},
nextPhraseId: 1,
authors: {},
authorIds: {},
nextAuthorId: 1,
messages: {},
nextMessageId: 1,
actions: {},
nextActionId: 1,
parameters: {},
nextParameterId: 1,
attributes: {},
nextAttributeId: 1,
concepts: {},
conceptIds: {},
nextConceptId: 1,
things: {},
thingIds: {},
nextThingId: 1
}
ai.listen = function(author, message) {
console.log(author + " сказал:");
console.log(message);
var aiAuthor = ai.addAuthor(ai.addPhrase(author).id);
var aiMessage = ai.addMessage(aiAuthor.id);
aiAuthor.messageIds.push(aiMessage.id);
for (var action in message) {
var aiAction = ai.addAction(ai.addPhrase(action).id);
aiMessage.actionIds.push(aiAction.id);
var aiActionConcept = ai.addConcept(aiAction.phraseId, 'action');
for (var parameter in message[action]) {
if (ai.isReference(message[action][parameter])) {
var reference = message[action][parameter][0];
var aiParameter = ai.findParameter(reference);
if (!aiParameter) {
throw new Error('Wrong reference');
}
var attributes = message[action][parameter][1];
} else {
var aiParameter = ai.addParamenter(ai.addPhrase(parameter).id);
var attributes = message[action][parameter];
}
aiAction.parameterIds.push(aiParameter.id);
var aiParameterConcept = ai.addConcept(aiParameter.phraseId, 'parameter');
ai.linkConcepts(aiParameterConcept, aiActionConcept);
for (var attribute in attributes) {
var aiAttribute = ai.addAttribute(ai.addPhrase(attribute).id);
aiParameter.attributeIds.push(aiAttribute.id);
var aiAttributeConcept = ai.addConcept(aiAttribute.phraseId, 'attribute');
ai.linkConcepts(aiAttributeConcept, aiParameterConcept);
}
//
if (ai.isReference(message[action][parameter])) {
var aiThing = ai.addThing(aiParameter.phraseId, aiParameter.id, aiParameterConcept.id);
aiAuthor.thingIds.push(aiThing.id);
aiParameterConcept.thingIds.push(aiThing.id);
}
}
}
}
ai.talk = function(author) {
console.log("Спрашиваем у человка с именем " + author + ":");
var phraseId = ai.phraseIds[author];
var authorId = ai.authorIds[phraseId];
var aiAuthor = ai.authors[authorId];
var thingId = aiAuthor.thingIds[(aiAuthor.thingIds.length -1)];
var aiThing = ai.things[thingId];
var aiConcept = ai.concepts[aiThing.conceptId];
for (var linkedConceptId in aiConcept.conceptIds) {
var linkedAiConcept = ai.concepts[linkedConceptId];
var question = ai.phrases[aiConcept.phraseId].name + " " + ai.phrases[linkedAiConcept.phraseId].name + "?";
console.log(question);
}
}
ai.answer = function() {
}
ai.isReference = (function() {
return ('isArray' in Array) ?
Array.isArray :
function(value) {
return toString.call(value) === '[object Array]';
}
})()
ai.addPhrase = function(phraseString) {
var phraseId;
if (ai.phraseIds[phraseString] === undefined) {
phraseId = ai.nextPhraseId;
ai.nextPhraseId++;
ai.phraseIds[phraseString] = phraseId;
var aiPhrase = {
id: phraseId,
name: phraseString,
weight: 0
};
ai.phrases[phraseId] = aiPhrase;
} else {
phraseId = ai.phraseIds[phraseString];
}
ai.phrases[phraseId].weight++;
return ai.phrases[phraseId];
}
ai.addAuthor = function(phraseId) {
var authorId;
if (ai.authorIds[phraseId] === undefined) {
authorId = ai.nextAuthorId;
ai.nextAuthorId++;
ai.authorIds[phraseId] = authorId;
var aiAuthor = {
id: authorId,
phraseId: phraseId,
messageIds: [],
thingIds: []
}
ai.authors[authorId] = aiAuthor;
} else {
authorId = ai.authorIds[phraseId];
}
return ai.authors[authorId];
}
ai.addMessage = function(authorId) {
var messageId = ai.nextMessageId;
ai.nextMessageId++;
var aiMessage = {
id: messageId,
authorId: authorId,
actionIds: []
}
ai.messages[messageId] = aiMessage;
return aiMessage;
}
ai.addAction = function(phraseId) {
var actionId = ai.nextActionId;
ai.nextActionId++;
var aiAction = {
id: actionId,
phraseId: phraseId,
parameterIds: []
}
ai.actions[actionId] =aiAction;
return aiAction;
}
ai.addParamenter = function(phraseId) {
var parameterId = ai.nextParameterId;
ai.nextParameterId++;
var parameter = {
id: parameterId,
phraseId: phraseId,
attributeIds: []
}
ai.parameters[parameterId] = parameter;
return parameter;
}
ai.addAttribute = function(phraseId) {
var attributeId = ai.nextAttributeId;
ai.nextAttributeId++;
var attribute = {
id: attributeId,
phraseId: phraseId
}
return attribute;
}
ai.addConcept = function(phraseId, type) {
var conceptId;
if (ai.conceptIds[phraseId] === undefined) {
conceptId = ai.nextConceptId;
ai.nextConceptId++;
ai.conceptIds[phraseId] = conceptId;
var aiConcept = {
id: conceptId,
type: type,
phraseId: phraseId,
conceptIds: {},
thingIds: []
}
ai.concepts[conceptId] = aiConcept;
} else {
conceptId = ai.conceptIds[phraseId];
}
return ai.concepts[conceptId];
}
ai.linkConcepts = function(a, b) {
if (a.conceptIds[b.id] === undefined) {
a.conceptIds[b.id] = 1;
} else {
a.conceptIds[b.id]++;
}
if (b.conceptIds[a.id] === undefined) {
b.conceptIds[a.id] = 1;
} else {
b.conceptIds[a.id]++;
}
}
ai.findParameter = function(reference) {
var foundAiParameter = null;
for (var actionId = ai.nextActionId - 1; actionId > 0; actionId--) {
var aiAction = ai.actions[actionId];
var action = ai.phrases[aiAction.phraseId].name;
if (reference[action] === undefined) {
continue;
}
for (var parameterIndex in ai.actions[actionId].parameterIds) {
var parameterId = ai.actions[actionId].parameterIds[parameterIndex];
var aiParameter = ai.parameters[parameterId];
var parameter = ai.phrases[aiParameter.phraseId].name;
if (reference[action][parameter] === undefined) {
continue;
}
foundAiParameter = aiParameter;
break;
}
if (foundAiParameter) {
break;
}
}
return foundAiParameter;
}
ai.addThing = function(phraseId, parameterId, conceptId) {
var aiThing;
if (ai.thingIds[parameterId] === undefined) {
var thingId = ai.nextThingId;
ai.nextThingId++;
ai.thingIds[parameterId] = thingId;
aiThing = {
id: thingId,
phraseId: phraseId,
conceptId: conceptId,
thingIds: [], // for the case when different authors mention the same thing
weight: 0
}
ai.things[thingId] = aiThing;
} else {
aiThing = ai.things[ai.thingIds[parameterId]];
}
aiThing.weight++;
return aiThing;
}
return {
listen: ai.listen,
talk: ai.talk,
answer: ai.answer
}
})();
ai.listen("Иванов", {
"летит": {
"птица": {},
"в небе" : {
"синем": {}
},
"быстро": {}
},
"ловит": {
"птица": [{
"летит": {
"птица": {}
}
}, {
"быстрая": {}
}],
"мошек": {}
}
});
ai.listen("Петров", {
"живёт": {
"дома": {
"у меня": {}
},
"птица": {}
},
"поёт": {
"она": [{
"живёт": {
"птица": {}
}
}, {
"песни": {}
}]
}
});
ai.talk("Петров");
@lbaptistavasquez
Copy link

hola

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment