private List <List <String> > getObjects(List <String> SubjectsURIs, String predicateURI, int limit) { //SparqlRemoteEndpoint endpoint = new SparqlRemoteEndpoint(new Uri("http://localhost:8890/sparql")); List <List <String> > objectsLabels = new List <List <String> >(); foreach (String i in SubjectsURIs) { string query = "select * where {<" + i + "> <" + predicateURI + "> ?obj} limit " + limit; List <String> buff = new List <String>(); //SparqlResultSet results = endpoint.QueryWithResultSet(query); SparqlResultSet results = Request.RequestWithHTTP(query); foreach (SparqlResult result in results) { if (((INode)result[0]).NodeType == NodeType.Uri) { buff.Add(util.getLabel(result.Value("obj").ToString())); } else { string s = ""; s += ((LiteralNode)result.Value("obj")).Value + " " + util.getLabel(((LiteralNode)result.Value("obj")).DataType.ToString()); buff.Add(s); } } objectsLabels.Add(buff); } return(objectsLabels); }
/// <summary> /// Gets label of given node. /// </summary> /// <param name="node">Node</param> /// <returns>String of the node label</returns> public static string getLabelIfAny(string node) { if (node.Contains("http://")) { //QueryHandler.startConnection(); //SparqlResultSet results = QueryHandler.ExecuteQueryWithString("select ?x where {<" + node + "> <http://www.w3.org/2000/01/rdf-schema#label> ?x}");// FILTER (langMatches(lang(?x), \"EN\"))}"); //QueryHandler.closeConnection(); SparqlResultSet results = Request.RequestWithHTTP("select ?x where {<" + node + "> <http://www.w3.org/2000/01/rdf-schema#label> ?x}"); if (results.Count != 0) { return(results[0].Value("x").ToString()); } else { return(node.ToString()); } } else { return(node.ToString()); } }
private List <List <string> > getResultsAsStrings(List <string> queries, string obj) { //QueryProcessor.startConnection(); List <SparqlResultSet> resultSet = new List <SparqlResultSet>(); foreach (string item in queries) { //resultSet.Add(QueryProcessor.ExecuteQueryWithString(item)); resultSet.Add(Request.RequestWithHTTP(item)); } return(convertResultSetListToStrings(resultSet, obj)); }
/// <summary> /// This function returns the redirection page to the pages which have ones /// </summary> /// <param name="uri">This function gets the abstract data of the resource with the URI sent using query</param> /// <returns>the redirection page if found else it returns the same URI</returns> private String useRedirection(String uri) { String query = "select * where {<" + uri + "><http://dbpedia.org/ontology/wikiPageRedirects> ?obj}"; SparqlResultSet results = Request.RequestWithHTTP(query); if (results.Count != 0) { SparqlResult result = results[0]; return(result.Value("obj").ToString()); } else { return(uri); } }
/// <summary> /// This function gets the abstract data of the resource with the URI sent using query /// </summary> /// <param name="SubjectURI">The URI of the subject resource as String</param> /// <returns>Abstract data about the entity of the URI</returns> private String getAbstract(String SubjectURI) { String query = "select * where {<" + SubjectURI + "><http://dbpedia.org/ontology/abstract> ?obj}"; SparqlResultSet results = Request.RequestWithHTTP(query); if (results.Count != 0) { SparqlResult result = results[0]; return(((LiteralNode)result.Value("obj")).Value); } else { return(null); } }
/// <summary> /// This function gets latitude and longitude of the resource if found with the URI sent using query /// </summary> /// <param name="SubjectURI">The URI of the subject resource as String</param> /// <returns>Location object(latitude and longitude)</returns> private Location getLocation(String SubjectURI) { Location location = new Location(); String query = "select * where {<" + SubjectURI + "><http://www.w3.org/2003/01/geo/wgs84_pos#long> ?obj}"; SparqlResultSet resultsLong = Request.RequestWithHTTP(query); query = "select * where {<" + SubjectURI + "><http://www.w3.org/2003/01/geo/wgs84_pos#lat> ?obj}"; SparqlResultSet resultsLat = Request.RequestWithHTTP(query); if (resultsLat.Count != 0 && resultsLong.Count != 0) { SparqlResult resultLat = resultsLat[0]; SparqlResult resultLong = resultsLong[0]; location.Latitude = ((LiteralNode)resultLat.Value("obj")).Value; location.Longitude = ((LiteralNode)resultLong.Value("obj")).Value; } return(location); }
private string check_object_unit(string object_string, string predicate_URI) { string object_temp_unit; predicate_URI = util.encodeURI(predicate_URI); if (object_string.Contains("/XMLSchema")) { object_temp_unit = util.getLabel(util.encodeURI(predicate_URI)); try { object_temp_unit = object_temp_unit.Substring(object_temp_unit.IndexOf('(')); } catch { return(object_temp_unit); } return(object_temp_unit); } else { query_predicate = "select distinct ?predlabel ?predrange where {<" + predicate_URI + "> <http://www.w3.org/2000/01/rdf-schema#label> ?predlabel .<" + predicate_URI + "><http://www.w3.org/2000/01/rdf-schema#range> ?predrange}"; SparqlResultSet predicate_range = Request.RequestWithHTTP(query_predicate); if (predicate_range.Count != 0)//&&predicate_range[0].Value("predlabel")!=null&&predicate_range[0].Value("predlabel")!=null) { object_temp_unit = check_object_unit(predicate_range[0].Value("predlabel").ToString(), predicate_range[0].Value("predrange").ToString()); return(object_temp_unit); } else { object_temp_unit = util.getLabel(util.encodeURI(predicate_URI)); // object_temp_unit = object_temp_unit.Substring(object_temp_unit.IndexOf('(')); //object_temp_unit.Replace("@en", ""); return(object_temp_unit); } } }
/// <summary> /// overload of Execute query /// </summary> /// <param name="input">the query text as string</param> /// <returns></returns> public static SparqlResultSet ExecuteQueryWithString(string input) { //list to hold the results //SparqlResultSet resultSet = new SparqlResultSet(); //try //{ // //just in cas someone didn't open the connection // if (!isConnectionStarted) // startConnection(); // //making the query // Object result = manager.Query(input); // //Object result = manager.ExecuteQuery(input); // resultSet = (SparqlResultSet)result; //} //catch { } //return resultSet;. return(Request.RequestWithHTTP(input)); }
/// <summary> /// this function will check first for dbpedia image link /// </summary> /// <param name="dbpediaUri">e.g http://dbpedia.org/resource/Egypt </param> /// <param name="imgsize">max sizeof the image required</param> /// <returns></returns> public static List <string> retrieve_img(string dbpediaUri, E imgsize = E.small) { // dbpediaUri = util.encodeURI(dbpediaUri); string image_query = "select distinct * where{<" + dbpediaUri + "><http://xmlns.com/foaf/0.1/depiction> ?z}"; SparqlResultSet dbimglink = Request.RequestWithHTTP(image_query); List <string> image_urls = new List <string>(); if (dbimglink.Count != 0 && check_link(dbimglink[0].Value("z").ToString())) { image_urls.Add(dbpediaUri); return(image_urls); } else if (dbimglink.Count != 0) { image_urls.Add(get_fb_link(dbpediaUri, imgsize)); return(image_urls); } else { return(null); } }
/// <summary> /// This function returns entities of subjects or objects depending on the first parameter /// if the result of the query is literal, its entity will have label only not URI /// </summary> /// <param name="type">If the query is responsible for getting objects or subjects</param> /// <param name="SubjectURI">The URI of the subject resource as String</param> /// <param name="predicateURI">The URI of the predicate as String</param> /// <param name="resultLimit">Max number of results got from queries</param> /// <returns>The entities got from the queries done in the function</returns> private List <Entity> getQueryResults(String type, String SubjectURI, String predicateURI, int resultLimit) { String query = ""; if (type == "getObjects") { query += "select * where {<" + SubjectURI + "><" + predicateURI + "> ?obj} limit " + resultLimit; } else if (type == "getSubjects") { query += "select * where{ ?obj <" + predicateURI + "> <" + SubjectURI + ">} limit " + resultLimit; } SparqlResultSet results = Request.RequestWithHTTP(query); List <Entity> entities = new List <Entity>(); foreach (SparqlResult result in results) { Entity en = new Entity(); if ((((INode)result[0]).NodeType == NodeType.Uri)) { if (util.isInternalURI(result.Value("obj").ToString())) { en.Label = util.getLabel(result.Value("obj").ToString()); en.URI = result.Value("obj").ToString(); } else //Hack to get webpage without considering it URIs { en.Label = result.Value("obj").ToString(); } } else { en.Label = ((LiteralNode)result.Value("obj")).Value; } entities.Add(en); } return(entities); }
private static int scorecalc(string keyword, SparqlResult singleuri) { List <string> disambiguate_links = new List <string>(); string check_disambig; int score_redirect = 0; int score_resource = 0; SparqlResultSet result; score_resource = computeLevenshteinDistance(keyword, singleuri.Value("literal").ToString().Replace("@en", "")); if (singleuri.Value("redirects") != null) { if (disambiguate_links.Contains(singleuri.Value("redirects").ToString())) { return(1000); } check_disambig = "ASK where {<" + util.encodeURI(singleuri.Value("redirects").ToString()) + "> <http://dbpedia.org/ontology/wikiPageDisambiguates> ?disamb }"; result = Request.RequestWithHTTP(check_disambig); if (result.Result == true) { disambiguate_links.Add(singleuri.Value("redirects").ToString()); return(1000); } string redirect_query = "select * where{ <" + singleuri.Value("redirects").ToString() + "><http://www.w3.org/2000/01/rdf-schema#label> ?redirect_label}"; result = Request.RequestWithHTTP(redirect_query); if (result.Count != 0) { score_redirect = computeLevenshteinDistance(keyword, result[0].Value("redirect_label").ToString().Replace("@en", "")); return(score_redirect <= score_resource ? score_redirect : score_resource); } else { return(score_resource); } } return(score_resource); }
/// <summary> /// takes the list of Predicates and add tothem the domain and Range data needed in two steps /// 1st : searching for direct Domain and ranges /// 2nd : searching for predicates who don't have domain and ranges and select the all predicates and objects /// get their types and add them to the domain and range field /// </summary> /// <param name="predicateList">the predicate list without the domain and range data</param> /// <returns>the predicate list with the predicates have domain and ranges</returns> private List <LexiconPredicate> addDomainAndRange(List <LexiconPredicate> predicateList) { //now interating over the final predicate list and fill the rest of it's details <Domain , Range> // step 1 : the Direct Way foreach (LexiconPredicate x in predicateList) { string Query = "Select distinct ?domain ?range where { {" + "<" + x.URI + ">" + "<http://www.w3.org/2000/01/rdf-schema#domain> ?domain.}" + "union { <" + x.URI + ">" + " <http://www.w3.org/2000/01/rdf-schema#range> ?range ." + "}}"; //QueryHandler.startConnection(); //SparqlResultSet resulSet = QueryHandler.ExecuteQueryWithString(Query); //QueryHandler.closeConnection(); SparqlResultSet resulSet = Request.RequestWithHTTP(Query); if (resulSet.Count() != 0) { foreach (SparqlResult result in resulSet) { // check that the domain field is not empty // and check that this domain wasn't added before if (result.Value("domain") != null) { if (!x.domains.Contains(result.Value("domain").ToString())) { x.domains.Add(result.Value("domain").ToString()); } } // check that the range field is not empty // and check that this range wasn't added before if (result.Value("range") != null) { if (!x.ranges.Contains(result.Value("range").ToString()) && result.Value("range") != null) { x.ranges.Add(result.Value("range").ToString()); } } } } } List <LexiconPredicate> toRemove = new List <LexiconPredicate>(); //step2 : the inDirect Way -- for predicates that didn't have a Domain or range selected before foreach (LexiconPredicate x in predicateList) { bool hasDomain = (x.domains.Count == 0) ? false : true; bool hasRange = (x.ranges.Count == 0) ? false : true; if (!hasDomain && !hasRange) { toRemove.Add(x); continue; } string Query = "Select distinct "; Query += (hasDomain) ? "" : "?domain"; Query += (hasRange) ? "" : "?range"; Query += " where {{"; if (!hasDomain) { Query += "{ ?X <" + x.URI + "> ?Y ." + "?X a ?domain . " + "filter(!REGEX(STR(?domain) ,'http://www.w3.org/2002/07/owl#Thing','i'))" + "}"; } Query += "}"; if (!hasRange) { Query += "union { ?X <" + x.URI + "> ?Y ." + "?Y a ?range . " + "filter(!REGEX(STR(?range) ,'http://www.w3.org/2002/07/owl#Thing','i'))" + "}"; } Query += "}"; Query += "limit 20 "; //QueryHandler.startConnection(); //SparqlResultSet resulSet = QueryHandler.ExecuteQueryWithString(Query); //QueryHandler.closeConnection(); SparqlResultSet resulSet = Request.RequestWithHTTP(Query); if (resulSet.Count() != 0) { foreach (SparqlResult result in resulSet) { // check that the domain field is not empty // and check that this domain wasn't added before if (!hasDomain) { if (result.Value("domain") != null) { if (!x.domains.Contains(result.Value("domain").ToString())) { x.domains.Add(result.Value("domain").ToString()); } } } // check that the range field is not empty // and check that this range wasn't added before if (!hasRange) { if (result.Value("range") != null) { if (!x.ranges.Contains(result.Value("range").ToString()) && result.Value("range") != null) { x.ranges.Add(result.Value("range").ToString()); } } } } } } foreach (LexiconPredicate x in toRemove) { predicateList.Remove(x); } return(predicateList); }
private String findURIs(List <String> subjectsNames) { //SparqlRemoteEndpoint remoteEndPoint = new SparqlRemoteEndpoint(new Uri("http://*****:*****@en }"; //result = remoteEndPoint.QueryWithResultSet(query); result = Request.RequestWithHTTP(query); //QueryProcessor.closeConnection(); if (result.Count == 0) { //a panic mode to be added to generate a more generic query with more results uris.Add(""); continue; } else if (result.Count == 1) { if ((result[0].Value("redirects") == null)) { uris.Add(result[0].Value("subject").ToString()); } else { uris.Add(result[0].Value("redirects").ToString()); } continue; } else { int new_value; int min_value = 1000; int max_index = 0; for (int j = 0; j < result.Count; j++) { new_value = (computeLevenshteinDistance(subjectsNames[i], result[j].Value("literal").ToString())); scores.Add(new_value); if (new_value < min_value) { max_index = j; min_value = new_value; } else if (new_value == min_value) { if (result[j].Value("redirects") == null) { max_index = j; min_value = new_value; } else { min_value = new_value; } } } if ((result[max_index].Value("redirects") == null)) { uris.Add(result[max_index].Value("subject").ToString()); } else { uris.Add(result[max_index].Value("redirects").ToString()); } min_value = 0; } } comma_sep_uris = string.Join(",", uris.ToArray()); return(comma_sep_uris); }
/// <summary> /// gets the relations between a list of uris the relations is in form of predicates and objects /// </summary> /// <param name="uri"> is the list of URI to fnd relation between</param> /// <param name="Distance">find the relations that lies at this distance ie : if distance = 3 there are 3 predicates between two URIs</param> /// <param name="Limit">the limit of relations to find between each two URIs</param> /// <returns>list of containing lists , each lists contains strings in order to discribe the relation /// Egypt >> type >> populated place << type<< syria /// </returns> public static List <List <string> > getRelations(List <string> uri, int Distance, int Limit = 50) { List <string> ignoredPredicates = Regex.Split(File.ReadAllText("ignoredPredicates.txt"), "\r\n|\n").ToList(); List <string> ignoredObjects = Regex.Split(File.ReadAllText("ignoredObjects.txt"), "\r\n|\n").ToList(); //Making combinations of URIs to get relations between List <KeyValuePair <string, string> > URIPairs = getCombinations(uri); SPARQLQueryBuilder QueryBuilder = new SPARQLQueryBuilder(); List <SPARQLQueryBuilder.InnerQuery> Queries = new List <SPARQLQueryBuilder.InnerQuery>(); //Building Queries foreach (KeyValuePair <string, string> pair in URIPairs) { List <SPARQLQueryBuilder.InnerQuery> tmpinnerQueries = QueryBuilder.buildQueries(pair.Key, pair.Value, Distance, Limit, ignoredObjects, ignoredPredicates, 1); Queries.AddRange(tmpinnerQueries); } //executing Queries List <List <string> > URIS = new List <List <string> >(); foreach (SPARQLQueryBuilder.InnerQuery Q in Queries) { //SparqlResultSet resultSet = QueryHandler.ExecuteQueryWithString(Q.queryText); SparqlResultSet resultSet = Request.RequestWithHTTP(Q.queryText); if (Q.connectState == SPARQLQueryBuilder.connectionType.connectedDirectly || Q.connectState == SPARQLQueryBuilder.connectionType.connectedDirectlyInverted) { foreach (SparqlResult result in resultSet) { List <string> toAdd = new List <string>(); toAdd.Add(Q.object1); /// foreach (KeyValuePair<string, INode> row in result.ToList()); for (int i = 1; i <= Distance; i++) { if (result.HasValue("pf" + i)) { toAdd.Add(result.Value("pf" + i).ToString()); } if (result.HasValue("of" + i)) { toAdd.Add(result.Value("of" + i).ToString()); } } toAdd.Add(Q.object2); URIS.Add(toAdd); } } else if (Q.connectState == SPARQLQueryBuilder.connectionType.connectedViaMiddle || Q.connectState == SPARQLQueryBuilder.connectionType.connectedViaMiddleInverted) { foreach (SparqlResult result in resultSet) { List <string> toAdd = new List <string>(); toAdd.Add(Q.object1); bool lastAdded = false; // false for literal . true for predicate for (int i = 1; i < Distance; i++) { if (result.HasValue("pf" + i) || result.HasValue("of" + i)) { if (!lastAdded) { if (result.HasValue("pf" + i)) { toAdd.Add(result.Value("pf" + i).ToString()); lastAdded = true; } if (result.HasValue("of" + i)) { toAdd.Add(result.Value("of" + i).ToString()); lastAdded = false; } } else { if (result.HasValue("of" + i)) { toAdd.Add(result.Value("of" + i).ToString()); lastAdded = false; } if (result.HasValue("pf" + i)) { toAdd.Add(result.Value("pf" + i).ToString()); lastAdded = true; } } } } toAdd.Add(result.Value("middle").ToString()); lastAdded = false; // false for literal . true for predicate for (int i = Distance; i >= 1; i--) { if (result.HasValue("ps" + i) || result.HasValue("os" + i)) { if (!lastAdded) { if (result.HasValue("ps" + i)) { toAdd.Add(result.Value("ps" + i).ToString()); lastAdded = true; } if (result.HasValue("os" + i)) { toAdd.Add(result.Value("os" + i).ToString()); lastAdded = false; } } else { if (result.HasValue("os" + i)) { toAdd.Add(result.Value("os" + i).ToString()); lastAdded = false; } if (result.HasValue("ps" + i)) { toAdd.Add(result.Value("ps" + i).ToString()); lastAdded = true; } } } } toAdd.Add(Q.object2); URIS.Add(toAdd); } } } //relationsComparer c = new relationsComparer(); return(URIS);//.Distinct(c).ToList(); }
/// <summary> /// get predicates is a method in lexicon class that get all LexiconLiterals objects that match some words in the Question /// </summary> /// <param name="question">question to get matched predicates of it </param> /// <param name="topN">the number of top matching results to be returned, default = 10</param> /// <param name="Limit">the limit of the number of returned results in the query, default = 20</param> /// <returns>list of top matching LexiconLiterals with it's type of owner and predicate </returns> public List <LexiconLiteral> getLiterals(string question, int topN = 30, int Limit = 30) { DateTime dt = DateTime.Now; // capturing time for testing List <LexiconLiteral> __literalList = new List <LexiconLiteral>(); //getting all permutation of words formed from the question string List <string> permutationList = getPermutations(question); //removing permutations that most propbably wont return results and will take time in querying permutationList = trimPermutations(permutationList); // to check if the literals are filled before - so returning the matching Literals only or not if (literalFilled) { foreach (LexiconLiteral literal in this.literalList) { if (permutationList.Contains(literal.QuestionMatch)) { __literalList.Add(literal); } } return(__literalList); } else { // iterating over each permutation of Question left and Query them from virtuoso and return predicate list and add them foreach (string questionleft in permutationList) { string Query = "select distinct ?subject ?literal ?redirects ?typeOfOwner ?redirectsTypeOfOwner where{" + "?subject <http://www.w3.org/2000/01/rdf-schema#label> ?literal." + "optional { ?subject <http://dbpedia.org/ontology/wikiPageRedirects> ?redirects . " + "optional {?redirects <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?redirectsTypeOfOwner ." + "}}." + "optional {?subject <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?typeOfOwner}." + "Filter ( !bound(?typeOfOwner) || " + " ( !(?typeOfOwner = <http://www.w3.org/2004/02/skos/core#Concept>)" + " && !(?typeOfOwner = <http://www.w3.org/2002/07/owl#Thing>) " + " && !(?typeOfOwner = <http://www.opengis.net/gml/_Feature>) " + " && !(?typeOfOwner = <http://www.w3.org/2002/07/owl#ObjectProperty>) " + " && !(?typeOfOwner = <http://www.w3.org/1999/02/22-rdf-syntax-ns#Property> ) " + " && !(?typeOfOwner = <http://www.w3.org/2002/07/owl#DatatypeProperty> )" + " && !(?typeOfOwner = <http://www.w3.org/2002/07/owl#Class> )))." + "?literal bif:contains '\"" + questionleft + "\"'.} limit " + Limit; //SparqlRemoteEndpoint remoteEndPoint = new SparqlRemoteEndpoint(new Uri("http://localhost:8890/sparql")); SparqlResultSet resultSet = new SparqlResultSet(); try { //resultSet = remoteEndPoint.QueryWithResultSet(Query); resultSet = Request.RequestWithHTTP(Query); } // skipping results that raised timeout exceptions catch { util.log("skipped Query3: " + questionleft + " ---- due to time out "); } //iterating over matched Literals in the resultset foreach (SparqlResult result in resultSet) { string resultTypeOfOwner = ""; string resultURI; string resultLabel = result.Value("literal").ToString(); string resultquestionMatch = questionleft; if (result.Value("redirects") != null) { resultURI = result.Value("redirects").ToString(); if (result.Value("redirectsTypeOfOwner") != null) { resultTypeOfOwner = result.Value("redirectsTypeOfOwner").ToString(); } } else { resultURI = result.Value("subject").ToString(); if (result.Value("typeOfOwner") != null) { resultTypeOfOwner = result.Value("typeOfOwner").ToString(); } } // check that the predicate doesn't exists in the predicateslist before bool exists = false; // URI + Label only Exists bool totallyExists = false; // URI + Label + TypeofOwner exists in the literal list foreach (LexiconLiteral x in __literalList) { if (x.URI == resultURI && x.label == resultLabel && x.QuestionMatch == resultquestionMatch) { exists = true; if (x.typeOfOwner.Contains(resultTypeOfOwner) && resultTypeOfOwner.Length > 0) { totallyExists = true; break; } } } // adding the new literals to the literallist. if (!exists) { LexiconLiteral tmpLexiconLiteral = new LexiconLiteral(resultURI, resultLabel, resultquestionMatch, resultTypeOfOwner); __literalList.Add(tmpLexiconLiteral); } if (!totallyExists && exists) { foreach (LexiconLiteral let in __literalList) { if (let.URI == resultURI && let.label == resultLabel) { let.typeOfOwner.Add(resultTypeOfOwner); } } } } } //scoring literals . trimming duplicates , __literalList = scoreLiterals(__literalList, topN); util.log("total time taken :" + DateTime.Now.Subtract(dt).TotalMilliseconds.ToString() + " msecs "); literalFilled = true; this.literalList = __literalList; return(__literalList); } }
/// <summary> /// this one will get freebaseimage directly without checking dbpedia link /// </summary> /// <param name="dbpedialink">e.g http://dbpedia.org/resource/Syria </param> /// <param name="imgsize"></param> /// <returns></returns> public static string get_fb_link(string dbpedialink, E imgsize) { //dbpedialink = util.encodeURI(dbpedialink); string SameAs_query = "select distinct ?freebaselink where {<" + dbpedialink + "> <" + sameAs_URI_property + "> ?freebaselink }"; string freebaselink = ""; string entity_freebase_id; // List<string> similaruris = new List<string>() ; // string[] sep = new string[] { "\n" }; //WebClient dondloadtext = new WebClient(); //dondloadtext.QueryString.Add("uri", dbpedialink); //try //{ // temp = dondloadtext.DownloadString(sameasServicelink); // similaruris = temp.Split(sep, StringSplitOptions.RemoveEmptyEntries).ToList<string>(); //} //catch( HttpListenerException noService) //{ // return null; //} SparqlResultSet freebase_sameAs = Request.RequestWithHTTP(SameAs_query); if (freebase_sameAs.Count != 0) { freebaselink = freebase_sameAs[0].Value("freebaselink").ToString(); freebaselink = freebaselink.Replace("<", ""); freebaselink = freebaselink.Replace(">", ""); try { entity_freebase_id = freebaselink.Substring(freebaselink.IndexOf("/m/") + 3); switch (imgsize) { case E.small: return(fb_imageApi + entity_freebase_id + smallimg + freebase_api_key); case E.large: return(fb_imageApi + entity_freebase_id + largeimg + freebase_api_key); case E.medium: return(fb_imageApi + entity_freebase_id + medimg + freebase_api_key); } } catch { string image_query = "select distinct * where{<" + dbpedialink + "><http://xmlns.com/foaf/0.1/depiction> ?z}"; SparqlResultSet dbpedia_imageurl = Request.RequestWithHTTP(image_query); if (dbpedia_imageurl.Count != 0) { return(dbpedia_imageurl[0].Value("z").ToString()); } } } else { string image_query = "select distinct * where{<" + dbpedialink + "><http://xmlns.com/foaf/0.1/depiction> ?z}"; SparqlResultSet dbpedia_imageurl = Request.RequestWithHTTP(image_query); if (dbpedia_imageurl.Count != 0) { return(dbpedia_imageurl[0].Value("z").ToString()); } } switch (imgsize) { case E.small: return(no_imageLink + smallimg + freebase_api_key); case E.large: return(no_imageLink + largeimg + freebase_api_key); case E.medium: return(no_imageLink + medimg + freebase_api_key); } return(no_imageLink + medimg + freebase_api_key); }
/// <summary> /// gets the label of the uri, if can't find, it removes the last / /// </summary> /// <param name="uri"></param> /// <returns></returns> public string getLabel(string uri) { HashSet <KeyValuePair <string, string> > LabelsAlreadyGot = new HashSet <KeyValuePair <string, string> >(); //at least best one for now uri = Uri.EscapeUriString(uri); DateTime now = DateTime.Now; //check to see if we already got it? foreach (KeyValuePair <string, string> item in LabelsAlreadyGot) { if (String.Equals(item.Key, uri)) { return(item.Value); } } //end check //Console.Write("ST=" + (DateTime.Now - now).TotalMilliseconds ); string query = "select * where {<" + uri + "> <http://www.w3.org/2000/01/rdf-schema#label> ?obj}"; //SparqlResultSet results = endpoint.QueryWithResultSet(query); SparqlResultSet results = Request.RequestWithHTTP(query); //if there's no results from the first query, we will try to get the name if (results.Count < 1) { string name_query = "select * where {<" + uri + "> <http://xmlns.com/foaf/0.1/name> ?obj}"; //results = endpoint.QueryWithResultSet(name_query); results = Request.RequestWithHTTP(name_query); //if there's no result from the second query //get the name after the / if (results.Count < 1) { string toreturn = new string(uri.ToCharArray().Reverse().ToArray());//uri.Reverse().ToString(); toreturn = toreturn.Remove(toreturn.IndexOf("/")); toreturn = new string(toreturn.ToCharArray().Reverse().ToArray()); toreturn = toreturn.Replace("_", " "); //TODO : get back the encoding toreturn = toreturn.Trim(); //Add it to the HashSet in RAM LabelsAlreadyGot.Add(new KeyValuePair <string, string>(uri, toreturn)); return(toreturn); } else { //Add it to the HashSet in RAM LabelsAlreadyGot.Add(new KeyValuePair <string, string>(uri, ((LiteralNode)results[0].Value("obj")).Value)); //returning return(((LiteralNode)results[0].Value("obj")).Value); } } else { //Add it to the HashSet in RAM LabelsAlreadyGot.Add(new KeyValuePair <string, string>(uri, ((LiteralNode)results[0].Value("obj")).Value)); //returning it return(((LiteralNode)results[0].Value("obj")).Value); } }
/// <summary> /// This method tries it's best to get a label for the wanted URI /// </summary> /// <param name="URI">uri to get label to</param> /// <returns>the label gotten</returns> public static string getLabel(string URI) { //if the string is empty return it as it is if (URI.Length == 0) { return(URI); } else { //gottenlabels.Select(e=>e //string temp = gottenlabels.Select(e=>e.Key.Equals(URI)).Value; string temp = ""; if (gottenlabels.TryGetValue(URI, out temp)) { return(temp); } else { //URI = Uri.EscapeUriString(URI); if (isInternalURI(URI)) { URI = encodeURI(URI); } if (isInternalURI(URI) && Uri.IsWellFormedUriString(URI, UriKind.Absolute)) { //at least best one for now //SparqlRemoteEndpoint endpoint = new SparqlRemoteEndpoint(new Uri("http://localhost:8890/sparql")); string query = "select * where {<" + URI + "> <http://www.w3.org/2000/01/rdf-schema#label> ?obj}"; //SparqlResultSet results = endpoint.QueryWithResultSet(query); SparqlResultSet results = Request.RequestWithHTTP(query); //if there's no results from the first query, we will try to get the name if (results.Count < 1) { string name_query = "select * where {<" + URI + "> <http://xmlns.com/foaf/0.1/name> ?obj}"; //results = endpoint.QueryWithResultSet(name_query); results = Request.RequestWithHTTP(name_query); //if there's no result from the second query //get the name after the / if (results.Count < 1) { //to decode the uri to remove the %xx characters string newURI = HttpUtility.UrlDecode(URI); while (newURI.Contains("%")) { newURI = HttpUtility.UrlDecode(newURI); } //reversing the string and removing the last / and returning what's after it string toreturn = new string(newURI.ToCharArray().Reverse().ToArray());//URI.Reverse().ToString(); toreturn = toreturn.Remove(toreturn.IndexOf("/")); toreturn = new string(toreturn.ToCharArray().Reverse().ToArray()); toreturn = toreturn.Replace("_", " "); //TODO : get back the encoding toreturn = toreturn.Trim(); //This part to return the one after the # if (toreturn.Contains("#") && toreturn.Length > toreturn.LastIndexOf("#")) { //adding to the hashset addtohashset(URI, toreturn.Substring(toreturn.IndexOf("#") + 1)); //return return(toreturn.Substring(toreturn.IndexOf("#") + 1)); } if (toreturn.Length > 0) { //adding to the hashset addtohashset(URI, toreturn); //return return(toreturn); } //this one is to return if empty else { //adding to the hashset addtohashset(URI, URI); //returning return(newURI); } } else { //adding to the hashset addtohashset(URI, ((LiteralNode)results[0].Value("obj")).Value); //returning return(((LiteralNode)results[0].Value("obj")).Value); } } else { //adding to the hashset addtohashset(URI, ((LiteralNode)results[0].Value("obj")).Value); //returning it return(((LiteralNode)results[0].Value("obj")).Value); } } else { return(URI); } } } }
/// <summary> /// getting entities from the same categories as the original entities and sorting them by the number of occurences /// </summary> /// <param name="uri">the uri to get the related entities for</param> /// <param name="MaxNumberOfEntities">Maximum number of related entities to return</param> /// <returns>list of the related entities</returns> public static List <String> getRelatedEntities(String uri, int MaxNumberOfEntities = 7) { //SparqlRemoteEndpoint endpoint = new SparqlRemoteEndpoint(new Uri("http://weetit:8890/sparql")); List <KeyValuePair <int, String> > relations = new List <KeyValuePair <int, String> >(); Console.WriteLine("Will make queries now "); //Getting Everything List <SparqlResultSet> sets = new List <SparqlResultSet>(); sets.Add(Request.RequestWithHTTP("select ?z where {<" + uri + "> ?x ?z.?z <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Thing> } limit 1000")); sets.Add(Request.RequestWithHTTP("select ?z where {?z ?x <" + uri + ">.?z <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Thing> } limit 1000")); //first stage, get related with the same subject //Query to extract subjects SparqlResultSet cateogires = Request.RequestWithHTTP("select ?z where {<" + uri + "> <http://purl.org/dc/terms/subject> ?z} limit 1000"); //list to hold all items of the same cateogires List <String> allItems = new List <String>(); //adding only the results foreach (SparqlResultSet item in sets) { foreach (SparqlResult res in item) { if (res.Value("z").NodeType == NodeType.Uri) { allItems.Add(((UriNode)res.Value("z")).Uri.ToString()); } } } //adding cateogires foreach (SparqlResult category in cateogires) { //get all entities in all categoreis SparqlResultSet itemsInCategory = Request.RequestWithHTTP( "select ?z where {?z <http://purl.org/dc/terms/subject> <" + ((UriNode)category.Value("z")).Uri.ToString() + ">." + "?z <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Thing>} limit 1000"); foreach (SparqlResult item in itemsInCategory) { allItems.Add(((UriNode)item.Value("z")).Uri.ToString()); } } //reordering var g = allItems.GroupBy(i => i).ToList(); List <KeyValuePair <int, String> > orderedEntities = new List <KeyValuePair <int, String> >(); foreach (var grp in g) { orderedEntities.Add(new KeyValuePair <int, String>(grp.Count(), grp.Key)); } orderedEntities = orderedEntities.OrderByDescending(k => k.Key).ToList(); //removing the original URI (sometimes it appears again, that's why we remove it) orderedEntities.RemoveAll(e => e.Value.Equals(System.Web.HttpUtility.UrlDecode((uri)))); orderedEntities.RemoveAll(e => e.Value.Equals(uri)); //Returning the required list List <String> toReturn = new List <String>(); int max = MaxNumberOfEntities; if (orderedEntities.Count < max) { max = orderedEntities.Count; } for (int i = 0; i < max; i++) { toReturn.Add(orderedEntities[i].Value); } return(toReturn); }
/// <summary> /// get predicates is a method in lexicon class that get all predicates objects that match some words in the Question /// </summary> /// <param name="question">question to get matched predicates of it </param> /// <param name="topN">the number of top matching results to be returned, default = 10</param> /// <param name="Limit">the limit of the number of returned results in the query, default = 20</param> /// <returns>list of top matching LexiconPredicates</returns> public List <LexiconPredicate> getPredicates(string question, int topN = 20, int Limit = 30) { DateTime dt = DateTime.Now; // capturing time for testing List <LexiconPredicate> __predicateList = new List <LexiconPredicate>(); //getting all permutation of words formed from the question string List <string> permutationList = getPermutations(question); //removing permutations that most propbably wont return results and will take time in querying permutationList = trimPermutations(permutationList); //Get the stemmed version of the question words Dictionary <string, List <string> > stemmedWords = GetStemmedWords(question); // to check if the predicates are filled before - so returning the matching predicates only - or not if (predicateFilled) { foreach (LexiconPredicate predicate in predicateList) { if (permutationList.Contains(predicate.QuestionMatch)) { __predicateList.Add(predicate); } } return(__predicateList); } else { string bifContainsValue = ""; // iterating over each permutation of Question left and Query them from virtuoso and return predicate list and add them foreach (string questionleft in permutationList) { //Get all forms of questionLeft by replacing words with its stemmed version bifContainsValue = ""; //empty string bifContainsValue += "\'" + questionleft + "\'"; //add the original questionleft //Replace words in questionleft with its stem and add it to the bifContainsValue foreach (string word in stemmedWords.Keys) { if (questionleft.Contains(word)) { foreach (string stem in stemmedWords[word]) //This is created because a wordcan has many stems (rare case) { bifContainsValue += "or\'" + questionleft.Replace(word, stem) + "\'"; } } } string Query = "SELECT * WHERE { { " + "?predicate <http://www.w3.org/2000/01/rdf-schema#label> ?label ." + "?predicate <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#DatatypeProperty>." + "?label bif:contains \"" + bifContainsValue + "\" } " + "union {" + "?predicate <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#ObjectProperty> ." + "?predicate <http://www.w3.org/2000/01/rdf-schema#label> ?label ." + "?label bif:contains \"" + bifContainsValue + "\" } " + "union {" + "?predicate <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/1999/02/22-rdf-syntax-ns#Property> ." + "?predicate <http://www.w3.org/2000/01/rdf-schema#label> ?label ." + "?label bif:contains \"" + bifContainsValue + "\" } " + "} limit " + Limit; //another Query to Get predicates untill deciding which of them is the best using statistics string Query2 = "SELECT ?predicate ?label WHERE { " + "{ ?predicate <http://www.w3.org/2000/01/rdf-schema#label> ?label . " + "?predicate <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?propertyType. " + "?label bif:contains \"" + bifContainsValue + "\" } " + "filter ( ?propertyType = <http://www.w3.org/2002/07/owl#DatatypeProperty> || " + "?propertyType = <http://www.w3.org/2002/07/owl#ObjectProperty> || " + "?propertyType = <http://www.w3.org/1999/02/22-rdf-syntax-ns#Property> ) " + "} limit " + Limit; //SparqlRemoteEndpoint remoteEndPoint = new SparqlRemoteEndpoint(new Uri("http://localhost:8890/sparql")); try { //executing the Query and finding results //SparqlResultSet resultSet = remoteEndPoint.QueryWithResultSet(Query); SparqlResultSet resultSet = Request.RequestWithHTTP(Query); //iterating over matched predicates in the resultset foreach (SparqlResult result in resultSet) { INode predicateURI = result.Value("predicate"); INode predicateLabel = result.Value("label"); LexiconPredicate tmplexiconpredicate = new LexiconPredicate(); // check that the property is used .. not a non-used property bool hasResuts = false; string checkQuery = "select distinct * where { ?x <" + predicateURI + "> ?y } limit 1 "; //QueryHandler.startConnection(); //SparqlResultSet checkResults = QueryHandler.ExecuteQueryWithString(checkQuery); //QueryHandler.closeConnection(); SparqlResultSet checkResults = Request.RequestWithHTTP(checkQuery); if (checkResults.Count != 0) { hasResuts = true; } // check that the predicate doesn't exists in the predicateslist before bool exists = false; foreach (LexiconPredicate x in __predicateList) { // we added Questionmatch == question left bec new predicates may be added with better score that the old ones so this should be considered if (x.URI == predicateURI.ToString() && x.QuestionMatch == questionleft) { exists = true; break; } } // adding the new predicate to the __predicatelist if (!exists && hasResuts) { tmplexiconpredicate.URI = predicateURI.ToString(); tmplexiconpredicate.QuestionMatch = questionleft; tmplexiconpredicate.label = predicateLabel.ToString(); __predicateList.Add(tmplexiconpredicate); } } } // skipping results that raised timeout exceptions catch { util.log("skipped : " + questionleft + " ---- due to time out "); } } util.log(" finished getting " + __predicateList.Count + " predicates " + " Time taken : " + DateTime.Now.Subtract(dt).TotalMilliseconds + " msec"); // now done of collecting predicates scoring them down and get the best n ones this.predicateList = scorePredicates(__predicateList, topN); this.predicateList = addDomainAndRange(this.predicateList); util.log("total time taken :" + DateTime.Now.Subtract(dt).TotalMilliseconds.ToString() + " mSecs"); predicateFilled = true; return(this.predicateList); } }
public void getData(List <string> Input_URIs, List <string> PredicatesToExclude) { //Generating filter string to add to the queries string excluded = ""; if (PredicatesToExclude.Count > 0) { for (int i = 0; i < PredicatesToExclude.Count; i++) { excluded += ". filter (?pred != <" + PredicatesToExclude[i] + ">)"; } } //should be returned List <ResourceInformation> ResourceInformationList = new List <ResourceInformation>(); foreach (string item in Input_URIs) { //constructing the queries, we had to do FROM<dbpedia.org> to prevent garbage data, should be faster too string querySide1 = "SELECT * WHERE {<" + item + "> ?pred ?obj" + excluded + "}"; string querySide2 = "SELECT * WHERE {?subj ?pred <" + item + ">" + excluded + "}"; //doing both queries SparqlResultSet res1 = new SparqlResultSet(); SparqlResultSet res2 = new SparqlResultSet(); //res1 = endpoint.QueryWithResultSet(querySide1); //res2 = endpoint.QueryWithResultSet(querySide2); res1 = Request.RequestWithHTTP(querySide1); res2 = Request.RequestWithHTTP(querySide2); //Initialize the resource ResourceInformation temp = new ResourceInformation(); temp.predicates_resourceIsSubj = new List <KeyValuePair <string, string> >(); temp.resources_resourceIsSubj = new List <KeyValuePair <string, string> >(); temp.predicates_resourceIsObj = new List <KeyValuePair <string, string> >(); temp.resources_resourceIsObj = new List <KeyValuePair <string, string> >(); temp.rawComparisonObject = new List <KeyValuePair <KeyValuePair <string, string>, List <KeyValuePair <string, string> > > >(); temp.FinalComparisonObject = new List <KeyValuePair <KeyValuePair <string, string>, List <KeyValuePair <string, string> > > >(); temp.ID = new KeyValuePair <string, string>(); //Filling all the information in case <ourResourceID> ?x ?y foreach (SparqlResult item_res1 in res1) { //string tempo=getLabel(((LiteralNode)item_res1.Value("pred")).ToString()); if (String.Equals(((INode)(item_res1.Value("obj"))).GetType().Name.ToString(), "UriNode")) { temp.resources_resourceIsSubj.Add(new KeyValuePair <string, string>(item_res1.Value("obj").ToString(), util.getLabel(item_res1.Value("obj").ToString()))); } else { string onlyValue = ((LiteralNode)item_res1.Value("obj")).Value; temp.resources_resourceIsSubj.Add(new KeyValuePair <string, string>(item_res1.Value("obj").ToString(), onlyValue)); //To fill out the ID component if (String.Equals(item_res1.Value("pred").ToString(), "http://www.w3.org/2000/01/rdf-schema#label")) { temp.ID = new KeyValuePair <string, string>(item, temp.resources_resourceIsSubj[temp.resources_resourceIsSubj.Count - 1].Value); } } temp.predicates_resourceIsSubj.Add(new KeyValuePair <string, string>(item_res1.Value("pred").ToString(), util.getLabel(item_res1.Value("pred").ToString()))); } //Filling all the information in case ?x ?y <ourResourceID> foreach (SparqlResult item_res2 in res2) { //We add of to the name, child of .....etc temp.predicates_resourceIsObj.Add(new KeyValuePair <string, string>(item_res2.Value("pred").ToString(), util.getLabel(item_res2.Value("pred").ToString()) + " of")); if (String.Equals(((INode)(item_res2.Value("subj"))).GetType().Name.ToString(), "UriNode")) { temp.resources_resourceIsObj.Add(new KeyValuePair <string, string>(item_res2.Value("subj").ToString(), util.getLabel(item_res2.Value("subj").ToString()))); } else { string onlyValue = ((LiteralNode)item_res2.Value("subj")).Value; temp.resources_resourceIsSubj.Add(new KeyValuePair <string, string>(item_res2.Value("subj").ToString(), onlyValue)); } } //filling comparison component temp = fillComparisonComponent(temp); //Addding the resource to the list of resourceInformation Objects ResourceInformationList.Add(temp); //Copying it to the globalVariable ComparisonOutput = ResourceInformationList; } //getting common between URIs //Console.WriteLine("getting Common"); ResourceInformationList = getCommon(ResourceInformationList); //logging //logResults(ResourceInformationList); }
public List <questionAnswer> executeQueries(List <QueryBucket> queryBuckets) { List <questionAnswer> answers = new List <questionAnswer>(); foreach (QueryBucket queryBucket in queryBuckets) { if (queryBucket.literalOnly == false) { foreach (string queryString in queryBucket.BucketQueryList) { //SparqlResultSet resultSet = QueryHandler.ExecuteQueryWithString(queryString); SparqlResultSet resultSet = Request.RequestWithHTTP(queryString); if (resultSet.Count > 0) { questionAnswer answer = new questionAnswer(queryBucket.QuestionType); answer.answerCount = resultSet.Results.Count(); foreach (LexiconToken token in queryBucket.tokens.Distinct <LexiconToken>()) { if (token is LexiconPredicate) { answer.predicateList.Add(token as LexiconPredicate); answer.predicateUriList.Add(token.URI, token.QuestionMatch); answer.predicateLabelList.Add(util.getLabel(token.URI), token.QuestionMatch); } else if (token is LexiconLiteral) { answer.subjectList.Add(token as LexiconLiteral); answer.subjectUriList.Add(token.URI, token.QuestionMatch); answer.subjectLabelList.Add(util.getLabel(token.URI), token.QuestionMatch); } } foreach (SparqlResult result in resultSet) { INode subjectNode = result[0]; INode objectNode = result[1]; Type resultType = objectNode.GetType(); if (answer.questiontype == util.questionTypes.literalOrURIAnswer) { if (resultType.Name == "LiteralNode") { answer.questiontype = util.questionTypes.literalAnswer; answer.objectLabelList.Add(objectNode.ToString()); } else if (resultType.Name == "UriNode") { answer.questiontype = util.questionTypes.URIAsnwer; answer.objectLabelList.Add(util.getLabel(objectNode.ToString())); } } else { if (resultType.Name == "LiteralNode") { answer.objectLabelList.Add(objectNode.ToString()); } else if (resultType.Name == "UriNode") { answer.objectLabelList.Add(util.getLabel(objectNode.ToString())); } } answer.objectNodetList.Add(objectNode); answer.objectUriList.Add(objectNode.ToString()); } answers.Add(answer); } } } else { questionAnswer answer = new questionAnswer(); answer.objectUriList.Add(queryBucket.tokens[0].URI); answer.objectLabelList.Add(queryBucket.tokens[0].label); answer.answerCount = 1; answer.questiontype = util.questionTypes.URIAsnwer; answers.Add(answer); } } return(answers); }
private static List <string> Find_URIs(string keyword, int MaxUris) { int levdistance; SparqlResultSet result = new SparqlResultSet(); string query = null; List <int> scores = new List <int>(1); List <string> uris = new List <string>(1); string bifcontains = bifcont_generator(keyword); query = "select distinct ?subject ?literal ?redirects where{" + "?subject <http://www.w3.org/2000/01/rdf-schema#label> ?literal." + "optional { ?subject <http://dbpedia.org/ontology/wikiPageRedirects> ?redirects}." + "optional {?subject <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?type}." + "optional { ?subject <http://dbpedia.org/ontology/wikiPageDisambiguates> ?disamb}" + "Filter ( ( !bound(?type) && !bound(?disamb)) || ( !(?type=<http://www.w3.org/2004/02/skos/core#Concept>)&& !(?type= <http://www.w3.org/1999/02/22-rdf-syntax-ns#Property>))) ." + "?literal bif:contains '" + bifcontains + "'.}" + "limit" + " 100"; result = Request.RequestWithHTTP(query); if (result.Count == 0) { //a panic mode to be added to generate a more generic query with more results bifcontains = bifcont_generator1(keyword); query = "select distinct ?subject ?literal ?redirects where{" + "?subject <http://www.w3.org/2000/01/rdf-schema#label> ?literal." + "optional { ?subject <http://dbpedia.org/ontology/wikiPageRedirects> ?redirects}." + "optional {?subject <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?type}." + "optional { ?subject <http://dbpedia.org/ontology/wikiPageDisambiguates> ?disamb}" + "Filter ( ( !bound(?type) && !bound(?disamb)) || ( !(?type=<http://www.w3.org/2004/02/skos/core#Concept>)&& !(?type= <http://www.w3.org/1999/02/22-rdf-syntax-ns#Property>))) ." + "?literal bif:contains '" + bifcontains + "'.}" + "limit" + " 100"; result = Request.RequestWithHTTP(query); if (result.Count == 0) { return(null); } } int iterator = 0; //add first result to list // levdistance = computeLevenshteinDistance(keyword, result[0].Value("literal").ToString()); levdistance = scorecalc(keyword, result[0]); scores.Add(levdistance); if (result[0].Value("redirects") == null) { uris.Add(result[0].Value("subject").ToString()); } else { uris.Add(result[0].Value("redirects").ToString()); } foreach (SparqlResult uri in result) { bool broke = false; iterator = 0; levdistance = scorecalc(keyword, uri); foreach (int score in scores.ToList()) { if (levdistance <= score) { if (uri.Value("redirects") == null && !(uris.Contains(uri.Value("subject").ToString()))) { scores.Insert(iterator, levdistance); uris.Insert(iterator, uri.Value("subject").ToString()); } else if (uri.Value("redirects") != null && !(uris.Contains(uri.Value("redirects").ToString()))) //ensure uri is not already in the list { scores.Insert(iterator, levdistance); uris.Insert(iterator, uri.Value("redirects").ToString()); } else if (uri.Value("redirects") != null && levdistance <= scores[uris.IndexOf(uri.Value("redirects").ToString())] && (uris.Contains(uri.Value("subject").ToString()) || uris.Contains(uri.Value("redirects").ToString()))) { int redundant = 0; try { redundant = uris.IndexOf(uri.Value("redirects").ToString()); } catch { redundant = uris.IndexOf(uri.Value("subject").ToString()); } uris.RemoveAt(redundant); scores.RemoveAt(redundant); scores.Insert(iterator, levdistance); try { uris.Insert(iterator, uri.Value("redirects").ToString()); } catch { uris.Insert(iterator, uri.Value("subject").ToString()); } } broke = true; break; } iterator++; } if (broke == false) { if (uri.Value("redirects") == null && !(uris.Contains(uri.Value("subject").ToString()))) { uris.Add(uri.Value("subject").ToString()); scores.Add(levdistance); } else { if (uri.Value("redirects") != null && !(uris.Contains(uri.Value("redirects").ToString()))) //ensure uri is not already in the list { uris.Add(uri.Value("redirects").ToString()); scores.Add(levdistance); } } } } if (uris.Count >= MaxUris) { return(uris.GetRange(0, MaxUris)); } else { return(uris); } }
/// <summary> /// The function gets the details part about profile according to its type and the content of XML file it uses /// </summary> /// <param name="profileType">Type of profile(full, mini, micro)</param> /// <param name="subjectURI">The URI of the subject resource as String</param> /// <param name="resultLimit">Max number of results got from queries</param> /// <returns>The details part of mini and full profiles</returns> private List <KeyValuePair <String, List <Entity> > > setProfileDetails(String profileType, String subjectURI, int resultLimit) { XDocument XMLDoc = XDocument.Load("profile content.xml"); String query = "select * where {<" + subjectURI + "> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?obj}"; List <String> types = new List <String>(); SparqlResultSet results = Request.RequestWithHTTP(query); List <KeyValuePair <String, List <Entity> > > profileContent = new List <KeyValuePair <String, List <Entity> > >(); bool typeFound = false; foreach (SparqlResult result in results) { types.Add(result.Value("obj").ToString()); } List <XElement> XMLList = XMLDoc.Root.Elements(profileType).Elements().ToList(); foreach (XElement element in XMLList) { foreach (String type in types) { if (element.Attribute("URI").Value == type) { var elements = element.Elements("predicate"); foreach (XElement elem in elements) { List <Entity> entities = new List <Entity>(); KeyValuePair <String, List <Entity> > key; if (elem.Attribute("queryType").Value == "getObjects") { entities = getQueryResults("getObjects", subjectURI, (elem.Value), resultLimit); key = new KeyValuePair <String, List <Entity> >(util.getLabel(elem.Value), entities); } else if (elem.Attribute("queryType").Value == "getSubjects") { entities = getQueryResults("getSubjects", subjectURI, (elem.Value), resultLimit); key = new KeyValuePair <String, List <Entity> >(util.getLabel(elem.Value) + " of", entities); } //KeyValuePair<String, List<Entity>> key = new KeyValuePair<String, List<Entity>>(util.getLabel(elem.Value), entities); else { key = new KeyValuePair <String, List <Entity> >(); } profileContent.Add(key); } typeFound = true; break; } } if (typeFound == true) { break; } } if (typeFound == false) { List <XElement> XMLListExecluded = XMLDoc.Root.Elements("execludedPredicates").Elements("predicate").ToList(); String execluded = ""; String x = profileType == "mini" ? "5" : "10"; String queryExecluded = "select distinct ?predicate where{" + "<" + subjectURI + ">" + "?predicate ?literal"; foreach (XElement elementExecluded in XMLListExecluded) { execluded += "!(?predicate=<" + elementExecluded.Value + "> ) &&"; } if (execluded != "") { execluded = execluded.Substring(0, execluded.Length - 2); queryExecluded += ".FILTER (" + execluded + ")"; } queryExecluded += "} limit " + x; SparqlResultSet resultsExecluded = Request.RequestWithHTTP(queryExecluded); List <Entity> entitiesExecluded = new List <Entity>(); List <String> predicateNames = new List <String>(); foreach (SparqlResult result in resultsExecluded) { predicateNames.Add(result.Value("predicate").ToString()); } foreach (String predicateName in predicateNames) { entitiesExecluded = getQueryResults("getObjects", subjectURI, predicateName, resultLimit); KeyValuePair <String, List <Entity> > key = new KeyValuePair <String, List <Entity> >(util.getLabel(predicateName), entitiesExecluded); profileContent.Add(key); } } return(profileContent); }