AMFP Enhanced
C++ reference: the code this unofficial fork added
Loading...
Searching...
No Matches
LuxKeywords.h
Go to the documentation of this file.
1/*
2 * AMFP-2026: clue words drawn in a warm colour (Guidance option, LuxGuidance.h). The list is a
3 * translatable string, AMFP2026/Keywords in the Enhanced lang file: words separated by "|".
4 * Latin words match whole words, any case, plural "s"/"es" included; CJK words match anywhere.
5 * The longest word wins ("fuse box" before "fuse"). Pure, unit tested in tests/cpp/test_keywords.cpp.
6 */
7#ifndef LUX_KEYWORDS_H
8#define LUX_KEYWORDS_H
9
10#include <algorithm>
11#include <string>
12#include <vector>
13
15{
16 size_t mlStart;
17 size_t mlLength;
19};
20
21inline bool LuxIsWordChar(wchar_t c)
22{
23 return (c >= L'a' && c <= L'z') || (c >= L'A' && c <= L'Z') || (c >= L'0' && c <= L'9') ||
24 c == L'\'' || (c >= 0xC0 && c < 0x2E80);
25}
26
27inline wchar_t LuxFoldCase(wchar_t c) { return (c >= L'A' && c <= L'Z') ? (wchar_t)(c - L'A' + L'a') : c; }
28
29inline bool LuxLongerFirst(const std::wstring& a, const std::wstring& b) { return a.size() > b.size(); }
30
31/** "fuse box| winch |" -> {"fuse box", "winch"}, longest first. */
32inline std::vector<std::wstring> LuxParseKeywords(const std::wstring& asList)
33{
34 std::vector<std::wstring> vOut;
35 size_t lStart = 0;
36 while(lStart <= asList.size())
37 {
38 size_t lEnd = asList.find(L'|', lStart);
39 if(lEnd == std::wstring::npos) lEnd = asList.size();
40 std::wstring s = asList.substr(lStart, lEnd - lStart);
41 size_t a = s.find_first_not_of(L" \t\r\n"), b = s.find_last_not_of(L" \t\r\n");
42 if(a != std::wstring::npos) vOut.push_back(s.substr(a, b - a + 1));
43 lStart = lEnd + 1;
44 }
45 std::stable_sort(vOut.begin(), vOut.end(), LuxLongerFirst);
46 return vOut;
47}
48
49/** Length of the keyword match at alPos (0: none). */
50inline size_t LuxKeywordMatchAt(const std::wstring& asText, size_t alPos, const std::wstring& asWord)
51{
52 if(asWord.empty() || alPos + asWord.size() > asText.size()) return 0;
53 for(size_t i = 0; i < asWord.size(); ++i)
54 if(LuxFoldCase(asText[alPos + i]) != LuxFoldCase(asWord[i])) return 0;
55 if(LuxIsWordChar(asWord[0]) == false) return asWord.size(); // CJK: anywhere
56 if(alPos > 0 && LuxIsWordChar(asText[alPos - 1])) return 0; // whole words only
57 size_t lEnd = alPos + asWord.size();
58 if(lEnd < asText.size() && LuxFoldCase(asText[lEnd]) == L's') ++lEnd; // plural
59 else if(lEnd + 1 < asText.size() && LuxFoldCase(asText[lEnd]) == L'e' && LuxFoldCase(asText[lEnd + 1]) == L's') lEnd += 2;
60 if(lEnd < asText.size() && LuxIsWordChar(asText[lEnd])) return 0;
61 return lEnd - alPos;
62}
63
64/** The text split into plain and keyword runs, in order, covering all of it. */
65inline std::vector<cLuxTextSpan> LuxKeywordSpans(const std::wstring& asText, const std::vector<std::wstring>& avKeywords)
66{
67 std::vector<cLuxTextSpan> vSpans;
68 size_t lPlain = 0, i = 0;
69 while(i < asText.size())
70 {
71 size_t lLen = 0;
72 for(size_t k = 0; k < avKeywords.size() && lLen == 0; ++k) lLen = LuxKeywordMatchAt(asText, i, avKeywords[k]);
73 if(lLen == 0) { ++i; continue; }
74 if(i > lPlain) { cLuxTextSpan p = { lPlain, i - lPlain, false }; vSpans.push_back(p); }
75 cLuxTextSpan kw = { i, lLen, true };
76 vSpans.push_back(kw);
77 i += lLen;
78 lPlain = i;
79 }
80 if(lPlain < asText.size()) { cLuxTextSpan p = { lPlain, asText.size() - lPlain, false }; vSpans.push_back(p); }
81 return vSpans;
82}
83
84#endif
bool LuxLongerFirst(const std::wstring &a, const std::wstring &b)
Definition LuxKeywords.h:29
wchar_t LuxFoldCase(wchar_t c)
Definition LuxKeywords.h:27
std::vector< std::wstring > LuxParseKeywords(const std::wstring &asList)
"fuse box| winch |" -> {"fuse box", "winch"}, longest first.
Definition LuxKeywords.h:32
size_t LuxKeywordMatchAt(const std::wstring &asText, size_t alPos, const std::wstring &asWord)
Length of the keyword match at alPos (0: none).
Definition LuxKeywords.h:50
bool LuxIsWordChar(wchar_t c)
Definition LuxKeywords.h:21
std::vector< cLuxTextSpan > LuxKeywordSpans(const std::wstring &asText, const std::vector< std::wstring > &avKeywords)
The text split into plain and keyword runs, in order, covering all of it.
Definition LuxKeywords.h:65
size_t mlStart
Definition LuxKeywords.h:16
size_t mlLength
Definition LuxKeywords.h:17