-
Notifications
You must be signed in to change notification settings - Fork 1
/
feature_extract.py
executable file
·88 lines (78 loc) · 2.64 KB
/
feature_extract.py
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
#!/usr/bin/env python
#coding=utf-8
import os
import sys
word_dict = {}
stop_word = set([])
num_doc = 0
#############################################################################
# init stop word set
#############################################################################
def stop_word_init(stop_word_file):
global stop_word
f = open(stop_word_file, 'r')
tmp = []
while True:
term = f.readline()
if not term: break
term = term.strip().rstrip(os.sep)
tmp.append(term)
f.close()
stop_word = set(tmp)
############################################################################
# statistic term frequency of word per document
############################################################################
def statistic_frequence(file_name):
global word_dict
global stop_word
global num_doc
doc_term = dict()
total_term = 0
f = open(file_name, 'r')
while True:
line = f.readline()
if not line: break
terms = line.strip().rstrip(os.sep).split(' ')
for t in terms:
t = t.strip().split('/')[0].strip()
if t not in stop_word:
times = doc_term.get(t, 0)
if times == 0:
doc_term[t] = 1
else:
doc_term[t] += 1
f.close()
num_doc += 1
for t in doc_term:
times = word_dict.get(t, 0)
if times == 0:
word_dict[t] = 1
else:
word_dict[t] += 1
############################################################################
# pick out all files recursive
############################################################################
def fetch_all_file(dir_name):
files = os.listdir(dir_name)
for f in files:
if os.path.isdir(dir_name + os.sep + f):
fetch_all_file(dir_name + os.sep + f)
else:
statistic_frequence(dir_name + os.sep + f)
############################################################################
# main module
############################################################################
if __name__ == "__main__":
if len(sys.argv) != 4:
print "Usage: python feature_extract.py dir_name result_file_name stop_word_file"
sys.exit(-1)
stop_word_init(sys.argv[3])
if os.path.isdir(sys.argv[1]):
fetch_all_file(sys.argv[1])
else:
statistic_frequence(sys.argv[1])
out = open(sys.argv[2], 'w')
tuple_list = sorted(word_dict.items(), key = lambda d:d[1], reverse = True)
for term in tuple_list:
print >> out, term[0] + '\t' + str(term[1]) + '\t' + str(num_doc - term[1])
out.close()