-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnormalize_text.py
executable file
·58 lines (48 loc) · 1.48 KB
/
normalize_text.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
#!/usr/bin/python
import sys
from os import listdir
from os.path import isfile, join
import nltk
from nltk.tokenize import RegexpTokenizer
from nltk.corpus import *
from nltk.stem.porter import *
if len(sys.argv) != 3:
print "Usage:", sys.argv[0], "in_dir out_dir"
exit(-1)
in_dir = sys.argv[1]
out_dir = sys.argv[2]
filenames = [join(in_dir, f) for f in listdir(in_dir) if isfile(join(in_dir, f))]
is_text = False
text = ""
corpus_index = 1
tokenizer = RegexpTokenizer(r'\w+')
stemmer = PorterStemmer()
def extract_text(line):
global is_text, text, corpus_index, tokenizer, stemmer
if line.startswith("<TEXT>"):
is_text = True
elif line.startswith("</TEXT>"):
tokens = tokenizer.tokenize(text)
#stop = set(stopwords.words("english"))
#words = [w for w in tokens if w not in stop]
#words_stemmed = []
#for word in words:
# words_stemmed.append(stemmer.stem(word))
#final_text = ' '.join(words_stemmed)
final_text = ' '.join(tokens)
#print final_text # FOR debugging
if len(final_text) > 0:
out_file = open(join(out_dir, "corpus" + str(corpus_index)), "w")
out_file.write(final_text)
out_file.close()
is_text = False
text = ""
corpus_index += 1
elif is_text == True:
text = text + line.strip().lower() + " "
return None
for filename in sorted(filenames):
print filename
[extract_text(line) for line in open(filename)]
#filename = filenames[0]
#[extract_text(line) for line in open(filename)]