自然语言处理
基础文本分析接口
接口名称 | 接口描述 | API地址 |
---|---|---|
分词 | 对文本进行智能分词识别,支持基础词与混排词粒度 | https://api.ai.qq.com/fcgi-bin/nlp/nlp_wordseg |
词性标注 | 对文本进行分词,同时为每个分词标注正确的词性 | https://api.ai.qq.com/fcgi-bin/nlp/nlp_wordpos |
专有名词识别 | 对文本进行专有名词的分词识别,找出文本中的专有名词 | https://api.ai.qq.com/fcgi-bin/nlp/nlp_wordner |
同义词识别 | 识别文本中存在同义词的分词,并返回相应的同义词 | https://api.ai.qq.com/fcgi-bin/nlp/nlp_wordsyn |
//json都是utf8编码的
//php有两个主要的转码函数。iconv和mb_convert_encoding
$data = mb_convert_encoding($json_data, "utf8", "gbk");
$data = json_decode($data,true);
echo $json_data;
echo "<hr>";
echo "<pre>";
print_r($data);
echo "</pre>";
<?php
/**
* @Author: 飛天
* @Date: 2017-07-01 11:19:07
* @Last Modified by: 飛天
* @Last Modified time: 2017-07-01 15:26:11
*/
header("Content-type:text/html;charset=gbk");
//注意文件存成gbk编码,否则乱码
//接口请求数据
//$text = "马来西亚20岁的女子艾微塔(Evita Delmundo)出生时全身就布满深棕色的斑点,从脖子到肩膀处还有一大块胎记,因为这些痣,艾微塔受了不少委屈。不过,随着年龄的增长,小伙伴们也渐渐地接受了艾微塔,而她也变得自信起来。目前,艾微塔已参加了马来西亚环球小姐的试镜,这样的勇气和自信值得赞扬。";
$text = "由腾讯基金会发起的中国互联网公益峰会在本月中旬于北京举行,峰会的主题是:互联网改变公益。会上陈一丹宣布,腾讯将出20亿资源助力公益生态。";
$app_id = 1106****7762; //应用标识 int正整数
$time_stamp = time(); //请求时间戳(秒级),用于防止请求重放(保证签名5分钟有效) int正整数
$nonce_str = generateRandomString(10); //请求随机字符串,用于保证签名不可预测 string 非空且长度上限32字节
$params = array(
"text" => $text,
"app_id" => $app_id,
"time_stamp" => $time_stamp,
"nonce_str" => $nonce_str
);
//1、API地址
//分词
$url = "https://api.ai.qq.com/fcgi-bin/nlp/nlp_wordseg";
//词性标注
//$url = "https://api.ai.qq.com/fcgi-bin/nlp/nlp_wordpos";
//专有名词识别
//$url = "https://api.ai.qq.com/fcgi-bin/nlp/nlp_wordner";
//同义词识别
//$url = "https://api.ai.qq.com/fcgi-bin/nlp/nlp_wordsyn";
ksort($params); //字典升级排序
$paramstring = http_build_query($params);
$strpram = $paramstring . "&app_key=p3jqgyh&&&&y62lQKpEM"; //拼接应用密钥,得到字符串S
//echo $strpram;
$md5str = strtoupper(md5($strpram)); //计算MD5摘要,得到签名字符串对其所有字母进行大写转换
$params2 = array(
"text" => $text,
"app_id" => $app_id,
"time_stamp" => $time_stamp,
"nonce_str" => $nonce_str,
"sign" =>$md5str
);
$paramstring2 = http_build_query($params2);
$json_data = postData($url, $paramstring2);
echo $json_data;
/**
* [postData description]
* @param [type] $url [description]
* @param [type] $data [description]
* @return [type] [description]
*/
function postData($url, $data)
{
$ch = curl_init();
$timeout = 300;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$handles = curl_exec($ch);
curl_close($ch);
return $handles;
}
/**
* [generateRandomString description]
* @param integer $length [description]
* @return [type] [description]
*/
function generateRandomString($length = 10)
{
$characters = '0123456789abcdefghijklmnopqrstuvwxyz';
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, strlen($characters) - 1)];
}
return $randomString;
}