-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfasttext2files.py
More file actions
42 lines (35 loc) · 993 Bytes
/
fasttext2files.py
File metadata and controls
42 lines (35 loc) · 993 Bytes
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
#!/usr/bin/env python3
"""
fasttext2files.py: extract texts from fasttext file, store in seperate files
usage: fasttext2files.py
20181206 erikt(at)xs4all.nl
"""
import sys
COMMAND = sys.argv.pop(0)
MINFILENAMELEN = 8
def getText(line):
fields = line.split()
label = fields.pop(0)
date = fields.pop(0)
text = " ".join(fields)
return(text)
def makeFileName(thisId):
fileName = str(thisId)+".txt"
while len(fileName) < MINFILENAMELEN: fileName = "0"+fileName
return(fileName)
def saveText(text,thisId):
fileName = makeFileName(thisId)
try: outFile = open(fileName,"w")
except Exception as e:
sys.exit(COMMAND+": cannot write file "+fileName+": "+str(e))
print(text,file=outFile)
outFile.close()
def main(argv):
thisId = 1
for line in sys.stdin:
line = line.strip()
text = getText(line)
saveText(text,thisId)
thisId += 1
if __name__ == "__main__":
sys.exit(main(sys.argv))