Java Reverse Engineer Demo

Input

[METHODS]
public BayesDisambiguatorOnLine(int) {
	0 aload_0
	1 iload_1
	2 invokespecial BayesDisambiguator(int)
	5 return
}

public void untrain(java.lang.Object, int) {
	0 aload_0
	1 getfield BayesDisambiguatorOnLine.priors
	4 iload_2
	5 dup2
	6 daload
	7 dconst_1
	8 dsub
	9 dastore
	10 aload_0
	11 getfield BayesDisambiguatorOnLine.weights
	14 aload_1
	15 invokeinterface java.util.Map.get(java.lang.Object)
	20 checkcast double[]
	23 astore_3
	24 aload_3
	25 iload_2
	26 dup2
	27 daload
	28 dconst_1
	29 dsub
	30 dastore
	31 iconst_1
	32 istore 4
	34 iconst_0
	35 istore 5
	37 iload 5
	39 aload_0
	40 getfield BayesDisambiguatorOnLine.numberOfClasses
	43 if_icmpge 66
	46 aload_3
	47 iload 5
	49 daload
	50 getstatic BayesDisambiguatorOnLine.SMOOTH
	53 dcmpl
	54 ifeq 60
	57 iconst_0
	58 istore 4
	60 iinc 5 1
	63 goto 37
	66 iload 4
	68 ifeq 113
	71 aload_0
	72 getfield BayesDisambiguatorOnLine.weights
	75 aload_1
	76 invokeinterface java.util.Map.remove(java.lang.Object)
	81 pop
	82 iconst_0
	83 istore 5
	85 iload 5
	87 aload_0
	88 getfield BayesDisambiguatorOnLine.numberOfClasses
	91 if_icmpge 113
	94 aload_0
	95 getfield BayesDisambiguatorOnLine.priors
	98 iload 5
	100 dup2
	101 daload
	102 getstatic BayesDisambiguatorOnLine.SMOOTH
	105 dsub
	106 dastore
	107 iinc 5 1
	110 goto 85
	113 return
}

public void normalize() {
	0 return
}

public int predict(java.lang.Object[]) {
	0 dconst_0
	1 dstore_2
	2 iconst_0
	3 istore 4
	5 iload 4
	7 aload_0
	8 getfield BayesDisambiguatorOnLine.numberOfClasses
	11 if_icmpge 30
	14 dload_2
	15 aload_0
	16 getfield BayesDisambiguatorOnLine.priors
	19 iload 4
	21 daload
	22 dadd
	23 dstore_2
	24 iinc 4 1
	27 goto 5
	30 dload_2
	31 invokestatic java.lang.Math.log(double)
	34 dstore_2
	35 aload_0
	36 getfield BayesDisambiguatorOnLine.numberOfClasses
	39 newarray double
	41 astore 4
	43 aload_0
	44 getfield BayesDisambiguatorOnLine.numberOfClasses
	47 newarray double
	49 astore 5
	51 iconst_0
	52 istore 6
	54 iload 6
	56 aload_0
	57 getfield BayesDisambiguatorOnLine.numberOfClasses
	60 if_icmpge 84
	63 aload 5
	65 iload 6
	67 aload_0
	68 getfield BayesDisambiguatorOnLine.priors
	71 iload 6
	73 daload
	74 invokestatic java.lang.Math.log(double)
	77 dastore
	78 iinc 6 1
	81 goto 54
	84 iconst_0
	85 istore 6
	87 iload 6
	89 aload_1
	90 arraylength
	91 if_icmpge 173
	94 aload_0
	95 getfield BayesDisambiguatorOnLine.weights
	98 aload_1
	99 iload 6
	101 aaload
	102 invokeinterface java.util.Map.containsKey(java.lang.Object)
	107 ifeq 167
	110 aload_0
	111 getfield BayesDisambiguatorOnLine.weights
	114 aload_1
	115 iload 6
	117 aaload
	118 invokeinterface java.util.Map.get(java.lang.Object)
	123 checkcast double[]
	126 astore 7
	128 iconst_0
	129 istore 8
	131 iload 8
	133 aload 4
	135 arraylength
	136 if_icmpge 167
	139 aload 4
	141 iload 8
	143 dup2
	144 daload
	145 aload 7
	147 iload 8
	149 daload
	150 invokestatic java.lang.Math.log(double)
	153 aload 5
	155 iload 8
	157 daload
	158 dsub
	159 dadd
	160 dastore
	161 iinc 8 1
	164 goto 131
	167 iinc 6 1
	170 goto 87
	173 ldc2_w -infinity
	176 dstore 6
	178 iconst_0
	179 istore 8
	181 iconst_0
	182 istore 9
	184 iload 9
	186 aload 4
	188 arraylength
	189 if_icmpge 220
	192 aload 4
	194 iload 9
	196 daload
	197 dload 6
	199 dcmpl
	200 ifle 214
	203 aload 4
	205 iload 9
	207 daload
	208 dstore 6
	210 iload 9
	212 istore 8
	214 iinc 9 1
	217 goto 184
	220 iload 8
	222 ireturn
}

