-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeclutter.py
More file actions
65 lines (54 loc) · 2.07 KB
/
Copy pathdeclutter.py
File metadata and controls
65 lines (54 loc) · 2.07 KB
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
import sklearn.feature_extraction.text as ext
from nltk import corpus
from nltk import word_tokenize
from nltk.stem.porter import PorterStemmer
import pandas as pd
import sys, getopt
stemmer = PorterStemmer()
def get_stemmed_tokens(tokens, stemmer):
stemmed_tokens = []
for token in tokens:
if token.isalpha():
stemmed_tokens.append(stemmer.stem(token))
return stemmed_tokens
def get_tokens(string):
tokens = word_tokenize(string)
stemmed_tokens = get_stemmed_tokens(tokens, stemmer)
return stemmed_tokens
def parseLogs(inputFile, outputFile):
vectorizer = ext.CountVectorizer(tokenizer=get_tokens, stop_words='english')
with open(inputFile) as file:
lines = [line.rstrip() for line in file]
lineNos = dict(zip(range(1, len(lines)), lines))
doc_matrix = vectorizer.fit_transform(lines)
tf_idf_transformer = ext.TfidfTransformer().fit(doc_matrix)
sparse = tf_idf_transformer.transform(doc_matrix).toarray()
perLineScore = []
for row in sparse:
perLineScore.append(row.sum()/len(row.nonzero()[0]))
lineScores = dict(zip(range(1, len(lines)), perLineScore))
df = pd.DataFrame([lineNos, lineScores]).T
df.columns = ['d{}'.format(i) for i, col in enumerate(df, 1)]
df = df.sort_values(by=['d2'], ascending = False)
with open(outputFile, 'w') as outFile:
for index, row in df.iterrows():
line = "{0:0=3d} {1}\n"
outFile.write(line.format(index, row['d1']))
def main(argv):
inputfile = ''
outputfile = ''
try:
opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="])
except getopt.GetoptError:
print ('try.py -i <inputfile>')
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print ('test.py -i <inputfile>')
sys.exit()
elif opt in ("-i", "--ifile"):
inputfile = arg
outputfile = inputfile + '.sorted.txt'
parseLogs(inputfile, outputfile)
if __name__ == "__main__":
main(sys.argv[1:])