不想看文章的;出门左转,哈工大自然语言处理:http://ltp.ai/index.html
不要急着走,文末有刚出锅的PHP代码[😊]

请求地址:http://39.96.43.154:8080/api
//不要怀疑,就是这个;看着破,但是好用。哈工大的api
请求类型:POST
Content-Type: application/json
参数:{"text":"待分词的文本,1024个字或256个词以内"}
返回json:其中 (返回的json)->words[0]->text 是分词,(返回的json)->words[0]->pos 是词性,第二个分词依次类推 (返回的json)->words[1]->text ,(返回的json)->words[1]->pos 
返回的例子:
{
    "text": "我是邓文怡,我在广东深圳!",
    "nes": [
        {
            "text": "邓文怡",
            "offset": 2,
            "ne": "nh",
            "length": 3
        },
        {
            "text": "广东深圳",
            "offset": 6,
            "ne": "ns",
            "length": 4
        }
    ],
    "words": [
        {
            "id": 0,
            "length": 1,
            "offset": 0,
            "text": "我",
            "pos": "r",
            "parent": 1,
            "relation": "SBV",
            "roles": [],
            "parents": [
                {
                    "parent": 1,
                    "relate": "EXP"
                }
            ]
        },
        {
            "id": 1,
            "length": 1,
            "offset": 1,
            "text": "是",
            "pos": "v",
            "parent": -1,
            "relation": "HED",
            "roles": [
                {
                    "text": "我",
                    "offset": 0,
                    "length": 1,
                    "type": "A0"
                },
                {
                    "text": "邓文怡",
                    "offset": 2,
                    "length": 3,
                    "type": "A1"
                }
            ],
            "parents": [
                {
                    "parent": -1,
                    "relate": "Root"
                }
            ]
        },
        {
            "id": 2,
            "length": 3,
            "offset": 2,
            "text": "邓文怡",
            "pos": "nh",
            "parent": 1,
            "relation": "VOB",
            "roles": [],
            "parents": [
                {
                    "parent": 1,
                    "relate": "LINK"
                }
            ]
        },
        {
            "id": 3,
            "length": 1,
            "offset": 5,
            "text": ",",
            "pos": "wp",
            "parent": 1,
            "relation": "WP",
            "roles": [],
            "parents": [
                {
                    "parent": 1,
                    "relate": "mPUNC"
                }
            ]
        },
        {
            "id": 4,
            "length": 1,
            "offset": 6,
            "text": "我",
            "pos": "r",
            "parent": 5,
            "relation": "SBV",
            "roles": [],
            "parents": [
                {
                    "parent": 5,
                    "relate": "EXP"
                }
            ]
        },
        {
            "id": 5,
            "length": 1,
            "offset": 7,
            "text": "在",
            "pos": "v",
            "parent": 1,
            "relation": "COO",
            "roles": [
                {
                    "text": "我",
                    "offset": 6,
                    "length": 1,
                    "type": "A0"
                },
                {
                    "text": "广东深圳",
                    "offset": 8,
                    "length": 4,
                    "type": "A1"
                }
            ],
            "parents": [
                {
                    "parent": 1,
                    "relate": "eSUCC"
                }
            ]
        },
        {
            "id": 6,
            "length": 2,
            "offset": 8,
            "text": "广东",
            "pos": "ns",
            "parent": 7,
            "relation": "ATT",
            "roles": [],
            "parents": [
                {
                    "parent": 5,
                    "relate": "LOC"
                },
                {
                    "parent": 7,
                    "relate": "FEAT"
                }
            ]
        },
        {
            "id": 7,
            "length": 2,
            "offset": 10,
            "text": "深圳",
            "pos": "ns",
            "parent": 5,
            "relation": "VOB",
            "roles": [],
            "parents": [
                {
                    "parent": 5,
                    "relate": "LOC"
                }
            ]
        },
        {
            "id": 8,
            "length": 1,
            "offset": 12,
            "text": "!",
            "pos": "wp",
            "parent": 1,
            "relation": "WP",
            "roles": [],
            "parents": [
                {
                    "parent": 5,
                    "relate": "mPUNC"
                }
            ]
        }
    ]
}

词性标记集

Tag Description 含义描述 Example
r pronoun 代词 我们
n general noun 名词 苹果
ns geographical name 地名 北京
wp punctuation 标点 ,。!
k suffix 后缀 界, 率
h prefix 前缀 阿, 伪
u auxiliary 助词 的, 地
c conjunction 连词 和, 虽然
v verb 动词 跑, 学习
p preposition 介词 在, 把
d adverb 副词
q quantity 量词
nh person name 人名 杜甫, 汤姆
m number 数词 一,第一
e exclamation 语气词
b other noun-modifier 状态词 大型, 西式
a adjective 形容词 美丽
nd direction noun 方位词 右侧
nl location noun 处所词 城郊
o onomatopoeia 拟声词 哗啦
nt temporal noun 时间词 近日, 明代
nz other proper noun 其他专名 诺贝尔奖
nl organization name 机构团体 保险公司
i idiom 成语 百花齐放
j abbreviation 缩写词 公检法
ws foreign words 外来词 CPU
g morpheme 词素 茨, 甥
x non-lexeme 非词位 萄, 翱

PHP调用:

<?php
/**
* 发送post请求
 * @param string $url 请求地址
 * @param array $post_data post键值对数据
 * @return string
 */
function send_post2($url, $postdata) {
  $options = array(
    'http' => array(
      'method' => 'POST',
      'header' => array(
              'Content-Type: application/json',
              'Origin: http://ltp.ai',
              'Host: 39.96.43.154:8080',
              'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.3 Safari/605.1.15',
              'Referer: http://ltp.ai/',
      ),        
      'content' => $postdata,
      'timeout' => 15 * 60 // 超时时间(单位:s)
    )   
  );    
  $context = stream_context_create($options);
  $result = file_get_contents($url, false, $context);

  return $result;
}
//请求结束
$typemsg = array('text'=>'我是邓文怡,我在广东深圳!');
$typemsg = json_encode($typemsg);//把数组转换为json
$value2 = send_post2('http://39.96.43.154:8080/api',$typemsg);
$value2 = json_decode($value2);//把json转换为对象
foreach($value2->words as $value3){//遍历获得所有分词
    if($value3->pos != 'u' && $value3->pos != 'c' && $value3->pos != 'p'){//排除:助词,例如“的, 地”、连词,例如“和, 虽然”、介词,例如“在, 把”
        echo $value3->text;
        echo '<br>';
    }
}

代码结果:
在这里插入图片描述

顺便说一声,也不是打广告哈,科大讯飞已经和哈工大合作建立了“哈工大-讯飞语言云”,
虽说窃读不能叫偷,但白嫖应有风度;我只是为了学习api使用方法用人家哈工大的,公司要用那肯定要科大讯飞,万一崩了怎么办。
好啦,文章就到这里;如果您有什么建议或者不懂的地方;请到评论区留言!
在这里插入图片描述

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