Example #1
0
        public static String GetAmrNode(IGraph g, AMRNode Parent)
        {
            String            result  = string.Empty;
            SparqlQueryParser qparser = new SparqlQueryParser();

            //{?r = http://amr.isi.edu/rdf/amr-terms#null_edge , ?p = http://amr.isi.edu/amr_data/1#x1}

            StringBuilder querystr = new StringBuilder();

            querystr.AppendLine("PREFIX amr-terms: <http://amr.isi.edu/rdf/amr-terms#>");
            querystr.AppendLine("SELECT ?r ?p WHERE { <" + Parent.Id + ">  amr-terms:location ?p }");
            SparqlQuery q = qparser.ParseFromString(querystr.ToString());

            var rset = (SparqlResultSet)g.ExecuteQuery(q);
            var SB   = new StringBuilder();

            if (rset.Result && rset.Results.Count > 0)
            {
                foreach (var set in rset.Results)
                {
                    foreach (var r in set)
                    {
                        result = r.Value.ToString();
                    }
                }
            }
            return(result);
        }
Example #2
0
        public void SaveNode(IGraphClient client, AMRNode Node, AMRNode Parent, AMREduSentence Sentence)
        {
            this.CreateNode(client, Node);

            //NODE HAS
            //client.Cypher.Match("(node:AMRNode)", "(edunode:EduSentence)")
            //.Where((AmrNode node) => node.Id == Node.Id)
            //.AndWhere((EduSentence edunode) => edunode.Id == Sentence.Id)
            //.Create("(edunode)-[:HAS]->(node)").ExecuteWithoutResults();

            if (Parent != null)
            {
                if (Node.Direction)
                {
                    client.Cypher.Match("(node:AMRNode)", "(child:AMRNode)")
                    .Where((AMRNode node) => node.Id == Parent.Id)
                    .AndWhere((AMRNode child) => child.Id == Node.Id)
                    .Create(string.Format("(node)-[r:wordrel {{name:'{0}',inverse:{1}, url:'{2}', argname: '{3}' }}]->(child)", Node.RelationName, !Node.Direction, Node.Relation, CalculateRelationArgName(Parent, Node))).ExecuteWithoutResults();
                }
                else
                {
                    client.Cypher.Match("(node:AMRNode)", "(child:AMRNode)")
                    .Where((AMRNode node) => node.Id == Parent.Id)
                    .AndWhere((AMRNode child) => child.Id == Node.Id)
                    .Create(string.Format("(child)-[r:wordrel {{name:'{0}',inverse:{1}, url:'{2}', argname: '{3}' }}]->(node)", Node.RelationName, !Node.Direction, Node.Relation, CalculateRelationArgName(Parent, Node))).ExecuteWithoutResults();
                }
            }

            foreach (var child in Node.Nodes)
            {
                this.SaveNode(client, child, Node, Sentence);
            }
        }
Example #3
0
 private void ToList(AMRNode node, List <AMRNode> state)
 {
     if (state.Contains(node))
     {
         return;
     }
     state.Add(node);
     foreach (var item in node.Nodes)
     {
         ToList(item, state);
     }
 }
Example #4
0
 private void CreateNode(IGraphClient client, AMRNode Node)
 {
     client.Cypher.Merge("(d:AMRNode { Id : {id}})").OnCreate()
     .Set("d = {newObject}")
     .WithParams(
         new
     {
         id        = Node.Id,
         newObject = new { Id = Node.Id, Name = Node.Name, Node.PropBank, Node.PropBankName, Node.Description, Node.AmrType, Node.RSTWeight }
     }
         ).ExecuteWithoutResults();
 }
