1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
use std::io::{Read, Write};
use std::iter::FromIterator;

use serde::Serialize;
use serde::de::DeserializeOwned;
use serde_json::{self, Value, Map};
use url::Url;
use uuid::Uuid;
use flate2::Compression;
use flate2::write::GzEncoder;
use reqwest::{Client, Method};
use reqwest::header::{CONTENT_LENGTH, USER_AGENT, ACCEPT, CONTENT_ENCODING, CONTENT_TYPE};

use errors::*;
use rep::{Dependency, NamedEntity, Tag, TextCluster, CommentsCluster, ConvertedTime, ClusterContent};
use task::{ClusterTask, CommentsTask, Task};


/// 默认的 `BosonNLP` API 服务器地址
const DEFAULT_BOSONNLP_URL: &'static str = "https://api.bosonnlp.com";

/// [`BosonNLP`](http://bosonnlp.com) REST API 访问的封装
#[derive(Debug, Clone)]
pub struct BosonNLP {
    /// 用于 API 鉴权的 API Token
    pub token: String,
    /// 是否压缩大于 10K 的请求体,默认为 true
    pub compress: bool,
    /// `BosonNLP` HTTP API 的 URL,默认为 `http://api.bosonnlp.com`
    bosonnlp_url: String,
    /// hyper http Client
    client: Client,
}

impl Default for BosonNLP {
    fn default() -> BosonNLP {
        BosonNLP {
            token: "".to_string(),
            compress: true,
            bosonnlp_url: DEFAULT_BOSONNLP_URL.to_owned(),
            client: Client::new(),
        }
    }
}

impl BosonNLP {
    /// 初始化一个新的 `BosonNLP` 实例
    pub fn new<T: Into<String>>(token: T) -> BosonNLP {
        BosonNLP {
            token: token.into(),
            ..Default::default()
        }
    }

    /// 使用自定义参数初始化一个新的 ``BosonNLP`` 实例
    pub fn with_options<T: Into<String>>(token: T, bosonnlp_url: T, compress: bool) -> BosonNLP {
        BosonNLP {
            token: token.into(),
            compress: compress,
            bosonnlp_url: bosonnlp_url.into(),
            ..Default::default()
        }
    }

    /// 使用自定义的 reqwest Client 初始化一个新的 ``BosonNLP`` 实例
    pub fn with_client<T: Into<String>>(token: T, client: Client) -> BosonNLP {
        BosonNLP {
            token: token.into(),
            client: client,
            ..Default::default()
        }
    }

    fn request<D, E>(&self, method: Method, endpoint: &str, params: Vec<(&str, &str)>, data: &E) -> Result<D>
    where
        D: DeserializeOwned,
        E: Serialize,
    {
        let url_string = format!("{}{}", self.bosonnlp_url, endpoint);
        let mut url = Url::parse(&url_string).unwrap();
        url.query_pairs_mut().extend_pairs(params.into_iter());
        let mut req = self.client.request(method.clone(), url);
        req = req.header(
                USER_AGENT,
                format!("bosonnlp-rs/{}", env!("CARGO_PKG_VERSION")),
            )
            .header(ACCEPT, "application/json")
            .header("X-Token", self.token.clone());
        let mut res = if method == Method::POST {
            let req = req.header(CONTENT_TYPE, "application/json");
            let body = serde_json::to_string(data)?;
            if self.compress && body.len() > 10240 {
                let mut encoder = GzEncoder::new(Vec::new(), Compression::default());
                encoder.write_all(body.as_bytes())?;
                let compressed = encoder.finish()?;
                let req = req.header(CONTENT_ENCODING, "gzip");
                req.body(compressed).send()?
            } else {
                req.body(body).send()?
            }
        } else {
            req.send()?
        };
        let content_len = res.headers().get(CONTENT_LENGTH)
            .and_then(|ct_len| ct_len.to_str().ok())
            .and_then(|ct_len| ct_len.parse().ok())
            .unwrap_or(0);
        let mut body = String::with_capacity(content_len);
        res.read_to_string(&mut body)?;
        let status = res.status();
        if !status.is_success() {
            let result: Value = match serde_json::from_str(&body) {
                Ok(obj) => obj,
                Err(..) => Value::Object(Map::new()),
            };
            let message = match result.get("message") {
                Some(msg) => msg.as_str().unwrap_or("").to_owned(),
                None => body,
            };
            return Err(
                Error::Api {
                    code: status,
                    reason: message
                }
            );
        }
        Ok(serde_json::from_str::<D>(&body)?)
    }

