Text Similarity

  • Example request
    //You are advised to construct and use the client in a singleton pattern to avoid frequent object creation.
    NlpfClient client = new NlpfClient(AuthMode.AKSK,authInfo);
    
    TextSimilarityReq req = new TextSimilarityReq();
    
    //Two texts for similarity comparison
    req.setText1("text-1");
    req.setText2("text-2");
    
    //Supported language of the text. The default value is zh, indicating Chinese. Only Chinese is currently supported.
    req.setLang("zh");
    
    try {
        TextSimilarityResp resp = client.getTextSimilarity(req);
    } catch (NlpException e) {
        //A failure is thrown as an exception. For details about exceptions, see section "Exceptions". The client automatically processes exceptions.
     }
    
  • Table 1 describes TextSimilarityReq parameters.
    Table 1 TextSimilarityReq parameters

    Parameter

    Mandatory

    Type

    Description

    text1

    Yes

    String

    Text 1, on which the text similarity is to be computed. The text is encoded using UTF-8 and contains 1 to 512 characters.

    text2

    Yes

    String

    Text 2, on which the text similarity is to be computed. The text is encoded using UTF-8 and contains 1 to 512 characters.

    lang

    No

    String

    Supported text language type. The default value is zh. Currently, only Chinese is supported.

  • Table 2 describes TextSimilarityResp parameters.
    Table 2 TextSimilarityResp parameters

    Parameter

    Type

    Description

    similarity

    Double

    Similarity score. The value ranges from 0 to 1. By default, eight digits are reserved after the decimal point.