public LAPCFGrammar CollapseNonTerminals(int[] tagMap, int newNTCount) { var pg = new LAPCFGrammar(); pg.NTCount = newNTCount; pg.PTCount = PTCount; pg.ROOTID = ROOTID; pg.subTagCounts = new int[PTCount + newNTCount]; ArrayHelper.Fill(pg.subTagCounts, 1); var sprobs = ComputeSymbolProb(); var psprobs = pg.subTagCounts.Select(x => new double[x]).ToArray(); ArrayHelper.Fill(psprobs, double.NegativeInfinity); for (int i = 0; i < PTCount; ++i) { for (int j = 0; j < subTagCounts[i]; ++j) { psprobs [i] [0] = MathHelper.LogAdd(psprobs [i] [0], sprobs [i] [j]); } } for (int i = PTCount; i < TotalTagCount; ++i) { for (int j = 0; j < subTagCounts[i]; ++j) { psprobs [tagMap [i]] [0] = MathHelper.LogAdd(psprobs [tagMap [i]] [0], sprobs [i] [j]); } } pg.brules = ArrayHelper.AllocateArray<BinaryRule>(PTCount + newNTCount, PTCount + newNTCount, PTCount + newNTCount); foreach (var x in brules.Where(x => x != null)) { foreach (var y in x.Where(y => y != null)) { foreach (var z in y.Where(z => z != null)) { int ltag = z.ltag >= PTCount ? tagMap [z.ltag] : z.ltag; int rtag = z.rtag >= PTCount ? tagMap [z.rtag] : z.rtag; int ptag = z.ptag >= PTCount ? tagMap [z.ptag] : z.ptag; double s = double.NegativeInfinity; for (int l = 0; l < z.scores.Length; ++l) { if (z.scores [l] == null) { continue; } for (int r = 0; r < z.scores[l].Length; ++r) { if (z.scores [l] [r] == null) { continue; } for (int p = 0; p < z.scores[l][r].Length; ++p) { double xs = z.scores [l] [r] [p]; if (double.IsNegativeInfinity(xs)) { continue; } xs += sprobs [z.ptag] [p]; s = MathHelper.LogAdd(xs, s); } } } if (double.IsNegativeInfinity(s)) { continue; } if (pg.brules [ltag] [rtag] [ptag] == null) { pg.brules [ltag] [rtag] [ptag] = new BinaryRule(ArrayHelper.AllocateArray<double>(1, 1, 1), ptag, ltag, rtag); pg.brules [ltag] [rtag] [ptag].scores [0] [0] [0] = s; } else { pg.brules [ltag] [rtag] [ptag].scores [0] [0] [0] = MathHelper.LogAdd(s, pg.brules [ltag] [rtag] [ptag].scores [0] [0] [0]); } } } } foreach (var x in pg.brules.Where(x => x != null)) { foreach (var y in x.Where(y => y != null)) { foreach (var z in y.Where(z => z != null)) { z.scores [0] [0] [0] = z.scores [0] [0] [0] - psprobs [z.ptag] [0]; } } } pg.urules = ArrayHelper.AllocateArray<UnaryRule>(PTCount + newNTCount, PTCount + newNTCount); foreach (var x in urules.Where(x => x != null)) { foreach (var y in x.Where(y => y != null)) { int ctag = y.ctag >= PTCount ? tagMap [y.ctag] : y.ctag; int ptag = y.ptag >= PTCount ? tagMap [y.ptag] : y.ptag; double s = double.NegativeInfinity; for (int c = 0; c < y.scores.Length; ++c) { if (y.scores [c] == null) { continue; } for (int p = 0; p < y.scores[c].Length; ++p) { double xs = y.scores [c] [p]; if (double.IsNegativeInfinity(xs)) { continue; } xs += sprobs [y.ptag] [p]; s = MathHelper.LogAdd(xs, s); } } if (double.IsNegativeInfinity(s)) { continue; } if (pg.urules [ctag] [ptag] == null) { pg.urules [ctag] [ptag] = new UnaryRule(ArrayHelper.AllocateArray<double>(1, 1), ptag, ctag); pg.urules [ctag] [ptag].scores [0] [0] = s; } else { pg.urules [ctag] [ptag].scores [0] [0] = MathHelper.LogAdd(s, pg.urules [ctag] [ptag].scores [0] [0]); } } } foreach (var x in pg.urules.Where(x => x != null)) { foreach (var y in x.Where(y => y != null)) { y.scores [0] [0] = y.scores [0] [0] - psprobs [y.ptag] [0]; } } var trace = subTagCounts.Select(x => new int[x]).ToArray(); pg.trules = trules.Select( x => x == null ? null : x.Select( y => y == null ? null : y.MergeSymbols(trace, sprobs, psprobs) ).ToArray() ).ToArray(); pg.Normalize(); return pg; }
public LAPCFGrammar ProjectGrammar(int[][] trace) { var pg = new LAPCFGrammar(); pg.NTCount = NTCount; pg.PTCount = PTCount; pg.ROOTID = ROOTID; pg.subTagCounts = trace.Select(x => x [x.Length - 1] + 1).ToArray(); var sprobs = ComputeSymbolProb(); var psprobs = pg.subTagCounts.Select(x => new double[x]).ToArray(); ArrayHelper.Fill(psprobs, double.NegativeInfinity); for (int i = 0; i < trace.Length; ++i) { for (int j = 0; j < trace[i].Length; ++j) { psprobs [i] [trace [i] [j]] = MathHelper.LogAdd(psprobs [i] [trace [i] [j]], sprobs [i] [j]); } } pg.brules = brules.Select( x => x == null ? null : x.Select( y => y == null ? null : y.Select( z => z == null ? null : z.MergeSymbols(trace, sprobs, psprobs) ).ToArray() ).ToArray() ).ToArray(); pg.urules = urules.Select( x => x == null ? null : x.Select( y => y == null ? null : y.MergeSymbols(trace, sprobs, psprobs) ).ToArray() ).ToArray(); pg.trules = trules.Select( x => x == null ? null : x.Select( y => y == null ? null : y.MergeSymbols(trace, sprobs, psprobs) ).ToArray() ).ToArray(); pg.Normalize(); return pg; }