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

修复加载用户字典时total不改变,添加是否加载默认字典配置 #58

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
是否使用默认字典可配置
  • Loading branch information
alexqdjay committed Jul 30, 2017
commit 51f3af1b5e0f6500c85cc8f950d5730b2fdf7d6b
25 changes: 19 additions & 6 deletions src/main/java/com/huaban/analysis/jieba/WordDictionary.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
import java.util.*;
import java.util.Map.Entry;
import java.util.Set;


public class WordDictionary {
Expand All @@ -27,10 +23,24 @@ public class WordDictionary {
private Double minFreq = Double.MAX_VALUE;
private Double total = 0.0;
private DictSegment _dict;
private boolean useDefaultDict = true;

private static final String CONFIG_NAME = "jieba.defaultDict";

private WordDictionary() {
this.loadDict();
loadConfig();

if (this.useDefaultDict) {
this.loadDict();
}
}

private void loadConfig() {
String configString = System.getenv(CONFIG_NAME);
if (configString == null) {
configString = System.getProperty(CONFIG_NAME, "true");
}
this.useDefaultDict = Boolean.valueOf(configString);
}


Expand Down Expand Up @@ -205,6 +215,9 @@ public boolean containsWord(String word) {
return freqs.containsKey(word);
}

public boolean isUseDefaultDict() {
return useDefaultDict;
}

public Double getFreq(String key) {
if (containsWord(key))
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/com/huaban/analysis/jieba/WordDictionaryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.huaban.analysis.jieba;

import org.junit.Assert;
import org.junit.Test;

import java.net.URISyntaxException;
Expand All @@ -42,4 +43,17 @@ public void test_loadDict() throws URISyntaxException {
System.out.println(d);
}

@Test
public void test_useDefaultDict() {
WordDictionary wordDict = WordDictionary.getInstance();
Assert.assertTrue(wordDict.isUseDefaultDict());
}

@Test
public void test_useDefaultDict2() {
System.setProperty("jieba.defaultDict", "false");
WordDictionary wordDict = WordDictionary.getInstance();
Assert.assertTrue(wordDict.isUseDefaultDict() == false);
}

}