-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathutils.lua
More file actions
59 lines (49 loc) · 1.43 KB
/
utils.lua
File metadata and controls
59 lines (49 loc) · 1.43 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
--[[--
Shared Utility Functions
Common helpers used across the audiobook plugin modules.
Eliminates duplication of commandExists, ws, countSyllables.
@module utils
--]]
local Utils = {}
--- Check if a command exists on the system PATH.
-- @param cmd string Command name (e.g. "piper", "espeak-ng")
-- @return boolean
function Utils.commandExists(cmd)
local handle = io.popen("which " .. cmd .. " 2>/dev/null")
if handle then
local result = handle:read("*a")
handle:close()
return result and result ~= ""
end
return false
end
--- Normalise whitespace: collapse runs to single space, trim edges.
-- @param s string
-- @return string
function Utils.ws(s)
if not s then return "" end
return s:gsub("%s+", " "):match("^%s*(.-)%s*$")
end
--- Count the number of syllables in an English word (heuristic).
-- @param word string
-- @return number Syllable count (minimum 1)
function Utils.countSyllables(word)
if not word or word == "" then return 1 end
word = word:lower()
local count = 0
local prev_vowel = false
for i = 1, #word do
local char = word:sub(i, i)
local is_vowel = char:match("[aeiouy]")
if is_vowel and not prev_vowel then
count = count + 1
end
prev_vowel = is_vowel
end
-- Silent-e rule
if word:sub(-1) == "e" and count > 1 then
count = count - 1
end
return math.max(count, 1)
end
return Utils