Questions and AnswersCategory: API Related QuestionHow do I use a variable in unirest.post while doing topics extraction?
Sridhar Nerur asked 5 months ago

The code example for topics extraction shown below works, but I would like to use a variable for the “text” parameter instead of embedding the string.
response = unirest.post(“https://rxnlp-core.p.mashape.com/generateTopics”,
headers={“X-Mashape-Key”: “iqVgO6c7iTmshDHYFaEWN7uwrpRDp1o53UBjsnaQ4PUys6l063”, “Content-Type”: “application/json”,
“Accept”: “application/json”
},
params=(“{\”type\”:\”chunk\”,\”text\”:\”PARIS (Reuters) – Charlie Hebdo’s first edition ………\”}”))

Why can’t i just say: params={“type”: “chunk”, “text”: txt} where txt is a string variable? The example code for topics extraction is not consistent with the unirest.post example in http://unirest.io/python.html.

Any help will be greatly appreciated.

2 Answers
KG Staff answered 5 months ago

Hi Sridhar,
I am not sure I understand your question. The unirest example does use two parameters. If you look at the CURL example it would be much clearer:

curl -X POST --include 'https://rxnlp-core.p.mashape.com/generateTopics' \
-H 'X-Mashape-Key: key' \
-H 'Content-Type: application/json' \
-H 'Accept: application/json' \
--data-binary '{"type":"chunk","text":"PARIS (Reuters) - Charlie Hebdo's first edition since an attack by Islamist gunmen sold out within minutes on Wednesday, featuring a cartoon of the Prophet Mohammad on a cover that defenders praised as art but critics saw as a new provocation. French readers queued at dawn for copies to support the satirical newspaper, even as al Qaeda's branch in Yemen claimed responsibility for the attack last week, saying it ordered the killings because it deemed the weekly had insulted the Prophet."}'

Depending on the libraries you are using you might be able to achieve what you suggested using variables. Here is an example python code for the Sentence Clustering API in python. The parameters are quite similar to Topics Extraction: https://gist.github.com/RxNLP/7e9d1d59ef46ac1dfa07a39448c57bac

-KG

KG Staff answered 5 months ago

Sridhar,

This is another example that uses variables in python to send the request:

https://github.com/suhashm/Meta-Crawler/blob/7c265b9b5ec50c1b8c73a9ccf1cdc403067b2436/Backend/SentimentAnalysis.py#L315-L327