Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 한글 자모음을 인자로 입력받아 한글 문자로 합성하는 함수 추가 #53

Merged
merged 7 commits into from
Apr 17, 2024

Conversation

evan-moon
Copy link
Member

@evan-moon evan-moon commented Apr 16, 2024

Overview

#18 에서 이슈레이징된 aseemble 함수를 구현하기 위해 한글 문자를 합성하는 함수를 추가합니다.

  • 한글 문자를 합성하는 combineHangulCharacter, combineVowels 함수 추가
  • combineHangulCharacter 함수의 커링된 버전인 curriedCombineHangulCharacter 함수 추가
  • canBeChosung, canBeJungsung, canBeJongsung 함수에 타입 가드 추가

combineHangulCharacter

초성, 중성, 종성을 인자로 받아 하나의 한글 문자를 생성합니다. 만약 잘못된 값을 입력받았다면 "Invalid hangul character" 에러를 throw 합니다.

combineHangulCharacter('ㄱ', 'ㅏ', 'ㅂㅅ') // '값'

curriedCombineHangulCharacter

combineHangulCharacter 함수의 커링된 버전입니다. #18 에서 이슈레이징된, 조건에 따라 순차적으로 문자를 합성해나가야하는 함수를 구현할 때 필요합니다.

const combineMiddleHangulCharacter = curriedCombineHangulCharacter('ㄱ')
const combineLastHangulCharacter = combineMiddleHangulCharacter('ㅏ')

combineLastHangulCharacter('ㄱ') // '각'

combineVowels

두 개의 단모음을 인자로 받아 겹모음을 생성합니다. 만약 올바르지 않은 단모음을 입력받았다면 Join하여 반환합니다.

combineVowels('ㅗ', 'ㅏ') // 'ㅘ'

PR Checklist

  • I read and included theses actions below
  1. I have read the Contributing Guide
  2. I have written documents and tests, if needed.

Copy link

changeset-bot bot commented Apr 16, 2024

🦋 Changeset detected

Latest commit: 1e2d292

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@evan-moon evan-moon self-assigned this Apr 16, 2024
@evan-moon evan-moon added the enhancement New feature or request label Apr 16, 2024
Copy link

netlify bot commented Apr 16, 2024

Deploy Preview for es-hangul ready!

Name Link
🔨 Latest commit 1e2d292
🔍 Latest deploy log https://app.netlify.com/sites/es-hangul/deploys/661f27697973e40008f3362d
😎 Deploy Preview https://deploy-preview-53--es-hangul.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Co-authored-by: Jonghyeon Ko <[email protected]>
Comment on lines 37 to 42
const 중성개수 = HANGUL_CHARACTERS_BY_MIDDLE_INDEX.length;
const 종성개수 = HANGUL_CHARACTERS_BY_LAST_INDEX.length;

const 초성인덱스 = HANGUL_CHARACTERS_BY_FIRST_INDEX.indexOf(firstCharacter);
const 중성인덱스 = HANGUL_CHARACTERS_BY_MIDDLE_INDEX.indexOf(middleCharacter);
const 종성인덱스 = HANGUL_CHARACTERS_BY_LAST_INDEX.indexOf(lastCharacter);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

마이너하긴 한데, 충분히 영어로 표현 가능한 변수명 같아보입니다. 기본적으로 변수명은 영어로 컨벤션을 가져가는 것은 어떨까요?

e.g.

  • 중성개수 -> numMiddleCharacter
  • 초성인덱스 -> firstCharacterIndex

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

좋습니당 컨벤션은 하나로 맞추는게 좋을 것 같네요!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1e2d292 에서 반영하였습니다! 하단의 코멘트에 질문해주신 유니코드에 대한 이론도 firstIndexOfTargetConsonant, firstIndexOfTargetVowel이라는 변수명으로 표현해봤어요 🙏

const 종성인덱스 = HANGUL_CHARACTERS_BY_LAST_INDEX.indexOf(lastCharacter);

const unicode =
COMPLETE_HANGUL_START_CHARCODE + 초성인덱스 * 중성개수 * 종성개수 + 중성인덱스 * 종성개수 + 종성인덱스;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

잘 몰라서 그러는데, 혹시 unicode 계산식이 있을까요? 단순하게 생각했을 때

초성인덱스 * 초성개수 * 중성개수 + 중성인덱스 * 종성개수 + 종성인덱스

가 되어야하는게 아닌가 의문이 들어서요.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

넵 요것은 유니코드표에 한글이 등재되어있는 순서가 조금 특이해서 나타나는 현상인데요.

유니코드 표 상에서 한글문자는 "가, 나, 다" 순이 아닌 "가, 각, 갂, 갃..." 등의 순서로 되어있습니다.

즉, 유니코드에 한글이 등재된 순서는 어떤 자음 문자 하나가 나타났을 때, 이 자음으로 조합할 수 있는 모든 문자를 나열한 이후 다음 자음으로 넘어간다고 볼 수 있습니다. 게다가 자음이나 모음의 나열 순서도 일반적으로 우리가 한글을 공부할 때 사용하는 "ㄱ, ㄴ, ㄷ, ㄹ..."이나 "ㅏ, ㅑ, ㅓ, ㅕ..." 순서가 아닌 "ㄱ, ㄲ, ㄴ, ㄷ, ㄹ, ㄸ..."와 "ㅏ, ㅐ, ㅑ, ㅒ..." 순서로 되어있어요.

그래서 만약 초성으로 , 중성으로 , 종성으로 이 들어온 상황이라면


  1. ㄴ의 초성 인덱스 3 * 중성 개수 21 * 종성 개수 28 = ㄱ, ㄲ으로 조합할 수 있는 모든 문자를 건너뛰어야 으로 시작하는 첫 문자인 까지 갈 수 있음
  2. 중성 인덱스 1 * 종성 개수 28 = 1번까지 수행했다면 부터 세야하니, ㄴ+ㅏ+종성으로 조합할 수 있는 모든 문자를 건너뛰어야 ㄴ+ㅐ로 시작하는 첫 문자인 "내"까지 갈 수 있음.
  3. 종성 인덱스 1= 여기서부터 내, 낵, 낶, 낷... 순서로 나타남. 그러니 종성 인덱스만큼만 앞으로 가면 원하는 문자에 도달.

와 같은 원리로 계산을 하여 원하는 문자를 찾아간다고 봐주시면 됩니다.

말씀해주신 "초성인덱스 * 초성개수"라는 식은 자음이 "ㄱ, ㄴ, ㄷ, ㄹ..." 순서로 나타난다는 상황을 가정하고 있지만 유니코드의 한글 문자 등재 순서가 "특정 자음 선택 + 해당 자음으로 조합할 수 있는 모든 문자를 나열"이기 때문에 계산식이 조금 특이합니다 😅

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@evan-moon 혹시나 오타일까 확인 차 여쭤봤는데 올바른 구현이었군요. 상세한 설명 넘 감사드립니다 👍

Copy link
Member

@okinawaa okinawaa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

긴 작업 수고하셨습니다! 😄

assert.throws(() => combineHangulCharacter('ㄱ', 'ㄴ', 'ㅃ'), Error, 'Invalid hangul Characters: ㄱ, ㄴ, ㅃ');
});

it('종성이 될 수 없는 문자가 종성으로 입력되면 에러를 반환한다. (ㄱ, ㅏ, ㅃ)', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

요런거 테스트 케이스 너무 좋네요👍

@evan-moon evan-moon merged commit 314f0a9 into toss:main Apr 17, 2024
4 checks passed
@github-actions github-actions bot mentioned this pull request Apr 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants