internal static int CheckisAvaiable(string input, string source) { string[] separators = new string[] { " " }; string[] inputarr = input.Split(separators, StringSplitOptions.RemoveEmptyEntries); string[] idvarr = source.Split(separators, StringSplitOptions.RemoveEmptyEntries); List <string> idvList = new List <string>(idvarr); int min = 1000; foreach (string inputIdv in inputarr) { if (idvList.IndexOf(inputIdv) != -1) { min = Math.Min(min, Levenshtein_Distance.Distance(input, source)); } } if (min != 1000) { return((int)(min * 0.8)); } min = 1000; foreach (string inputIdv in inputarr) { foreach (string idv in idvList) { if (Levenshtein_Distance.Distance(inputIdv, idv) <= idv.Length / 3) { min = Math.Min(min, Levenshtein_Distance.Distance(input, source)); } } } if (min != 1000) { return((int)(min * 1.2)); } if (Levenshtein_Distance.Distance(source, input) <= source.Length / 2) { return(Levenshtein_Distance.Distance(source, input)); } return(-1); }
/// <summary> /// Tìm sách có sặn trong thư viện /// </summary> /// <param name="sDTO"></param> /// <returns></returns> public List <sachDTO> TimSach(sachDTO sDTO) { List <sachDTO> list = new List <sachDTO>(); List <sachDTO> result = new List <sachDTO>(); string tensach = sDTO.Tensach; string theloai = sDTO.Theloai; string tacgia = sDTO.Tacgia; string nhaxuatban = sDTO.Nxb; string masach; if (sDTO.Masach == -1) { masach = ""; } else { masach = sDTO.Masach.ToString(); } if (tensach == null) { tensach = ""; } if (theloai == null) { theloai = ""; } if (tacgia == null) { tacgia = ""; } if (nhaxuatban == null) { nhaxuatban = ""; } int max_masach_distance = masach.ToString().Length / 2; int max_name_distance = tensach.Length / 2; int max_author_distance = tacgia.Length / 2; int max_publishcompany_distance = nhaxuatban.Length / 2; sachDAL saxDAL = new sachDAL(); sachDTO temp = new sachDTO(); list = DanhSachSach(); //if(masach != "") //if (saxDAL.isSach(sDTO.Masach,ref temp)) //{ // result.Add(temp); //} if (masach != "") { if (saxDAL.isSach(sDTO.Masach)) { foreach (sachDTO sax in list) { if (sax.Masach == int.Parse(masach)) { result.Add(sax); return(result); } } } } if (tensach != "") { List <ResultItem> item = new List <ResultItem>(); foreach (sachDTO sa in list) { int check = TimKiemBUS.CheckisAvaiable(tensach, sa.Tensach); if (check != -1) { ResultItem rItem; rItem.mark = check; rItem.sach = sa; item.Add(rItem); } } item.Sort((s1, s2) => s1.mark.CompareTo(s2.mark)); foreach (ResultItem i in item) { result.Add(i.sach); } } if (theloai != "") { foreach (sachDTO sach in list) { if (sach.Theloai == theloai) { if (result.IndexOf(sach) == -1) { result.Add(sach); } } } } if (tacgia != "") { foreach (sachDTO sach in list) { if (TimKiemBUS.CheckisAvaiable(tacgia, sach.Tacgia) != -1) { if (result.IndexOf(sach) == -1) { result.Add(sach); } } } } if (nhaxuatban != "") { foreach (sachDTO sach in list) { if (Levenshtein_Distance.Distance(sach.Nxb, nhaxuatban) <= sach.Nxb.Length) { if (result.IndexOf(sach) == -1) { result.Add(sach); } } } } return(result); }
internal static List <string> TimKiem(string input, List <string> sources) { List <resultItem> result = new List <resultItem>(); foreach (string individual in sources) { string[] separators = new string[] { " " }; string[] inputarr = input.Split(separators, StringSplitOptions.RemoveEmptyEntries); string[] idvarr = individual.Split(separators, StringSplitOptions.RemoveEmptyEntries); List <string> idvList = new List <string>(idvarr); int min = 1000; foreach (string inputIdv in inputarr) { if (idvList.IndexOf(inputIdv) != -1) { min = Math.Min(min, Levenshtein_Distance.Distance(input, individual)); } } if (min != 1000) { resultItem newiten; newiten.distance = (int)(min * 0.8); newiten.individual = individual; if (newiten.distance < newiten.individual.Length / 2) { result.Add(newiten); } continue; } min = 1000; foreach (string inputIdv in inputarr) { foreach (string idv in idvList) { if (Levenshtein_Distance.Distance(inputIdv, idv) <= idv.Length / 3) { min = Math.Min(min, Levenshtein_Distance.Distance(input, individual)); } } } if (min != 1000) { resultItem newiten; newiten.distance = (int)(min * 1.2); newiten.individual = individual; if (newiten.distance < newiten.individual.Length / 2) { result.Add(newiten); } continue; } if (Levenshtein_Distance.Distance(individual, input) <= individual.Length / 2) { resultItem newiten; newiten.distance = Levenshtein_Distance.Distance(individual, input); newiten.individual = individual; if (newiten.distance < newiten.individual.Length / 2) { result.Add(newiten); } } } result.Sort((s1, s2) => s1.distance.CompareTo(s2.distance)); List <string> finalresult = new List <string>(); foreach (resultItem item in result) { finalresult.Add(item.individual); } return(finalresult); }
/// <summary> /// Tìm đọc giả theo tên và số cmnd /// </summary> /// <param name="cmnd"> Mã số cmnd </param> /// <param name="name"> Họ tên bạn đọc </param> /// <returns></returns> public List <docgiaDTO> TimDocGia(string cmnd, string name) { //cmnd = cmnd.Replace(" ", ""); if (cmnd == null) { cmnd = ""; } if (name == null) { name = ""; } List <docgiaDTO> result = new List <docgiaDTO>(); List <docgiaDTO> danhsach = this.DanhSachDocGia(); docgiaDAL docgiaDAL = new docgiaDAL(); if (cmnd != "") { if (docgiaDAL.isDocGia(int.Parse(cmnd))) { foreach (docgiaDTO dg in danhsach) { if (dg.MaThe == int.Parse(cmnd)) { result.Add(dg); return(result); } } } else { foreach (docgiaDTO dg in danhsach) { if (Levenshtein_Distance.Distance(dg.MaThe.ToString(), cmnd) <= (MAX_LEVENSTEIN_DISTANCE / 2)) { if (result.IndexOf(dg) == -1) { result.Add(dg); } } } } } if (name != "") { List <ResultItem> item = new List <ResultItem>(); foreach (docgiaDTO dg in danhsach) { int check = TimKiemBUS.CheckisAvaiable(name, dg.HoTen); if (check != -1) { ResultItem rItem; rItem.mark = check; rItem.docgia = dg; item.Add(rItem); } } item.Sort((s1, s2) => s1.mark.CompareTo(s2.mark)); foreach (ResultItem i in item) { result.Add(i.docgia); } } return(result); }