-
Notifications
You must be signed in to change notification settings - Fork 0
/
romaji_to_hiragana.c
143 lines (117 loc) · 4.46 KB
/
romaji_to_hiragana.c
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <stdlib.h>
#include <string.h>
#define HIRAGANA_SIZE 16
void romaji_to_hiragana(
char word[],
char **save_to) { // receives two arrays, one with the input
// word and the pointer to pointer to where the hiragana
// converstion should be saved to
char *individual_hiragana = (char *)malloc(sizeof("ああ"));
char *complete_hiragana = (char *)malloc(18 * sizeof("あ") + 1);
complete_hiragana[0] = '\0';
char *result;
int consonant = 0;
// flags
// char to string
char charString[2] = {' ', '\0'};
// char to string
char *hiragana[HIRAGANA_SIZE][9] = {
// a i u e o ya yu yo
{"\0", "a", "i", "u", "e", "o", "a", "u", "o"},
{"\0", "あ", "い", "う", "え", "お", "\0", "\0", "\0"}, // vowels
{"k", "か", "き", "く", "け", "こ", "きゃ", "きゅ", "きょ"},
// S special case shi
{"s", "さ", "し", "す", "せ", "そ", "しゃ", "しゅ", "しょ"},
// T especial case tsu, ya =cha, yu =chu, yo = cho
{"t", "た", "ち", "つ", "て", "と", "ちゃ", "ちゅ", "ちょ"},
{"n", "な", "に", "ぬ", "ね", "の", "にゃ", "にゅ", "にょ"},
// H especial case fu
{"h", "は", "ひ", "ふ", "へ", "ほ", "ひゃ", "ひゅ", "ひょ"},
{"m", "ま", "み", "む", "め", "も", "みゃ", "みゅ", "みょ"},
{"y", "や", "\0", "ゆ", "\0", "よ", "\0", "\0", "\0"},
{"r", "ら", "り", "る", "れ", "ろ", "りゃ", "りゅ", "りょ"},
{"w", "わ", "ゐ", "\0", "ゑ", "を", "\0", "\0", "\0"},
{"g", "が", "ぎ", "ぐ", "げ", "ご", "ぎゃ", "ぎゅ", "ぎょ"},
// z especial case ji TODO
{"z", "ざ", "じ", "ず", "ぜ", "ぞ", "じゃ", "じゅ", "じょ"},
// d especial case ji, zu TODO
{"d", "だ", "ぢ", "づ", "で", "ど", "ぢゃ", "ぢゅ", "ぢょ"},
{"b", "ば", "び", "ぶ", "べ", "ぼ", "びゃ", "びゅ", "びょ"},
{"p", "ぱ", "ぴ", "ぷ", "ぺ", "ぽ", "ぴゃ", "ぴゅ", "ぴょ"}
};
int row = 0;
int column = 0;
int hflag = 0;
// int word_index;
int word_index;
// for loop for word index.
for (word_index = 0; word_index < strlen(word); word_index++) {
for (row = 0; row < HIRAGANA_SIZE; row++) {
// convert chars from word to string
if (word[word_index] == 'c') {
if (word[word_index + 1] == 'h') {
word[word_index + 1] = 'y';
}
word[word_index] = 't';
}
if (word[word_index] == 'f') {
word[word_index] = 'h';
}
charString[0] = word[word_index]; // word index
// if charString is c, then interpret it as t
if (!strcmp(charString, hiragana[row][0])) {
consonant = 1;
// if statement to check if its especial case of S
//
++word_index; // move to next letter of word
break;
}
}
if (consonant != 1) {
row = 1;
}
// printf("row %d column %d\n", row, column);
// scans first row the the vowels and others are found
// it should only scan 6 to 9 if there is a consonant before
// if consonant>1, then use ++word_index for the word index
for (column = 0; column < 9; column++) {
if (word[word_index] == 'y') {
charString[0] = word[++word_index];
column = 6;
// handle h in the word
} else if (word[word_index] == 'h') {
// if previous char was t, then column = 6
if (word[word_index - 1] == 't') {
column = 6;
}
charString[0] = word[++word_index];
// handle especial case tsu
} else if (word[word_index] == 's') {
if (word[word_index - 1] == 't') {
charString[0] = word[++word_index];
}
}
else
charString[0] = word[word_index]; // word index
if (!strcmp(charString, hiragana[0][column])) {
break;
}
}
// if (!strcmp(individual_hiragana, "n")) {
// individual_hiragana = strdup("ん");
// } else
strcpy(individual_hiragana, hiragana[row][column]);
// printf("%s\t", individual_hiragana);
if (!strcmp(individual_hiragana, "n")) {
strcpy(individual_hiragana, "ん");
// printf("\n");
}
// printf("%s\t", individual_hiragana);
// put all hiragana symbols together
strcat(complete_hiragana, individual_hiragana);
consonant = 0;
}
// printf("%s\n", complete_hiragana);
// printf("%li\n", strlen(complete_hiragana));
*save_to = strdup(complete_hiragana);
}