-
Notifications
You must be signed in to change notification settings - Fork 0
/
transcribe.js
135 lines (110 loc) · 4.17 KB
/
transcribe.js
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
document.getElementById("rulesSelect").addEventListener("change", function () {
const selectedRuleFile = this.value;
loadRules(selectedRuleFile);
});
function loadRules(ruleFileName) {
fetch(ruleFileName + ".json")
.then((response) => response.json())
.then((data) => {
// Assuming 'transcribeEsperantoToPolish' function will use this data
// Update your transcription rules here
rules = data;
})
.catch((error) => console.error("Error loading rules:", error));
}
// Initial load
loadRules(document.getElementById("rulesSelect").value);
// Your transcribing functions here, modified for the browser
function transcribeNumber(whole, fraction, rules) {
const transcribeNumberPart = (number) => {
const getNumber = (num) => rules.numbers[num.toString()] || num.toString();
const transcribe = (one, ten, hundred, includeOne) => {
let result = "";
if (hundred > 0) {
result +=
hundred === 1
? ` ${getNumber(100)}`
: ` ${getNumber(hundred)} ${getNumber(100)}`;
}
if (ten > 0) {
result +=
ten === 1
? ` ${getNumber(10)}`
: ` ${getNumber(ten)} ${getNumber(10)}`;
}
if (one === 1) {
if (includeOne) {
result += ` ${getNumber(1)}`;
}
} else if (one !== 0) {
result += ` ${getNumber(one)}`;
}
return result.trim();
};
let ones = number % 10;
let tens = Math.floor((number / 10) % 10);
let hundreds = Math.floor((number / 100) % 10);
let thousands = Math.floor((number / 1000) % 10);
let tenThousands = Math.floor((number / 10000) % 10);
let hundredThousands = Math.floor((number / 100000) % 10);
let millions = Math.floor((number / 1000000) % 10);
let tenMillions = Math.floor((number / 10000000) % 10);
let hundredMillions = Math.floor((number / 100000000) % 10);
let first = transcribe(ones, tens, hundreds, true);
let second = transcribe(thousands, tenThousands, hundredThousands, false);
let third = transcribe(millions, tenMillions, hundredMillions, false);
let result = "";
if (millions || tenMillions || hundredMillions) {
result += `${third} ${getNumber(1000000)},`;
}
if (thousands || tenThousands || hundredThousands) {
result += ` ${second} ${getNumber(1000)},`;
}
if (ones || tens || hundreds) {
result += ` ${first}`;
}
return result.trim().replace(/,$/, ""); // Remove trailing comma
};
let result = transcribeNumberPart(whole);
if (fraction !== 0 && fraction < 100) {
result = `${result}, komo ${transcribeNumberPart(fraction)}`;
}
return result;
}
function transcribeEsperantoToPolish(text) {
// Convert text to lowercase
text = text.toLowerCase();
// Apply overrides first
for (let override of rules.overrides) {
text = text.replace(new RegExp(override.eo, "g"), override.pl);
}
// Apply fragment replacements
for (let fragment of rules.fragments) {
text = text.replace(new RegExp(fragment.match, "g"), fragment.replace);
}
// Apply number replacements with more complex logic
text = text.replace(/\b(\d+)(?:\.(\d+))?\b/g, (match, whole, fraction) => {
whole = parseInt(whole, 10);
fraction = fraction ? parseInt(fraction, 10) : 0;
return transcribeNumber(whole, fraction, rules);
});
// Transcribe letter by letter
let transcribedText = "";
for (let char of text) {
if (rules.letters[char]) {
transcribedText += rules.letters[char];
} else {
transcribedText += char; // Keep the character as is if no rule applies
}
}
return transcribedText;
}
// Add event listener to the button
document.getElementById("transcribeButton").addEventListener("click", () => {
const inputText = document.getElementById("inputField").value;
console.log("Transcribing:", inputText);
const transcribedText = transcribeEsperantoToPolish(inputText);
console.log("Result:", transcribedText);
document.getElementById("outputField").innerText =
transcribedText || "No transcription available";
});