Expand description
BosonNLP
REST API 访问的封装
Fields
token: String
用于 API 鉴权的 API Token
compress: bool
是否压缩大于 10K 的请求体,默认为 true
Implementations
sourceimpl BosonNLP
impl BosonNLP
sourcepub fn with_options<T: Into<String>>(
token: T,
bosonnlp_url: T,
compress: bool
) -> BosonNLP
pub fn with_options<T: Into<String>>(
token: T,
bosonnlp_url: T,
compress: bool
) -> BosonNLP
使用自定义参数初始化一个新的 BosonNLP
实例
sourcepub fn with_client<T: Into<String>>(token: T, client: Client) -> BosonNLP
pub fn with_client<T: Into<String>>(token: T, client: Client) -> BosonNLP
使用自定义的 reqwest Client 初始化一个新的 BosonNLP
实例
sourcepub fn sentiment<T: AsRef<str>>(
&self,
contents: &[T],
model: &str
) -> Result<Vec<(f32, f32)>>
pub fn sentiment<T: AsRef<str>>(
&self,
contents: &[T],
model: &str
) -> Result<Vec<(f32, f32)>>
sourcepub fn convert_time<T: AsRef<str>>(
&self,
content: T,
basetime: Option<T>
) -> Result<ConvertedTime>
pub fn convert_time<T: AsRef<str>>(
&self,
content: T,
basetime: Option<T>
) -> Result<ConvertedTime>
content
: 需要做时间转换的文本
basetime
: 时间描述时的基准时间戳。如果为 None
,使用服务器当前的GMT+8时间
使用示例
extern crate bosonnlp;
use bosonnlp::BosonNLP;
fn main() {
let nlp = BosonNLP::new(env!("BOSON_API_TOKEN"));
let time = nlp.convert_time("2013年二月二十八日下午四点三十分二十九秒", None).unwrap();
assert_eq!("2013-02-28 16:30:29", &time.timestamp.unwrap());
assert_eq!("timestamp", &time.format);
}
sourcepub fn keywords<T: AsRef<str>>(
&self,
text: T,
top_k: usize,
segmented: bool
) -> Result<Vec<(f32, String)>>
pub fn keywords<T: AsRef<str>>(
&self,
text: T,
top_k: usize,
segmented: bool
) -> Result<Vec<(f32, String)>>
text
: 需要做关键词提取的文本
top_k
: 返回结果的条数,最大值可设定为 100
segmented
: text
是否已经进行了分词,若为 true
则不会再对内容进行分词处理
使用示例
extern crate bosonnlp;
use bosonnlp::BosonNLP;
fn main() {
let nlp = BosonNLP::new(env!("BOSON_API_TOKEN"));
let rs = nlp.keywords("病毒式媒体网站:让新闻迅速蔓延", 2, false).unwrap();
assert_eq!(2, rs.len());
}
sourcepub fn depparser<T: AsRef<str>>(&self, contents: &[T]) -> Result<Vec<Dependency>>
pub fn depparser<T: AsRef<str>>(&self, contents: &[T]) -> Result<Vec<Dependency>>
contents
: 需要做依存文法分析的文本序列
使用示例
extern crate bosonnlp;
use bosonnlp::BosonNLP;
fn main() {
let nlp = BosonNLP::new(env!("BOSON_API_TOKEN"));
let rs = nlp.depparser(&["今天天气好"]).unwrap();
assert_eq!(1, rs.len());
let dep0 = &rs[0];
assert_eq!(vec![2isize, 2isize, -1isize], dep0.head);
let rs = nlp.depparser(&["今天天气好", "美好的世界"]).unwrap();
assert_eq!(2, rs.len());
}
sourcepub fn ner<T: AsRef<str>>(
&self,
contents: &[T],
sensitivity: usize,
segmented: bool
) -> Result<Vec<NamedEntity>>
pub fn ner<T: AsRef<str>>(
&self,
contents: &[T],
sensitivity: usize,
segmented: bool
) -> Result<Vec<NamedEntity>>
contents
: 需要做命名实体识别的文本序列
sensitivity
: 准确率与召回率之间的平衡。
设置成 1 能找到更多的实体,设置成 5 能以更高的精度寻找实体
一般设置为 3
segmented
: 输入是否已经为分词结果
使用示例
extern crate bosonnlp;
use bosonnlp::BosonNLP;
fn main() {
let nlp = BosonNLP::new(env!("BOSON_API_TOKEN"));
let rs = nlp.ner(&["成都商报记者 姚永忠"], 2, false).unwrap();
assert_eq!(1, rs.len());
let rs = nlp.ner(&["成都商报记者 姚永忠", "微软XP操作系统今日正式退休"], 2, false).unwrap();
assert_eq!(2, rs.len());
}
sourcepub fn tag<T: AsRef<str>>(
&self,
contents: &[T],
space_mode: usize,
oov_level: usize,
t2s: bool,
special_char_conv: bool
) -> Result<Vec<Tag>>
pub fn tag<T: AsRef<str>>(
&self,
contents: &[T],
space_mode: usize,
oov_level: usize,
t2s: bool,
special_char_conv: bool
) -> Result<Vec<Tag>>
contents
: 需要做分词与词性标注的文本序列
space_mode
: 空格保留选项,0-3 有效
oov_level
: 枚举强度选项,0-4 有效
t2s
: 是否开启繁体转简体
special_char_conv
: 是否转化特殊字符,针对回车、Tab 等特殊字符。
使用示例
extern crate bosonnlp;
use bosonnlp::BosonNLP;
fn main() {
let nlp = BosonNLP::new(env!("BOSON_API_TOKEN"));
let rs = nlp.tag(&["成都商报记者 姚永忠"], 0, 3, false, false).unwrap();
assert_eq!(1, rs.len());
}
sourcepub fn summary<T: Into<String>>(
&self,
title: T,
content: T,
word_limit: f32,
not_exceed: bool
) -> Result<String>
pub fn summary<T: Into<String>>(
&self,
title: T,
content: T,
word_limit: f32,
not_exceed: bool
) -> Result<String>
title
: 需要做摘要的新闻标题,如果没有则传入空字符串
content
: 需要做摘要的新闻正文
word_limit
: 摘要字数限制
not_exceed
: 是否严格限制字数
使用示例
extern crate bosonnlp;
use bosonnlp::BosonNLP;
fn main() {
let nlp = BosonNLP::new(env!("BOSON_API_TOKEN"));
let title = "前优酷土豆技术副总裁黄冬加盟芒果TV任CTO";
let content = "腾讯科技讯(刘亚澜)10月22日消息,前优酷土豆技术副总裁黄冬已于日前正式加盟芒果TV,出任CTO一职。";
let rs = nlp.summary(title, content, 1.0, false);
assert!(rs.is_ok());
}
sourcepub fn cluster<T: AsRef<str>>(
&self,
contents: &[T],
task_id: Option<&str>,
alpha: f32,
beta: f32,
timeout: Option<u64>
) -> Result<Vec<TextCluster>>
pub fn cluster<T: AsRef<str>>(
&self,
contents: &[T],
task_id: Option<&str>,
alpha: f32,
beta: f32,
timeout: Option<u64>
) -> Result<Vec<TextCluster>>
task_id
: 唯一的 task_id,话题聚类任务的名字,可由字母和数字组成
alpha
: 聚类最大 cluster 大小,一般为 0.8
beta
: 聚类平均 cluster 大小,一般为 0.45
timeout
: 等待文本聚类任务完成的秒数,一般为 1800 秒
使用示例
extern crate bosonnlp;
use bosonnlp::BosonNLP;
fn main() {
let nlp = BosonNLP::new(env!("BOSON_API_TOKEN"));
let contents = vec![
"今天天气好",
"今天天气好",
"今天天气不错",
"点点楼头细雨",
"重重江外平湖",
"当年戏马会东徐",
"今日凄凉南浦",
];
let rs = nlp.cluster(&contents, None, 0.8, 0.45, Some(10)).unwrap();
assert_eq!(1, rs.len());
}
sourcepub fn comments<T: AsRef<str>>(
&self,
contents: &[T],
task_id: Option<&str>,
alpha: f32,
beta: f32,
timeout: Option<u64>
) -> Result<Vec<CommentsCluster>>
pub fn comments<T: AsRef<str>>(
&self,
contents: &[T],
task_id: Option<&str>,
alpha: f32,
beta: f32,
timeout: Option<u64>
) -> Result<Vec<CommentsCluster>>
task_id
: 唯一的 task_id,典型意见任务的名字,可由字母和数字组成
alpha
: 聚类最大 cluster 大小,一般为 0.8
beta
: 聚类平均 cluster 大小,一般为 0.45
timeout
: 等待典型意见任务完成的秒数,一般为 1800 秒
使用示例
extern crate bosonnlp;
use bosonnlp::BosonNLP;
fn main() {
let nlp = BosonNLP::new(env!("BOSON_API_TOKEN"));
let contents = vec![
"今天天气好",
"今天天气好",
"今天天气不错",
"点点楼头细雨",
"重重江外平湖",
"当年戏马会东徐",
"今日凄凉南浦",
"今天天气好",
"今天天气好",
"今天天气不错",
"点点楼头细雨",
"重重江外平湖",
"当年戏马会东徐",
"今日凄凉南浦",
];
let rs = nlp.comments(&contents, None, 0.8, 0.45, Some(10)).unwrap();
assert_eq!(4, rs.len());
}