-
Notifications
You must be signed in to change notification settings - Fork 6
/
lib.js
100 lines (81 loc) · 3.13 KB
/
lib.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
/**
* greek-name-klitiki
*
* @module greek-name-klitiki
* @author Christos Panagiotakopoulos <[email protected]>
* @copyright Copyright(c) 2018 Christos Panagiotakopoulos
* @license MIT
*
*/
const Hypher = require('hypher');
const greek = require('hyphenation.el-monoton');
const h = new Hypher(greek);
/**
* Checks if a syllable is accented
* @param {string} string
* @returns {boolean}
*/
function isAccented(string) {
const accentedCharacters = ['ά', 'έ', 'ή', 'ί', 'ό', 'ύ', 'ώ'];
for (char of accentedCharacters)
if (string.indexOf(char) > -1 || string.indexOf(char.toUpperCase()) > -1)
return true;
return false;
}
/**
* Categorizes a word depending to the syllable that is accented
* @param {string} word
* @returns {string|false} - Returns 'LIG', 'PAR' or 'PRO' depending on where the accent is. If the word is not accented it returns 'false'
*/
function accentCategorization(word) {
const hyphenated = h.hyphenate(word);
const syllableNum = hyphenated.findIndex(s => isAccented(s));
// This word is not accented
if (syllableNum == -1)
return false;
// Λήγουσα - (eg: Πηγή)
if (syllableNum == (hyphenated.length - 1) && !isAccented(word[0]))
return 'LIG';
// Παραλήγουσα - (eg: Χρήστος)
if (syllableNum == (hyphenated.length - 2))
return 'PAR';
// Προπαραλήγουσα - (eg: Αλέξανδρος)
return 'PRO';
}
/**
* Transforms a word to its vocative form (currently works best with names)
* @example
* // returns "Χρήστο"
* klitiki("Χρήστος")
* @param {string} word
* @param {boolean} [onlyUppercase=true]
* @returns {string} - The Vocative of the word
*/
function klitiki(word, onlyUppercase = true) {
if (!(word[0].toUpperCase() == word[0]) && onlyUppercase) {
console.log("Not a name");
return word;
}
// Αρσενικά σε -ας / ης (eg: Επαμεινώνδας, Αναστάσης)
if (word.endsWith('ας') || word.endsWith('άς') || word.endsWith('ης') || word.endsWith('ής'))
return word.slice(0, word.length - 1);
// Αρσενικά σε -ος
if (!word.endsWith('ος') && !word.endsWith('ός'))
return word;
// Αρσενικά σε -ιος (Αναστάσιος, Γεώργιος, Γρηγόριος)
if (word.endsWith('ιος'))
return word.slice(0, word.length - 2) + 'ε';
// Εξαίρεση αρσενικά σε -ίνος (Κωνσταντίνος, Αυγουστίνος)
if (word.endsWith('ίνος'))
return word.slice(0, word.length - 2) + 'ε';
const category = accentCategorization(word);
if (category == 'LIG')
return word.slice(0, word.length - 2) + 'ό'; // Νικολός -> Νικολό
if (category == 'PAR')
return word.slice(0, word.length - 1); // Χρήστος -> Χρήστο
if (category == 'PRO')
return word.slice(0, word.length - 2) + 'ε'; // Αλέξανδρος -> Αλέξανδρε
// If it ends in -ος and it doesn't have an accent :/
return word;
}
module.exports = klitiki;