Skip to content

Commit 4af8428

Browse files
author
redpony
committed
initial checkin
git-svn-id: https://mosesdecoder.svn.sourceforge.net/svnroot/mosesdecoder/trunk@1645 1f5c12ca-751b-0410-a591-d2e778427230
1 parent 1341096 commit 4af8428

11 files changed

+257
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.statmt.ghgd;
2+
3+
import java.util.ArrayList;
4+
5+
public class And implements Comparable<And> {
6+
7+
Weight myWeight = null;
8+
Axiom axiom = null;
9+
ArrayList<Or> children = null;
10+
11+
public And(Axiom a, ArrayList<Or> ants, Weight w) {
12+
axiom = a;
13+
children = ants;
14+
myWeight = w;
15+
}
16+
17+
public int compareTo(And o) {
18+
return myWeight.compareTo(o.myWeight);
19+
}
20+
21+
public Weight getWeight() { return myWeight; }
22+
23+
public boolean isAxiom() { return children == null; }
24+
25+
public ArrayList<Or> getChildren() { return children; }
26+
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.statmt.ghgd;
2+
3+
public class Derivation implements Comparable<Derivation> {
4+
5+
And hyperArc;
6+
int[] positions;
7+
public Derivation(And hyperArc, int[] positions) {
8+
this.hyperArc = hyperArc;
9+
this.positions = positions;
10+
}
11+
public int compareTo(Derivation o) {
12+
return 0;
13+
}
14+
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.statmt.ghgd;
2+
3+
public class FWeight extends Weight {
4+
5+
float f;
6+
7+
public FWeight(float x) { f = x; }
8+
@Override
9+
public Weight add(Weight x) {
10+
return new FWeight(((FWeight)x).f + f);
11+
}
12+
13+
public int compareTo(Weight o) {
14+
float d = f - ((FWeight)o).f;
15+
if (d != 0.0f) {
16+
if (d < 0.0f) return -1; else return 1;
17+
}
18+
return 0;
19+
}
20+
21+
public String toString() { return Float.toString(f); }
22+
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.statmt.ghgd;
2+
3+
public class FloatArrayWeight extends Weight {
4+
5+
float[] v;
6+
7+
public FloatArrayWeight(int n) { v = new float[n]; }
8+
public FloatArrayWeight(float[] f) { v = f; }
9+
@Override
10+
public Weight add(Weight x) {
11+
FloatArrayWeight w=(FloatArrayWeight)x;
12+
assert(w.v.length == this.v.length);
13+
float[] r = new float[v.length];
14+
for (int i =0; i<v.length; i++) {
15+
r[i] = v[i] + w.v[i];
16+
}
17+
return new FloatArrayWeight(r);
18+
}
19+
20+
public float innerProduct(FloatArrayWeight o) {
21+
FloatArrayWeight w = (FloatArrayWeight)o;
22+
float res = 0.0f;
23+
for (int i = 0; i<v.length; i++) {
24+
res += v[i] * w.v[i];
25+
}
26+
return res;
27+
}
28+
29+
public int compareTo(Weight o) {
30+
FloatArrayWeight w = (FloatArrayWeight)o;
31+
float sum1 =0.0f;
32+
float sum2 =0.0f;
33+
for (int i = 0; i<v.length; i++) {
34+
sum1 += w.v[i];
35+
}
36+
for (int i = 0; i<v.length; i++) {
37+
sum2 += v[i];
38+
}
39+
float d = sum1 - sum2;
40+
if (d != 0.0f)
41+
if (d < 0.0f) return -1; else return 1;
42+
return 0;
43+
}
44+
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.statmt.ghgd;
2+
3+
import java.util.ArrayList;
4+
import java.util.Iterator;
5+
import java.util.PriorityQueue;
6+
7+
// this is a vertex
8+
public class Or implements Comparable<Or>, Iterable<And> {
9+
10+
ArrayList<And> alts = new ArrayList<And>();
11+
Weight myWeight;
12+
PriorityQueue<And> candidates = null;
13+
14+
public Or(ArrayList<And> hyperarcsIn, Weight estWeightIn) {
15+
alts = hyperarcsIn;
16+
myWeight = estWeightIn;
17+
}
18+
19+
public int compareTo(Or o) {
20+
return myWeight.compareTo(o.myWeight);
21+
}
22+
23+
public void getCandidates() {
24+
candidates = new PriorityQueue<And>(alts);
25+
}
26+
27+
public Iterator<And> iterator() {
28+
return alts.iterator();
29+
}
30+
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.statmt.ghgd;
2+
3+
import java.util.ArrayList;
4+
import java.util.Properties;
5+
6+
public class PhrasePairRule extends Rule {
7+
8+
String e;
9+
String f;
10+
Weight w;
11+
12+
public PhrasePairRule(String f, String e, Weight w) {
13+
this.f = f;
14+
this.e = e;
15+
this.w = w;
16+
}
17+
18+
@Override
19+
public String recoverString(ArrayList<Or> children, Properties p) {
20+
if (children == null) return e;
21+
assert(children.size() == 1);
22+
Or prevHypo = children.get(0);
23+
// And curBestArc = prevHypo.alts.get(d.positions[0]);
24+
return null;
25+
}
26+
27+
public String toString() {
28+
return "<PPA " + f + " ||| " + e + " ||| " + w + ">";
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.statmt.ghgd;
2+
3+
import java.util.ArrayList;
4+
import java.util.Properties;
5+
6+
public abstract class Rule {
7+
8+
public abstract String recoverString(ArrayList<Or> children, Properties p);
9+
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.statmt.ghgd;
2+
3+
import java.util.ArrayList;
4+
import java.util.Properties;
5+
6+
public class SCFGRule extends Rule {
7+
8+
@Override
9+
public String recoverString(ArrayList<Or> children, Properties p) {
10+
// TODO Auto-generated method stub
11+
return null;
12+
}
13+
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package org.statmt.ghgd;
2+
3+
import java.util.ArrayList;
4+
5+
public class TestHyperGraph {
6+
7+
/**
8+
* @param args
9+
*/
10+
public static void main(String[] args) {
11+
PhrasePairRule p1 = new PhrasePairRule("guten", "good", new FWeight(0.7f));
12+
PhrasePairRule p2 = new PhrasePairRule("guten", "well", new FWeight(0.3f));
13+
PhrasePairRule p3 = new PhrasePairRule("tag", "day", new FWeight(0.6f));
14+
PhrasePairRule p4 = new PhrasePairRule("tag", "hi", new FWeight(0.4f));
15+
PhrasePairRule p5 = new PhrasePairRule("guten tag", "hello", new FWeight(0.2f));
16+
PhrasePairRule p6 = new PhrasePairRule("guten tag", "good day", new FWeight(0.8f));
17+
And d1 = new And(p1, null, p1.w);
18+
And d2 = new And(p2, null, p2.w);
19+
ArrayList<And> a1 = new ArrayList<And>(2);
20+
a1.add(d1); a1.add(d2);
21+
Weight w1 = d1.getWeight();
22+
Weight w2 = d2.getWeight();
23+
Weight best = null;
24+
if (w1.compareTo(w2) < 0) best = w2; else best = w1;
25+
Or c1 = new Or(a1, best);
26+
27+
ArrayList<Or> ants1 = new ArrayList<Or>(1); ants1.add(c1);
28+
And d3 = new And(p3, ants1, p3.w);
29+
And d4 = new And(p4, ants1, p4.w);
30+
ArrayList<And> a2 = new ArrayList<And>(2);
31+
a2.add(d3); a2.add(d4);
32+
w1 = d3.getWeight();
33+
w2 = d4.getWeight();
34+
best = null;
35+
if (w1.compareTo(w2) < 0) best = w2; else best = w1;
36+
Or c2 = new Or(a2, best);
37+
38+
And d5 = new And(p5, null, p5.w);
39+
And d6 = new And(p6, null, p6.w);
40+
ArrayList<And> a3 = new ArrayList<And>(2);
41+
a3.add(d5); a3.add(d6);
42+
w1 = d5.getWeight();
43+
w2 = d6.getWeight();
44+
best = null;
45+
if (w1.compareTo(w2) < 0) best = w2; else best = w1;
46+
Or c3 = new Or(a3, best);
47+
48+
}
49+
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package org.statmt.ghgd;
2+
3+
public class ViterbiVisitor {
4+
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.statmt.ghgd;
2+
3+
public abstract class Weight implements Comparable<Weight> {
4+
5+
abstract public Weight add(Weight x);
6+
7+
}

0 commit comments

Comments
 (0)