User:Jarble/Java user interface
Appearance
I wrote this Java class, which can can be used to create natural language user interfaces. Also see the JavaScript version of this project.
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);
}
}