En |

TexSmart HTTP API for Text Graph


Introduction to Text Graph

Text Graph in TexSmart is a knowledge base that stores and represents the relationships between texts in the form of graphs. The nodes in the graph are natural language texts, such as characters, words, and short sentences, and the edges represent the relationship between the nodes. The current version of Text Graph supports two languages (Chinese and English) and five relationships: "synonym_of", "antonym_of", "class_of", "instance_of" and "similar_to".
synonym_of:The two texts have the same or similar meanings, such as "I love you" and "I miss you", "happy" and "glad".
antonym_of:The meanings of the two texts are (approximately) opposite, such as "happy" and "sad", "somebody" and "nobody".;
class_of:Point to its type name from a text, such as "pineapple" to "fruit", "Andy Lau" to "superstars";
instance_of:The inverse relationship of "class_of", such as "fruit" to "pineapple", "superstars" to "Andy Lau".
similar_to:The two texts are of the same type (for example, both are city names, or are adjectives that can be used to describe a person's character, etc.), and have strong semantic relevance. For example, "New York" and "Pennsylvania", "striving" and "working hard".

Note that "Apple" and "Jobs" are not similar (although they have a strong correlation), because the two words are of different types (the former is an institution, and the latter is a person).

TexSmart HTTP API for Text Graph

Next, we introduce Text Graph API. The other APIs of TexSmart can be found in: Text Understanding APIText Matching API。 The API supports access via HTTP-POST and its url is: https://texsmart.qq.com/api/text_graph.
The input post-data needs to be in JSON format, and the output is also in JSON format. It is recommended to use Postman for testing.

Here is a simple example request (as HTTP-POST body):
obj = {
        "text": happy,
        "lang": "en",
        "relation": "similar_to",
        "k": 10
}
Example codes for calling the API: Python Code | Java Code | C++ Code | C# Code

The results returned after calling the API is also in JSON format as follows:
{
  "header":{"time_cost_ms":0.243,"time_cost":0.000243,"core_time_cost_ms":0.215,"ret_code":"succ"},
  "item_list":[
    {"order":1,"text":"excited"},
    {"order":2,"text":"confident"},
    {"order":3,"text":"grateful"},
    {"order":4,"text":"unhappy"},
    {"order":5,"text":"proud"},
    {"order":6,"text":"surprised"},
    {"order":7,"text":"satisfied"},
    {"order":8,"text":"glad"},
    {"order":9,"text":"beautiful"},
    {"order":10,"text":"frustrated"}
  ]
}
Note that, the field “header” gives some auxiliary information (time cost, error codes, etc.) which explains this API call. The field “item_list” returns the results of the query word under the given conditions.

Instructions on Input and Output Format

An introduction of each field of the input JSON object is as follows:

Field Name Data Type Field Introduction
text str The word to be retrieved.
lang str The language of the queried text, "zh" and "en" are supported.
relation str The queried relation, supportting: "synonym_of" (synonym), "antonym_of" (antonym), "instance_of"(下位词), "class_of" (hypernym), "similar_to" (hyponym).
k int The number of returned items.
Notice: (1) For the relation "class_of", only the top 10 items will be returned if k>10. For the other relations, only the top 20 items will be returned if k>20. (2) When the number of results (items) in the database is fewer than k, all the items will be returned and it is smaller than k.

The introduction of each field of output JSON object is as follows:

Field Name Data Type Field Introduction
header JSON Object Auxiliary information returned after calling and execution.
Field time_cost_ms: The total time for processing request calculated with millisecond (ms).
Field time_cost: The total time for processing request calculated with second (s).
Field ret_code: Return code. "succ" denotes success, and others are false codes. The main error is "error.invalid_mode": the requested relation is not in the candidate relation set.
item_list JSON list The returned results
It is a list of JSON objects, each object corresponds to a text and its index.


Code Example

API Call with Python

Code Example 1(with http.client):
# -*- coding: utf8 -*-
import json
import http.client
import requests

obj = {
        "text": happy,
        "lang": "en",
        "relation": "similar_to",
        "k": 10
}
r = requests.post(url="https://texsmart.qq.com/api/text_graph", json= obj)
print(r.text)

API Call with Java

[TBD]

API Call with C++

[TBD]

API Call with C#

[TBD]

About TexSmart

TexSmart is a text understanding system built by NLP Team at Tencent AI Lab, which is used to analyze morphology, syntax and semantics for text in both Chinese and English. It provides basic natural language understanding functions such as word segmentation, part-of-speech tagging, named entity recognition(NER), semantic expansion, and particularly supports some key functions including fine-grained named entity recognition, semantic expansion and deep semantic expression for specific entities.

Experience Demo | System Introduction