Esempio n. 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 = 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> )))." +
                                    "?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);
                    }
                    // 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;
            }
        }
Esempio n. 2
0
        /// <summary>
        /// return a clone of the token send to the function
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public override LexiconToken getClone(LexiconToken token)
        {
            LexiconLiteral literalToReplace = new LexiconLiteral();
            literalToReplace.URI = token.URI;
            literalToReplace.label = token.label;
            literalToReplace.typeOfOwner = (token as LexiconLiteral).typeOfOwner.ToList();
            literalToReplace.QuestionMatch = token.QuestionMatch;
            literalToReplace.score = token.score;

            return literalToReplace;
        }