    pub(crate) fn get<D>(&self, endpoint: &str, params: Vec<(&str, &str)>) -> Result<D>
    where
        D: DeserializeOwned,
    {
        self.request(Method::GET, endpoint, params, &Value::Null)
    }

    pub(crate) fn post<D, E>(&self, endpoint: &str, params: Vec<(&str, &str)>, data: &E) -> Result<D>
    where
        D: DeserializeOwned,
        E: Serialize,
    {
        self.request(Method::POST, endpoint, params, data)
    }

    /// [情感分析接口](http://docs.bosonnlp.com/sentiment.html)
    ///
    /// ``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());
    /// }
    /// ```
    pub fn sentiment<T: AsRef<str>>(&self, contents: &[T], model: &str) -> Result<Vec<(f32, f32)>> {
        let endpoint = format!("/sentiment/analysis?{}", model);
        let data = contents.iter().map(|c| c.as_ref()).collect::<Vec<_>>();
        self.post(&endpoint, vec![], &data)
    }

    /// [时间转换接口](http://docs.bosonnlp.com/time.html)
    ///
    /// ``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);
    /// }
    /// ```
    pub fn convert_time<T: AsRef<str>>(&self, content: T, basetime: Option<T>) -> Result<ConvertedTime> {
        if let Some(base) = basetime {
            let params = vec![("pattern", content.as_ref()), ("basetime", base.as_ref())];
            return self.post("/time/analysis", params, &Value::Null);
        } else {
            let params = vec![("pattern", content.as_ref())];
            return self.post("/time/analysis", params, &Value::Null);
        };
    }

    /// [新闻分类接口](http://docs.bosonnlp.com/classify.html)
    ///
    /// ``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);
    /// }
    /// ```
    pub fn classify<T: AsRef<str>>(&self, contents: &[T]) -> Result<Vec<usize>> {
        let data = contents.iter().map(|c| c.as_ref()).collect::<Vec<_>>();
        self.post("/classify/analysis", vec![], &data)
    }

    /// [语义联想接口](http://docs.bosonnlp.com/suggest.html)
    ///
    /// ``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());
    /// }
    /// ```
    pub fn suggest<T: AsRef<str>>(&self, word: T, top_k: usize) -> Result<Vec<(f32, String)>> {
        self.post(
            "/suggest/analysis",
            vec![("top_k", &top_k.to_string())],
            &word.as_ref(),
        )
    }

    /// [关键词提取接口](http://docs.bosonnlp.com/keywords.html)
    ///
    /// ``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());
    /// }
    /// ```
    pub fn keywords<T: AsRef<str>>(&self, text: T, top_k: usize, segmented: bool) -> Result<Vec<(f32, String)>> {
        let top_k_str = top_k.to_string();
        let params = if segmented {
            vec![("top_k", top_k_str.as_ref()), ("segmented", "1")]
        } else {
            vec![("top_k", top_k_str.as_ref())]
        };
        self.post("/keywords/analysis", params, &text.as_ref())
    }

    /// [依存文法分析接口](http://docs.bosonnlp.com/depparser.html)
    ///
    /// ``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());
    /// }
    /// ```
    pub fn depparser<T: AsRef<str>>(&self, contents: &[T]) -> Result<Vec<Dependency>> {
        let data = contents.iter().map(|c| c.as_ref()).collect::<Vec<_>>();
        self.post("/depparser/analysis", vec![], &data)
    }

