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;
- using System.Collections;
- using System.IO;
- namespace Search
- {
- struct Index
- {
- public string Document;
- public ulong Occurences;
- public override string ToString()
- {
- return "Document : " + Document + "\n" + "Occurences : " + Occurences.ToString() + "\n\n";
- }
- }
- class Program
- {
- static Hashtable mIndexs = new Hashtable();
- static void Main(string[] args)
- {
- string FilesPath = @"y:\sample";// Console.ReadLine();
- string[] files = Directory.GetFiles(FilesPath, "*.txt");
- Parallel.ForEach(files, (f) => {
- parseFile(f);
- });
- }
- static void parseFile(string path)
- {
- Hashtable temp = new Hashtable();
- string content = File.ReadAllText(path);
- content = content.Replace("\r" , "");
- content = content.Replace("\n" , " ");
- string[] words = content.Split(' ');
- foreach (string word in words)
- {
- if (temp[word] == null)
- temp.Add(word, ulong.MinValue);
- ulong matches =(ulong) temp[word];
- matches++;
- temp[word] = matches;
- }
- if (temp.Contains(""))
- temp.Remove("");
- foreach (string key in temp.Keys)
- {
- lock (mIndexs)
- {
- if (mIndexs[key] == null)
- mIndexs[key] = new List<Index>();
- Index i = new Index();
- i.Document = path;
- i.Occurences = (ulong)temp[key];
- ((List<Index>) mIndexs[key]).Add(i);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement