Example #1
0
        /// <summary>
        /// Geography Information Retrieval (WHAT - REL - WHERE)
        /// Analyze input query to 3 different phases of What - Relation - Where
        /// </summary>
        /// <param name="inputQuery">inputQuery</param>
        public async Task GIRQueryAnalyzerAsync(string inputQuery)
        {
            string what         = string.Empty;     // What phrase
            string tempWhat     = string.Empty;     // Temporary value for What phrase
            string relation     = string.Empty;     // Relation word
            string tempRelation = string.Empty;     // Temporary value for Relation word

            string where = string.Empty;            // Where phrase
            string tempWhere  = string.Empty;       // Temporary value for Where phrase
            bool   isComplete = false;              // Indicate if the process is complete or not

            // Remove redundant white space between words in inputquery
            inputQuery = Regex.Replace(inputQuery, @"\s+", " ");

            // Create a list of tokens
            List <string> tokens       = StringUtil.StringTokenizer(inputQuery);
            int           sizeOfTokens = tokens.Count();


            // Load relation word dictionary
            List <string> wordDic = await DictionaryUtil.LoadRelationWordAsync();

            // Load list of cities
            List <City> cityList = await LocationUtil.LoadCityAsync();

            // Load list of districts
            List <District> districtList = await LocationUtil.LoadAllDistrictAsync();

            // Check if the lists are load successfully
            if ((wordDic == null) &&
                (cityList == null) &&
                (districtList == null))
            {
                return;
            }

            what = StringUtil.ConcatTokens(tokens, 0, sizeOfTokens - 1);

            // Check every token in the list
            for (int n = 0; n < sizeOfTokens; n++)
            {
                for (int i = n; i < sizeOfTokens; i++)
                {
                    // Concate tokens to create a string with the original starting
                    // word is the first token, and this word is shift to left one index every n loop
                    // if relaton word is not found.
                    // New tokens is add to original token every i loop to check for valid relation word
                    tempRelation = StringUtil.ConcatTokens(tokens, n, i);

                    // Check if token string is matched with relation word in database
                    if (IsValidRelationWord(tempRelation, wordDic))
                    {
                        // If it matches, assign temporary What phrase value with
                        // the value of leading words before Relation word
                        tempWhat = inputQuery.Substring(0, inputQuery.IndexOf(tempRelation) - 1);

                        // Assign Where phrase value with the value of trailing
                        // words after Relation word
                        where = StringUtil.ConcatTokens(tokens, i + 1, sizeOfTokens - 1).Trim().ToLower();

                        // Check if Where phrase is matched with locaitons in database
                        // and handle Where phrase to locate exactly search locations
                        if (IsValidWherePhrase(where, cityList, districtList))
                        {
                            // Change status of isComplete varialbe
                            isComplete = true;
                            // If matches, assign Relation word is with the value
                            // of temporary relation
                            relation = tempRelation;
                            // Assign n value again to break the outside loop
                            n = sizeOfTokens;
                            break;
                        }

                        // Assign Relation word with the value of temporary relation
                        // every i loop
                        relation = tempRelation;
                    }
                }
            }

            // Check if the process is completely finished
            if (!isComplete)
            {
                // Handle query in case of input string is not well-formed
                // with Relation word and Where phrase is not found.
                // Auto check Where phrase with the first location value in input query,
                // if Where phrase is valid, auto assign Relation word with default value.
                if (string.IsNullOrEmpty(relation) && string.IsNullOrEmpty(where))
                {
                    int i = 0;

                    // Take first location in input query string
                    // and handle Where phrase (if any) to locate exactly search locations
                    string firstLocation = TakeFirstLocationInQueryString(inputQuery, cityList, districtList);
                    if (!string.IsNullOrEmpty(firstLocation))
                    {
                        i        = inputQuery.IndexOf(firstLocation.ToLower());
                        tempWhat = inputQuery.Substring(0, i);
                        if (string.IsNullOrEmpty(tempWhat))
                        {
                            where = firstLocation;
                            if (firstLocation.Length != inputQuery.Length)
                            {
                                tempWhat = inputQuery.Substring(firstLocation.Length).Trim().ToLower();
                            }
                        }
                        else
                        {
                            where = inputQuery.Substring(i).Trim().ToLower();
                        }
                    }
                    else
                    {
                        tempWhat = what;
                        relation = string.Empty;
                        where    = string.Empty;
                    }
                }
            }

            // Make sure What phrase have the value.
            // At the worst case if the input query is not well-formed,
            // assign What phrase with the input query
            if (!string.IsNullOrEmpty(tempWhat))
            {
                what = tempWhat.Trim().ToLower();
            }

            // Check if What phrase is equal to Where phrase
            if (what.Equals(where))
            {
                what = string.Empty;
            }

            string a               = string.Format("[{0}][{1}][{2}]", what, relation, where);
            int    hospitalId      = this.HospitalID;
            string hospitalName    = this.HospitalName;
            int    cityId          = this.CityID;
            string cityName        = this.CityName;
            int    districtId      = this.DistrictID;
            string districtName    = this.DistrictName;
            int    specialityId    = this.SpecialityID;
            string speacialityName = this.SpecialityName;
            int    diseaseId       = this.DiseaseID;
            string diseaseName     = this.DiseaseName;

            this.WhatPhrase = what;
        }