diff options
| author | schneefux <schneefux+commit@schneefux.xyz> | 2014-09-11 10:06:37 -0400 |
|---|---|---|
| committer | schneefux <schneefux+commit@schneefux.xyz> | 2014-09-11 10:06:37 -0400 |
| commit | 2b6f6469189cbff93a16a2025fa3560e5086667c (patch) | |
| tree | 1600682ae02be5003124ef192092b91367f1bda5 /client | |
| parent | 6be18f4b365edcf2c38e766122992cf6621fc7af (diff) | |
| parent | 8e721cd3bcade4fb526d2f7a1d8b02f3fba6bdf5 (diff) | |
| download | jasper-client-2b6f6469189cbff93a16a2025fa3560e5086667c.tar.gz jasper-client-2b6f6469189cbff93a16a2025fa3560e5086667c.zip | |
Merge pull request #165 from Holzhaus/use-tempfile-module
Use tempfile module in g2p
Diffstat (limited to 'client')
| -rw-r--r-- | client/g2p.py | 17 |
1 files changed, 8 insertions, 9 deletions
diff --git a/client/g2p.py b/client/g2p.py index d22445a..3225c73 100644 --- a/client/g2p.py +++ b/client/g2p.py @@ -1,9 +1,9 @@ # -*- coding: utf-8-*- import os +import tempfile import subprocess import re -TEMP_FILENAME = "g2ptemp" PHONE_MATCH = re.compile(r'<s> (.*) </s>') PHONETISAURUS_PATH = os.environ['JASPER_HOME'] + "/phonetisaurus" @@ -25,12 +25,12 @@ def translateWord(word): def translateWords(words): full_text = '\n'.join(words) - f = open(TEMP_FILENAME, "wb") - f.write(full_text) - f.flush() + with tempfile.NamedTemporaryFile(suffix='.g2p', delete=False) as f: + temp_filename = f.name + f.write(full_text) - output = translateFile(TEMP_FILENAME) - os.remove(TEMP_FILENAME) + output = translateFile(temp_filename) + os.remove(temp_filename) return output @@ -43,9 +43,8 @@ def translateFile(input_filename, output_filename=None): if output_filename: out = '\n'.join(out) - f = open(output_filename, "wb") - f.write(out) - f.close() + with open(output_filename, "wb") as f: + f.write(out) return None |
