#public class Haha { class StateObject def initialize(s, i) @state = s @index = i @h = 1 @a = 2 @ex = 3 @p = 4 end # def initialize # @state = -1 # @index = -1 # end def setState(s) @state = s end def getState @state end def setIndex(i) @index = i end def getIndex @index end end def recognize(index, currentState, transitionTable, inputNum) while index <= inputNum.length && currentState != -1 if index == inputNum.length if currentState == 4 #end state* return 1 else return -1 end # This is actually completely the wrong variable. This should depend on which input letter I just received.*/ elsif transitionTable[inputNum[index, index + 1].to_i][currentState] == -1 #//check order of indecies return -1 else index = index +1; currentState = transitionTable[index][currentState] #check order of indecies puts "index = " + index.to_s #debugging puts "currentState = " + currentState.to_s puts "transitionTable[index][currentState] = " + transitionTable[index][currentState].to_s end end return 17 end #String input = JOptionPane.showInputDialog("Input phrase?"); inputString = "ha" index = 0 inputNum = "" while index <= inputString.length if inputString[index, index + 1] == "h" inputNum.concat("1") elsif inputString[index, index + 1] == "a" inputNum.concat("2") elsif inputString[index, index + 1] == "." || inputString[index, index + 1] == "!" inputNum.concat("3") else inputNum.concat("7") end index = index + 1 end currentState = 0 transitionTable = [[1, -1, -1, -1, -1],[-1, 2, -1, -1, -1],[1, -1, 4, 4, 3], [-1, -1, 4, 4, 4], [-1, -1, -1, -1, -1]] # Stack agenda = new Stack(); agenda = [] origState = StateObject.new(0,0) agenda.push(origState); puts "Result: " + recognize(index, currentState, transitionTable, inputNum).to_s