public async Task <ActionResult> SearchResult(HomeModel model, int page = 1) { List <HospitalEntity> hospitalList = null; IPagedList <HospitalEntity> pagedHospitalList = null; try { // Indicate which button is clicked string button = Request[Constants.Button]; #region Normal Search // Normal search form if ((string.IsNullOrEmpty(button)) || Constants.NormalSearchForm.Equals(button)) { ViewBag.SearchValue = model.SearchValue; // Check if input search query is null or empty if (!string.IsNullOrEmpty(model.SearchValue)) { // Check if input search value is understandable string[] suggestSentence = StringUtil.CheckVocabulary(model.SearchValue); if (Constants.False.Equals(suggestSentence[0])) { ViewBag.SuggestionSentence = suggestSentence[1]; } // Analyze to GIR query await model.GIRQueryAnalyzerAsync(model.SearchValue); // Search hospitals hospitalList = await model.NormalSearchHospital(); pagedHospitalList = hospitalList.ToPagedList(page, Constants.PageSize); // Search Query Statistic DataModel.StoreSearchQuery(model.SearchValue, hospitalList.Count); } } #endregion #region Advanced Search // Load list of cities cityList = await LocationUtil.LoadCityAsync(); ViewBag.CityList = new SelectList(cityList, Constants.CityID, Constants.CityName); // Load list of districts districtList = await LocationUtil.LoadDistrictInCityAsync(model.CityID); ViewBag.DistrictList = new SelectList(districtList, Constants.DistrictID, Constants.DistrictName); // Load list of specialities specialityList = await SpecialityUtil.LoadSpecialityAsync(); ViewBag.SpecialityList = new SelectList(specialityList, Constants.SpecialityID, Constants.SpecialityName); // Load list of disease diseaseList = new List <Disease>(); ViewBag.DiseaseList = new SelectList(diseaseList, Constants.DiseaseID, Constants.DiseaseName); // Advanced search form if (Constants.AdvancedSearchForm.Equals(button)) { ViewBag.DiseaseName = model.DiseaseName; hospitalList = await model.AdvancedSearchHospital(model.CityID, model.DistrictID, model.SpecialityID, model.DiseaseName); pagedHospitalList = hospitalList.ToPagedList(page, Constants.PageSize); ViewBag.SearchType = Constants.AdvancedSearchForm; } #endregion #region Location Search List <SelectListItem> locationTypeListItem = new List <SelectListItem>() { new SelectListItem { Value = "2", Text = "Nhập vị trí" }, new SelectListItem { Value = "1", Text = "Vị trí hiện tại" } }; ViewBag.LocationTypeList = new SelectList(locationTypeListItem, "Value", "Text", 2); List <SelectListItem> radiusListItem = new List <SelectListItem>() { new SelectListItem { Value = "0.3", Text = "300 mét" }, new SelectListItem { Value = "0.5", Text = "500 mét" }, new SelectListItem { Value = "1", Text = "1 km" }, new SelectListItem { Value = "3", Text = "3 km" }, new SelectListItem { Value = "5", Text = "5 km" }, new SelectListItem { Value = "10", Text = "10 km" }, new SelectListItem { Value = "15", Text = "15 km" }, new SelectListItem { Value = "20", Text = "20 km" } }; ViewBag.RadiusList = new SelectList(radiusListItem, "Value", "Text", 0.3); // Location search form if (Constants.LocationSearchForm.Equals(button)) { ViewBag.SearchType = Constants.LocationSearchForm; // Search hospitals double lat = 0; double lng = 0; WebClient client = new WebClient(); string coordinate = model.Coordinate; string position = model.Position; if (!(0 < model.Radius && model.Radius <= 20)) { model.Radius = 10; } double radius = model.Radius; if (model.LocationType == 1) { if (coordinate != null) { if (coordinate.Split(',').Length > 1) { double.TryParse(coordinate.Split(',')[0], out lat); double.TryParse(coordinate.Split(',')[1], out lng); } } } else if (model.LocationType == 2) { if (!string.IsNullOrEmpty(position)) { string geoJsonResult = client.DownloadString(string.Concat(Constants.GeoCodeJsonQuery, position)); // Json.Net is really helpful if you have to deal // with Json from .Net http://json.codeplex.com/ JObject geoJsonObject = JObject.Parse(geoJsonResult); if (geoJsonObject.Value <string>("status").Equals("OK")) { lat = geoJsonObject["results"].First["geometry"]["location"].Value <double>("lat"); lng = geoJsonObject["results"].First["geometry"]["location"].Value <double>("lng"); } } } hospitalList = await HomeModel.LocationSearchHospital(lat, lng, radius * 1000); pagedHospitalList = hospitalList.ToPagedList(page, Constants.PageSize); string distanceMatrixUrl = string.Concat("http://maps.googleapis.com/maps/api/distancematrix/json?origins=", lat, ",", lng, "&destinations="); int index = 0; foreach (HospitalEntity hospital in pagedHospitalList) { distanceMatrixUrl += (index == 0 ? string.Empty : "|") + hospital.Coordinate.Split(',')[0].Trim() + "," + hospital.Coordinate.Split(',')[1].Trim(); index = -1; } string dMatrixJsonResult = client.DownloadString(distanceMatrixUrl); JObject dMatrixJsonObject = JObject.Parse(dMatrixJsonResult); if (dMatrixJsonObject.Value <string>("status").Equals("OK")) { index = 0; foreach (HospitalEntity hospital in pagedHospitalList) { hospital.Distance = dMatrixJsonObject["rows"].First["elements"].ElementAt(index++)["distance"].Value <double>("value"); } model.Coordinate = lat + ", " + lng; } } #endregion // Transfer list of hospitals to Search Result page ViewBag.HospitalList = pagedHospitalList; ViewBag.JsonHospitalList = JsonConvert.SerializeObject(pagedHospitalList).Replace("\r\n", string.Empty); NameValueCollection queryString = System.Web.HttpUtility.ParseQueryString(Request.Url.Query); queryString.Remove("page"); ViewBag.Query = queryString.ToString(); if (hospitalList.Count == 0) { ViewBag.SearchValue = model.SearchValue; } ViewBag.FeedbackStatus = TempData["FeedbackStatus"]; ViewBag.FeedbackMessage = TempData["FeedbackMessage"]; } catch (Exception exception) { LoggingUtil.LogException(exception); return(RedirectToAction(Constants.SystemFailureHomeAction, Constants.ErrorController)); } // Move to result page return(View(model)); }
/// <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; }
public async Task <ActionResult> Index() { try { // Load list of cities cityList = await LocationUtil.LoadCityAsync(); ViewBag.CityList = new SelectList(cityList, Constants.CityID, Constants.CityName); // Load list of districts districtList = new List <District>(); ViewBag.DistrictList = new SelectList(districtList, Constants.DistrictID, Constants.DistrictName); // Load list of specialities specialityList = await SpecialityUtil.LoadSpecialityAsync(); ViewBag.SpecialityList = new SelectList(specialityList, Constants.SpecialityID, Constants.SpecialityName); // Load list of disease diseaseList = new List <Disease>(); ViewBag.DiseaseList = new SelectList(diseaseList, Constants.DiseaseID, Constants.DiseaseName); List <SelectListItem> locationTypeListItem = new List <SelectListItem>() { new SelectListItem { Value = "2", Text = "Nhập vị trí" }, new SelectListItem { Value = "1", Text = "Vị trí hiện tại", } }; ViewBag.LocationTypeList = new SelectList(locationTypeListItem, "Value", "Text", 2); List <SelectListItem> radiusListItem = new List <SelectListItem>() { new SelectListItem { Value = "0.3", Text = "300 mét" }, new SelectListItem { Value = "0.5", Text = "500 mét" }, new SelectListItem { Value = "1", Text = "1 km" }, new SelectListItem { Value = "3", Text = "3 km" }, new SelectListItem { Value = "5", Text = "5 km" }, new SelectListItem { Value = "10", Text = "10 km" }, new SelectListItem { Value = "15", Text = "15 km" }, new SelectListItem { Value = "20", Text = "20 km" } }; ViewBag.RadiusList = new SelectList(radiusListItem, "Value", "Text", 0.3); ViewBag.FeedbackStatus = TempData["FeedbackStatus"]; ViewBag.FeedbackMessage = TempData["FeedbackMessage"]; } catch (Exception exception) { LoggingUtil.LogException(exception); return(RedirectToAction(Constants.SystemFailureHomeAction, Constants.ErrorController)); } return(View()); }