-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathetokenize.py
executable file
·49 lines (45 loc) · 1.49 KB
/
etokenize.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
#!/usr/bin/python -W all
"""
etokenize.py: tokenize a text
usage: etokenize [-c] < file
notes: cannot be named tokenized.py: causes an import loop
option -c: keep upper case characters
20170626 erikt(at)xs4all.nl
"""
import csv
import getopt
import naiveBayes
import re
import sys
COMMAND = naiveBayes.COMMAND
USAGE = "usage: "+COMMAND+" [-c] < file"
NONE = -1
LABELPREFIX = "__label__" # label prefix of fasttext
classColumn = NONE
tweetColumn = NONE
keepUpperCase = False
try: options,arguments = getopt.getopt(sys.argv,"c",[])
except: sys.exit(USAGE)
for option in options:
if option[0] == "-c": keepUpperCase = True
csvreader = csv.reader(sys.stdin,delimiter=',',quotechar='"')
lineNbr = 0
for row in csvreader:
lineNbr += 1
# first line is a heading
if lineNbr == 1:
for i in range(0,len(row)):
if row[i] == "tweet": tweetColumn = i
elif row[i] == "class" or row[i] == naiveBayes.CLASSCOLUMNNAME: classColumn = i
else:
# sanity check
if tweetColumn == NONE: sys.exit(COMMAND+": tweet column definition missing in heading: "+str(row))
# tokenize tweet text
tokenized, = naiveBayes.tokenize([row[tweetColumn]],keepUpperCase)
# print tokenized text
if classColumn != NONE: sys.stdout.write(LABELPREFIX+row[classColumn]+" ")
for i in range(0,len(tokenized)):
if i > 0: sys.stdout.write(" ")
sys.stdout.write(tokenized[i].encode("utf8"))
print
sys.exit()