本系统支持多种中文处理功能,包括:
进入node-hanlp目录,执行脚本:
bash
cd node-hanlp
./scripts/build-docker-image.sh
或者直接拉取镜像:
bash
docker pull samurais/hanlp-api:1.0.0
运行以下命令启动容器:
bash
docker run -it --rm -p 3002:3000 samurais/hanlp-api:1.0.0
通过发送POST请求来访问服务: ```http POST /tokenizer HTTP/1.1 Host: localhost:3002 Content-Type: application/json
{
"type": "nlp",
"content": "刘德华和张学友创作了很多流行歌曲"
}
返回示例:
json
{
"status": "success",
"data": [
{
"word": "刘德华",
"nature": "nr",
"offset": 0
},
{
"word": "和",
"nature": "cc",
"offset": 0
},
{
"word": "张学友",
"nature": "nr",
"offset": 0
},
{
"word": "创作",
"nature": "v",
"offset": 0
},
{
"word": "了",
"nature": "ule",
"offset": 0
},
{
"word": "很多",
"nature": "m",
"offset": 0
},
{
"word": "流行歌曲",
"nature": "n",
"offset": 0
}
]
}
```
tokenizerkeywordsummaryphrasesuggesterconversionpinyin使用npm安装模块:
bash
npm install node-hanlp
配置文件路径为:
bash
node_modules/node-hanlp/lib/src-java/hanLP.properties
请修正root为您的目录路径,并将词典文件放入./data目录。
```javascript const Hanlp = require("node-hanlp");
// 初始化及配置 const HanLP = new Hanlp({ CustomDict: true, // 使用自定义词典 NameRecognize: true, // 中国人名识别 TranslatedNameRecognize: true, // 音译人名识别 JapaneseNameRecognize: true, // 日本人名识别 PlaceRecognize: true, // 地名识别 OrgRecognize: true // 机构名识别 });
// 标准分词 let words = HanLP.Tokenizer("商品和服务"); console.log(words);
// NLP分词 words = HanLP.NLPTokenizer("中国科学院计算技术研究所的宗成庆教授正在教授自然语言处理课程"); console.log(words);
// 索引分词 words = HanLP.IndexTokenizer("主副食品"); console.log(words);
// CRF分词 words = HanLP.CRFTokenizer("你好,欢迎使用HanLP汉语处理包!"); console.log(words);
// 去除停用词分词 words = HanLP.NoStopWord("你好,欢迎使用HanLP汉语处理包!"); console.log(words);
// 最短路分词 words = HanLP.ShortSegment("明天,刘志军案的关键人物,山西女商人丁淑苗在市二中院出庭受审。"); console.log(words);
// N-最短分词 words = HanLP.NShortSegment("刘喜杰石国祥会见吴亚琴先进事迹报告团成员"); console.log(words);
// 极速词典分词 words = HanLP.SpeedTokenizer("江西鄱阳湖干枯,中国最大淡水湖变成大草原"); console.log(words);
// 关键词提取 words = HanLP.Keyword("江西鄱阳湖干枯,中国最大淡水湖变成大草原", 3); console.log(words);
// 短语提取 words = HanLP.Phrase("江西鄱阳湖干枯,中国最大淡水湖变成大草原", 2); console.log(words);
// 提取文章摘要 let text = "据美国福克斯新闻报道,俄罗斯黑海舰队一艘护卫舰格里戈罗维奇海军上将号,正在驶向美国军舰发射导弹攻击叙利亚的区域。该护卫舰是俄罗斯最先进的护卫舰,2016年才刚服役,除防空、反舰导弹外,也可以发射巡航导弹。格里戈罗维奇海军上将号原定于本周访问叙利亚的塔尔图斯港。"; words = HanLP.Summary(text, 3); console.log(words);
// 文本推荐 // 句子级别,从一系列句子中挑选与输入句子最相似的那个
// 语义距离 // 输入一组词,计算它们之间的语义距离
// 简繁转换 let convertedText = HanLP.ConversionFont("简体转繁体", "ft"); console.log(convertedText);
// 拼音转换 let pinyinText = HanLP.Pinyin("你好", "tone"); console.log(pinyinText); ```