Advertisement
gasaichan

Huyandex

Oct 2nd, 2017
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.65 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace IndexSearch
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Search s = new IndexSearch.Search(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + @"\Query");
  14.             DateTime now = DateTime.Now;
  15.             Dictionary<string, Dictionary<long, string>> Index = new Dictionary<string, Dictionary<long, string>>(s.BuildIndex());
  16.             Console.WriteLine("Time elapsed after indexing: " + DateTime.Now.Subtract(now).Milliseconds + "ms");
  17.  
  18.             foreach (var v in Index)
  19.             {
  20.                 Console.WriteLine(v.Key);
  21.                 foreach (var k in v.Value)
  22.                 {
  23.                     Console.WriteLine(k.Key + " " + k.Value);
  24.                 }
  25.             }
  26.  
  27.            
  28.             s.Find(Console.ReadLine(), Index);
  29.             Console.ReadLine();
  30.         }  
  31.     }
  32. }
  33.  
  34.  
  35.  
  36.  
  37. using System;
  38. using System.Collections.Generic;
  39. using System.Linq;
  40. using System.Text;
  41. using System.Threading.Tasks;
  42. using System.IO;
  43. using System.Collections;
  44.  
  45. namespace IndexSearch
  46. {
  47.     class Search
  48.     {
  49.         // A string that contains a path to folder with files
  50.         private string Path;
  51.  
  52.  
  53.         public Search (string Path)
  54.         {
  55.             this.Path = Path;
  56.         }
  57.  
  58.         public Dictionary<string, Dictionary<long, string>> BuildIndex()
  59.         {
  60.             Dictionary<string, Dictionary<long, string>> Index = new Dictionary<string, Dictionary<long, string>>();
  61.             string[] Files;
  62.             Files = System.IO.Directory.GetFiles(Path);
  63.             foreach (string s in Files)
  64.             {
  65.                 using (FileStream fs = new FileStream(s, FileMode.Open, FileAccess.Read))
  66.                 {
  67.                     int curByte;
  68.                     string curWord = null;
  69.                     while (true)
  70.                     {
  71.                         curByte = fs.ReadByte();
  72.                         if (curByte < 0)
  73.                         {
  74.                             if (curWord != null)
  75.                             {
  76.                                 if (!Index.ContainsKey(curWord))
  77.                                 {
  78.                                     Index.Add(curWord, new Dictionary<long, string>());
  79.                                     Index[curWord].Add(fs.Position - curWord.Length, s);
  80.                                     curWord = null;
  81.                                     break;
  82.                                 }
  83.                                 else
  84.                                 {
  85.                                     Index[curWord].Add(fs.Position - curWord.Length, s);
  86.                                     curWord = null;
  87.                                     break;
  88.                                 }
  89.                             }
  90.                             else
  91.                             {
  92.                                 break;
  93.                             }
  94.                         }
  95.                        
  96.                         if (char.IsLetterOrDigit((char)curByte))
  97.                         {
  98.                             curWord += (char)curByte;
  99.                         }
  100.                         else
  101.                         {
  102.                             if (curWord == null) { continue; }
  103.                             if (!Index.ContainsKey(curWord))
  104.                             {
  105.                                 Index[curWord] = new Dictionary<long, string>();
  106.                                 Index[curWord].Add(fs.Position - curWord.Length, s);
  107.                                 curWord = null;
  108.                             }
  109.                             else
  110.                             {
  111.                                 Index[curWord][fs.Position - curWord.Length] = s;
  112.                                 curWord = null;
  113.                             }
  114.                         }
  115.                     }
  116.                 }
  117.             }
  118.  
  119.             return Index;
  120.         }
  121.  
  122.         public void Find(string Query, Dictionary<string, Dictionary<long, string>> Index)
  123.         {
  124.             if (Index.ContainsKey(Query))
  125.             {
  126.                 Console.WriteLine("Нашлось запрошенное слово \"" + Query + "\" в: ");
  127.                 foreach (var v in Index[Query])
  128.                 {
  129.                     Console.WriteLine(v.Value + " " + v.Key);
  130.                 }
  131.             }
  132.             else
  133.             {
  134.                 Console.WriteLine("Слово не найдено.");
  135.             }
  136.         }
  137.     }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement