hanlp_restful

class hanlp_restful.HanLPClient(url: str, auth: str = None, language=None, timeout=5)[source]
Parameters
  • url (str) – An API endpoint to a service provider.

  • auth (str) – An auth key licenced from a service provider.

  • language (str) – The default language for each parse() call. Contact the service provider for the list of languages supported. Conventionally, zh is used for Chinese and mul for multilingual. Leave None to use the default language on server.

  • timeout (int) – Maximum waiting time in seconds for a request.

__call__(text: Union[str, List[str]] = None, tokens: List[List[str]] = None, tasks: Optional[Union[str, List[str]]] = None, skip_tasks: Optional[Union[str, List[str]]] = None, language: str = None)hanlp_common.document.Document[source]

A shortcut of parse().

about() → Dict[str, Any][source]

Get the information about server and your client.

Returns

A dict containing your rate limit and server version etc.

parse(text: Union[str, List[str]] = None, tokens: List[List[str]] = None, tasks: Optional[Union[str, List[str]]] = None, skip_tasks: Optional[Union[str, List[str]]] = None, language: str = None)hanlp_common.document.Document[source]

Parse a piece of text.

Parameters
  • text – A paragraph (str), or a list of sentences (List[str]).

  • tokens – A list of sentences where each sentence is a list of tokens.

  • tasks – The tasks to predict.

  • skip_tasks – The tasks to skip.

  • language – The language of input text or tokens. None to use the default language on server.

Returns

A Document.

Raises

HTTPError – Any errors happening on the Internet side or the server side. Refer to the code and msg of the exception for more details. A list of common errors :

  • 400 Bad Request indicates that the server cannot process the request due to a client fault (e.g., text too long, language unsupported).

  • 401 Unauthorized indicates that the request lacks valid auth credentials for the API.

  • 422 Unprocessable Entity indicates that the content type of the request entity is not in proper json format.

  • 429 Too Many Requests indicates the user has sent too many requests in a given amount of time (“rate limiting”).

semantic_textual_similarity(text: Union[Tuple[str, str], List[Tuple[str, str]]], language: str = None) → Union[float, List[float]][source]

Semantic textual similarity deals with determining how similar two pieces of texts are.

Parameters
  • text – A pair or pairs of text.

  • language – The language of input text. None to use the default language.

Examples:

HanLP.semantic_textual_similarity([
    ('看图猜一电影名', '看图猜电影'),
    ('无线路由器怎么无线上网', '无线上网卡和无线路由器怎么用'),
    ('北京到上海的动车票', '上海到北京的动车票'),
])
# Output: [0.9764469861984253, 0.0, 0.003458738327026367]
Returns

Similarities.

text_style_transfer(text: Union[str, List[str]], target_style: str, language: str = None) → Union[str, List[str]][source]

Text style transfer aims to change the style of the input text to the target style while preserving its content.

Parameters
  • text – Source text.

  • target_style – Target style.

  • language – The language of input text. None to use the default language.

Examples:

HanLP.text_style_transfer(['国家对中石油抱有很大的期望.', '要用创新去推动高质量的发展。'],
                          target_style='gov_doc')
# Output: ['国家对中石油寄予厚望。', '要以创新驱动高质量发展。']
Returns

Text or a list of text of the target style.