|
| 1 | +import sys |
| 2 | +import re |
| 3 | +import sqlite3 |
| 4 | +import getopt |
| 5 | +import codecs |
| 6 | +from collections import defaultdict |
| 7 | +from learn import Learn |
| 8 | +from classify import Classify |
| 9 | +from reset import Reset |
| 10 | +from status import Status |
| 11 | +from db import Db |
| 12 | + |
| 13 | +''' |
| 14 | +create table ok_words(word, count); |
| 15 | +create table scam_words(word, count); |
| 16 | +create table ad_counts(is_scam, count); |
| 17 | +insert into ad_counts (is_scam, count) values (1, 0); |
| 18 | +insert into ad_counts (is_scam, count) values (0, 0); |
| 19 | +
|
| 20 | +create index i1 on ok_words(word); |
| 21 | +create index i2 on scam_words(word); |
| 22 | +
|
| 23 | +P(W|L) = P(L|W) . P(W) / P(L) |
| 24 | +
|
| 25 | +
|
| 26 | +delete from ok_words; |
| 27 | +delete from scam_words; |
| 28 | +update ad_counts set count = 0; |
| 29 | +
|
| 30 | +''' |
| 31 | +commonWords = ('the','be','to','of','and','a','in','that','have','it','is','im','are','was','for','on','with','he','as','you','do','at','this','but','his','by','from','they','we','say','her','she','or','an','will','my','one','all','would','there','their','what','so','up','out','if','about','who','get','which','go','me','when','make','can','like','time','just','him','know','take','person','into','year','your','some','could','them','see','other','than','then','now','look','only','come','its','over','think','also','back','after','use','two','how','our','way','even','because','any','these','us') |
| 32 | +''' |
| 33 | +def cleanUpWord(word): |
| 34 | + word = re.sub('\W', '', word.lower()) |
| 35 | + if (len(word) < 2): |
| 36 | + return None |
| 37 | + elif (word.isdigit()): |
| 38 | + return None |
| 39 | + elif (word in commonWords): |
| 40 | + return None |
| 41 | + |
| 42 | + return word |
| 43 | +
|
| 44 | +
|
| 45 | +def update_db_with_dict(d, ad_count, is_scam_data): |
| 46 | + db = Db() |
| 47 | + db.update_ad_count(ad_count, is_scam_data) |
| 48 | + db.update_word_counts(d, is_scam_data) |
| 49 | +
|
| 50 | +def add_words_to_dict(l, d): |
| 51 | + for word in l: |
| 52 | + d[word] += 1 |
| 53 | +
|
| 54 | +def ad_to_word_list(ad_text): |
| 55 | + cleaned_words = map(cleanUpWord, ad_text.split(' ')) |
| 56 | + return filter(lambda word : word and (len(word) > 0), cleaned_words) |
| 57 | +
|
| 58 | +def file_to_ad_list(file_name): |
| 59 | + return codecs.open(file_name, 'r', 'utf-8').read().split('\n') |
| 60 | +
|
| 61 | +def process_file(file, is_scam_data): |
| 62 | + print 'Processing ', file |
| 63 | + ad_list = file_to_ad_list(file) |
| 64 | + ad_count = len(ad_list) |
| 65 | + print 'Found ',ad_count,'adverts in file - building dictionary...' |
| 66 | + d = defaultdict(int) |
| 67 | + i=1 |
| 68 | + for ad_text in ad_list: |
| 69 | + word_list = ad_to_word_list(ad_text) |
| 70 | + add_words_to_dict(word_list, d) |
| 71 | + i += 1 |
| 72 | + if i % 10000 == 0: |
| 73 | + print 'Processed ',i |
| 74 | + if i > 100000: |
| 75 | + break |
| 76 | +
|
| 77 | + update_db_with_dict(d, ad_count, is_scam_data) |
| 78 | +
|
| 79 | +def learn(non_scam_file, scam_file): |
| 80 | + process_file(non_scam_file, False) |
| 81 | + process_file(scam_file, True) |
| 82 | +
|
| 83 | +def calc_p_word(word): |
| 84 | + pass |
| 85 | +
|
| 86 | +def calc_p_words(p_word_list): |
| 87 | + pass |
| 88 | +
|
| 89 | +def get_total_word_count(c, for_scams): |
| 90 | + pass |
| 91 | +
|
| 92 | +def classify(input_file): |
| 93 | + text = open(input_file).read() |
| 94 | + words = ad_to_word_list(text) |
| 95 | +
|
| 96 | + conn = sqlite3.connect('./scam.db') |
| 97 | + c = conn.cursor() |
| 98 | + scam_word_count = get_ad_count(c, True) |
| 99 | + ok_word_count = get_ad_count(c, False) |
| 100 | +
|
| 101 | +
|
| 102 | + p_word_list = [] |
| 103 | + for word in words: |
| 104 | + p_word_list.append(calc_p_word(word)) |
| 105 | +
|
| 106 | + return calc_p_words(p_word_list) |
| 107 | +
|
| 108 | +if __name__ == '__main__': |
| 109 | + if (len(sys.argv) != 2): |
| 110 | + print 'Usage: %s <input file>' % sys.argv[0] |
| 111 | + |
| 112 | + else: |
| 113 | + input_file = sys.argv[1] |
| 114 | + classify(input_file) |
| 115 | + |
| 116 | +''' |
| 117 | + |
| 118 | +modes = {} |
| 119 | + |
| 120 | +def register_mode(mode_class): |
| 121 | + modes[mode_class.__name__.lower()] = mode_class |
| 122 | + |
| 123 | +if __name__ == '__main__': |
| 124 | + register_mode(Learn) |
| 125 | + register_mode(Classify) |
| 126 | + register_mode(Reset) |
| 127 | + register_mode(Status) |
| 128 | + |
| 129 | + args = sys.argv |
| 130 | + usage = 'Usage: %s %s <mode specific args>' % (args[0], '|'.join(modes.keys())) |
| 131 | + |
| 132 | + try: |
| 133 | + if (len(args) < 2): |
| 134 | + raise ValueError(usage) |
| 135 | + |
| 136 | + mode_name = args[1] |
| 137 | + if mode_name not in modes: |
| 138 | + raise ValueError(usage + '\nUnrecognised mode: ' + mode_name) |
| 139 | + |
| 140 | + mode = modes[mode_name]() |
| 141 | + mode.validate(args) |
| 142 | + mode.execute() |
| 143 | + |
| 144 | + except Exception as e: |
| 145 | + print e |
0 commit comments