} // класс многопоточности public string MyThreadIni(List <string> ArrayShearch, int First, int Lenght, string ShearchString, int ShearchType, decimal Distance) { this.ArrayShearch = ArrayShearch; this.First = First; this.Lenght = Lenght; this.ShearchString = ShearchString; this.Distance = Distance; switch (ShearchType) { case 0: ShearhFunc = new ShearhFunction(L5.LevenshteinDistance.StrContains); break; case 1: ShearhFunc = new ShearhFunction(L5.LevenshteinDistance.WagnweFisher); break; case 2: ShearhFunc = new ShearhFunction(L5.LevenshteinDistance.DamerauLevenshteinDistance); break; default: break; } MyThread = new Thread(ShearchStart); MyThread.Name = NameThread; MyThread.Start(); return(MyThread.Name); }
static void Main() { ShearhFunction ShearhF = null; Func <string, string, ShearchItemClass> ShearhFunc = null; List <string> words = new List <string>(); string WorkFile = OpenFiletoList(ref words); Console.WriteLine("Строка для поиска :"); string FindString = Console.ReadLine(); int FindType = FindTypeEnter(); switch (FindType) { case 1: ShearhF = new ShearhFunction(L5.LevenshteinDistance.StrContains); ShearhFunc = new Func <string, string, ShearchItemClass>((firstText, secondText) => { ShearchItemClass ShearchIt = new ShearchItemClass(firstText, 0); if (firstText.Contains(secondText)) { ShearchIt.Distance = 0; } else { ShearchIt.Distance = 100; } return(ShearchIt); }); //(L5.LevenshteinDistance.StrContains);) break; case 2: ShearhF = new ShearhFunction(L5.LevenshteinDistance.WagnweFisher); ShearhFunc = new Func <string, string, ShearchItemClass>(L5.LevenshteinDistance.WagnweFisher); break; case 3: ShearhF = new ShearhFunction(L5.LevenshteinDistance.DamerauLevenshteinDistance); ShearhFunc = new Func <string, string, ShearchItemClass>(L5.LevenshteinDistance.DamerauLevenshteinDistance); break; default: break; } int i = 0; ShearchItemClass ShearchItem = null; foreach (string s in words) { ShearchItem = ShearhFunc(s, FindString); Console.WriteLine("Func {0} : Строка - {1}, Дистанция - {2}", i, s, ShearchItem.Distance); ShearchItem = ShearhF(s, FindString); Console.WriteLine("delegate {0} : Строка - {1}, Дистанция - {2}", i, s, ShearchItem.Distance); Console.WriteLine(); i++; } Console.ReadLine(); }
private void ShearchButton_Click(object sender, EventArgs e) { if (words == null) { return; } timer1.Stop(); stopwatchwork = new Stopwatch(); progressBar1.Minimum = 0; progressBar1.Maximum = words.Count + 1; progressBar1.Value = 0; progressBar1.Visible = true; stopwatchwork.Start(); ListData.BeginUpdate(); ListData.Items.Clear(); string substring = SearchText.Text.ToLower(); switch (ShearchTypeBox.SelectedIndex) { case 0: ShearhFunc = new ShearhFunction(lab5.LevenshteinDistance.StrContains); break; case 1: ShearhFunc = new ShearhFunction(lab5.LevenshteinDistance.WagnerFisher); break; case 2: ShearhFunc = new ShearhFunction(lab5.LevenshteinDistance.DamerauLevenshtein); break; default: break; } foreach (string s in words) { int Distance = ShearhFunc(s, substring); if (Distance <= Dopusk.Value) { ListData.Items.Add(progressBar1.Value + ": Строка - " + s + " - Дистанция - " + Distance); } progressBar1.Value++; } stopwatchwork.Stop(); toolStripStatusLabel2.Text = "Всё нашли за " + stopwatchwork.ElapsedMilliseconds + " ms"; timer1.Start(); ListData.EndUpdate(); }
private void FindGo_Click(object sender, EventArgs e) { if (words == null) { return; } timer1.Stop(); stopwatchwork = new Stopwatch(); progressBar1.Minimum = 0; progressBar1.Maximum = words.Count + 1; progressBar1.Value = 0; progressBar1.Visible = true; int len = 0; LenCol = 50; CountItemsUp = 0; stopwatchwork.Start(); ListData.BeginUpdate(); ListData.Items.Clear(); ItemListAdd.Clear(); ResultTimeStrim = ""; //ItemListStop.Clear(); string substring = FindText.Text.ToLower(); if (FindType.SelectedIndex > 2) { int k = Convert.ToInt32(NumStream.Text); int Lenght = words.Count / k; int CountThread = 0; Array.Resize(ref ListThreadFind, k); Array.Clear(ListThreadFind, 0, k); for (int i = 0; i < words.Count; i = i + Lenght) { if (((words.Count - i) % (k - CountThread)) == 0) { Lenght = ((words.Count - i) / (k - CountThread)); } ListThreadFind[CountThread] = new MyThreadFind(); ListThreadFind[CountThread].OneShearchAddNow += OneShearchAddOk; ListThreadFind[CountThread].ShearchCompletedNow += ShearchCompletedNow; ListThreadFind[CountThread].NameThread = " Stream - " + CountThread; if (CountThread == k - 1) { Lenght = words.Count - i; } LogTextBox.AppendText(ListThreadFind[CountThread].NameThread + " c " + i + " по " + (i + Lenght - 1) + " в потоке " + Lenght + " элементов \r\n"); ListThreadFind[CountThread].MyThreadIni(words, i, Lenght, substring, FindType.SelectedIndex - 3, Convert.ToInt32(Distance.Text)); CountThread++; } CountThread--; timer2.Tag = 0; // счетчик переноса в ListBox timer2.Start(); } else { switch (FindType.SelectedIndex) { case 0: ShearhFunc = new ShearhFunction(L5.LevenshteinDistance.StrContains); break; case 1: ShearhFunc = new ShearhFunction(L5.LevenshteinDistance.WagnweFisher); break; case 2: ShearhFunc = new ShearhFunction(L5.LevenshteinDistance.DamerauLevenshteinDistance); break; default: break; } foreach (string s in words) { ShearchItemClass ShearchItem = ShearhFunc(s, substring); if (ShearchItem.Distance <= Convert.ToInt32(Distance.Text)) { ShearchItem.IndexItem = progressBar1.Value; len = TextRenderer.MeasureText((ListData.Items[ListData.Items.Add(ShearchItem)] as ShearchItemClass).ShearchString, ListData.Font).Width; if (LenCol < len) { LenCol = len + 10; } } progressBar1.Value++; } progressBar1.Value = progressBar1.Maximum; stopwatchwork.Stop(); TimeWorkStr.Text = "Время поиска " + stopwatchwork.ElapsedMilliseconds + " ms"; timer1.Start(); } if ((FindType.SelectedIndex == 2) || (FindType.SelectedIndex == 5)) { ListData.Items.Insert(0, "Строка , Редакционное предписание , Номер"); } else { ListData.Items.Insert(0, "Строка , , Номер"); } ListData.EndUpdate(); }
static void Main(string[] args) { ShearhFunction FuncShearh = null; List <string> ListStr = new List <string>(); string FileNameS = ""; do { FileNameS = ConsoleOpenFile(ref ListStr); }while (FileNameS == ""); Console.WriteLine("Файл : {0} ОТКРЫТ !", FileNameS); Console.Write("Введите строку для поиска : "); string StrShearh = Console.ReadLine(); Console.WriteLine("Введите номер алгоритма"); Console.WriteLine("1 - Простой поиск"); Console.WriteLine("2 - Расстояние Дамерау-Левенштейна"); Console.WriteLine("3 - Алгоритм Вагнера — Фишера"); Console.WriteLine("4 - выход"); int indicator = -1; do { bool success = Int32.TryParse(Console.ReadLine(), out indicator); if ((!success) || (indicator > 4) || (indicator < 1)) { Console.WriteLine("Не верно введен номер. Повторите ввод!"); indicator = -1; } if (indicator == 4) { return; } }while ((indicator < 1) || (indicator > 3)); string substring = StrShearh.ToLower(); switch (indicator) { case 1: ShearhFunction FShearh = delegate(string firstText, string secondText) { int Distance = 0; if (firstText.Contains(secondText)) { Distance = Math.Abs(firstText.Length - secondText.Length); } else { Distance = 100; } return(Distance); }; DistanceFunc(substring, ListStr, (firstText, secondText) => { int Distance = 0; if (firstText == secondText) { Distance = 0; } else if (firstText.Contains(secondText)) { Distance = Math.Abs(firstText.Length - secondText.Length); } else { Distance = 100; } return(Distance); }, FShearh); break; case 2: FuncShearh = new ShearhFunction(lab5.LevenshteinDistance.DamerauLevenshtein); DistanceFunc(substring, ListStr, lab5.LevenshteinDistance.DamerauLevenshtein, FuncShearh); break; case 3: FuncShearh = lab5.LevenshteinDistance.DamerauLevenshtein; Func <string, string, int> FuncShearhFunc = lab5.LevenshteinDistance.WagnerFisher; DistanceFunc(substring, ListStr, FuncShearhFunc, FuncShearh); break; default: break; } }
static void DistanceFunc(string substring, List <string> ListStr, Func <string, string, int> FuncShearhFunc, ShearhFunction FuncShearh) { int k = 0; foreach (string s in ListStr) { int Distance = FuncShearh(s, substring); Console.WriteLine("заранее объявленный Delegat : " + k + " Строка - " + s + " - Дистанция - " + Distance); Distance = FuncShearhFunc(s, substring); Console.WriteLine("Delegat объявленный Func<> : " + k + " Строка - " + s + " - Дистанция - " + Distance); Console.WriteLine(); k++; } Console.ReadLine(); }