public Configuration(ICoreMap sentence) { this.stack = new List <int>(); this.buffer = new List <int>(); this.tree = new DependencyTree(); this.sentence = sentence; }
public Configuration(Edu.Stanford.Nlp.Parser.Nndep.Configuration config) { stack = new List <int>(config.stack); buffer = new List <int>(config.buffer); tree = new DependencyTree(config.tree); sentence = new CoreLabel(config.sentence); }
public virtual bool HasOtherChild(int k, DependencyTree goldTree) { for (int i = 1; i <= tree.n; ++i) { if (goldTree.GetHead(i) == k && tree.GetHead(i) != k) { return(true); } } return(false); }
internal override bool IsOracle(Configuration c, string t, DependencyTree dTree) { if (!CanApply(c, t)) { return(false); } if (t.StartsWith("L") && !dTree.GetLabel(c.GetStack(1)).Equals(Sharpen.Runtime.Substring(t, 2, t.Length - 1))) { return(false); } if (t.StartsWith("R") && !dTree.GetLabel(c.GetStack(0)).Equals(Sharpen.Runtime.Substring(t, 2, t.Length - 1))) { return(false); } Configuration ct = new Configuration(c); Apply(ct, t); return(CanReach(ct, dTree)); }
// O(n) implementation public override string GetOracle(Configuration c, DependencyTree dTree) { int w1 = c.GetStack(1); int w2 = c.GetStack(0); if (w1 > 0 && dTree.GetHead(w1) == w2) { return("L(" + dTree.GetLabel(w1) + ')'); } else { if (w1 >= 0 && dTree.GetHead(w2) == w1 && !c.HasOtherChild(w2, dTree)) { return("R(" + dTree.GetLabel(w2) + ')'); } else { return("S"); } } }
public static void WriteConllFile(string outFile, IList <ICoreMap> sentences, IList <DependencyTree> trees) { try { PrintWriter output = IOUtils.GetPrintWriter(outFile); for (int i = 0; i < sentences.Count; i++) { ICoreMap sentence = sentences[i]; DependencyTree tree = trees[i]; IList <CoreLabel> tokens = sentence.Get(typeof(CoreAnnotations.TokensAnnotation)); for (int j = 1; j <= size; ++j) { CoreLabel token = tokens[j - 1]; output.Printf("%d\t%s\t_\t%s\t%s\t_\t%d\t%s\t_\t_%n", j, token.Word(), token.Tag(), token.Tag(), tree.GetHead(j), tree.GetLabel(j)); } output.Println(); } output.Close(); } catch (Exception e) { throw new RuntimeIOException(e); } }
/// <summary> /// Determine whether applying the given transition in the given /// configuration tree will leave in us a state in which we can reach /// the gold tree. /// </summary> /// <remarks> /// Determine whether applying the given transition in the given /// configuration tree will leave in us a state in which we can reach /// the gold tree. (Useful for building a dynamic oracle.) /// </remarks> internal abstract bool IsOracle(Configuration c, string t, DependencyTree dTree);
/// <summary> /// Provide a static-oracle recommendation for the next parsing step /// to take. /// </summary> /// <param name="c">Current parser configuration</param> /// <param name="dTree">Gold tree which parser needs to reach</param> /// <returns>Transition string</returns> public abstract string GetOracle(Configuration c, DependencyTree dTree);
// TODO replace with GrammaticalStructure#readCoNLLGrammaticalStructureCollection public static void LoadConllFile(string inFile, IList <ICoreMap> sents, IList <DependencyTree> trees, bool unlabeled, bool cPOS) { CoreLabelTokenFactory tf = new CoreLabelTokenFactory(false); try { using (BufferedReader reader = IOUtils.ReaderFromString(inFile)) { IList <CoreLabel> sentenceTokens = new List <CoreLabel>(); DependencyTree tree = new DependencyTree(); foreach (string line in IOUtils.GetLineIterable(reader, false)) { string[] splits = line.Split("\t"); if (splits.Length < 10) { if (sentenceTokens.Count > 0) { trees.Add(tree); ICoreMap sentence = new CoreLabel(); sentence.Set(typeof(CoreAnnotations.TokensAnnotation), sentenceTokens); sents.Add(sentence); tree = new DependencyTree(); sentenceTokens = new List <CoreLabel>(); } } else { string word = splits[1]; string pos = cPOS ? splits[3] : splits[4]; string depType = splits[7]; int head = -1; try { head = System.Convert.ToInt32(splits[6]); } catch (NumberFormatException) { continue; } CoreLabel token = tf.MakeToken(word, 0, 0); token.SetTag(pos); token.Set(typeof(CoreAnnotations.CoNLLDepParentIndexAnnotation), head); token.Set(typeof(CoreAnnotations.CoNLLDepTypeAnnotation), depType); sentenceTokens.Add(token); if (!unlabeled) { tree.Add(head, depType); } else { tree.Add(head, Config.Unknown); } } } } } catch (IOException e) { throw new RuntimeIOException(e); } }
// NOTE: need to check the correctness again. private static bool CanReach(Configuration c, DependencyTree dTree) { int n = c.GetSentenceSize(); for (int i = 1; i <= n; ++i) { if (c.GetHead(i) != Config.Nonexist && c.GetHead(i) != dTree.GetHead(i)) { return(false); } } bool[] inBuffer = new bool[n + 1]; bool[] depInList = new bool[n + 1]; int[] leftL = new int[n + 2]; int[] rightL = new int[n + 2]; for (int i_1 = 0; i_1 < c.GetBufferSize(); ++i_1) { inBuffer[c.buffer[i_1]] = true; } int nLeft = c.GetStackSize(); for (int i_2 = 0; i_2 < nLeft; ++i_2) { int x = c.stack[i_2]; leftL[nLeft - i_2] = x; if (x > 0) { depInList[dTree.GetHead(x)] = true; } } int nRight = 1; rightL[nRight] = leftL[1]; for (int i_3 = 0; i_3 < c.GetBufferSize(); ++i_3) { // boolean inList = false; int x = c.buffer[i_3]; if (!inBuffer[dTree.GetHead(x)] || depInList[x]) { rightL[++nRight] = x; depInList[dTree.GetHead(x)] = true; } } int[][] g = new int[][] { }; for (int i_4 = 1; i_4 <= nLeft; ++i_4) { for (int j = 1; j <= nRight; ++j) { g[i_4][j] = -1; } } g[1][1] = leftL[1]; for (int i_5 = 1; i_5 <= nLeft; ++i_5) { for (int j_1 = 1; j_1 <= nRight; ++j_1) { if (g[i_5][j_1] != -1) { int x = g[i_5][j_1]; if (j_1 < nRight && dTree.GetHead(rightL[j_1 + 1]) == x) { g[i_5][j_1 + 1] = x; } if (j_1 < nRight && dTree.GetHead(x) == rightL[j_1 + 1]) { g[i_5][j_1 + 1] = rightL[j_1 + 1]; } if (i_5 < nLeft && dTree.GetHead(leftL[i_5 + 1]) == x) { g[i_5 + 1][j_1] = x; } if (i_5 < nLeft && dTree.GetHead(x) == leftL[i_5 + 1]) { g[i_5 + 1][j_1] = leftL[i_5 + 1]; } } } } return(g[nLeft][nRight] != -1); }