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

jieba怎么在本地词库动态添加新词 #108

Open
zhaochuanzhen opened this issue Feb 24, 2020 · 4 comments
Open

jieba怎么在本地词库动态添加新词 #108

zhaochuanzhen opened this issue Feb 24, 2020 · 4 comments

Comments

@zhaochuanzhen
Copy link

zhaochuanzhen commented Feb 24, 2020

背景:我引入本地词库,项目启动成功,通过接口可以正常调用并获取分词结果

新需求:通过接口或者手动修改本地词库的方式,添加一些新词,并且项目不重启、新词生效。

我看了python的代码,有一个add_word的方法可以调用,java也有一个addWord,但是addWord是private修饰的,不能直接调用,各位大神有方法能动态刷新本地词库吗

附addWord方法

    private String addWord(String word) {
        if (null != word && !"".equals(word.trim())) {
            String key = word.trim().toLowerCase(Locale.getDefault());
            _dict.fillSegment(key.toCharArray());
            return key;
        }
        else
            return null;
    }
@zhaochuanzhen
Copy link
Author

求关注、求解答

@zhaochuanzhen
Copy link
Author

有人看到这个问题吗

@donglianghan
Copy link

这作者没有考虑的运行环境,我也遇到这个问题,load函数不能正确加载文件 loadUserDict(path);
想要 自己add 但是接口却是 private。
考虑this.getClass().getResourceAsStream(MAIN_DICT) 这个可以正常加载到文件。开放一个接口
而且作者自己不会异常也不应该,这样用的人并不知道自定义词典没有加载成功,不能做响应处理

@caofanqi
Copy link

caofanqi commented Jun 2, 2021

这个很好解决,只需要在自己的项目中创建一个全限定名相同的类com.huaban.analysis.jieba.WordDictionary,就可以随意修改他的代码了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants