summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPeng Wu <alexepico@gmail.com>2011-07-15 16:44:46 +0800
committerPeng Wu <alexepico@gmail.com>2011-07-15 16:44:46 +0800
commitfe2d8789c1b1ba6fab743ea7ff9d16c4306e46e3 (patch)
tree1ad6db8b71997c918c7cc81b8589721edd947d80
parent61be0be890cba36e4b6915d2a1311c3a946cbb29 (diff)
downloadtrainer-fe2d8789c1b1ba6fab743ea7ff9d16c4306e46e3.tar.gz
trainer-fe2d8789c1b1ba6fab743ea7ff9d16c4306e46e3.tar.xz
trainer-fe2d8789c1b1ba6fab743ea7ff9d16c4306e46e3.zip
begin to write segment.py
-rw-r--r--lib/utils.py13
-rwxr-xr-xreduce.py2
-rw-r--r--segment.py35
3 files changed, 48 insertions, 2 deletions
diff --git a/lib/utils.py b/lib/utils.py
index b2e6f10..156fbaf 100644
--- a/lib/utils.py
+++ b/lib/utils.py
@@ -1,3 +1,4 @@
+import os
import json
#Utils
@@ -17,8 +18,18 @@ def write_file(outfile, data):
#JSON Load/Store
def load_status(infile):
- return json.loads(read_file(infile))
+ data = '{}'
+ if os.access(infile, os.R_OK):
+ data = read_file(infile)
+
+ return json.loads(data)
def store_status(outfile, obj):
write_file(outfile, json.dumps(obj))
return
+
+#test case
+if __name__ == '__main__':
+ obj = load_status('/tmp/test.status')
+ print(obj)
+ store_status('/tmp/test.status', obj)
diff --git a/reduce.py b/reduce.py
index 6ef834b..62119e5 100755
--- a/reduce.py
+++ b/reduce.py
@@ -46,7 +46,7 @@ def iterateSubDirectory(oldroot, newroot, level):
if __name__ == '__main__':
- parser = ArgumentParser(description='Reduce the categories.')
+ parser = ArgumentParser(description='Reduce the levels of categories.')
parser.add_argument('--level', action='store', nargs=1, default=2, \
help='reduce to n levels of index')
parser.add_argument('origdir', action='store', \
diff --git a/segment.py b/segment.py
new file mode 100644
index 0000000..942a70e
--- /dev/null
+++ b/segment.py
@@ -0,0 +1,35 @@
+#!/usr/bin/python3
+import os
+import os.path
+from subprocess import Popen, PIPE
+
+
+def handleError(error):
+ sys.exit(error)
+
+def segmentOneText(infile, outfile):
+ pass
+
+def handleOneIndex(indexpath):
+ indexfile = open(indexpath, 'r')
+ for oneline in indexfile.readlines():
+ (title, textpath) = oneline.split('#')
+ infile = config.getTextDir() + textpath
+ outfile = config.getTextDir() + textpath + config.getSegmentPostfix()
+ print("Processing " + title)
+ segmentOneText(infile, outfile)
+ print("Processed "+ title)
+ indexfile.close()
+
+def walkThroughIndex(path):
+ for root, dirs, files in os.walk(path, topdown=True, onerror=handleError):
+ for onefile in files:
+ filepath = os.path.join(root, onefile)
+ if onefile.endswith(config.getIndexPostfix()):
+ handleOneIndex(filepath)
+ else:
+ print('Unexpected file:' + filepath)
+
+
+if __name__ == '__main__':
+ pass