1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class NLP |
4
|
|
|
* |
5
|
|
|
* @link https://www.icy2003.com/ |
6
|
|
|
* @author icy2003 <[email protected]> |
7
|
|
|
* @copyright Copyright (c) 2017, icy2003 |
8
|
|
|
*/ |
9
|
|
|
namespace icy2003\php\iapis\baidu; |
10
|
|
|
|
11
|
|
|
use Exception; |
12
|
|
|
use icy2003\php\I; |
13
|
|
|
use icy2003\php\ihelpers\Arrays; |
14
|
|
|
use icy2003\php\ihelpers\Http; |
15
|
|
|
use icy2003\php\ihelpers\Json; |
16
|
|
|
use icy2003\php\ihelpers\Strings; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* 自然语言处理 |
20
|
|
|
* |
21
|
|
|
* @link https://ai.baidu.com/docs#/NLP-Basic-API/top |
22
|
|
|
*/ |
23
|
|
|
class NLP extends Base |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* 选项列表 |
28
|
|
|
* |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected $_options = [ |
32
|
|
|
'text' => null, |
33
|
|
|
'word_1' => null, |
34
|
|
|
'word_2' => null, |
35
|
|
|
'mode' => 0, |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* 设置选项 |
40
|
|
|
* |
41
|
|
|
* @param array $options |
42
|
|
|
* - text:待分析文本,长度限制根据不同接口定 |
43
|
|
|
* - word_1:词 1,最大 64 字节 |
44
|
|
|
* - word_2:词 2,最大 64 字节 |
45
|
|
|
* - mode:模型选择。默认值为0,可选值mode=0(对应web模型);mode=1(对应query模型),默认为 0 |
46
|
|
|
* 1. Query模型:该模型的训练数据来源于用户在百度的日常搜索数据,适用于处理信息需求类的搜索或口语query。例如:手机缝隙灰尘怎么清除 |
47
|
|
|
* 2. Web模型:该模型的训练数据来源于全网网页数据,适用于处理网页文本等书面表达句子。例如:一般而言,股份的表现形式可以是股票、股权份额等等 |
48
|
|
|
* |
49
|
|
|
* @return static |
50
|
|
|
*/ |
51
|
|
|
public function setOptions($options) |
52
|
|
|
{ |
53
|
|
|
return parent::setOptions($options); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* 词法分析(通用版) |
58
|
|
|
* |
59
|
|
|
* - 向用户提供分词、词性标注、专名识别三大功能 |
60
|
|
|
* - 能够识别出文本串中的基本词汇(分词),对这些词汇进行重组、标注组合后词汇的词性,并进一步识别出命名实体 |
61
|
|
|
* - setOptions():text(限制为 20000 字节) |
62
|
|
|
* |
63
|
|
|
* @return static |
64
|
|
|
*/ |
65
|
|
|
public function lexer() |
66
|
|
|
{ |
67
|
|
|
if (Strings::byteLength($this->_options['text']) > 20000) { |
68
|
|
|
throw new Exception('文字太长,不允许超过 20000 字节'); |
69
|
|
|
} |
70
|
|
|
$this->requestToken(); |
71
|
|
|
$this->_result = Json::decode(Http::body('https://aip.baidubce.com/rpc/2.0/nlp/v1/lexer', Json::encode(Arrays::some($this->_options, [ |
72
|
|
|
'text', |
73
|
|
|
])), [ |
74
|
|
|
'access_token' => $this->_token, |
75
|
|
|
'charset' => 'UTF-8', |
76
|
|
|
])); |
77
|
|
|
|
78
|
|
|
$this->_toArrayCall = function ($result) { |
79
|
|
|
return Arrays::column(I::get($result, 'items'), 'item'); |
|
|
|
|
80
|
|
|
}; |
81
|
|
|
|
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* 依存句法分析 |
87
|
|
|
* |
88
|
|
|
* - 依存句法分析接口可自动分析文本中的依存句法结构信息,利用句子中词与词之间的依存关系来表示词语的句法结构信息(如“主谓”、“动宾”、“定中”等结构关系),并用树状结构来表示整句的结构(如“主谓宾”、“定状补”等) |
89
|
|
|
* - setOptions():text(限制为 256 字节)、mode |
90
|
|
|
* |
91
|
|
|
* @return static |
92
|
|
|
*/ |
93
|
|
|
public function depparser() |
94
|
|
|
{ |
95
|
|
|
if (Strings::byteLength($this->_options['text']) > 256) { |
96
|
|
|
throw new Exception('文字太长,不允许超过 256 字节'); |
97
|
|
|
} |
98
|
|
|
$this->requestToken(); |
99
|
|
|
$this->_result = Json::decode(Http::body('https://aip.baidubce.com/rpc/2.0/nlp/v1/depparser', Json::encode(Arrays::some($this->_options, [ |
100
|
|
|
'text', |
101
|
|
|
'mode', |
102
|
|
|
])), [ |
103
|
|
|
'access_token' => $this->_token, |
104
|
|
|
'charset' => 'UTF-8', |
105
|
|
|
])); |
106
|
|
|
$this->_toArrayCall = function ($result) { |
107
|
|
|
return Arrays::column(I::get($result, 'items'), 'word'); |
|
|
|
|
108
|
|
|
}; |
109
|
|
|
|
110
|
|
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* 词义相似度 |
115
|
|
|
* |
116
|
|
|
* - 输入两个词,得到两个词的相似度结果 |
117
|
|
|
* - setOptions():word_1、word_2 |
118
|
|
|
* |
119
|
|
|
* @return static |
120
|
|
|
*/ |
121
|
|
|
public function wordSim() |
122
|
|
|
{ |
123
|
|
|
if (Strings::byteLength($this->_options['word_1']) > 64) { |
124
|
|
|
throw new Exception('词 1 太长,不允许超过 64 字节'); |
125
|
|
|
} |
126
|
|
|
if (Strings::byteLength($this->_options['word_2']) > 64) { |
127
|
|
|
throw new Exception('词 2 太长,不允许超过 64 字节'); |
128
|
|
|
} |
129
|
|
|
$this->requestToken(); |
130
|
|
|
$this->_result = Json::decode(Http::body('https://aip.baidubce.com/rpc/2.0/nlp/v2/word_emb_sim', Json::encode(Arrays::some($this->_options, [ |
131
|
|
|
'word_1', |
132
|
|
|
'word_2', |
133
|
|
|
])), [ |
134
|
|
|
'access_token' => $this->_token, |
135
|
|
|
'charset' => 'UTF-8', |
136
|
|
|
])); |
137
|
|
|
$this->_toArrayCall = function ($result) { |
138
|
|
|
return I::get($result, 'score'); |
139
|
|
|
}; |
140
|
|
|
|
141
|
|
|
return $this; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|