Example #5
0
        public void LoadData(IGraph graph, AMRNode Target)
        {
            SparqlQueryParser qparser  = new SparqlQueryParser();
            StringBuilder     querystr = new StringBuilder();

            querystr.AppendLine("PREFIX amr-core: <http://amr.isi.edu/rdf/core-amr#>");
            querystr.AppendLine("PREFIX amr-data: <http://amr.isi.edu/amr_data#>");
            querystr.AppendLine("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>");
            querystr.AppendLine("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>");
            querystr.AppendLine("PREFIX amr-terms: <http://amr.isi.edu/rdf/amr-terms#>");
            //querystr.AppendLine("SELECT  ?p WHERE { ?s rdf:type ?p }");
            //querystr.Append("SELECT ?s ?sentence ?id ?root ?rtype ?amrtype");
            querystr.Append("SELECT ?rtype ?amrtype ?amrtypelbl ");
            querystr.Append("WHERE {");
            querystr.Append("@root rdf:type ?rtype. ");
            querystr.Append("?rtype rdf:type ?amrtype. ");
            querystr.Append("?amrtype rdfs:label ?amrtypelbl ");
            querystr.Append("}");

            SparqlParameterizedString command = new SparqlParameterizedString();

            command.CommandText = querystr.ToString();
            command.SetUri("root", Target.Id);

            SparqlQuery q = qparser.ParseFromString(command);

            var rset = (SparqlResultSet)graph.ExecuteQuery(q);
            var SB   = new StringBuilder();

            if (rset.Result && rset.Results.Count > 0)
            {
                foreach (var result in rset.Results)
                {
                    foreach (var r in result)
                    {
                        if (r.Key == "rtype")
                        {
                            Target.PropBank = ((UriNode)r.Value).Uri;
                        }
                        if (r.Key == "amrtypelbl")
                        {
                            Target.AmrType = r.Value.ToString();
                        }
                        if (r.Key == "amrtype")
                        {
                            Target.AmrTypeUri = ((UriNode)r.Value).Uri;
                        }
                    }
                }
            }
        }
Example #6
0
 public string CalculateRelationArgName(AMRNode Parent, AMRNode Child)
 {
     if (Child.Relation.ToString().StartsWith("http://amr.isi.edu/rdf/amr-terms"))
     {
         return(Child.Relation.Fragment.Replace("#", ""));
     }
     else if (Child.Direction)
     {
         return(Child.Relation.ToString().Replace(Parent.PropBank.ToString(), "").Replace(".", ""));
     }
     else
     {
         return(Child.Relation.ToString().Replace(Child.PropBank.ToString(), "OF-").Replace(".", ""));
     }
 }
Example #7
0
        public void LoadRoot(IGraph graph)
        {
            SparqlQueryParser qparser  = new SparqlQueryParser();
            StringBuilder     querystr = new StringBuilder();

            querystr.AppendLine("PREFIX amr-core: <http://amr.isi.edu/rdf/core-amr#>");
            querystr.AppendLine("PREFIX amr-data: <http://amr.isi.edu/amr_data#>");
            querystr.AppendLine("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>");
            querystr.AppendLine("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>");
            querystr.AppendLine("PREFIX amr-terms: <http://amr.isi.edu/rdf/amr-terms#>");
            querystr.Append("SELECT ?root ");
            querystr.Append("WHERE {");
            querystr.Append("@sentenceuri amr-core:root ?root ");
            querystr.Append("}");

            SparqlParameterizedString command = new SparqlParameterizedString();

            command.CommandText = querystr.ToString();
            command.SetUri("sentenceuri", this.urlid);

            SparqlQuery q = qparser.ParseFromString(command);

            var rset = (SparqlResultSet)graph.ExecuteQuery(q);
            var SB   = new StringBuilder();

            if (rset.Result && rset.Results.Count > 0)
            {
                foreach (var result in rset.Results)
                {
                    this.Root    = new AMRNode();
                    this.Root.Id = ((UriNode)result.ElementAt(0).Value).Uri;
                    LoadData(graph, this.Root);
                    LoadChildren(graph, this.Root, new List <AMRNode>()
                    {
                        this.Root
                    });
                }
            }
        }
Example #8
0
 private void CreateRootRelation(IGraphClient client, AMREduSentence sentence, AMRNode Root)
 {
     client.Cypher.Match("(edu:EduSentence)", "(node:AMRNode)")
     .Where((AMREduSentence edu) => edu.Id == sentence.Id)
     .AndWhere((AMRNode node) => node.Id == Root.Id)
     .Create("(edu)-[:Root]->(node)").ExecuteWithoutResults();
 }
