Naver Papago Translation API

qkrrnjswo·2023년 5월 9일
0

공부 정리

목록 보기
2/24

종류

  • 텍스트 번역 API
    (Papago Text Translation API)
  • 문서 번역 API
    (Papago Doc Translation API)
  • 웹 번역 API
    (Papago Website Translation API)
  • 언어 감지 API
    (Papago Language Detection API)

사용

유료 텍스트 번역 API (Papago Text Translation API)

: 번역할 내용, 번역할 언어 코드, 번역될 언어 코드를 전달하면 Papago Translation 엔진에서 번역한 결과를 반환하는 REST API

JSON 요청

curl -i -X POST \
   -H "X-NCP-APIGW-API-KEY-ID:{앱 등록 시 발급받은 Client ID}" \
   -H "X-NCP-APIGW-API-KEY:{앱 등록 시 발급 받은 Client Secret}" \
   -H "Content-Type:application/json" \
   -d \
'{
  "source": "{원본 언어 코드}",
  "target": "{번역 결과 언어 코드}",
  "text": "{번역할 text}"
}' \
 'https://naveropenapi.apigw.ntruss.com/nmt/v1/translation'

파라미터 정보: https://api.ncloud-docs.com/docs/ai-naver-papagonmt-translation

Config

@Component
public class PapagoConfig {
    @Value("${PAPAGO-TRANSLATE-KEY}")
    private String apiKey;

    @Value("${PAPAGO-TRANSLATE-ID}")
    private String apiId;

    public static final String
            CONTENT_TYPE = "application/json;";

    public static final String KEY_NAME = "X-NCP-APIGW-API-KEY";
    public static final String ID_NAME = "X-NCP-APIGW-API-KEY-ID";

    public static final String source = "ko";
    public static final String target = "en";

    public static final String URL = "https://naveropenapi.apigw.ntruss.com/nmt/v1/translation";


    public String getApiKey() {
        return apiKey;
    }

    public String getApiId() {
        return apiId;
    }


}

Dto

@Getter
@Setter
public class PapagoMessage {
    @JsonProperty("result")
    private PapagoResult result;

}


@Setter
@Getter
@NoArgsConstructor
public class PapagoRequestDto {

    @JsonProperty("text")
    private String text;

    @JsonProperty("target")
    private String target;

    @JsonProperty("source")
    private String source;

    @Builder
    public PapagoRequestDto(String text, String target, String source) {
        this.text = text;
        this.target = target;
        this.source = source;
    }
}

@Getter
@Setter
public class PapagoResponseDto {
    @JsonProperty("message")
    private PapagoMessage message;
}

@Setter
@Getter
@NoArgsConstructor
public class PapagoResult {

    @JsonProperty("translatedText")
    private String translatedtext;
    @JsonProperty("tarLangType")
    private String tarlangtype;
    @JsonProperty("srcLangType")
    private String srclangtype;

    @Builder
    public PapagoResult(String translatedtext, String tarlangtype, String srclangtype) {
        this.translatedtext = translatedtext;
        this.tarlangtype = tarlangtype;
        this.srclangtype = srclangtype;
    }
}

Service

@Service
@RequiredArgsConstructor
public class PapagoService {

    private final PapagoConfig papagoConfig;

    private static RestTemplate restTemplate = new RestTemplate();

    public String textTranslation(String text) {
        PapagoRequestDto papagoRequestDto = PapagoRequestDto.builder()
                .source(PapagoConfig.source)
                .target(PapagoConfig.target)
                .text(text).build();

        PapagoResponseDto papagoResponseDto = this.getResponse(buildHttpEntity(papagoRequestDto));

        return papagoResponseDto.getMessage().getResult().getTranslatedtext();
    }

    private HttpEntity<PapagoRequestDto> buildHttpEntity(PapagoRequestDto papagoRequestDto) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.parseMediaType(PapagoConfig.CONTENT_TYPE));///?
        headers.add(PapagoConfig.KEY_NAME,papagoConfig.getApiKey());
        headers.add(PapagoConfig.ID_NAME,papagoConfig.getApiId());
        return new HttpEntity<>(papagoRequestDto, headers);
    }

    public PapagoResponseDto getResponse(HttpEntity<PapagoRequestDto> papagoRequestDtoHttpEntity) {
        ResponseEntity<PapagoResponseDto> responseEntity = restTemplate.postForEntity(
                PapagoConfig.URL,
                papagoRequestDtoHttpEntity,
                PapagoResponseDto.class);

        return responseEntity.getBody();
    }
}

0개의 댓글