Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 40 additions & 42 deletions src/lib/utils/thaiwords.ts
Original file line number Diff line number Diff line change
@@ -1,77 +1,75 @@
import dict from '$lib/utils/dict'
import dict from '$lib/utils/dict';

export function splitWord(word: string) {
var alphas = word.split("");
var out: string[] = [];
alphas.forEach(function (a) {
if (a.match(/[ก-ฮ]/) || a.match(/[ใเแโไาำะๆฯฤา]/) || a.match(/[\.\*\/]/)) {
out.push(a);
}
else {
out[out.length - 1] += a;
}
});
return out;
var alphas = word.split('');
var out: string[] = [];
alphas.forEach(function (a) {
if (a.match(/[ก-ฮ]/) || a.match(/[ใเแโไาำะๆฯฤา]/) || a.match(/[\.\*\/]/)) {
out.push(a);
} else {
out[out.length - 1] += a;
}
});
return out;
}

export function isLegal(a: string) {
return a.length == 1 && a.match(/[\u0E00-\u0E4C]/) !== null
return a.length == 1 && a.match(/[\u0E00-\u0E4C]/) !== null;
}

export function appendable(a: string, b: string) {
return a.match(consonantRegex) && (b.match(upperLetterRegex) || b.match(lowerLetterRegex))
return a.match(consonantRegex) && (b.match(upperLetterRegex) || b.match(lowerLetterRegex));
}

export function isUpper(a: string) {
return a.match(upperLetterRegex) !== null
return a.match(upperLetterRegex) !== null;
}

export function isLower(a: string) {
return a.match(lowerLetterRegex) !== null
return a.match(lowerLetterRegex) !== null;
}

export function isMiddle(a: string) {
return a.match(/[ก-ฮ]/) || a.match(/[ใเแโไาำะๆฯฤา]/)
return a.match(/[ก-ฮ]/) || a.match(/[ใเแโไาำะๆฯฤา]/);
}

export function isTall(a: string) {
return a === 'ใ' || a === 'ไ' || a === 'โ'
return a === 'ใ' || a === 'ไ' || a === 'โ';
}

export function wordLength(word: string) {
return word.replace(/[\u0E31\u0E34-\u0E3A\u0E47-\u0E4E]/g, "").length
return word.replace(/[\u0E31\u0E34-\u0E3A\u0E47-\u0E4E]/g, '').length;
}

export function getFirstLetter(word: string) {
for(var l of word.split(""))
if(l.match(/[ก-ฮ]/)) return l
return ""
for (var l of word.split('')) if (l.match(/[ก-ฮ]/)) return l;
return '';
}

export const consonantRegex = /[\u0E01-\u0E2E]/
export const upperLetterRegex = /[\u0E31\u0E34-\u0E37\u0E47-\u0E4C`]/
export const lowerLetterRegex = /[\u0E38-\u0E39]/
export const consonantRegex = /[\u0E01-\u0E2E]/;
export const upperLetterRegex = /[\u0E31\u0E34-\u0E37\u0E47-\u0E4C`]/;
export const lowerLetterRegex = /[\u0E38-\u0E39]/;

export function search(word: string) {
return dict.includes(word)
return dict.includes(word);
}

export function getSubWords(word: string, allowRepeat: boolean = true) {
let subWords: string[] = []
let subWords: string[] = [];

if(allowRepeat)
for(const w of dict) {
if(w.split("").every(l => word.includes(l))) {
subWords = [...subWords, w]
}
}
else
for(const w of dict) {
const a = w.split("")
if(new Set(a).size == a.length && a.every(l => word.includes(l))) {
subWords = [...subWords, w]
}
}
if (allowRepeat)
for (const w of dict) {
if (w.split('').every((l) => word.includes(l))) {
subWords.push(w);
}
}
else
for (const w of dict) {
const a = w.split('');
if (new Set(a).size == a.length && a.every((l) => word.includes(l))) {
subWords.push(w);
}
}

return subWords
}
return subWords;
}