Example #9
0
        private List <AMRNode> FindInvertNodes(IGraph graph, AMRNode Parent, List <AMRNode> Historic)
        {
            List <AMRNode>    response = new List <AMRNode>();
            SparqlQueryParser qparser  = new SparqlQueryParser();
            StringBuilder     querystr = new StringBuilder();

            querystr.AppendLine("PREFIX amr-core: <http://amr.isi.edu/rdf/core-amr#>");
            querystr.AppendLine("PREFIX amr-data: <http://amr.isi.edu/amr_data#>");
            querystr.AppendLine("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>");
            querystr.AppendLine("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>");
            querystr.AppendLine("PREFIX amr-terms: <http://amr.isi.edu/rdf/amr-terms#>");
            querystr.Append("SELECT ?child ?amrrelation ");
            querystr.Append("WHERE {");
            //querystr.Append("?amrrelation rdf:type amr-core:Role. ");
            querystr.Append("?child ?amrrelation @parenturi ");
            querystr.Append("}");

            SparqlParameterizedString command = new SparqlParameterizedString();

            command.CommandText = querystr.ToString();
            command.SetUri("parenturi", Parent.Id);

            SparqlQuery q = qparser.ParseFromString(command);

            var rset = (SparqlResultSet)graph.ExecuteQuery(q);
            var SB   = new StringBuilder();

            if (rset.Result && rset.Results.Count > 0)
            {
                foreach (var result in rset.Results)
                {
                    var child = new AMRNode();
                    foreach (var r in result)
                    {
                        if (r.Key == "child")
                        {
                            if (r.Value is UriNode)
                            {
                                child.Id = ((UriNode)r.Value).Uri;
                            }
                            else if (r.Value is LiteralNode)
                            {
                                child.Id = new Uri(string.Format("http://literal/#{0}", value.ToString()));
                            }
                            else
                            {
                                throw new ApplicationException("Problem");
                            }
                        }
                        if (r.Key == "amrrelation")
                        {
                            child.Relation  = ((UriNode)r.Value).Uri;
                            child.Direction = false;
                        }
                    }
                    //only data object and avoid recursive
                    if (child.Id.ToString().Contains("http://amr.isi.edu/amr_data") &&
                        !child.Id.ToString().EndsWith("root01") &&
                        Historic.Where(c => c.Id.ToString() == child.Id.ToString()).Count() == 0
                        )
                    {
                        this.LoadData(graph, child);
                        response.Add(child);
                    }
                }
            }
            return(response);
        }
Example #10
0
        private void LoadChildren(IGraph graph, AMRNode Parent, List <AMRNode> Historic)
        {
            //if (Parent.Id.ToString() == "http://amr.isi.edu/amr_data/3#x5")
            //    Debugger.Break();

            SparqlQueryParser qparser  = new SparqlQueryParser();
            StringBuilder     querystr = new StringBuilder();

            querystr.AppendLine("PREFIX amr-core: <http://amr.isi.edu/rdf/core-amr#>");
            querystr.AppendLine("PREFIX amr-data: <http://amr.isi.edu/amr_data#>");
            querystr.AppendLine("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>");
            querystr.AppendLine("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>");
            querystr.AppendLine("PREFIX amr-terms: <http://amr.isi.edu/rdf/amr-terms#>");
            querystr.Append("SELECT ?child ?amrrelation ");
            querystr.Append("WHERE {");
            querystr.Append("@parenturi ?amrrelation ?child ");
            querystr.Append("}");

            SparqlParameterizedString command = new SparqlParameterizedString();

            command.CommandText = querystr.ToString();
            command.SetUri("parenturi", Parent.Id);

            SparqlQuery q = qparser.ParseFromString(command);

            var rset = (SparqlResultSet)graph.ExecuteQuery(q);
            var SB   = new StringBuilder();

            if (rset.Result && rset.Results.Count > 0)
            {
                foreach (var result in rset.Results)
                {
                    var child = new AMRNode();
                    foreach (var r in result)
                    {
                        if (r.Key == "child")
                        {
                            if (r.Value is UriNode)
                            {
                                child.Id = ((UriNode)r.Value).Uri;
                            }
                            else if (r.Value is LiteralNode)
                            {
                                child.Id = new Uri(string.Format("http://literal/#{0}", value.ToString()));
                            }
                            else
                            {
                                throw new ApplicationException("Problem");
                            }
                        }
                        if (r.Key == "amrrelation")
                        {
                            child.Relation = ((UriNode)r.Value).Uri;
                        }
                    }
                    //only data object and avoid recursive
                    if (child.Id.ToString().Contains("http://amr.isi.edu/amr_data") &&
                        child.Id.ToString() != Parent.Id.ToString() &&
                        Historic.Where(c => c.Id.ToString() == child.Id.ToString()).Count() == 0
                        )
                    {
                        this.LoadData(graph, child);
                        Parent.Nodes.Add(child);
                        Historic.Add(child);
                    }
                }
            }
            //find invert

            var nodes = this.FindInvertNodes(graph, Parent, Historic);

            foreach (var node in nodes)
            {
                Parent.Nodes.Add(node);
                Historic.Add(node);
            }

            foreach (var item in Parent.Nodes)
            {
                this.LoadChildren(graph, item, Historic);
            }
        }