-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
809 lines (751 loc) · 28.7 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace word_count
{
public class CommandLineArgument
{
List<CommandLineArgument> _arguments;
int _index;
string _argumentText;
public CommandLineArgument Next
{
get
{
if (_index < _arguments.Count - 1)
{
return _arguments[_index + 1];
}
throw new System.ArgumentException("Need more parameters for " + _arguments[_index]._argumentText);
}
}
public CommandLineArgument Previous
{
get
{
if (_index > 0)
{
return _arguments[_index - 1];
}
return null;
}
}
internal CommandLineArgument(List<CommandLineArgument> args, int index, string argument)
{
_arguments = args;
_index = index;
_argumentText = argument;
}
public CommandLineArgument Take()
{
return Next;
}
public IEnumerable<CommandLineArgument> Take(int count)
{
var list = new List<CommandLineArgument>();
var parent = this;
for (int i = 0; i < count; i++)
{
var next = parent.Next;
if (next == null)
break;
list.Add(next);
parent = next;
}
return list;
}
public static implicit operator string(CommandLineArgument argument)
{
return argument._argumentText;
}
public override string ToString()
{
return _argumentText;
}
}
public class CommandLineArgumentParser
{
public int length;
List<CommandLineArgument> _arguments;
public static CommandLineArgumentParser Parse(string[] args)
{
return new CommandLineArgumentParser(args);
}
public CommandLineArgumentParser(string[] args)
{
_arguments = new List<CommandLineArgument>();
length = args.Length;
for (int i = 0; i < length; i++)
{
_arguments.Add(new CommandLineArgument(_arguments, i, args[i]));
}
}
public CommandLineArgument Get_by_index(int index)
{
return _arguments[index];
}
public CommandLineArgument Get(string argumentName)
{
return _arguments.FirstOrDefault(p => p == argumentName);
}
public bool Has(string argumentName)
{
return _arguments.Count(p => p == argumentName) > 0;
}
}
public class modes
{
public string path = "";
public int phrase_number = 2;
public bool count_topk = false;
public int top_number = -1;
public bool count_char = false;
public bool count_word = false;
public bool count_phrase = false;
public bool directory = false;
public bool recurrent = false;
public bool stopword = false;
public string stopword_path;
public bool verb_origin = false;
public string verb_path = "";
public bool verb_prep_phrase = false;
public string prep_path = "";
public modes(string[] args)
{
List<string> returnlist = new List<string>();
var arguments = CommandLineArgumentParser.Parse(args);
List<string> paramlist = new List<string>() { "-c", "-f", "-q", "-p" };
int num = 0;
foreach (string param in paramlist)
{
if (arguments.Has(param))
num++;
}
if (num != 1)
throw new System.ArgumentException("Parameter conflict: use only one of -c, -f, -q and -p");
if (arguments.Has("-c"))
{
this.count_char = true;
}
else if (arguments.Has("-f"))
{
this.count_word = true;
}
else if (arguments.Has("-p"))
{
this.count_phrase = true;
var arg = arguments.Get("-p");
int number = int.Parse(arg.Next);
if (!(number > 0))
throw new System.ArgumentException("Phrase length error! Must be larger than zero.");
this.phrase_number = number;
}
else if (arguments.Has("-q"))
{
this.verb_prep_phrase = true;
if (!arguments.Has("-v"))
throw new System.ArgumentException("-q must be used with -v!");
var arg = arguments.Get("-q");
string path = arg.Next;
if (!File.Exists(path))
throw new System.ArgumentException("File " + path + " not exists!");
this.prep_path = path;
}
if (arguments.Has("-d"))
{
this.directory = true;
var arg = arguments.Get("-d");
string directory = arg.Next;
if (directory == "-s")
{
this.recurrent = true;
directory = arg.Next;
}
if (!Directory.Exists(directory))
throw new System.ArgumentException(("Directory " + directory + " not exists!"));
this.path = directory;
}
else
{
int path_index = arguments.length - 1;
string path = arguments.Get_by_index(path_index);
if (!File.Exists(path))
throw new System.ArgumentException("File " + path + " not exists!");
this.path = path;
}
if (arguments.Has("-n"))
{
this.count_topk = true;
var arg = arguments.Get("-n");
int number = int.Parse(arg.Next);
if (number < 1)
throw new System.ArgumentException("You must assign a number which is larger than zero");
this.top_number = number;
}
if (arguments.Has("-x"))
{
this.stopword = true;
var arg = arguments.Get("-x");
string filename = arg.Next;
if (!File.Exists(filename))
throw new System.ArgumentException("File " + path + " not exists!");
this.stopword_path = filename;
}
if (arguments.Has("-v"))
{
this.verb_origin = true;
var arg = arguments.Get("-v");
string filename = arg.Next;
if (!File.Exists(filename))
throw new System.ArgumentException("File " + path + " not exists!");
this.verb_path = filename;
}
//todo: consider multi-mode conflict
// sort error to one place
}
}
class VocabTree
{
public char name;
public string originverb;
Dictionary<char, VocabTree> childs;
public VocabTree(char name)
{
this.name = name;
this.originverb = null;
this.childs = new Dictionary<char, VocabTree>();
}
public bool Contains(string word)
{
search_word(ref word);
if (word.Count() == 0)
return true;
return false;
}
public string verb_map(string word)
{
VocabTree tree = search_word(ref word);
if (word.Count() != 0)
return null;
return tree.originverb;
}
VocabTree search_word(ref string word)
{
if (word.Count() == 0)
return this;
char c = word[0];
if (this.childs.ContainsKey(c))
{
//Console.WriteLine(word);
word = word.Substring(1);
//Console.WriteLine(word);
return childs[c].search_word(ref word);
}
return this;
}
public int add_word(string word)
{
VocabTree tree = search_word(ref word);
if (word.Count() == 0)
return 0;
char c = word[0];
tree.childs[c] = new VocabTree(c);
word = word.Substring(1);
add_word(word);
return 1;
}
public int add_word(string word, string originword)
{
VocabTree tree = search_word(ref word);
if (word.Count() == 0)
{
this.originverb = originword;
return 0;
}
char c = word[0];
tree.childs[c] = new VocabTree(c);
word = word.Substring(1);
tree.childs[c].add_word(word, originword);
return 1;
}
}
class Program
{
public static char[] SPACES = new char[] { ' ', '\t', '\r', '\n' };
public static char[] SPLITOR;
public static char[] PHRASE_SPLITOR;
static int Main(string[] args)
{
//string testfile = "D:/ASE/word_count/test_file/pride-and-prejudice.txt";
//string verbfile = "D:/ASE/word_count/src_file/verbs_all.txt";
//string prepfile = "D:/ASE/word_count/src_file/prepositions.txt";
try
{
modes mode = new modes(args);
string[] files;
if (mode.directory && mode.recurrent)
files = Directory.GetFiles(mode.path, "*.*", SearchOption.AllDirectories);
else if (mode.directory)
files = Directory.GetFiles(mode.path);
else
files = new string[] { mode.path };
foreach (string file in files)
{
process_onefile(mode, file);
}
return 0;
}
catch (ArgumentException e)
{
Console.WriteLine(e.Message);
return -1;
}
}
static void process_onefile(modes mode, string file)
{
Console.WriteLine("Processing file " + file);
int num = mode.top_number;
if (mode.count_char)
{
print_dictionary<char>(count_char(mode, file), num);
return;
}
find_splitor(file);
if (mode.count_word)
{
print_dictionary<string>(count_word(mode, file), num);
}
else if (mode.count_phrase)
{
print_dictionary<string>(count_phrase(mode, file), num);
}
else if (mode.verb_prep_phrase)
{
print_dictionary<string>(count_verbprep_phrase(mode, file), num);
}
return;
}
static void find_splitor(string infile)
{
HashSet<char> splitors = new HashSet<char>();
HashSet<char> phrase_splitors = new HashSet<char>();
StreamReader sr = new StreamReader(infile);
int read_size = 1000;
char[] buffer = new char[read_size];
while (!sr.EndOfStream)
{
sr.Read(buffer, 0, read_size);
//for (int i = 0; i < 1000; i++)
// buffer[i] = char(sr.Read());
foreach (char c in buffer)
{
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'))
continue;
if (splitors.Contains(c))
continue;
splitors.Add(c);
if (!SPACES.Contains(c))
phrase_splitors.Add(c);
}
SPLITOR = splitors.ToArray();
PHRASE_SPLITOR = phrase_splitors.ToArray();
}
}
static Dictionary<char, int> count_char(modes mode, string infile)
{
Dictionary<char, int> char_freq = new Dictionary<char, int>();
for (char c = 'a'; c <= 'z'; c++)
{
char_freq[c] = 0;
}
for (char c = 'A'; c <= 'Z'; c++)
{
char_freq[c] = 0;
}
StreamReader sr = new StreamReader(infile);
int read_size = 10000;
char[] buffer = new char[read_size];
while (!sr.EndOfStream)
{
int num = sr.Read(buffer, 0, read_size);
//for (int i = 0; i < 1000; i++)
// buffer[i] = char(sr.Read());
for (int i = 0; i < num; i++)
{
char c = buffer[i];
if (!char_freq.ContainsKey(c))
continue;
char_freq[c]++;
}
}
for (char c = 'A'; c <= 'Z'; c++)
{
char tmp = c.ToString().ToLower()[0];
char_freq[tmp] += char_freq[c];
char_freq.Remove(c);
}
sr.Close();
//num2freq<char>(char_freq);
return char_freq;
}
static Dictionary<string, int> count_word(modes mode, string infile)
{
Dictionary<string, int> word_freq = new Dictionary<string, int>();
StreamReader sr = new StreamReader(infile);
while (!sr.EndOfStream)
{
//TODO: line overflow
string line = sr.ReadLine().Trim().ToLower();
string[] words = line.Split(SPLITOR, StringSplitOptions.RemoveEmptyEntries);
foreach (string word in words)
{
//TODO: optimize pipeline (1)judge a-z (2)contains (3)verb origin
char c = word[0];
if (!(c >= 'a' && c <= 'z') && !(c >= 'A' && c <= 'Z'))
continue;
if (!word_freq.ContainsKey(word))
word_freq[word] = 0;
word_freq[word]++;
}
}
sr.Close();
//num2freq<string>(word_freq);
if (mode.stopword)
word_freq = remove_stopwords(word_freq, mode.stopword_path);
//if (mode.verb_origin)
// word_freq = verb2origin(word_freq, mode.verb_path, 0);
return word_freq;
}
static Dictionary<string, int> remove_stopwords(Dictionary<string, int> dict, string stopword_path)
{
string[] stopwords = File.ReadAllLines(stopword_path);
foreach (string word in stopwords)
{
dict.Remove(word);
}
return dict;
}
static Dictionary<string, int> count_phrase(modes mode, string infile)
{
Dictionary<string, int> phrase_freq = new Dictionary<string, int>();
Dictionary<string, string> verb_map = new Dictionary<string, string>();
if (mode.verb_origin)
verb_map = gen_verb_map(mode.verb_path);
HashSet<string> stopwords = new HashSet<string>();
if (mode.stopword)
stopwords = gen_wordlist(mode.stopword_path);
int len = mode.phrase_number;
StreamReader sr = new StreamReader(infile);
string line_last_words = "";
int buffer_size = 1000 * len;
while (!sr.EndOfStream)
{
string buffer = line_last_words;
while (!sr.EndOfStream && buffer.Length < buffer_size)
{
string line = sr.ReadLine().Trim().ToLower();
buffer = buffer + " " + line;
//buffer = string.Join(" ", buffer, line);
}
string[] sentences = buffer.Split(PHRASE_SPLITOR, StringSplitOptions.None);
//string[] words = new string[] { };
List<string> words = new List<string>();
foreach (string sentence in sentences)
{
words = new List<string>(sentence.Split(SPACES, StringSplitOptions.RemoveEmptyEntries));
int last_index = words.Count();
for (int i = 0; i < last_index;)
{
string word = words[i];
char c = word[0];
if (c >= '0' && c <= '9')
{
words.Remove(word);
last_index -= 1;
continue;
}
if (mode.stopword)
{
if (stopwords.Contains(word))
{
words.Remove(word);
last_index -= 1;
continue;
}
}
if (mode.verb_origin)
{
if (verb_map.ContainsKey(word))
words[i] = verb_map[word];
}
i++;
}
int tmp = words.Count();
if (tmp < len)
{
continue;
}
last_index = tmp - len + 1;
for (int i = 0; i < last_index; i++)
{
string phrase = "";
int max = i + len;
for (int j = i; j < max; j++)
phrase = string.Join(" ", phrase, words[j]).Trim();
if (!phrase_freq.ContainsKey(phrase))
phrase_freq[phrase] = 0;
phrase_freq[phrase] += 1;
//if (phrase.Contains("sight"))
// Console.WriteLine();
}
}
line_last_words = "";
if (words.Count() < len)
{
line_last_words = sentences.Last();
}
else
{
words.RemoveRange(0, words.Count() - len + 1);
//words = words.Skip(words.Count() - len).ToArray();
line_last_words = string.Join(" ", words);
}
}
sr.Close();
//num2freq<string>(phrase_freq);
//if (mode.verb_origin)
// phrase_freq = verb2origin(phrase_freq, mode.verb_path, 2);
return phrase_freq;
}
static Dictionary<string, int> count_verbprep_phrase(modes mode, string infile)
{
string verb_file = mode.verb_path;
Dictionary<string, string> verb_map = gen_verb_map(verb_file);
HashSet<string> stopwords = new HashSet<string>();
if (mode.stopword)
stopwords = gen_wordlist(mode.stopword_path);
string prep_file = mode.prep_path;
HashSet<string> preps = gen_wordlist(prep_file);
Dictionary<string, int> phrase_freq = new Dictionary<string, int>();
StreamReader sr = new StreamReader(infile);
string line_last_word = "";
int buffer_size = 1000;
while (!sr.EndOfStream)
{
string buffer = line_last_word;
int cnt = 0;
while (!sr.EndOfStream && buffer.Length < buffer_size)
{
cnt++;
string line = sr.ReadLine().Trim().ToLower();
//buffer = buffer + " " + line;
buffer = string.Join(" ", buffer, line);
}
string[] sentences = buffer.Split(PHRASE_SPLITOR, StringSplitOptions.None);
List<string> words = new List<string>();
int last_index;
foreach (string sentence in sentences)
{
words = new List<string>(sentence.Split(SPACES, StringSplitOptions.RemoveEmptyEntries));
last_index = words.Count();
for (int i = 0; i < last_index;)
{
string word = words[i];
char c = word[0];
if (c >= '0' && c <= '9')
{
words.Remove(word);
last_index -= 1;
continue;
}
if (mode.stopword)
{
if (stopwords.Contains(word))
{
words.Remove(word);
last_index -= 1;
continue;
}
}
if (mode.verb_origin)
{
if (verb_map.ContainsKey(word))
words[i] = verb_map[word];
}
i++;
}
int tmp = words.Count();
if (tmp < 2)
{
continue;
}
last_index = tmp - 1;
for (int i = 0; i < last_index; i++)
{
string first_word = words[i];
string second_word = words[i + 1];
if (!preps.Contains(second_word))
continue;
if (!verb_map.ContainsKey(first_word))
continue;
if (mode.verb_origin)
first_word = verb_map[first_word];
//Console.WriteLine(first_word);
string key = string.Join(" ", first_word, second_word);
if (!phrase_freq.ContainsKey(key))
phrase_freq[key] = 0;
phrase_freq[key] += 1;
//if (key.Contains("sight with"))
// Console.WriteLine();
//i++;
}
}
last_index = words.Count() - 1;
if (last_index >= 0)
line_last_word = words[last_index];
else
line_last_word = "";
}
sr.Close();
//num2freq<string>(phrase_freq);
//if (mode.verb_origin)
// phrase_freq = verb2origin(phrase_freq, mode.verb_path, 1);
return phrase_freq;
}
//static Dictionary<T, int> num2freq<T>(Dictionary<T, int> dict)
//{
// int sum = 0;
// foreach (T phrase in dict.Keys)
// {
// sum += dict[phrase];
// }
// List<T> keys = new List<T>(dict.Keys);
// foreach (T phrase in keys)
// {
// dict[phrase] /= sum;
// }
// return dict;
//}
static void print_dictionary<T>(Dictionary<T, int> dict, int num)
{
int cnt = 0;
List<T> keys = new List<T>(dict.Keys);
keys.OrderBy(o => o);
keys = new List<T>(keys.OrderByDescending(o => dict[o]));
int buffer_size = 200;
string buffer = "";
foreach (T c in keys)
{
string new_c = c.ToString();
string tmp = string.Format("{0, 40}\t{1}", new_c, dict[c].ToString());
buffer = string.Join("\n", buffer, tmp);
//buffer = buffer + '\n' + tmp;
cnt++;
if (cnt % buffer_size == 0)
{
Console.Write(buffer.Trim('\n') + '\n');
buffer = "";
}
if (cnt == num)
break;
}
Console.Write(buffer.Trim('\n') + '\n');
return;
}
static VocabTree gen_verb_tree(string infile)
{
VocabTree tree = new VocabTree('\0');
StreamReader sr = new StreamReader(infile);
while (!sr.EndOfStream)
{
string line = sr.ReadLine();
string[] words = line.Split(new string[] { " -> " }, StringSplitOptions.None);
string verb_origin = words[0];
string[] verbs = words[1].Split(',');
//sw.WriteLine(verb_origin);
tree.add_word(verb_origin, verb_origin);
foreach (string verb in verbs)
{
tree.add_word(verb, verb_origin);
}
}
sr.Close();
return tree;
}
static Dictionary<string, string> gen_verb_map(string infile)
{
Dictionary<string, string> verb_map = new Dictionary<string, string>();
StreamReader sr = new StreamReader(infile);
while (!sr.EndOfStream)
{
string line = sr.ReadLine();
string[] words = line.Split(new string[] { " -> " }, StringSplitOptions.None);
string verb_origin = words[0].Trim();
string[] verbs = words[1].Split(',');
verb_map[verb_origin] = verb_origin;
foreach (string verb in verbs)
{
verb_map[verb.Trim()] = verb_origin;
}
}
sr.Close();
return verb_map;
}
static Dictionary<string, string> gen_prep_map(string infile)
{
Dictionary<string, string> prep_map = new Dictionary<string, string>();
string text = File.ReadAllText(infile);
string[] words = text.Split(SPACES);
foreach (string word in words)
prep_map[word] = word;
return prep_map;
}
static HashSet<string> gen_wordlist(string infile)
{
HashSet<string> wordlist = new HashSet<string>();
string text = File.ReadAllText(infile);
string[] words = text.Split(SPACES);
foreach (string word in words)
wordlist.Add(word);
return wordlist;
}
static VocabTree gen_vocab_tree(string infile)
{
VocabTree tree = new VocabTree('\0');
string text = File.ReadAllText(infile);
string[] words = text.Split(SPACES);
foreach (string word in words)
tree.add_word(word);
return tree;
}
static List<string> process_verb_file(string infile)
{
////TODO: possible speed slower because of file IO
//string base_dir = System.Environment.CurrentDirectory;
//string outfile = Path.Combine(base_dir, "verblist.txt");
//StreamWriter sw = new StreamWriter(outfile);
List<string> verblist = new List<string>();
StreamReader sr = new StreamReader(infile);
while (!sr.EndOfStream)
{
string line = sr.ReadLine();
string[] words = line.Split(new string[] { " -> " }, StringSplitOptions.None);
string verb_origin = words[0];
string[] verbs = words[1].Split(',');
//sw.WriteLine(verb_origin);
verblist.Add(verb_origin);
foreach (string verb in verbs)
{
verblist.Add(verb);
}
}
sr.Close();
//sw.Close();
return verblist;
}
}
}