-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate_file.py
132 lines (112 loc) · 3.28 KB
/
generate_file.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
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
#!/usr/bin/python
import os
import commands
import string
import re
import sys
import gzip
import zipfile
import pickle
import random
from operator import itemgetter
global _tokenize_regexps
_tokenize_regexps = [
(re.compile(r'\`'), r'\''),
(re.compile(r'(\s|^)rt@[a-z0-9]+ '), r'-user- '),
#(re.compile(r'''),r'\''),
#(re.compile(r'`'),r'\''),
#(re.compile(r'rt'),r''),
#(re.compile(r'"'),r''),
#(re.compile(r'?'),r''),
#(re.compile(r'"'),r''),
#(re.compile(r'&[a-z][a-z];'),r''),
#(re.compile(r'&[a-z][a-z]'),r''),
#(re.compile(r'\s#[a-z]+\s'),r' -topic- '),
#(re.compile(r'\'\''), r'"'),
#(re.compile(r'\`\`'), r'"'),
# Combine dots separated by whitespace to be a single token:
#(re.compile(r'\.\s\.\s\.'), r'...'),
# fix %, $, &
#(re.compile(r'(\d)%'), r' -percent- '),
#(re.compile(r'\$(\.?\d)'), r' -money- '),
#(re.compile(r'(\w)& (\w)'), r'\1&\2'),
#(re.compile(r'(\w\w+)&(\w\w+)'), r'\1 & \2'),
# Only separate comma if space follows:
#(re.compile(r'(.)(,)(\s|$)'), r'\1 \2\3'),
# Treat double-hyphen as one token:
#(re.compile(r'([^-])(\-\-+)([^-])'), r'\1 \2 \3'),
#(re.compile(r'(\s|^)(,)(?=(\S))'), r'\1\2 '),
# Separate words from ellipses
#(re.compile(r'([^\.]|^)(\.{2,})(.?)'), r'\1 \2 \3'),
#(re.compile(r'(^|\s)(\.{2,})([^\.\s])'), r'\1\2 \3'),
#(re.compile(r'([^\.\s])(\.{2,})($|\s)'), r'\1 \2\3'),
# Separate punctuation (except period) from words:
#(re.compile(r'(^|\s)(\')'), r'\1\2 '),
#(re.compile(r'(?=[\(\"\`{\[:;&\#\*@])(.)'), r'\1 '),
#(re.compile(r'(.)(?=[?!)\";}\]\*:@\'])'), r'\1 '),
#(re.compile(r'(?=[\)}\]])(.)'), r'\1 '),
#(re.compile(r'(.)(?=[({\[])'), r'\1 '),
#(re.compile(r'((^|\s)\-)(?=[^\-])'), r'\1 '),
]
def tokenize(s):
"""
Tokenize a string using the rule above
"""
global _tokenize_regexps
for (regexp, repl) in _tokenize_regexps:
s = regexp.sub(repl, s)
return s
def save_pickle(data, path):
sys.stderr.write("\nWriting lookup table file "+path+"\n")
with open(path, 'wb') as f:
pickle.dump(data, f)
def load_pickle(path):
sys.stderr.write("\nLoading lookup table file "+path+"\n")
with open(path, 'rb') as f:
data = pickle.load(f)
return data
def get_files(path, pattern):
"""
Recursively find all files rooted in <path> that match the regexp <pattern>
"""
L = []
# base case: path is just a file
if (re.match(pattern, os.path.basename(path)) != None) and os.path.isfile(path):
L.append(path)
return L
# general case
if not os.path.isdir(path):
return L
contents = os.listdir(path)
for item in contents:
item = path + item
if (re.search(pattern, os.path.basename(item)) != None) and os.path.isfile(item):
L.append(item)
elif os.path.isdir(path):
L.extend(get_files(item + '/', pattern))
return L
if __name__ == '__main__':
#debug()
files = get_files("./",".*")
for file in files:
if file.find("py") >= 0:continue
if file.find(".bat") >= 0:continue
lines = open(file).read().splitlines()
f = open("./new/"+file,'w')
p = 0
for line in lines:
line = line.replace("''","\"")
line = line.replace("``","\"")
if line.lower() == "<p>":
p=1
f.write(line+"\n")
tmp=""
if line.lower() == "</p>":
p=0
f.write(tmp+"\n")
tmp=""
if p == 1 and line.lower() != "<p>":
tmp = tmp+" "+line
elif p == 0:
f.write(line+"\n")
f.close