This repository was archived by the owner on Jul 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
128 lines (95 loc) · 3.65 KB
/
Copy pathindex.js
File metadata and controls
128 lines (95 loc) · 3.65 KB
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
var debugging = false;
$(document).ready(() => {
new ClipboardJS('button');
$('.input').on('input', () => {
var sentence = $(".input").val() || ' ';
var sentenceWordsAndPunctuation = sentence.match(/&[\w']+&|[\w']+|[^\w'&]+|&+/g);
var franklinVersionOfSentence = sentenceWordsAndPunctuation.map(word => {
if (shouldNotBeConverted(word))
return word.slice(1, -1);
if (!isAWord(word))
return escapeHtml(word);
try {
var franklinWord = convertEnglishWordToFranklinWord(word.toUpperCase());
if (wordIsCapitalized(word))
return capitalizeFranklinWord(franklinWord);
return franklinWord;
}
catch (error) {
if (error.message === `'${word.toUpperCase()}' is not in the CMU dictionary.`) {
log(error.message);
return `<span class="red">${word}</span>`;
} else
throw error;
}
}).join('');
log(`Original sentence: ${sentence}`)
log(`Franklin sentence: ${franklinVersionOfSentence}`);
$('.output-text').html(franklinVersionOfSentence);
});
});
function isAWord(string) {
return /^[\w']+$/.test(string);
}
function shouldNotBeConverted(string) {
return /^&[\w']+&$/.test(string);
}
function escapeHtml(string) {
return string.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>');
}
function wordIsCapitalized(word) {
return word.charAt(0).toUpperCase() === word.charAt(0);
}
function capitalizeFranklinWord(franklinWord) {
var firstLetterOfFranklinWord = franklinWord.charAt(0);
var newFirstLetterOfFranklinWord = firstLetterOfFranklinWord;
switch (firstLetterOfFranklinWord) {
case 'ʃ':
newFirstLetterOfFranklinWord = 'S';
break;
case 'ɑ':
newFirstLetterOfFranklinWord = 'A';
break;
default:
if (isANormalLetter(firstLetterOfFranklinWord))
newFirstLetterOfFranklinWord = firstLetterOfFranklinWord.toUpperCase();
}
return newFirstLetterOfFranklinWord + franklinWord.slice(1);
}
function isANormalLetter(letter) {
return /[a-z]/.test(letter);
}
function convertEnglishWordToFranklinWord(word) {
var arpabetVersionOfWord = englishToArpabet[word]?.split(' ');
if (!arpabetVersionOfWord)
throw new Error(`'${word}' is not in the CMU dictionary.`);
log(`Parsing: ${word}`);
log(`Arpabet\t\tFranklin`);
var franklinVersionOfWord = arpabetVersionOfWord.map((arpabetSound, i) => {
var franklinLetter;
if (isLastItemOfArray(i, arpabetVersionOfWord.length) && arpabetSound === 'S')
franklinLetter = 's'
if (removeStressFromArpabetSound(arpabetSound) === 'EH' && arpabetVersionOfWord[i + 1] === 'R')
franklinLetter = 'ee';
if (!franklinLetter)
franklinLetter = arpabetToFranklin[arpabetSound];
if (!franklinLetter)
franklinLetter = arpabetToFranklin[removeStressFromArpabetSound(arpabetSound)];
if (!franklinLetter)
throw new Error(`Couldn't find a Franklin letter for ${arpabetSound}`);
log(`${arpabetSound}\t\t${franklinLetter}`);
return franklinLetter
}).join('');
log(`Result: ${franklinVersionOfWord}\n`);
return franklinVersionOfWord;
}
function isLastItemOfArray(index, arrayLength) {
return index + 1 === arrayLength;
}
function removeStressFromArpabetSound(string) {
return string.slice(0, -1);
}
function log(text) {
if (debugging)
console.log(text);
}