Inheritance: LexiconToken
Esempio n. 1
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);
        }
Esempio n. 2
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. 3
0
        /// removing the non used predicates domains and the literals type of owners
        /// </summary>
        /// <param name="tokens">list </param>of tokens
        /// <returns>cleaned list of tokens </returns>
        private List <QueryBucket> cleanBucket(List <QueryBucket> queryBuckets)
        {
            #region removing Buckets which still have question left  >1

            foreach (QueryBucket querybucket in queryBuckets.ToList())
            {
                if (querybucket.questionLeft.Length > 0)
                {
                    queryBuckets.Remove(querybucket);
                }
            }

            #endregion

            #region remove Predicates domains and type of owners

            foreach (QueryBucket bucket in queryBuckets.ToList())
            {
                //adding predicates and literals to a list
                List <LexiconPredicate> predicateList = new List <LexiconPredicate>();
                List <LexiconLiteral>   literalList   = new List <LexiconLiteral>();
                foreach (LexiconToken token in bucket.tokens)
                {
                    if (token is LexiconPredicate)
                    {
                        predicateList.Add(token as LexiconPredicate);
                    }

                    if (token is LexiconLiteral)
                    {
                        literalList.Add(token as LexiconLiteral);
                    }
                }

                if (predicateList.Count > 0)
                {
                    //removing domains and ranges that are not used
                    foreach (LexiconToken token in bucket.tokens.ToList())
                    {
                        if (token is LexiconPredicate)
                        {
                            //casting the lexicontoken to lexicon predicate
                            LexiconPredicate oldPredicate = token as LexiconPredicate;
                            //cloning the token to be modified
                            LexiconPredicate predicateToReplace = (LexiconPredicate)token.getClone(token);

                            foreach (string oldPredDomain in oldPredicate.domains.ToList())
                            {
                                bool exist = false;
                                foreach (LexiconLiteral tmpliteral in literalList)
                                {
                                    if (tmpliteral.typeOfOwner.Contains(oldPredDomain))
                                    {
                                        exist = true;
                                    }
                                }

                                //if this domains doesn't contained in any of literals type of owners remove it as it wont match|join with anything
                                if (!exist)
                                {
                                    //old bucket = new bucket and then modify in the new in order then to be able to remove the old
                                    predicateToReplace = oldPredicate.getClone(oldPredicate) as LexiconPredicate;

                                    //removing domain not used
                                    predicateToReplace.domains.Remove(oldPredDomain);
                                    //remove the old bucket and replace it with new modified one // needed because of reference issues
                                    bucket.tokens.Remove(oldPredicate);
                                    bucket.tokens.Add(predicateToReplace);

                                    oldPredicate = predicateToReplace;

                                    //remove the predicate if it doesnt have any domains left
                                    if (oldPredicate.domains.Count == 0)
                                    {
                                        bucket.tokens.Remove(oldPredicate);
                                        predicateList.Remove(oldPredicate as LexiconPredicate);
                                        //remove the bucket if it's free from predicates
                                        if (bucket.tokens.Count == 0)
                                        {
                                            queryBuckets.Remove(bucket);
                                        }
                                    }
                                }
                            }
                        }

                        if (token is LexiconLiteral)
                        {
                            LexiconLiteral oldLiteral = token as LexiconLiteral;
                            LexiconLiteral newLiteral = token.getClone(token) as LexiconLiteral;

                            foreach (string typeofowner in oldLiteral.typeOfOwner.ToList())
                            {
                                bool exist = false;
                                foreach (LexiconPredicate tmmpredicate in predicateList)
                                {
                                    if (tmmpredicate.domains.Contains(typeofowner))
                                    {
                                        exist = true;
                                    }
                                }

                                if (!exist)
                                {
                                    //taking a copy from the old literal in order to remove it from the bucket when replacing it with the newliteral
                                    newLiteral = oldLiteral.getClone(oldLiteral) as LexiconLiteral;

                                    // removing typeofowner not used
                                    newLiteral.typeOfOwner.Remove(typeofowner);
                                    // updating the bucket tokens by replacing the old literal with the new one
                                    bucket.tokens.Remove(oldLiteral);
                                    bucket.tokens.Add(newLiteral);

                                    oldLiteral = newLiteral;

                                    if (oldLiteral.typeOfOwner.Count == 0)
                                    {
                                        bucket.tokens.Remove(oldLiteral);
                                        literalList.Remove(oldLiteral as LexiconLiteral);
                                        //remove the bucket if it's free from Tokens
                                        if (bucket.tokens.Count == 0)
                                        {
                                            queryBuckets.Remove(bucket);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                else
                {
                    bucket.literalOnly = true;
                }
            }

            #endregion

            #region remove the multiple domains and multiple ranges
            foreach (QueryBucket bucket in queryBuckets)
            {
                foreach (LexiconToken predicateToken in bucket.tokens)
                {
                    if (predicateToken is LexiconPredicate)
                    {
                        foreach (LexiconToken literalToken in bucket.tokens)
                        {
                            if (literalToken is LexiconLiteral && Enumerable.SequenceEqual((predicateToken as LexiconPredicate).domains, (literalToken as LexiconLiteral).typeOfOwner))
                            {
                                (predicateToken as LexiconPredicate).domains.RemoveRange(1, (predicateToken as LexiconPredicate).domains.Count - 1);
                                (literalToken as LexiconLiteral).typeOfOwner.RemoveRange(1, (literalToken as LexiconLiteral).typeOfOwner.Count - 1);
                            }
                        }
                    }
                }
            }
            #endregion

            return(queryBuckets);
        }
Esempio n. 4
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> )" +
                                   " && !(?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>
        /// 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;
        }