IPTC text classification example
Example 1: IPTC text classification example
import sys
import meaningcloud
model = 'IAB_en'
license_key = '<<<<< your license key >>>>>'
text = 'London is a very nice city but I also love Madrid.'
try:
topics_response = meaningcloud.TopicsResponse(meaningcloud.TopicsRequest(license_key, txt=text, lang='en',
topicType='e').sendReq())
if topics_response.isSuccessful():
print("\nThe request to 'Topics Extraction' finished successfully!\n")
entities = topics_response.getEntities()
if entities:
print("\tEntities detected (" + str(len(entities)) + "):\n")
for entity in entities:
print("\t\t" + topics_response.getTopicForm(entity) + ' --> ' +
topics_response.getTypeLastNode(topics_response.getOntoType(entity)) + "\n")
else:
print("\tNo entities detected!\n")
else:
if topics_response.getResponse() is None:
print("\nOh no! The request sent did not return a Json\n")
else:
print("\nOh no! There was the following error: " + topics_response.getStatusMsg() + "\n")
lang_response = meaningcloud.LanguageResponse(meaningcloud.LanguageRequest(license_key, txt=text).sendReq())
if lang_response.isSuccessful():
print("\nThe request to 'Language Identification' finished successfully!\n")
first_lang = lang_response.getFirstLanguage()
if first_lang:
language = lang_response.getLanguageCode(first_lang)
print("\tLanguage detected: " + lang_response.getLanguageName(first_lang) + ' (' + language + ")\n")
else:
print("\tNo language detected!\n")
parser_response = meaningcloud.ParserResponse(
meaningcloud.ParserRequest(license_key, txt=text, lang='en').sendReq())
if parser_response.isSuccessful():
print("\nThe request to 'Lemmatization, PoS and Parsing' finished successfully!\n")
lemmatization = parser_response.getLemmatization(True)
print("\tLemmatization and PoS Tagging:\n")
for token, analyses in lemmatization.items():
print("\t\tToken -->", token)
for analysis in analyses:
print("\t\t\tLemma -->", analysis['lemma'])
print("\t\t\tPoS Tag -->", analysis['pos'], "\n")
except ValueError:
e = sys.exc_info()[0]
print("\nException: " + str(e))
Example 2: IPTC text classification example
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(curl, CURLOPT_URL, "https://api.meaningcloud.com/class-1.1?key=<your_key>&txt=<text>&model=<model>");
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
struct curl_slist *headers = NULL;
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
res = curl_easy_perform(curl);
}
curl_easy_cleanup(curl);