Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace IndexSearch
- {
- class Program
- {
- static void Main(string[] args)
- {
- Search s = new IndexSearch.Search(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + @"\Query");
- DateTime now = DateTime.Now;
- Dictionary<string, Dictionary<long, string>> Index = new Dictionary<string, Dictionary<long, string>>(s.BuildIndex());
- Console.WriteLine("Time elapsed after indexing: " + DateTime.Now.Subtract(now).Milliseconds + "ms");
- foreach (var v in Index)
- {
- Console.WriteLine(v.Key);
- foreach (var k in v.Value)
- {
- Console.WriteLine(k.Key + " " + k.Value);
- }
- }
- s.Find(Console.ReadLine(), Index);
- Console.ReadLine();
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.IO;
- using System.Collections;
- namespace IndexSearch
- {
- class Search
- {
- // A string that contains a path to folder with files
- private string Path;
- public Search (string Path)
- {
- this.Path = Path;
- }
- public Dictionary<string, Dictionary<long, string>> BuildIndex()
- {
- Dictionary<string, Dictionary<long, string>> Index = new Dictionary<string, Dictionary<long, string>>();
- string[] Files;
- Files = System.IO.Directory.GetFiles(Path);
- foreach (string s in Files)
- {
- using (FileStream fs = new FileStream(s, FileMode.Open, FileAccess.Read))
- {
- int curByte;
- string curWord = null;
- while (true)
- {
- curByte = fs.ReadByte();
- if (curByte < 0)
- {
- if (curWord != null)
- {
- if (!Index.ContainsKey(curWord))
- {
- Index.Add(curWord, new Dictionary<long, string>());
- Index[curWord].Add(fs.Position - curWord.Length, s);
- curWord = null;
- break;
- }
- else
- {
- Index[curWord].Add(fs.Position - curWord.Length, s);
- curWord = null;
- break;
- }
- }
- else
- {
- break;
- }
- }
- if (char.IsLetterOrDigit((char)curByte))
- {
- curWord += (char)curByte;
- }
- else
- {
- if (curWord == null) { continue; }
- if (!Index.ContainsKey(curWord))
- {
- Index[curWord] = new Dictionary<long, string>();
- Index[curWord].Add(fs.Position - curWord.Length, s);
- curWord = null;
- }
- else
- {
- Index[curWord][fs.Position - curWord.Length] = s;
- curWord = null;
- }
- }
- }
- }
- }
- return Index;
- }
- public void Find(string Query, Dictionary<string, Dictionary<long, string>> Index)
- {
- if (Index.ContainsKey(Query))
- {
- Console.WriteLine("Нашлось запрошенное слово \"" + Query + "\" в: ");
- foreach (var v in Index[Query])
- {
- Console.WriteLine(v.Value + " " + v.Key);
- }
- }
- else
- {
- Console.WriteLine("Слово не найдено.");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement