From 085b09c3ea06e45a475890196324439dff1a2f42 Mon Sep 17 00:00:00 2001 From: Felix Yan Date: Wed, 21 Nov 2012 18:07:19 +0800 Subject: [PATCH] add file-like object support --- jieba/__init__.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/jieba/__init__.py b/jieba/__init__.py index 1f8c6a2..e02431d 100644 --- a/jieba/__init__.py +++ b/jieba/__init__.py @@ -165,9 +165,11 @@ def cut(sentence,cut_all=False): if x!="": yield x -def load_userdict(f_name): +def load_userdict(f): global trie,total,FREQ - content = open(f_name,'rb').read().decode('utf-8') + if isinstance(f, (str, unicode)): + f = open(f, 'rb') + content = f.read().decode('utf-8') for line in content.split("\n"): if line.rstrip()=='': continue word,freq = line.split(" ")