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

real time update words dict support #105

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.huaban.analysis.jieba;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -28,7 +29,6 @@ public void initUserDict(Path path){

public void initUserDict(String[] paths){
wordDict.init(paths);

}

private Map<Integer, List<Integer>> createDAG(String sentence) {
Expand Down
32 changes: 24 additions & 8 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 Down Expand Up @@ -145,7 +141,7 @@ public void loadDict() {
}


private String addWord(String word) {
public String addWord(String word) {
if (null != word && !"".equals(word.trim())) {
String key = word.trim().toLowerCase(Locale.getDefault());
_dict.fillSegment(key.toCharArray());
Expand All @@ -154,7 +150,27 @@ private String addWord(String word) {
else
return null;
}



public String addWord(String word, double freq) {
word = addWord(word);
freqs.put(word, freq);
return word;
}


public void addWords(List<String> words) {
for(String word : words)
addWord(word);
}


public void addWords(Map<String, Double> words) {
for(Map.Entry<String, Double> entry : words.entrySet()) {
addWord(entry.getKey(), entry.getValue());
}
}


public void loadUserDict(Path userDict) {
loadUserDict(userDict, StandardCharsets.UTF_8);
Expand Down Expand Up @@ -183,7 +199,7 @@ public void loadUserDict(Path userDict, Charset charset) {
double freq = 3.0d;
if (tokens.length == 2)
freq = Double.valueOf(tokens[1]);
word = addWord(word);
word = addWord(word);
freqs.put(word, Math.log(freq / total));
count++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class FinalSeg {
private static Map<Character, Double> start;
private static Map<Character, Map<Character, Double>> trans;
private static Map<Character, char[]> prevStatus;
private static Double MIN_FLOAT = -3.14e100;;
private static Double MIN_FLOAT = -3.14e100;


private FinalSeg() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ public void testBugSentence() {
}
}


@Test
public void testSegmentSpeed() {
long length = 0L;
Expand All @@ -179,8 +178,7 @@ public void testSegmentSpeed() {
System.out.println(String.format(Locale.getDefault(), "time elapsed:%d, rate:%fkb/s, sentences:%.2f/s", elapsed,
(length * 1.0) / 1024.0f / (elapsed * 1.0 / 1000.0f), wordCount * 1000.0f / (elapsed * 1.0)));
}



@Test
public void testLongTextSegmentSpeed() {
long length = 0L;
Expand Down