Jump to content

User:Jarble/Natural language user interface

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Jarble (talk | contribs) at 00:32, 14 January 2015. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

This is a JavaScript library that translates English commands into JavaScript functions. Here's an online demo of this script.

Also see the Java version of this project.

JavaScript

//getOutput is the function that converts English statements into JavaScript statements
alert(getOutput("3 is greater than 4")); //prints false
alert(getOutput("4 is greater than 3")); //prints true
alert(getOutput("2 to the power of 3")); //prints 8
getOutput("print hello 5 times"); //prints hello 5 times
 
function isAString(theString){
    return (typeof theString === 'string' || theString instanceof String);
}
 
function trimString(theString){
   //alert(JSON.stringify(theString) + " is theString");
   //alert(theString.substring(1, theString.length));
   return theString.substring(1, theString.length);
}
 
function getOutput(inputString){
    var theList = inputString.split(" ");
    function startsAndEndsWith(theString){
        if(isAString(theString)){
            return theString.charAt(0) === "@";
        }
        return false;
    }
 
    var p = {};
 
    function matchesPattern(pattern){
        var thePattern = pattern.split(" ");
        for(var i = 0; i < theList.length; i++){
            if(!startsAndEndsWith(thePattern[i])){
                if(theList[i] !== thePattern[i]){
                    return false;
                }
            }
            else{
                //alert("Adding "+thePattern[i])
                p[trimString(thePattern[i])] = theList[i];
            }
        }
        //alert(thePattern);
        //alert(theList);
        return true;
    }
    dictionary = {
        "@foo is greater than @bar": function(){
            return (p.foo > p.bar);
        },
        "@foo is less than @bar": function(){
            return (p.foo < p.bar);
        },
        "@foo to the power of @bar": function(){
            return Math.pow(p.foo, p.bar);
       },
        "@a is divisible by @b": function(){
           return (p.a % p.b === 0);
       },
        "print @a @b times":function(){
            for(var i = 0; i < p.b; i++){
                alert(p.a);
            }
       }
   };
  for (var key in dictionary) {
      if (dictionary.hasOwnProperty(key)) {
          if(matchesPattern(key)){
              return dictionary[key]();
          }
      }
  }
}

C#

using System; class NaturalLanguageUserInterface{ private string[] thePattern; private string[] theString; public static void Main(string[] args){ string[] toPrint = { "3 plus 4", "3 plus 17", "sum of 3 and 17", "the sum of 3 and 2" }; for(int i = 0; i < toPrint.Length; i++){ Console.WriteLine(toPrint[i] + " = " + output(toPrint[i])); } }

public string getOutput(string aString){ theString = aString.Split(' '); if(matchesAPattern("@a plus @b,the sum of @a and @b,sum of @a and @b")){ return Convert.ToString(Convert.ToInt32(p("a")) + Convert.ToInt32(p("b"))); }

return "No output!"; }

public bool matchesAPattern(string patterns){ var thePatterns = patterns.Split(','); for(int i = 0; i < thePatterns.Length; i++){ if(matchesPattern(thePatterns[i])){ return true; } } return false; }

public static bool isParameter(string theString){ return (theString[0] == '@');} public string[] splitWithSpace(string theString){ return theString.Split(' ');} public bool matchesPattern(string pattern1){ var toReturn = true; thePattern = pattern1.Split(' '); if(thePattern.Length != theString.Length){ return false; } for(int i = 0; i < theString.Length; i++){ if(!isParameter(thePattern[i]) && !(theString[i].Equals(thePattern[i], StringComparison.Ordinal))){ toReturn = false;}} return toReturn;} public string p(string theParameter){ for(int i = 0; i < thePattern.Length; i++){ if(("@"+theParameter).Equals(thePattern[i], StringComparison.Ordinal)){ return theString[i]; } } return ""; } public static string output(string theString){ return new NaturalLanguageUserInterface().getOutput(theString); } }