Exemple #1
0
        /// <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 = 10, int Limit = 20)
        {
            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);

            // iterating over each permutation of Question left and Query them from virtuoso and return predicate list and add them
            foreach (string questionleft in permutationList)
            {

                // Query 1 is suitable for Keywords like : United states which match the most popular resource
                string Query1 = "SELECT distinct ?subject ?literal ?typeOfOwner " +
                               "where {              " +
                                "        ?subject <http://www.w3.org/2000/01/rdf-schema#label> ?literal . " +
                                " optional { ?subject <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?typeOfOwner ." +
                                "        ?literal bif:contains '\"" + questionleft + "\"' OPTION (score ?sc) .  " +
                                "FILTER (" +
                                "!(?typeOfOwner  = <http://www.w3.org/2002/07/owl#Thing> " +
                                "|| ?typeOfOwner = <http://www.w3.org/2004/02/skos/core#Concept> " +
                                "|| ?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>) " +
                                ")" +
                                "} limit " + Limit;

                // Query2 is suitable for Keywords like : USA  , which match the redirections
                string Query2 = "SELECT distinct ?subject ?literal ?typeOfOwner " +
                                "WHERE { " +
                                "      ?subject2 <http://www.w3.org/2000/01/rdf-schema#label> ?literal . " +
                                "      ?subject2 <http://dbpedia.org/ontology/wikiPageRedirects> ?subject ." +
                                "      ?subject  <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?typeOfOwner ." +
                                "      ?literal bif:contains '\"" + questionleft + "\"' OPTION (score ?sc) ." +
                                "FILTER (" +
                                "!(?typeOfOwner  = <http://www.w3.org/2002/07/owl#Thing> " +
                                "|| ?typeOfOwner = <http://www.w3.org/2004/02/skos/core#Concept> " +
                                "|| ?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>) " +
                                ")" +
                                "} limit " + Limit;

                SparqlRemoteEndpoint remoteEndPoint = new SparqlRemoteEndpoint(new Uri("http://localhost:8890/sparql"));
                SparqlResultSet resultSet1 = new SparqlResultSet();
                SparqlResultSet resultSet2 = new SparqlResultSet();
                List<SparqlResult> resultSet = new List<SparqlResult>();
                try
                {
                    //executing the Query and finding results
                    resultSet1 = remoteEndPoint.QueryWithResultSet(Query1);
                }
                // skipping results that raised timeout exceptions
                catch
                {
                    util.log("skipped Query1 : " + questionleft + " ---- due to time out ");
                }
                try
                {
                    resultSet2 = remoteEndPoint.QueryWithResultSet(Query2);
                }
                // skipping results that raised timeout exceptions
                catch
                {
                    util.log("skipped  Query2: " + questionleft + " ---- due to time out ");
                }

                resultSet = (resultSet1.Count != 0) ? resultSet1.ToList<SparqlResult>() : resultSet;
                resultSet = (resultSet2.Count != 0) ? resultSet.Concat<SparqlResult>(resultSet2.ToList<SparqlResult>()).ToList() : resultSet;

                //iterating over matched Literals in the resultset
                foreach (SparqlResult result in resultSet)
                {
                    INode resourceURI = result.Value("subject");
                    INode literalLabel = result.Value("literal");
                    INode literalTypeOfOwner = result.Value("typeOfOwner");

                    // 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 == resourceURI.ToString() && x.label == literalLabel.ToString())
                        {
                            exists = true;
                            if (x.typeOfOwner.Contains(literalTypeOfOwner.ToString()))
                            {
                                totallyExists = true;
                                break;
                            }

                        }
                    }

                    // adding the new literals to the literallist .
                    if (!exists)
                    {
                        LexiconLiteral tmpLexiconLiteral = new LexiconLiteral(resourceURI.ToString(), literalLabel.ToString(), questionleft, literalTypeOfOwner.ToString());
                        __literalList.Add(tmpLexiconLiteral);
                    }

                    if (!totallyExists && exists)
                    {
                        foreach (LexiconLiteral let in __literalList)
                        {
                            if (let.URI == resourceURI.ToString() && let.label == literalLabel.ToString())
                            {
                                let.typeOfOwner.Add(literalTypeOfOwner.ToString());
                            }
                        }
                    }
                }

            }

            //scoring literals . trimming duplicates ,
            __literalList = scoreLiterals(__literalList, topN);

            util.log("total time taken :" + DateTime.Now.Subtract(dt).TotalMilliseconds.ToString() + " msecs ");
            return __literalList;
        }