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 06:42, 17 January 2015 (Updating link). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

These are natural language user interfaces that I have written in several programming languages. Here's an online demo of this script.

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);
	}
}

Java

import java.util.*;
import java.lang.*;
import java.io.*;

class NaturalLanguageUserInterface{
	private String[] thePattern;
	private String[] theString;
	public static void main (String[] args) throws java.lang.Exception{
		final 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++){
			System.out.println(toPrint[i] + " = " + output(toPrint[i]));
		}
	}
	
	public String getOutput(String theString){
		this.theString = theString.split(" ");
		if(matchesAPattern("@a plus @b,the sum of @a and @b,sum of @a and @b")){
			return Integer.toString(Integer.parseInt(this.p("@a")) + Integer.parseInt(this.p("@b")));
		}
		
		return "No output!";
	}
	
	public boolean matchesAPattern(String patterns){
		String[] thePatterns = patterns.split(",");
		for(int i = 0; i < thePatterns.length; i++){
			if(matchesPattern(thePatterns[i])){
				return true;
			}
		}
		return false;
	}
	
	public static boolean isParameter(String theString){
		return (theString.charAt(0) == '@');}
	public String[] splitWithSpace(String theString){
		return theString.split(" ");}
	public boolean matchesPattern(String thePattern){
		boolean toReturn = true;
		this.thePattern = thePattern.split(" ");
		if(this.thePattern.length != this.theString.length){
			return false;
		}
		for(int i = 0; i < theString.length; i++){
			if(!isParameter(this.thePattern[i]) && !(this.theString[i].equals(this.thePattern[i]))){
				toReturn = false;}}
		return toReturn;}
	public String p(String theParameter){
		theString = this.theString;
		thePattern = this.thePattern;
		for(int i = 0; i < thePattern.length; i++){
			if(theParameter.equals(thePattern[i])){
				return theString[i];
			}
		}
		return "";
	}
	public static String output(String theString){
		return new NaturalLanguageUserInterface().getOutput(theString);
	}
			
}