public static string Rank(this Uri uri, Uri endPointUri) { var endpoint = new SparqlRemoteEndpoint(endPointUri); var querySb = new StringBuilder(); querySb.AppendLine(Prefixes.GetSparqlPrefix("rdfs")); querySb.AppendLine($"select ?rank"); querySb.AppendLine($"where {{"); querySb.AppendLine($" <{uri}> <http://purl.obolibrary.org/obo/ncbitaxon#has_rank> ?r ."); querySb.AppendLine($" ?r rdfs:label ?rank ."); querySb.AppendLine($"}}"); var results = endpoint.QueryWithResultSet(querySb.ToString()); if (results.Count != 1) { throw new Exception("Unexpected results."); } results[0].TryGetValue("rank", out var node); if (!(node is LiteralNode literalNode)) { throw new Exception("Unexpected results."); } return(literalNode.Value); }
public static List<KeyValuePair<Uri, string>> StaticSearch(string word, Uri endPointUri) { var endpoint = new SparqlRemoteEndpoint(endPointUri); var querySb = new StringBuilder(); querySb.AppendLine(Prefixes.GetSparqlPrefix("rdfs")); querySb.AppendLine($"select ?subject"); querySb.AppendLine($"where {{"); querySb.AppendLine($" ?subject rdfs:label \"{word.Replace("\r", "").Replace("\n", "")}\""); querySb.AppendLine($"}}"); var query = querySb.ToString(); var results = endpoint.QueryWithResultSet(query); var list = new List<KeyValuePair<Uri, string>>(); foreach (var result in results) { if (result.TryGetValue<UriNode>("subject", out var subjectNode)) { list.Add(new KeyValuePair<Uri, string>(subjectNode.Uri, word)); } } return list; }
/// <summary> /// Searches for nodes that have an rdfs:label containing a given string. /// </summary> /// <param name="word">The string the label has to contain.</param> /// <param name="endPointUri">The SPARQL end-point.</param> /// <returns></returns> public static List<KeyValuePair<Uri, string>> ContainsSearch(string word, Uri endPointUri) { var endpoint = new SparqlRemoteEndpoint(endPointUri); var querySb = new StringBuilder(); querySb.AppendLine(Prefixes.GetSparqlPrefix("rdfs")); querySb.AppendLine($"select ?subject ?label"); querySb.AppendLine($"where {{"); querySb.AppendLine($" ?subject rdfs:label ?label"); querySb.AppendLine($" filter contains(lcase(str(?label)), '{word.ToLower()}')"); querySb.AppendLine($"}}"); var results = endpoint.QueryWithResultSet(querySb.ToString()); var list = new List<KeyValuePair<Uri, string>>(); foreach (var result in results) { if (result.TryGetValue<UriNode>("subject", out var subjectNode) && result.TryGetValue<LiteralNode>("label", out var labelNode)) { list.Add(new KeyValuePair<Uri, string>(subjectNode.Uri, labelNode.Value)); } } return list; }
public static string Label(this Uri uri, Uri endPointUri) => uri.GetLiteralValue(Prefixes.GetUri("rdfs", "label"), endPointUri);