1
$\begingroup$

I want to use Chinese Word Segmentation and find this API.

sample case work:

URLRead[HTTPRequest["api.ltp-cloud.com/analysis/", <|"Query" -> {"api_key" -> "x1e3E1Z3H4AzgRQolEGE8PxHlCEXtTnDbJvkjw5p", 
                    "text" -> "这是一个简单的报文请求", 
                    "pattern" -> "ws", 
                    "format" -> "plain"}, 
                    "Method" -> "GET"|>]["URL"], "Body", CharacterEncoding -> "UTF-8"]

这 是 一个 简单 的 报文 请求

For adding custom keyworks,we have to build a XML tree.

enter image description here

the text parameter of body is the string object of the XML tree.

I use Postman to simulate this process:

POST http://api.ltp-cloud.com/analysis/ HTTP/1.1
Host: api.ltp-cloud.com
Content-Type: application/x-www-form-urlencoded
Cache-Control: no-cache
Postman-Token: 6a28f2a0-79f4-af0c-ef5b-23f2c9b03f3f

text=%3Cxml4nlp%3E%3Cnote+lstmsemparser%3D%22n%22+ne%3D%22n%22+parser%3D%22n%22+pos%3D%22n%22+semparser%3D%22n%22+sent%3D%22y%22+srl%3D%22n%22+word%3D%22y%22+%2F%3E%3Cdoc%3E%3Cpara+id%3D%220%22%3E%3Csent+cont%3D%22%E8%87%AA%E5%AE%9A%E4%B9%89%E5%88%86%E8%AF%8D%E7%BB%93%E6%9E%9C%E7%9A%84%E7%A4%BA%E4%BE%8B%22+id%3D%220%22%3E%3Cword+cont%3D%22%E8%87%AA%E5%AE%9A%E4%B9%89%22+id%3D%220%22+%2F%3E%3Cword+cont%3D%22%E5%88%86%E8%AF%8D%22+id%3D%221%22+%2F%3E%3Cword+cont%3D%22%E7%BB%93%E6%9E%9C%22+id%3D%222%22+%2F%3E%3Cword+cont%3D%22%E7%9A%84%22+id%3D%223%22+%2F%3E%3Cword+cont%3D%22%E7%A4%BA%E4%BE%8B%22+id%3D%224%22+%2F%3E%3C%2Fsent%3E%3C%2Fpara%3E%3C%2Fdoc%3E%3C%2Fxml4nlp%3E&api_key=x1e3E1Z3H4AzgRQolEGE8PxHlCEXtTnDbJvkjw5p&pattern=ws&xml_input=true&format=plain

enter image description here

自定义 分词 结果 的 示例

I use mma to call this API:

xml = Import["https://wolfr.am/nhIeHvVf"];
URLRead[HTTPRequest["api.ltp-cloud.com/analysis/", 
       <|"Body" -> {"api_key" -> "x1e3E1Z3H4AzgRQolEGE8PxHlCEXtTnDbJvkjw5p", 
                    "text" -> xml, 
                    "pattern" -> "ws", 
                    "format" -> "plain", 
                    "xml_input" -> "true"}, 
        "Method" -> "POST", 
        "Headers" -> <|"Content-Type" -> "application/x-www-form-urlencoded"|>|>]["URL"], 
                               "Body", CharacterEncoding -> "UTF-8"]

It tells me to input username and password, but this is not necessary.

In Fiddler,I find it not sent the body of HTTPRequest because it use GET. (weird!)

enter image description here

How to get the same result as Postman?

$\endgroup$
7
  • $\begingroup$ I hit a similar snag the other month: mathematica.stackexchange.com/questions/147526/… and was able to partially figure it out maybe something there will help $\endgroup$
    – b3m2a1
    Commented Jul 20, 2017 at 5:05
  • $\begingroup$ @b3m2a1 what the method have you used? $\endgroup$
    – partida
    Commented Jul 20, 2017 at 5:15
  • 1
    $\begingroup$ @b3m2a1 and partida, though in this case is appears to be a simple mistake due to "URL" in HTTPRequest[...]["URL"]. $\endgroup$
    – Kuba
    Commented Jul 20, 2017 at 5:20
  • $\begingroup$ I was forced to use URLFetch and so something about passing "Parameters" to it interfered with the "Body" but I hit a similar snag with HTTPRequest. What happens if you pre-export your body to a string? $\endgroup$
    – b3m2a1
    Commented Jul 20, 2017 at 5:21
  • 1
    $\begingroup$ @Kuba I delete the ["URL"] and it can works!!! But before,it throwed an error....... $\endgroup$
    – partida
    Commented Jul 20, 2017 at 5:23

0

Browse other questions tagged or ask your own question.