-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
executable file
·54 lines (50 loc) · 1.79 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using System;
using System.Collections.Generic;
namespace spellcheck
{
class Program
{
static HashSet<string> getWords()
{
string[] allWords = System.IO.File.ReadAllLines(@"words.txt");
return new HashSet<string>(allWords);
}
static void FindMatches(string word, HashSet<string> dictionary)
{
int maxFinds = 5;
var foundWords = new HashSet<string>();
IEnumerable<string> edits = Permutations.GetEdits(word,
2,
Permutations.GetDeletions,
Permutations.GetSwaps,
Permutations.GetInsertions,
Permutations.GetReplacements);
foreach(string edit in edits)
{
if(dictionary.Contains(edit) && !foundWords.Contains(edit))
{
Console.Out.WriteLine(edit);
foundWords.Add(edit);
if(foundWords.Count == maxFinds)
{
return;
}
}
}
}
static void Main(string[] args)
{
if(args.Length < 1) {
Console.Out.WriteLine("Usage: spellcheck <word>");
return;
}
string word = args[0];
HashSet<string> dictionary = getWords();
if(dictionary.Contains(word)) {
Console.Out.WriteLine("Correct!");
return;
}
FindMatches(word, dictionary);
}
}
}