Skip to content

Commit

Permalink
Feat : decomposing hangeul into syllables feature added
Browse files Browse the repository at this point in the history
- 2 decompose option added
  - jamo option
  - qwerty option
- test code added
- gitignore added

Signed-off-by: ymw0407 <yunminwo1211@gmail.com>
  • Loading branch information
ymw0407 committed Apr 6, 2024
1 parent 9ed3902 commit bb444c8
Show file tree
Hide file tree
Showing 13 changed files with 640 additions and 0 deletions.
Empty file added .gitignore
Empty file.
Empty file added README.md
Empty file.
12 changes: 12 additions & 0 deletions examples/ex.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

import (
"fmt"

"github.com/ymw0407/golang-jamo/pkg/jamo"
"github.com/ymw0407/golang-jamo/pkg/options"
)

func main() {
fmt.Println(jamo.DecomposeHangeul("얘를롦놈", options.Jamo()))
}
11 changes: 11 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module github.com/ymw0407/golang-jamo

go 1.21.6

require github.com/stretchr/testify v1.9.0

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
143 changes: 143 additions & 0 deletions internal/data/data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*
Copyright 2024 Yun Minwoo. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package data

const (
StartHangeul = rune(0xAC00) // '가'
EndHangeul = rune(0xD7A3) // '힣'
)

var (
ChoSung = []rune{
'ㄱ', 'ㄲ', 'ㄴ', 'ㄷ', 'ㄸ', 'ㄹ', 'ㅁ', 'ㅂ', 'ㅃ', 'ㅅ', 'ㅆ', 'ㅇ', 'ㅈ', 'ㅉ', 'ㅊ', 'ㅋ', 'ㅌ', 'ㅍ', 'ㅎ',
}
JungSung = []rune{
'ㅏ', 'ㅐ', 'ㅑ', 'ㅒ', 'ㅓ', 'ㅔ', 'ㅕ', 'ㅖ', 'ㅗ', 'ㅘ', 'ㅙ', 'ㅚ', 'ㅛ', 'ㅜ', 'ㅝ', 'ㅞ', 'ㅟ', 'ㅠ', 'ㅡ', 'ㅢ', 'ㅣ',
}
JongSung = []rune{
0, 'ㄱ', 'ㄲ', 'ㄳ', 'ㄴ', 'ㄵ', 'ㄶ', 'ㄷ', 'ㄹ', 'ㄺ', 'ㄻ', 'ㄼ', 'ㄽ', 'ㄾ', 'ㄿ', 'ㅀ', 'ㅁ', 'ㅂ', 'ㅄ', 'ㅅ', 'ㅆ', 'ㅇ', 'ㅈ', 'ㅊ', 'ㅋ', 'ㅌ', 'ㅍ', 'ㅎ',
}
)

var ( // QWERTY Keyboard
// Qwerty keyboard form without shift
Qwerty = []string{"ㅘ", "ㅙ", "ㅚ", "ㅝ", "ㅞ", "ㅟ", "ㅢ", "ㄳ", "ㄵ", "ㄶ", "ㄺ", "ㄻ", "ㄼ", "ㄽ", "ㄾ", "ㄿ", "ㅀ", "ㅄ"}
// Qwerty keyboard form without shift
/*
fmt.Println(QwertyDecomposer["ㅘ"]) // "ㅗㅏ"
fmt.Println(QwertyDecomposer["ㄳ"]) // "ㄱㅅ"
fmt.Println(QwertyDecomposer["ㄲ"]) // "ㄲ"
// to make "ㄱㄱ" use QwertyOnlyShiftDecomposer1 or QwertyOnlyShiftDecomposer2
*/
QwertyDecomposer = map[string]string{
"ㅘ": "ㅗㅏ",
"ㅙ": "ㅗㅐ",
"ㅚ": "ㅗㅣ",
"ㅝ": "ㅜㅓ",
"ㅞ": "ㅜㅔ",
"ㅟ": "ㅜㅣ",
"ㅢ": "ㅡㅣ",
"ㄳ": "ㄱㅅ",
"ㄵ": "ㄴㅈ",
"ㄶ": "ㄴㅎ",
"ㄺ": "ㄹㄱ",
"ㄻ": "ㄹㅁ",
"ㄼ": "ㄹㅂ",
"ㄽ": "ㄹㅅ",
"ㄾ": "ㄹㅌ",
"ㄿ": "ㄹㅍ",
"ㅀ": "ㄹㅎ",
"ㅄ": "ㅂㅅ",
}
// Qwerty keyboard form only with shift
QwertyOnlyShift = []string{"ㄲ", "ㄸ", "ㅃ", "ㅆ", "ㅉ", "ㅒ", "ㅖ"}
// Qwerty keyboard form only with shift
/*
fmt.Println(QwertyOnlyShiftDecomposer1["ㄲ"]) // "ㄱㄱ"
fmt.Println(QwertyOnlyShiftDecomposer1["ㅒ"]) // "ㅑㅣ"
fmt.Println(QwertyOnlyShiftDecomposer1["ㅖ"]) // "ㅕㅣ"
*/
QwertyOnlyShiftDecomposer1 = map[string]string{
"ㄲ": "ㄱㄱ",
"ㄸ": "ㄷㄷ",
"ㅃ": "ㅂㅂ",
"ㅆ": "ㅅㅅ",
"ㅉ": "ㅈㅈ",
"ㅒ": "ㅑㅣ",
"ㅖ": "ㅕㅣ",
}
// Qwerty keyboard form only with shift
/*
fmt.Println(QwertyOnlyShiftDecomposer2["ㄲ"]) // "ㄱㄱ"
fmt.Println(QwertyOnlyShiftDecomposer2["ㅒ"]) // "ㅐㅐ"
fmt.Println(QwertyOnlyShiftDecomposer2["ㅖ"]) // "ㅔㅔ"
*/
QwertyOnlyShiftDecomposer2 = map[string]string{
"ㄲ": "ㄱㄱ",
"ㄸ": "ㄷㄷ",
"ㅃ": "ㅂㅂ",
"ㅆ": "ㅅㅅ",
"ㅉ": "ㅈㅈ",
"ㅒ": "ㅐㅐ",
"ㅖ": "ㅔㅔ",
}
)

