Jump to content

User:Jarble/Natural language user interface

From Wikipedia, the free encyclopedia

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

JavaScript

[edit]
//EngScript is a natural language user interface for JavaScript.
alert(EngScript(3, "is greater than", 4));
alert(EngScript(3, "is less than", 4));
alert(EngScript(7, "multiplied by", 3));
alert(EngScript(3, "to the power of", 2));


function splitString(theString){
    var theArray = [];
    theString = theString.split(" ");
    for(var i = 0; i < theString.length; i++){
        if(theString[i][0] === "$"){
            theArray[theArray.length] = theString[i];
        }
        else{
            if(theArray[theArray.length-1][0] === "$"){
                theArray[theArray.length] = theString[i];
            }
            else{
                theArray[theArray.length-1] += " " + theString[i];
            }
        }
    }
    return theArray;
}
function EngScript(){
    var theList = arguments;
    var d = {};
    function m(thePattern){
        thePattern = splitString(thePattern);
        if(theList.length !== thePattern.length){
            return false;
        }
        for(var i = 0; i < thePattern.length; i++){
            if(thePattern[i][0] === "$"){
                d[thePattern[i].substring(1, thePattern[i].length)] = theList[i];
            }
            else if(theList[i].replace(new RegExp(thePattern[i], 'g'), '') !== ''){
                return false;
            }
        }
    return true;
    }
    if(m("$a is (greater|more) than $b")){
        return (d.a > d.b);
    }
    else if(m("$a is less than $b")){
        return (d.a < d.b);
    }
    else if(m("$a (raised |)to the power of $b")){
        return Math.pow(d.a, d.b);
    }
    else if(m("$a (multiplied by|times) $b")){
        return d.a*d.b;
    }
}

C#

[edit]
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

[edit]
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);
	}
			
}