    /// [命名实体识别接口](http://docs.bosonnlp.com/ner.html)
    ///
    /// ``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());
    /// }
    /// ```
    pub fn ner<T: AsRef<str>>(&self, contents: &[T], sensitivity: usize, segmented: bool) -> Result<Vec<NamedEntity>> {
        let data = contents.iter().map(|c| c.as_ref()).collect::<Vec<_>>();
        let sensitivity_str = sensitivity.to_string();
        let params = if segmented {
            vec![
                ("sensitivity", sensitivity_str.as_ref()),
                ("segmented", "1"),
            ]
        } else {
            vec![("sensitivity", sensitivity_str.as_ref())]
        };
        self.post("/ner/analysis", params, &data)
    }

    /// [分词与词性标注接口](http://docs.bosonnlp.com/tag.html)
    ///
    /// ``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());
    /// }
    /// ```
    pub fn tag<T: AsRef<str>>(
        &self,
        contents: &[T],
        space_mode: usize,
        oov_level: usize,
        t2s: bool,
        special_char_conv: bool,
    ) -> Result<Vec<Tag>> {
        let data = contents.iter().map(|c| c.as_ref()).collect::<Vec<_>>();
        let t2s_str = if t2s { "1" } else { "0" };
        let special_char_conv_str = if special_char_conv { "1" } else { "0" };
        let space_mode_str = space_mode.to_string();
        let oov_level_str = oov_level.to_string();
        let params = vec![
            ("space_mode", space_mode_str.as_ref()),
            ("oov_level", oov_level_str.as_ref()),
            ("t2s", t2s_str),
            ("special_char_conv", special_char_conv_str),
        ];
        self.post("/tag/analysis", params, &data)
    }

    /// [新闻摘要接口](http://docs.bosonnlp.com/summary.html)
    ///
    /// ``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());
    /// }
    /// ```
    pub fn summary<T: Into<String>>(&self, title: T, content: T, word_limit: f32, not_exceed: bool) -> Result<String> {
        let not_exceed = if not_exceed { 1 } else { 0 };
        let data = json!({
            "title": title.into(),
            "content": content.into(),
            "percentage": word_limit,
            "not_exceed": not_exceed
        });
        self.post("/summary/analysis", vec![], &data)
    }

    /// [文本聚类接口](http://docs.bosonnlp.com/cluster.html)
    ///
    /// ``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());
    /// }
    /// ```
    pub fn cluster<T: AsRef<str>>(
        &self,
        contents: &[T],
        task_id: Option<&str>,
        alpha: f32,
        beta: f32,
        timeout: Option<u64>,
    ) -> Result<Vec<TextCluster>> {
        let mut task = match task_id {
            Some(_id) => ClusterTask::new(self, _id),
            None => {
                let _id = Uuid::new_v4().to_simple_ref().to_string();
                ClusterTask::new(self, _id)
            }
        };
        let tasks: Vec<ClusterContent> = Vec::from_iter(contents.iter().map(|c| c.into()));
        if !task.push(&tasks)? {
            return Ok(vec![]);
        }
        task.analysis(alpha, beta)?;
        task.wait(timeout)?;
        let result = task.result()?;
        task.clear()?;
        Ok(result)
    }

    /// [典型意见接口](http://docs.bosonnlp.com/comments.html)
    ///
    /// ``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());
    /// }
    /// ```
    pub fn comments<T: AsRef<str>>(
        &self,
        contents: &[T],
        task_id: Option<&str>,
        alpha: f32,
        beta: f32,
        timeout: Option<u64>,
    ) -> Result<Vec<CommentsCluster>> {
        let mut task = match task_id {
            Some(_id) => CommentsTask::new(self, _id),
            None => {
                let _id = Uuid::new_v4().to_simple_ref().to_string();
                CommentsTask::new(self, _id)
            }
        };
        let tasks: Vec<ClusterContent> = Vec::from_iter(contents.iter().map(|c| c.into()));
        if !task.push(&tasks)? {
            return Ok(vec![]);
        }
        task.analysis(alpha, beta)?;
        task.wait(timeout)?;
        let result = task.result()?;
        task.clear()?;
        Ok(result)
    }
}