var (
// tense consonants(된소리 자음)
TenseConsonants = []string{"ㄲ", "ㄸ", "ㅃ", "ㅆ", "ㅉ"}
// tense consonants(된소리 자음) decomposer
TenseConsonantsDecomposer = map[string]string{
"ㄲ": "ㄱㄱ",
"ㄸ": "ㄷㄷ",
"ㅃ": "ㅂㅂ",
"ㅆ": "ㅅㅅ",
"ㅉ": "ㅈㅈ",
}
// complex consonants(복합 자음)
ComplexConsonants = []string{"ㄳ", "ㄵ", "ㄶ", "ㄺ", "ㄻ", "ㄼ", "ㄽ", "ㄾ", "ㄿ", "ㅀ", "ㅄ"}
// complex consonants(복합 자음) decomposer
ComplexConsonantsDecomposer = map[string]string{
"ㄳ": "ㄱㅅ",
"ㄵ": "ㄴㅈ",
"ㄶ": "ㄴㅎ",
"ㄺ": "ㄹㄱ",
"ㄻ": "ㄹㅁ",
"ㄼ": "ㄹㅂ",
"ㄽ": "ㄹㅅ",
"ㄾ": "ㄹㅌ",
"ㄿ": "ㄹㅍ",
"ㅀ": "ㄹㅎ",
"ㅄ": "ㅂㅅ",
}
// Dipthong(이중 모음)
Diphthong = []string{"ㅐ", "ㅒ", "ㅔ", "ㅖ", "ㅘ", "ㅙ", "ㅚ", "ㅝ", "ㅞ", "ㅢ"}
// Dipthong(이중 모음) decomposer
DiphthongDecomposer = map[string]string{
"ㅐ": "ㅏㅣ",
"ㅒ": "ㅑㅣ",
"ㅔ": "ㅓㅣ",
"ㅖ": "ㅕㅣ",
"ㅘ": "ㅗㅏ",
"ㅙ": "ㅗㅏㅣ",
"ㅚ": "ㅗㅣ",
"ㅝ": "ㅜㅓ",
"ㅞ": "ㅜㅓㅣ",
"ㅟ": "ㅜㅣ",
"ㅢ": "ㅡㅣ",
}
)
63 changes: 63 additions & 0 deletions pkg/jamo/jamo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
Copyright 2024 Yun Minwoo. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package jamo

import (
"github.com/ymw0407/golang-jamo/internal/data"
"github.com/ymw0407/golang-jamo/pkg/options"
)

// Decompose Hangeul words into Syllables
/*
// example
fmt.Println(DecomposeHangeul("한글 is hangeul!")) // "ㅎㅏㄴㄱㅡㄹ is hangeul!"
*/
//* TODO: Allow to apply serveral option (now only first option can apply)
func DecomposeHangeul(hangeuls string, opts ...options.Options) (decomposedHangeul string) {
filterFunc := func(syllable string) string {
return syllable
}

if len(opts) > 0 {
option := opts[0]
filterFunc = option.GetFilterFunc()
}

for _, hangeul := range hangeuls {
if hangeul < data.StartHangeul || hangeul > data.EndHangeul { // non-hangeul character
decomposedHangeul += string(hangeul)
continue
}

diff := hangeul - data.StartHangeul

choIdx := diff / (21 * 28)
jungIdx := (diff % (21 * 28)) / 28
jongIdx := diff % 28

chosung := string(data.ChoSung[choIdx])
jungsung := string(data.JungSung[jungIdx])
jongsung := ""
if jongIdx > 0 {
jongsung = string(data.JongSung[jongIdx])
}

decomposedHangeul += filterFunc(chosung) + filterFunc(jungsung) + filterFunc(jongsung)
}

return decomposedHangeul
}
47 changes: 47 additions & 0 deletions pkg/jamo/jamo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
Copyright 2024 Yun Minwoo. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package jamo_test

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/ymw0407/golang-jamo/pkg/jamo"
)

func TestDecomposeHangeul(t *testing.T) {
t.Run("한글 is hangeul!", func(t *testing.T) {
expected := "ㅎㅏㄴㄱㅡㄹ is hangeul!"
res := jamo.DecomposeHangeul("한글 is hangeul!")

assert.Equal(t, expected, res, res+" : `"+expected+"` is expected!")
})

t.Run("ㄱ is giyeok!", func(t *testing.T) {
expected := "ㄱ is giyeok!"
res := jamo.DecomposeHangeul("ㄱ is giyeok!")

assert.Equal(t, expected, res, res+" : `"+expected+"` is expected!")
})

t.Run("윤민우", func(t *testing.T) {
expected := "ㅇㅠㄴㅁㅣㄴㅇㅜ"
res := jamo.DecomposeHangeul("윤민우")

assert.Equal(t, expected, res, res+" : `"+expected+"` is expected!")
})
}
23 changes: 23 additions & 0 deletions pkg/options/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
Copyright 2024 Yun Minwoo. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package options

type Options interface {
GetFilterFunc() func(syllable string) string
}

//* TODO: Add Cheonjiin(천지인) Keyboard form
Loading

0 comments on commit bb444c8

Please sign in to comment.