Skip to content

Instantly share code, notes, and snippets.

@rkatic
Created August 14, 2023 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rkatic/33d6ece5eb37632459bf2a8a5aae7cea to your computer and use it in GitHub Desktop.
Save rkatic/33d6ece5eb37632459bf2a8a5aae7cea to your computer and use it in GitHub Desktop.
const HR = `
# sintaksa: regularni_izraz [dio_za_dodat]
# prva grupa u regularnom izrazu je i dio koji se brise
PFX (je|ni|ne|ho|naj|pra|is)
PFX (ne) i
SFX (i|mo|te|u) am
SFX (e|i|u|o) a
SFX (om|oj|og|im|em) a
SFX (ima|emu|oga|ome|omu|oma|oje|ije|jema) a
SFX (ea) u
SFX (e|emo|ste) u
SFX [ai](ma)
SFX (ci|aka) ka
SFX . ti
SFX (h|o|smo|ste|jah|jaše|jasmo|jaste|jahu) ti
SFX (jeh|ješe|jesmo|jeste|jehu) iti
SFX (la|lo|li|le) ti
SFX (a|u|e|i|m)
SFX (om|ovi|ova|ove|ima)
SFX (ovima)
SFX (a|u) o
SFX (čiju|čima|či) ko
SFX (ala) lo
SFX (ima) o
SFX (la|lo|li|le) ao
SFX (m|a|mo|ju|te) ti
SFX (na|ne|nu|no|ni) an
SFX (nim|noj|nom|nog|nima|nih) an
SFX (noga|nomu|nome) an
SFX (ju|ma)
SFX (oma|oje) i
SFX (če|ci|cima) k
SFX (a|mo|te)
SFX (gu) ći
SFX (žeš|žemo|žete) ći
SFX (e) i
SFX (žeš|žem|žemo|žete|že|žu) zati
`
function rootGeneratorFor(definition) {
const rules = []
for (const line of definition.split('\n')) {
const [cmd, pat, add = ''] = line.trim().split(/\s+/g, 3)
if (cmd === 'PFX') {
rules.push([true, new RegExp(pat === '.' ? '.' : `^(?:${pat}).`), add])
}
if (cmd === 'SFX') {
rules.push([false, new RegExp(pat === '.' ? '.' : `.(?:${pat})$`), add])
}
}
return function * genRoots(term) {
// yield term
const rightOps = []
const leftOps = []
for (const [isPfx, re, add] of rules) {
re.lastIndex = 0
const m = term.match(re)
if (!m) continue
const rm = (m[1] || '').length
if (isPfx) {
yield add + term.slice(rm)
leftOps.push([rm, add])
} else {
yield term.slice(0, term.length - rm) + add
rightOps.push([rm, add])
}
}
for (const [rmLeft, addLeft] of leftOps) {
const maxRmRight = term.length - rmLeft - 1
for (const [rmRight, addRight] of rightOps) {
if (rmRight <= maxRmRight) {
yield `${addLeft}${term.slice(rmLeft, term.length - rmRight)}${addRight}`
}
}
}
}
}
const genHrRoots = rootGeneratorFor(HR)
console.log([...genHrRoots('prakodovima')])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment