private void Tagging_Cat(string question) { if (question.Contains("used literature")) { question = question.Replace("used literature", "used_literature"); } lx.getAll().Clear(); int questmark = question.IndexOf('?'); if (question.IndexOf('?') != -1) { question = question.Substring(0, question.Length - 1); } string[] q = question.Split(); bool IsAdded = false; foreach (var w in q) { foreach (var list in lx.Language_tags) { foreach (var name in list) { if (name == w) { lx.Add(new TaggedWord { Word = w, Category = Tag.Object, Tag_TableColumn = list[0] }); IsAdded = true; continue; } } } foreach (var s in lx.value_tags) { if (!IsAdded && string.IsNullOrEmpty(g.isFunction(w)) && (s.Value == w || s.Value.Contains(w))) { lx.Add(new TaggedWord { Word = w, Category = Tag.Value, Tag_TableColumn = s.Column }); IsAdded = true; continue; } } if (!IsAdded) { lx.Add(new TaggedWord { Word = w }); } IsAdded = false; } }
public void POS_Tagging(Lexicon lx) { /* Grammar for the statement language is: # S -> P is AR Ns | P is A | P Is | P Ts P # AR -> a | an*/ var wlist = lx.getAll(); int indexLike = -1; foreach (var w in wlist) { if ('A' <= w.Word[0] && w.Word[0] <= 'Z' && isFunction(w.Word) == "") { w.POSTag = "P"; } string f = isFunction(w.Word); if (!string.IsNullOrEmpty(f)) { w.POSTag = f; if (f == "LIKE") { indexLike = wlist.IndexOf(w); } } } if (indexLike != -1) { for (int i = indexLike + 2; i < wlist.Count; i++) { wlist[indexLike + 1].Word += " " + wlist[i].Word; } wlist.RemoveRange(indexLike + 2, wlist.Count - indexLike - 2); wlist[indexLike + 1].POSTag = "NP"; } if (wlist[1].Word == "is") { if (wlist[2].Word == "a" || wlist[2].Word == "an" || wlist[2].Word == "the") { if (wlist[3].POSTag != "N") { wlist[3].POSTag = "N"; } else if (wlist[2].POSTag != "A") { wlist[3].POSTag = "A"; } } } if (wlist[0].Word == "Whose") { wlist[1].POSTag = "NP"; } for (int i = 0; i < wlist.Count; i++) { if (wlist[i].Word == "of") { wlist[i + 1].POSTag = "NP"; } if (wlist[i].Word == "a" || wlist[i].Word == "an" || wlist[i].Word == "the" || wlist[i].Word == "with") { if (wlist[i + 1].POSTag != "N") { wlist[i + 1].POSTag = "N"; } } if (i > 1 && (wlist[i].Word == "is") && wlist[i - 1].POSTag == "NP" && string.IsNullOrEmpty(wlist[i + 1].POSTag)) { wlist[i + 1].POSTag = "NP"; for (int j = i + 2; j < wlist.Count; j++) { wlist[i + 1].Word += " " + wlist[j].Word; } wlist.RemoveRange(i + 2, wlist.Count - i - 2); } if (string.IsNullOrEmpty(wlist[i].POSTag)) { if (string.IsNullOrEmpty(wlist[i].POSTag)) { wlist[i].POSTag = "NP"; } } } lx.changeLx(wlist); }
public ParseTreeBuilder(Lexicon lx, Grammar g) { var tagged_words = lx.getAll(); top_level = new List <ParseNode>(); foreach (var w in tagged_words) { top_level.Add(new ParseNode { value = w.POSTag, children = null, word = w }); } tree.Add(top_level); while (tree[tree.Count - 1][0].value != "S") { if (tree.Count > 50) { root = null; return; } top_level = new List <ParseNode>(); int count = tree[tree.Count - 1].Count; var child = new List <ParseNode>(); var rule = containsInRight(tree[tree.Count - 1], g); top_level = new List <ParseNode>(); foreach (var n in tree[tree.Count - 1]) { bool isAdded = false; foreach (var r in rule) { var left = leftNode(top_level, r.left); var node = rightNodes(tree[tree.Count - 1], r.right); if (r.left == "S" && node.Count != tree[tree.Count - 1].Count) { continue; } if (node.Contains(n)) { if (left == null || left.word != null) { top_level.Add(new ParseNode { value = r.left, children = node }); } else { if (left.children == null) { left.children = new List <ParseNode>(); } if (!left.children.Contains(n)) { if (leftContains(left.children, n))// && node.Contains(n)) { left.children.Add(n); } else { top_level.Add(new ParseNode { value = r.left, children = node }); } } } isAdded = true; } } if (!isAdded && !top_level.Contains(n)) { top_level.Add(n); } } tree.Add(top_level); } root = tree[tree.Count - 1][0]; }