|
2 | 2 | from mode import Mode
|
3 | 3 | from words import list_to_dict
|
4 | 4 | from words import text_to_list
|
| 5 | +import csv |
| 6 | +import codecs |
5 | 7 |
|
6 | 8 | class Learn(Mode):
|
7 |
| - def validate(self, args): |
8 |
| - valid_args = False |
9 |
| - usage = 'Usage: %s learn <doc type> <file> <count>' % args[0] |
| 9 | + ext="" |
| 10 | + def validate(self, args): |
| 11 | + valid_args = False |
| 12 | + usage = 'Usage: %s learn <doc type> <file> <count>' % args[0] |
10 | 13 |
|
11 |
| - if len(args) == 5: |
12 |
| - doc_type = args[2] |
13 |
| - |
14 |
| - file_contents = None |
15 |
| - try: |
16 |
| - file_contents = open(args[3], 'r').read() |
17 |
| - except Exception as e: |
18 |
| - raise ValueError(usage + '\nUnable to read specified file "%s", the error message was: %s' % (args[3], e)) |
| 14 | + if len(args) == 5: |
| 15 | + doc_type = args[2] |
| 16 | + |
| 17 | + file_contents = None |
| 18 | + try: |
| 19 | + ext = (args[3])[-3:] |
| 20 | + if (ext=="csv"): |
| 21 | + f_open = codecs.open('args[3]','r',encoding='utf-8',errors='ignore') |
| 22 | + text = list(csv.reader(f_open, delimiter=',')) |
| 23 | + file_contents = "" |
| 24 | + for sen in text: |
| 25 | + file_contents = file_contents + (sen[1]) |
| 26 | + if (ext=="txt"): |
| 27 | + file_contents = open(args[3], 'r').read() |
| 28 | + except Exception as e: |
| 29 | + raise ValueError(usage + '\nUnable to read specified file "%s", the error message was: %s' % (args[3], e)) |
19 | 30 |
|
20 |
| - count = 0 |
21 |
| - try: |
22 |
| - count = int(args[4]) |
23 |
| - except: |
24 |
| - raise ValueError(usage + '\nEnter an integer value for the "count" parameter') |
| 31 | + count = 0 |
| 32 | + try: |
| 33 | + count = int(args[4]) |
| 34 | + except: |
| 35 | + raise ValueError(usage + '\nEnter an integer value for the "count" parameter') |
25 | 36 |
|
26 |
| - self.file_contents = file_contents |
27 |
| - self.count = count |
28 |
| - self.doc_type = doc_type |
| 37 | + self.file_contents = file_contents |
| 38 | + self.count = count |
| 39 | + self.doc_type = doc_type |
| 40 | + if (ext=="csv"): |
| 41 | + f_open.close() |
29 | 42 |
|
30 |
| - else: |
31 |
| - raise ValueError(usage) |
| 43 | + else: |
| 44 | + raise ValueError(usage) |
32 | 45 |
|
33 |
| - def execute(self): |
34 |
| - db = Db() |
35 |
| - l = text_to_list(self.file_contents) |
36 |
| - d = list_to_dict(l) |
37 |
| - db.update_word_counts(d, self.doc_type) |
38 |
| - db.update_doctype_count(self.count, self.doc_type) |
39 |
| - return self.count |
| 46 | + def execute(self): |
| 47 | + db = Db() |
| 48 | + l = text_to_list(self.file_contents) |
| 49 | + d = list_to_dict(l) |
| 50 | + db.update_word_counts(d, self.doc_type) |
| 51 | + db.update_doctype_count(self.count, self.doc_type) |
| 52 | + return self.count |
40 | 53 |
|
41 |
| - def output(self, _): |
42 |
| - print("Processed %s documents of type '%s'" % (self.count, self.doc_type)) |
| 54 | + def output(self, _): |
| 55 | + print("Processed %s documents of type '%s'" % (self.count, self.doc_type)) |
0 commit comments