사용 모듈
https://github.com/wooorm/franc
GitHub - wooorm/franc: Natural language detection
Natural language detection. Contribute to wooorm/franc development by creating an account on GitHub.
github.com
사람이 알아볼수 있게 바로 판독은 힘들고
문자열을 받아 ISO 코드로 변환해준다
https://github.com/adlawson/nodejs-langs
GitHub - adlawson/nodejs-langs: ISO 639-1/2/3 Language codes with English and local names
ISO 639-1/2/3 Language codes with English and local names - GitHub - adlawson/nodejs-langs: ISO 639-1/2/3 Language codes with English and local names
github.com
그 ISO 를 해석해서 사람이 볼수 있게 반환해준다
https://www.npmjs.com/package/colors
colors
get colors in your node.js console. Latest version: 1.4.0, last published: 4 years ago. Start using colors in your project by running `npm i colors`. There are 20994 other projects in the npm registry using colors.
www.npmjs.com
에러인지 성공인지 명확히 구분해주기 위해서 사용
1. npm init
package.json 생성
2. npm install
위에서 소개한 패키지들을 모두 설치해준다.
3. entry point 생성
index.js을 생성해준다
4. 사용할 모듈들을 모두 import 해준다.
const franc = require("franc"); // ISO 코드로 변환해줌
const langs = require("langs"); // ISO 코드를 해석해줌
const colors = require('colors'); // 오류 색상주기
5. node로 전달한 인수를 담아준다
const input = process.argv[2]; // node에서 인수 전달
6. 해당 문서를 참고해서 상황에 맞는 코드를 적용해준다.
if(langCode === 'und'){ // und 이상한 언어일때 출력됨
console.log("알 수 없는 언어입니다.".red)
}else {
const language = langs.where("3", langCode); // ISO 분석 코드
console.log(`추측 결과 : ${language.name}입니다`.green);
}
전체 코드
const franc = require("franc"); // ISO 코드로 변환해줌
const langs = require("langs"); // ISO 코드를 해석해줌
const colors = require('colors'); // 오류 색상주기 red/green
const input = process.argv[2]; // node에서 인수 전달
const langCode = franc(input); // ISO 코드로 변환
if(langCode === 'und'){
console.log("알 수 없는 언어입니다.".red)
}else {
const language = langs.where("3", langCode); // ISO 분석코드
console.log(`추측 결과 : ${language.name}입니다`.green);
}
성공시
실패시
원래 언어 처리는 아주 복잡하고 이해하기 힘들다고한다.
'해보기' 카테고리의 다른 글
[프로젝트 회고록] 쌍용교육센터 3차 프로젝트 (Next.js, Spring Boot, JPA, MySQL ,AWS ,Docker) (2) | 2024.10.15 |
---|---|
axios를 이용한 TV 프로그램 검색 (0) | 2023.04.17 |
자바스크립트로 todolist 만들기 (0) | 2023.03.16 |
자바스크립트로 추측 게임 만들기 (0) | 2023.03.15 |
한 페이지 이동하는 스크롤(원페이지 스크롤) (1) | 2023.02.25 |