public BinaryRule Clone() { BinaryRule clone = new BinaryRule (); clone.ptag = ptag; clone.ltag = ltag; clone.rtag = rtag; clone.scores = ArrayHelper.Clone (scores); return clone; }
public BinaryRule MergeSymbols(int[][] trace, double[][] symbolProbs, double[][] projectedSymbolProbs) { int ptagcount = trace [ptag] [trace [ptag].Length - 1] + 1; int ltagcount = trace [ltag] [trace [ltag].Length - 1] + 1; int rtagcount = trace [rtag] [trace [rtag].Length - 1] + 1; var newScores = ArrayHelper.AllocateArray<double> (ltagcount, rtagcount, ptagcount); ArrayHelper.Fill<double> (newScores, double.NegativeInfinity); for (int i = 0; i < scores.Length; ++i) { if (scores [i] == null) { continue; } int pl = trace [ltag] [i]; for (int j = 0; j < scores[i].Length; ++j) { if (scores [i] [j] == null) { continue; } int pr = trace [rtag] [j]; for (int k = 0; k < scores[i][j].Length; ++k) { int pp = trace [ptag] [k]; newScores [pl] [pr] [pp] = MathHelper.LogAdd (newScores [pl] [pr] [pp], scores [i] [j] [k] + symbolProbs [ptag] [k]); } } } for (int i = 0; i < newScores.Length; ++i) { for (int j = 0; j < newScores[i].Length; ++j) { for (int k = 0; k < newScores[i][j].Length; ++k) { if (!double.IsNegativeInfinity (newScores [i] [j] [k]) && !double.IsNegativeInfinity (projectedSymbolProbs [ptag] [k])) { newScores [i] [j] [k] -= projectedSymbolProbs [ptag] [k]; } } } } var rule = new BinaryRule (); rule.ptag = ptag; rule.ltag = ltag; rule.rtag = rtag; rule.scores = newScores; return rule; }
public BinaryRule SplitSymbols(int[] subtagCount, int[] newSubtagCount, Random RNG, double randomness) { int parentSplitFactor = subtagCount [ptag] == newSubtagCount [ptag] ? 1 : 2; int leftSplitFactor = subtagCount [ltag] == newSubtagCount [ltag] ? 1 : 2; int rightSplitFactor = subtagCount [rtag] == newSubtagCount [rtag] ? 1 : 2; int oldpc = subtagCount [ptag]; int oldlc = subtagCount [ltag]; int oldrc = subtagCount [rtag]; int newpc = newSubtagCount [ptag]; int newlc = newSubtagCount [ltag]; int newrc = newSubtagCount [rtag]; var oldScores = scores; var newScores = new double[newlc][][]; // allocating resources... for (int ls = 0; ls < oldlc; ++ls) { if (oldScores [ls] == null) { continue; } for (int l = 0; l < leftSplitFactor; ++l) { int newls = ls * leftSplitFactor + l; newScores [newls] = new double[newrc][]; for (int rs = 0; rs < oldrc; ++rs) { if (oldScores [ls] [rs] == null) { continue; } for (int r = 0; r < rightSplitFactor; ++r) { int newrs = rs * rightSplitFactor + r; newScores [newls] [newrs] = new double[newpc]; } } } } for (int ps = 0; ps < oldpc; ++ps) { for (int ls = 0; ls < oldlc; ++ls) { if (oldScores [ls] == null) { continue; } for (int rs = 0; rs < oldrc; ++rs) { if (oldScores [ls] [rs] == null) { continue; } double score = oldScores [ls] [rs] [ps]; double div = (double)Math.Log (leftSplitFactor * rightSplitFactor); for (int p = 0; p < parentSplitFactor; ++p) { double lrd = (double)(randomness / 100 * (RNG.NextDouble () - 0.5)); if (leftSplitFactor == 1) { lrd = 0; } for (int l = 0; l < leftSplitFactor; ++l) { lrd *= -1.0f; int newls = ls * leftSplitFactor + l; double rrd = (double)(randomness / 100 * (RNG.NextDouble() - 0.5)); if (rightSplitFactor == 1) { rrd = 0; } for (int r = 0; r < rightSplitFactor; ++r) { rrd *= -1.0f; int newrs = rs * rightSplitFactor + r; int newps = ps * parentSplitFactor + p; newScores [newls] [newrs] [newps] = score - div + (double)Math.Log (1.0 + lrd) + (double)Math.Log(1.0 + rrd); } } } } } } var newRule = new BinaryRule (newScores, ptag, ltag, rtag); return newRule; }
public BinaryRule CreateRule(int[] subtagCount) { var rule = new BinaryRule (); rule.scores = ArrayHelper.AllocateArray<double> (subtagCount [ltag], subtagCount [rtag], subtagCount [ptag]); ArrayHelper.Fill<double> (rule.scores, double.NegativeInfinity); rule.ptag = ptag; rule.ltag = ltag; rule.rtag = rtag; return rule; }
public void CopyTo(BinaryRule rule) { rule.ltag = ltag; rule.rtag = rtag; rule.ptag = ptag; ArrayHelper.CopyTo (scores, rule.scores); }
public LAPCFGrammar(TagSet set, double[][][] brawScores, double[][] urawScores, double[][] trawScores) { NTCount = set.NTCount; PTCount = set.PTCount; ROOTID = set.ROOTID; var tagCount = TotalTagCount; brules = new BinaryRule[tagCount][][]; for (int l = 0; l < tagCount; ++l) { if (brawScores [l] == null) { continue; } brules [l] = new BinaryRule[tagCount][]; for (int r = 0; r < tagCount; ++r) { if (brawScores [l] [r] == null) { continue; } brules [l] [r] = new BinaryRule[tagCount]; for (int p = 0; p < tagCount; ++p) { if (!double.IsInfinity(brawScores [l] [r] [p]) && !double.IsNaN(brawScores [l] [r] [p])) { double[][][] s = new double[1][][]; s [0] = new double[1][]; s [0] [0] = new double[1]; s [0] [0] [0] = brawScores [l] [r] [p]; brules [l] [r] [p] = new BinaryRule(s, p, l, r); } } } } urules = new UnaryRule[tagCount][]; for (int c = 0; c < tagCount; ++c) { if (urawScores [c] == null) { continue; } urules [c] = new UnaryRule[tagCount]; for (int p = 0; p < tagCount; ++p) { if (!double.IsNaN(urawScores [c] [p]) && !double.IsInfinity(urawScores [c] [p])) { double[][] s = new double[1][]; s [0] = new double[1]; s [0] [0] = urawScores [c] [p]; urules [c] [p] = new UnaryRule(s, p, c); } } } trules = new TerminalRule[trawScores.Length][]; for (int w = 0; w < trawScores.Length; ++w) { if (trawScores [w] == null) { continue; } trules [w] = new TerminalRule[trawScores [w].Length]; for (int t = 0; t < trules[w].Length; ++t) { if (!double.IsNaN(trawScores [w] [t]) && !double.IsInfinity(trawScores [w] [t])) { double[] s = new double[1]; s [0] = trawScores [w] [t]; trules [w] [t] = new TerminalRule(s, t, w); } } } subTagCounts = new int[tagCount]; for (int i = 0; i < subTagCounts.Length; ++i) { subTagCounts [i] = 1; } }
public static BinaryRule[][][] CloneRules(BinaryRule[][][] rules) { if (rules == null) { return null; } return rules.Select( x => x == null ? null : x.Select( y => y == null ? null : y.Select( z => z == null ? null : z.Clone() ).ToArray() ).ToArray() ).ToArray(); }
private void BuildBinaryRule(string ruleString, TagSet tagSet) { string[] parts = ruleString.Split(new string[] { "\t", "_" }, StringSplitOptions.RemoveEmptyEntries); int ptag = tagSet.GetID(parts [0]); int subptag = int.Parse(parts [1]); int ltag = tagSet.GetID(parts [2]); int subltag = int.Parse(parts [3]); int rtag = tagSet.GetID(parts [4]); int subrtag = int.Parse(parts [5]); double s = double.Parse(parts [6]); if (brules == null) { brules = new BinaryRule[TotalTagCount][][]; } if (brules [ltag] == null) { brules [ltag] = new BinaryRule[TotalTagCount][]; } if (brules [ltag] [rtag] == null) { brules [ltag] [rtag] = new BinaryRule[TotalTagCount]; } if (brules [ltag] [rtag] [ptag] == null) { brules [ltag] [rtag] [ptag] = new BinaryRule( ArrayHelper.AllocateArray<double>(subTagCounts [ltag], subTagCounts [rtag], subTagCounts [ptag]), ptag, ltag, rtag); brules [ltag] [rtag] [ptag].ClearScore(); } brules [ltag] [rtag] [ptag].scores [subltag] [subrtag] [subptag] = s; }