public bool Add(int s, int type, int dir, double score, FeatureVector fv) { bool added = false; if (Chart[s, s, dir, 0, 0] == null) { for (int i = 0; i < K; i++) Chart[s, s, dir, 0, i] = new ParseForestItem(s, type, dir, double.NegativeInfinity, null); } if (Chart[s, s, dir, 0, K - 1].Prob > score) return false; for (int i = 0; i < K; i++) { if (Chart[s, s, dir, 0, i].Prob < score) { ParseForestItem tmp = Chart[s, s, dir, 0, i]; Chart[s, s, dir, 0, i] = new ParseForestItem(s, type, dir, score, fv); for (int j = i + 1; j < K && tmp.Prob != double.NegativeInfinity; j++) { ParseForestItem tmp1 = Chart[s, s, dir, 0, j]; Chart[s, s, dir, 0, j] = tmp; tmp = tmp1; } added = true; break; } } return added; }
// returns pairs of indeces and -1,-1 if < K pairs public int[,] GetKBestPairs(ParseForestItem[] items1, ParseForestItem[] items2) { // in_ this case K = items1.Length var beenPushed = new bool[K,K]; var result = new int[K,2]; for (int i = 0; i < K; i++) { result[i, 0] = -1; result[i, 1] = -1; } if (items1 == null || items2 == null || items1[0] == null || items2[0] == null) return result; var heap = new BinaryHeap(K + 1); int n = 0; var vip = new ValueIndexPair(items1[0].Prob + items2[0].Prob, 0, 0); heap.Add(vip); beenPushed[0, 0] = true; while (n < K) { vip = heap.RemoveMax(); if (vip.Val == double.NegativeInfinity) break; result[n, 0] = vip.I1; result[n, 1] = vip.I2; n++; if (n >= K) break; if (!beenPushed[vip.I1 + 1, vip.I2]) { heap.Add(new ValueIndexPair(items1[vip.I1 + 1].Prob + items2[vip.I2].Prob, vip.I1 + 1, vip.I2)); beenPushed[vip.I1 + 1, vip.I2] = true; } if (!beenPushed[vip.I1, vip.I2 + 1]) { heap.Add(new ValueIndexPair(items1[vip.I1].Prob + items2[vip.I2 + 1].Prob, vip.I1, vip.I2 + 1)); beenPushed[vip.I1, vip.I2 + 1] = true; } } return result; }
public FeatureVector GetFeatureVector(ParseForestItem pfi) { if (pfi.Left == null) return pfi.FV; return Cat(pfi.FV, Cat(GetFeatureVector(pfi.Left), GetFeatureVector(pfi.Right))); }
public ParseForestItem[] GetItems(int s, int t, int dir, int comp) { if (Chart[s, t, dir, comp, 0] != null) { int len = Chart.GetLength(4); var items = new ParseForestItem[len]; for (int i = 0; i < len; i++) { items[i] = Chart[s, t, dir, comp, i]; } return items; } return null; }
public string GetDepString(ParseForestItem pfi) { if (pfi.Left == null) return ""; if (pfi.Comp == 0) { return (GetDepString(pfi.Left) + " " + GetDepString(pfi.Right)).Trim(); } if (pfi.Dir == 0) { return ((GetDepString(pfi.Left) + " " + GetDepString(pfi.Right)).Trim() + " " + pfi.S + "|" + pfi.T + ":" + pfi.Type).Trim(); } return (pfi.T + "|" + pfi.S + ":" + pfi.Type + " " + (GetDepString(pfi.Left) + " " + GetDepString(pfi.Right)).Trim()).Trim(); }