Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

調整使用自定義的tf-idf語料庫(set_idf_path),可讀取有空格的英文詞彙 #679

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion jieba/analyse/tfidf.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import jieba
import jieba.posseg
from operator import itemgetter
import re

_get_module_path = lambda path: os.path.normpath(os.path.join(os.getcwd(),
os.path.dirname(__file__), path))
Expand Down Expand Up @@ -42,12 +43,14 @@ def __init__(self, idf_path=None):
self.set_new_path(idf_path)

def set_new_path(self, new_idf_path):
re_idf = re.compile('^(.+?)( [0-9,.]+)?$', re.U)
if self.path != new_idf_path:
self.path = new_idf_path
content = open(new_idf_path, 'rb').read().decode('utf-8')
self.idf_freq = {}
for line in content.splitlines():
word, freq = line.strip().split(' ')
word, freq = re_idf.match(line).groups()
# word, freq = line.strip().split(' ')
self.idf_freq[word] = float(freq)
self.median_idf = sorted(
self.idf_freq.values())[len(self.idf_freq) // 2]
Expand Down