diff g_collocation.py @ 1:fb617586f4b2 draft

planemo upload commit a81826fe44f09a3710a35c183aa88b745aeec064-dirty
author stevecassidy
date Mon, 05 Dec 2016 05:22:05 -0500
parents e991d4e60c17
children a47980ef2b96
line wrap: on
line diff
--- a/g_collocation.py	Wed Oct 12 22:17:53 2016 -0400
+++ b/g_collocation.py	Mon Dec 05 05:22:05 2016 -0500
@@ -18,8 +18,9 @@
 
 def collocation(inp, outp, freq_filter, results, coll_type, pos):
     pos = bool(pos == 'true')
-    i = str(unicode(open(inp, 'r').read(), errors='ignore'))
-    o = open(outp, 'w')
+    with open(inp, 'r') as fd:
+        i = fd.read()
+
     all_words = []
     if pos:
         text = i.split(' ')[:-1]
@@ -37,10 +38,10 @@
         finder = TrigramCollocationFinder.from_words(all_words)
     finder.apply_freq_filter(int(freq_filter))
     colls = finder.nbest(measures.pmi, int(results))
-    for coll in colls:
-        o.write("%s\t%s" % coll)
-        o.write('\n')
-    o.close()
+    with  open(outp, 'w') as output:
+        for coll in colls:
+            output.write("%s\t%s" % coll)
+            output.write('\n')
 
 if __name__ == '__main__':
     args = Parser()