Output

Original

import edu.columbia.cs.pablo.match.MatchedText;
import edu.columbia.cs.pablo.contsel.SelectionDatum;
import edu.columbia.cs.pablo.langmod.LanguageModel;
import edu.columbia.cs.pablo.data.FrameData;
import edu.columbia.cs.pablo.data.FrameObject;
import edu.columbia.cs.pablo.refexpr.Dictionary;
import edu.columbia.cs.pablo.util.Counter;

import java.io.Serializable;
import java.io.ObjectOutputStream;
import java.io.FileOutputStream;
import java.util.Arrays;
import java.util.Map;
import java.util.HashMap;
import java.util.Set;
import java.util.HashSet;
import java.util.List;
import java.util.Collections;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Iterator;

public class BayesDisambiguator{
    public static double SMOOTH=HMMTagger.SMOOTH;
    public int numberOfClasses;
    public double[]priors;
    public Map weights; // token to array of double
    public BayesDisambiguator(int numberOfClasses){
	this.numberOfClasses=numberOfClasses;
	this.weights=new HashMap();
	this.priors=new double[numberOfClasses];
    }
    public void train(Object token,int tag){
	priors[tag]++;
	if(weights.containsKey(token))
	    ((double[])weights.get(token))[tag]++;
	else{
	    double[]w=new double[numberOfClasses];w[tag]=1;
	    for(int i=0;i<numberOfClasses;i++){
		priors[i]+=SMOOTH;
		w[i]=SMOOTH;
	    }
	    w[tag]+=1;
	    weights.put(token,w);
	}
    }
    public void normalize(){
	double totalCount=0;
	for(int i=0;i<numberOfClasses;i++)
	    totalCount+=priors[i];
	totalCount=Math.log(totalCount);
	for(int i=0;i<numberOfClasses;i++)
	    priors[i]=Math.log(priors[i]);
	Iterator v=weights.values().iterator();
	while(v.hasNext()){
	    double[]w=(double[])v.next();
	    for(int i=0;i<numberOfClasses;i++)
		w[i]=Math.log(w[i])-priors[i];
	}
	// priors
	for(int i=0;i<numberOfClasses;i++)
	    priors[i]-=totalCount;
    }
    public int predict(Object[]tokens){
	double[]predict=this.fullPredict(tokens);

	double max=Double.NEGATIVE_INFINITY;
	int result=0;
	for(int i=0;i<predict.length;i++)
	    if(predict[i]>max){
		max=predict[i];
		result=i;
	    }
	//System.err.println("Predict: "+Arrays.asList(tokens).toString()+" 0="+predict[0]+
	//		   " 1="+predict[1]);
	return result;
    }
    public double[]fullPredict(Object[]tokens){
	double[]result=new double[numberOfClasses];
	//System.arraycopy(priors,0,predict,0,numberOfClasses);
	for(int i=0;i<tokens.length;i++)
	    if(weights.containsKey(tokens[i])){
		double[]w=(double[])weights.get(tokens[i]);
		for(int j=0;j<result.length;j++)
		    result[j]+=w[j];
	    }
	return result;
    }
}