Search in sources :

Example 1 with RecognitionResult

use of com.mizhousoft.cloudsdk.nlp.RecognitionResult in project cloud-sdk by mizhousoft.

the class TestSensitiveWordsRecognitionServiceImpl method testRecognizes.

@Test
public void testRecognizes() {
    String text1 = "ddddddddddddddddddd";
    String text2 = "ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd我的敏感词包二奶仓井空";
    text1 = StringUtils.leftPad(text1, 1950, "0");
    try {
        RecognitionResult result = this.sensitiveWordsRecognitionService.recognize(text1, text2);
        Assert.assertTrue(result.isContainSensitiveWord());
    } catch (CloudSDKException e) {
        Assert.fail(e.getMessage());
    }
}
Also used : CloudSDKException(com.mizhousoft.cloudsdk.CloudSDKException) RecognitionResult(com.mizhousoft.cloudsdk.nlp.RecognitionResult) Test(org.junit.Test)

Example 2 with RecognitionResult

use of com.mizhousoft.cloudsdk.nlp.RecognitionResult in project cloud-sdk by mizhousoft.

the class SensitiveWordsRecognitionServiceImpl method recognize.

/**
 * {@inheritDoc}
 */
@Override
public RecognitionResult recognize(String... texts) throws CloudSDKException {
    RecognitionResult result = new RecognitionResult();
    if (ArrayUtils.isEmpty(texts)) {
        return result;
    }
    StringBuilder buffer = new StringBuilder(100);
    for (String text : texts) {
        if (!StringUtils.isBlank(text)) {
            buffer.append(text.trim());
        }
    }
    String value = buffer.toString();
    while (true) {
        if (value.length() > MAX_TEXT_LENGTH) {
            String data = value.substring(0, MAX_TEXT_LENGTH);
            result = recognizeOne(data);
            if (result.isContainSensitiveWord()) {
                break;
            } else {
                value = value.substring(MAX_TEXT_LENGTH);
            }
        } else {
            result = recognizeOne(value);
            break;
        }
    }
    return result;
}
Also used : RecognitionResult(com.mizhousoft.cloudsdk.nlp.RecognitionResult)

Example 3 with RecognitionResult

use of com.mizhousoft.cloudsdk.nlp.RecognitionResult in project cloud-sdk by mizhousoft.

the class SensitiveWordsRecognitionServiceImpl method recognizeOne.

public RecognitionResult recognizeOne(String text) throws CloudSDKException {
    RecognitionResult result = new RecognitionResult();
    if (StringUtils.isBlank(text)) {
        return result;
    }
    try {
        SensitiveWordsRecognitionRequest request = new SensitiveWordsRecognitionRequest();
        request.setText(text);
        SensitiveWordsRecognitionResponse resp = nlpClient.SensitiveWordsRecognition(request);
        String[] words = resp.getSensitiveWords();
        if (!ArrayUtils.isEmpty(words)) {
            result.setSensitiveWords(Arrays.asList(words));
        }
    } catch (TencentCloudSDKException e) {
        throw new CloudSDKException(e.getMessage(), e);
    }
    return result;
}
Also used : TencentCloudSDKException(com.tencentcloudapi.common.exception.TencentCloudSDKException) CloudSDKException(com.mizhousoft.cloudsdk.CloudSDKException) TencentCloudSDKException(com.tencentcloudapi.common.exception.TencentCloudSDKException) SensitiveWordsRecognitionRequest(com.tencentcloudapi.nlp.v20190408.models.SensitiveWordsRecognitionRequest) SensitiveWordsRecognitionResponse(com.tencentcloudapi.nlp.v20190408.models.SensitiveWordsRecognitionResponse) RecognitionResult(com.mizhousoft.cloudsdk.nlp.RecognitionResult)

Example 4 with RecognitionResult

use of com.mizhousoft.cloudsdk.nlp.RecognitionResult in project cloud-sdk by mizhousoft.

the class TestSensitiveWordsRecognitionServiceImpl method testRecognize.

@Test
public void testRecognize() {
    String text = "我的敏感词包二奶仓井空";
    try {
        RecognitionResult result = this.sensitiveWordsRecognitionService.recognize(text);
        Assert.assertTrue(result.isContainSensitiveWord());
    } catch (CloudSDKException e) {
        Assert.fail(e.getMessage());
    }
}
Also used : CloudSDKException(com.mizhousoft.cloudsdk.CloudSDKException) RecognitionResult(com.mizhousoft.cloudsdk.nlp.RecognitionResult) Test(org.junit.Test)

Aggregations

RecognitionResult (com.mizhousoft.cloudsdk.nlp.RecognitionResult)4 CloudSDKException (com.mizhousoft.cloudsdk.CloudSDKException)3 Test (org.junit.Test)2 TencentCloudSDKException (com.tencentcloudapi.common.exception.TencentCloudSDKException)1 SensitiveWordsRecognitionRequest (com.tencentcloudapi.nlp.v20190408.models.SensitiveWordsRecognitionRequest)1 SensitiveWordsRecognitionResponse (com.tencentcloudapi.nlp.v20190408.models.SensitiveWordsRecognitionResponse)1