Advertisement
8thbit

search.cs

Nov 4th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.95 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. using System.Collections;
  7. using System.IO;
  8.  
  9. namespace Search
  10. {
  11.     struct Index
  12.     {
  13.         public string Document;
  14.         public ulong Occurences;
  15.  
  16.         public override string ToString()
  17.         {
  18.             return "Document : " + Document + "\n" + "Occurences : " + Occurences.ToString() + "\n\n";
  19.         }
  20.  
  21.     }
  22.     class Program
  23.     {
  24.         static Hashtable mIndexs = new Hashtable();
  25.         static void Main(string[] args)
  26.         {
  27.             string FilesPath = @"y:\sample";// Console.ReadLine();            
  28.             string[] files = Directory.GetFiles(FilesPath, "*.txt");
  29.             Parallel.ForEach(files, (f) => {
  30.                 parseFile(f);
  31.             });
  32.  
  33.  
  34.         }
  35.         static void parseFile(string path)
  36.         {
  37.             Hashtable temp = new Hashtable();
  38.             string content = File.ReadAllText(path);
  39.             content = content.Replace("\r" , "");
  40.             content = content.Replace("\n" , " ");
  41.             string[] words = content.Split(' ');
  42.             foreach (string word in words)
  43.             {
  44.                 if (temp[word] == null)
  45.                     temp.Add(word, ulong.MinValue);
  46.                 ulong matches =(ulong) temp[word];
  47.                 matches++;
  48.                 temp[word] = matches;
  49.             }
  50.             if (temp.Contains(""))
  51.                 temp.Remove("");
  52.             foreach (string key in temp.Keys)
  53.             {
  54.                 lock (mIndexs)
  55.                 {
  56.                     if (mIndexs[key] == null)
  57.                         mIndexs[key] = new List<Index>();
  58.                     Index i = new Index();
  59.                     i.Document = path;
  60.                     i.Occurences = (ulong)temp[key];
  61.                     ((List<Index>) mIndexs[key]).Add(i);
  62.                 }  
  63.             }
  64.         }
  65.  
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement