BosonNLP

Struct BosonNLP 

Source
pub struct BosonNLP {
    pub token: String,
    pub compress: bool,
    /* private fields */
}
Expand description

BosonNLP REST API 访问的封装

Fields§

§token: String

用于 API 鉴权的 API Token

§compress: bool

是否压缩大于 10K 的请求体,默认为 true

Implementations§

Source§

impl BosonNLP

Source

pub fn new<T: Into<String>>(token: T) -> BosonNLP

初始化一个新的 BosonNLP 实例

Source

pub fn with_options<T: Into<String>>( token: T, bosonnlp_url: T, compress: bool, ) -> BosonNLP

使用自定义参数初始化一个新的 BosonNLP 实例

Source

pub fn with_client<T: Into<String>>(token: T, client: Client) -> BosonNLP

使用自定义的 reqwest Client 初始化一个新的 BosonNLP 实例

Source

pub fn sentiment<T: AsRef<str>>( &self, contents: &[T], model: &str, ) -> Result<Vec<(f32, f32)>>

情感分析接口

contents: 需要做情感分析的文本序列

model: 使用不同的语料训练的模型

§使用示例
extern crate bosonnlp;

use bosonnlp::BosonNLP;

fn main() {
    let nlp = BosonNLP::new(env!("BOSON_API_TOKEN"));
    let rs = nlp.sentiment(&["这家味道还不错"], "food").unwrap();
    assert_eq!(1, rs.len());
}
Source

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);
}
Source

pub fn classify<T: AsRef<str>>(&self, contents: &[T]) -> Result<Vec<usize>>

新闻分类接口

contents: 需要做分类的新闻文本序列

§使用示例
extern crate bosonnlp;

use bosonnlp::BosonNLP;

fn main() {
    let nlp = BosonNLP::new(env!("BOSON_API_TOKEN"));
    let rs = nlp.classify(&["俄否决安理会谴责叙军战机空袭阿勒颇平民"]).unwrap();
    assert_eq!(vec![5usize], rs);
}
Source

pub fn suggest<T: AsRef<str>>( &self, word: T, top_k: usize, ) -> Result<Vec<(f32, String)>>

语义联想接口

word: 需要做语义联想的词

top_k: 返回结果的条数,最大值可设定为 100

§使用示例
extern crate bosonnlp;

use bosonnlp::BosonNLP;

fn main() {
    let nlp = BosonNLP::new(env!("BOSON_API_TOKEN"));
    let rs = nlp.suggest("北京", 2).unwrap();
    assert_eq!(2, rs.len());
}
Source

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());
}
Source

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());
}
Source

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());
}
Source

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());
}
Source

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());
}
Source

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());
}
Source

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());
}

Trait Implementations§

Source§

impl Clone for BosonNLP

Source§

fn clone(&self) -> BosonNLP

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for BosonNLP

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for BosonNLP

Source§

fn default() -> BosonNLP

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,