forked from myevan/csjosa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsjosa.cs
126 lines (113 loc) · 4.06 KB
/
csjosa.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections.Generic;
namespace Myevan
{
public class Korean
{
public class Josa
{
class JosaPair
{
public JosaPair(string josa1, string josa2)
{
this.josa1 = josa1;
this.josa2 = josa2;
}
public string josa1
{ get; private set; }
public string josa2
{ get; private set; }
}
public string Replace(string src)
{
var strBuilder = new StringBuilder(src.Length);
var josaMatches = _josaRegex.Matches(src);
var lastHeadIndex = 0;
foreach (Match josaMatch in josaMatches)
{
var josaPair = _josaPatternPaird[josaMatch.Value];
strBuilder.Append(src, lastHeadIndex, josaMatch.Index - lastHeadIndex);
if (josaMatch.Index > 0)
{
var prevChar = src[josaMatch.Index - 1];
if ((HasJong(prevChar) && josaMatch.Value != "(으)로") ||
(HasJongExceptRieul(prevChar) && josaMatch.Value == "(으)로"))
{
strBuilder.Append(josaPair.josa1);
}
else
{
strBuilder.Append(josaPair.josa2);
}
}
else
{
strBuilder.Append(josaPair.josa1);
}
lastHeadIndex = josaMatch.Index + josaMatch.Length;
}
strBuilder.Append(src, lastHeadIndex, src.Length - lastHeadIndex);
return strBuilder.ToString();
}
static bool HasJong(char inChar)
{
if (inChar >= 0xAC00 && inChar <= 0xD7A3) // 가 ~ 힣
{
int localCode = inChar - 0xAC00; // 가~ 이후 로컬 코드
int jongCode = localCode % 28;
if (jongCode > 0)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
static bool HasJongExceptRieul(char inChar)
{
if (inChar >= 0xAC00 && inChar <= 0xD7A3)
{
int localCode = inChar - 0xAC00;
int jongCode = localCode % 28;
if (jongCode == 8 || jongCode == 0)
{
return false;
}
else
{
return true;
}
}
else
{
return false;
}
}
Regex _josaRegex = new Regex(@"\(이\)가|\(와\)과|\(을\)를|\(은\)는|\(아\)야|\(이\)여|\(으\)로|\(이\)라");
Dictionary<string, JosaPair> _josaPatternPaird = new Dictionary<string, JosaPair>
{
{ "(이)가", new JosaPair("이", "가") },
{ "(와)과", new JosaPair("과", "와") },
{ "(을)를", new JosaPair("을", "를") },
{ "(은)는", new JosaPair("은", "는") },
{ "(아)야", new JosaPair("아", "야") },
{ "(이)여", new JosaPair("이여", "여") },
{ "(으)로", new JosaPair("으로", "로") },
{ "(이)라", new JosaPair("이라", "라") },
};
}
public static string ReplaceJosa(string src)
{
return _josa.Replace(src);
}
static Josa _josa = new Josa();
}
}