From 1fcd3a417cacced075b87773304ce030d5a022d2 Mon Sep 17 00:00:00 2001 From: Dingyuan Wang Date: Fri, 13 Nov 2015 13:16:19 +0800 Subject: [PATCH] fix compatibility problem with `with` statememt --- jieba/__init__.py | 30 +++++++++++++++--------------- jieba/posseg/__init__.py | 22 +++++++++++----------- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/jieba/__init__.py b/jieba/__init__.py index 079e193..222bccf 100644 --- a/jieba/__init__.py +++ b/jieba/__init__.py @@ -71,21 +71,21 @@ class Tokenizer(object): lfreq = {} ltotal = 0 f_name = resolve_filename(f) - with f: - for lineno, line in enumerate(f, 1): - try: - line = line.strip().decode('utf-8') - word, freq = line.split(' ')[:2] - freq = int(freq) - lfreq[word] = freq - ltotal += freq - for ch in xrange(len(word)): - wfrag = word[:ch + 1] - if wfrag not in lfreq: - lfreq[wfrag] = 0 - except ValueError: - raise ValueError( - 'invalid dictionary entry in %s at Line %s: %s' % (f_name, lineno, line)) + for lineno, line in enumerate(f, 1): + try: + line = line.strip().decode('utf-8') + word, freq = line.split(' ')[:2] + freq = int(freq) + lfreq[word] = freq + ltotal += freq + for ch in xrange(len(word)): + wfrag = word[:ch + 1] + if wfrag not in lfreq: + lfreq[wfrag] = 0 + except ValueError: + raise ValueError( + 'invalid dictionary entry in %s at Line %s: %s' % (f_name, lineno, line)) + f.close() return lfreq, ltotal def initialize(self, dictionary=None): diff --git a/jieba/posseg/__init__.py b/jieba/posseg/__init__.py index 429ba69..75998e0 100644 --- a/jieba/posseg/__init__.py +++ b/jieba/posseg/__init__.py @@ -88,17 +88,17 @@ class POSTokenizer(object): def load_word_tag(self, f): self.word_tag_tab = {} f_name = resolve_filename(f) - with f: - for lineno, line in enumerate(f, 1): - try: - line = line.strip().decode("utf-8") - if not line: - continue - word, _, tag = line.split(" ") - self.word_tag_tab[word] = tag - except Exception: - raise ValueError( - 'invalid POS dictionary entry in %s at Line %s: %s' % (f_name, lineno, line)) + for lineno, line in enumerate(f, 1): + try: + line = line.strip().decode("utf-8") + if not line: + continue + word, _, tag = line.split(" ") + self.word_tag_tab[word] = tag + except Exception: + raise ValueError( + 'invalid POS dictionary entry in %s at Line %s: %s' % (f_name, lineno, line)) + f.close() def makesure_userdict_loaded(self): if self.tokenizer.user_word_tag_tab: