-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSynapse.cpp
More file actions
223 lines (203 loc) Β· 11.5 KB
/
Copy pathSynapse.cpp
File metadata and controls
223 lines (203 loc) Β· 11.5 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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include "Synapse.h"
#include <QDir>
#include <QFile>
#include <QHash>
#include <QSet>
#include <QStringList>
#include <QRegularExpression>
#include <algorithm>
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// Estrazione delle "sinapsi" (entitΓ ) da un fatto. Ogni entitΓ Γ¨ un canale che
// collega fatti diversi. Pesi: un nominativo condiviso lega molto piΓΉ di una
// parola comune.
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
namespace {
// Parole italiane troppo comuni per fare da sinapsi (non discriminano nulla).
const QSet<QString>& stop() {
static const QSet<QString> s = {
"che","chi","come","dove","quando","perche","quale","quali","cosa","con","per","tra","fra",
"del","dello","della","dei","degli","delle","dal","dallo","dalla","dai","dagli","dalle",
"nel","nello","nella","nei","negli","nelle","sul","sullo","sulla","sui","sugli","sulle",
"uno","una","gli","mio","mia","miei","mie","tuo","tua","suo","sua","loro","questo","questa",
"sono","sei","siamo","siete","essere","stato","stata","avere","fatto","fatta","detto",
"molto","poco","piu","meno","anche","ancora","sempre","mai","gia","oggi","ieri","domani",
"bene","male","cosi","ecco","solo","ogni","tutto","tutti","tutte","altro","altra","altri",
"verso","dopo","prima","sopra","sotto","dentro","fuori","contro","senza","circa","quasi" };
return s;
}
struct Ent { QString key; int weight; };
// Estrae le entitΓ (con peso) da un testo: nominativi, bande/frequenze, modi,
// [[wikilink]] e parole-contenuto. Le chiavi sono normalizzate (minuscole/UPPER).
QList<Ent> entities(const QString& textIn) {
QList<Ent> out;
QSet<QString> seen;
auto add = [&](const QString& k, int w) {
if (k.isEmpty() || seen.contains(k)) return;
seen.insert(k); out.append({k, w});
};
const QString text = textIn;
// Nominativi VERI (stessa forma del motore d'intenti: prefisso + cifra + lettera).
static const QRegularExpression reCall(
QStringLiteral("\\b([A-Za-z]{0,3}[0-9][A-Za-z][A-Za-z0-9/]*)\\b"));
auto it = reCall.globalMatch(text);
while (it.hasNext()) add("call:" + it.next().captured(1).toUpper(), 5);
// Modi digitali/analogici noti.
static const QRegularExpression reMode(
QStringLiteral("\\b(FT8|FT4|FT2|CW|SSB|USB|LSB|AM|FM|RTTY|PSK31|PSK|JS8|SSTV)\\b"),
QRegularExpression::CaseInsensitiveOption);
auto im = reMode.globalMatch(text);
while (im.hasNext()) add("mode:" + im.next().captured(1).toUpper(), 3);
// Bande in metri (40m, 20 m, "20 metri") e frequenze MHz (14.074, 7.074).
static const QRegularExpression reBand(
QStringLiteral("\\b(\\d{1,3})\\s?(?:m|metri)\\b"), QRegularExpression::CaseInsensitiveOption);
auto ib = reBand.globalMatch(text);
while (ib.hasNext()) add("band:" + ib.next().captured(1) + "m", 3);
static const QRegularExpression reFreq(QStringLiteral("\\b(\\d{1,3}\\.\\d{2,3})\\b"));
auto ifq = reFreq.globalMatch(text);
while (ifq.hasNext()) add("qrg:" + ifq.next().captured(1), 3);
// [[wikilink]] espliciti = sinapsi forti volute dall'utente.
static const QRegularExpression reLink(QStringLiteral("\\[\\[([^\\]]+)\\]\\]"));
auto il = reLink.globalMatch(text);
while (il.hasNext()) add("link:" + il.next().captured(1).trimmed().toLower(), 5);
// Parole-contenuto (>=4 lettere, non stopword): sinapsi deboli ma utili.
static const QRegularExpression reWord(QStringLiteral("[A-Za-zΓ-ΓΏ]{4,}"));
auto iw = reWord.globalMatch(text);
while (iw.hasNext()) {
const QString w = iw.next().captured(0).toLower();
if (!stop().contains(w)) add("w:" + w, 1);
}
return out;
}
// Toglie il prefisso "- [2026-06-23] " lasciando il fatto nudo.
QString stripFactMarkup(QString l) {
l = l.trimmed();
if (l.startsWith(QStringLiteral("- "))) l = l.mid(2).trimmed();
static const QRegularExpression reDate(QStringLiteral("^\\[\\d{4}-\\d{2}-\\d{2}\\]\\s*"));
l.remove(reDate);
return l.trimmed();
}
} // namespace
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
QString synapseRecall(const QString& vaultPath, const QString& query,
int maxFacts, int maxChars) {
QDir d(vaultPath);
if (!d.exists()) return QString();
// 1) Carica tutti i FATTI (righe "- β¦") da ogni nota .md del vault β nodi.
QStringList facts; // testo del fatto (nudo)
QList<QList<Ent>> factEnt; // entitΓ di ciascun fatto
const QStringList files = d.entryList(QStringList{QStringLiteral("*.md")}, QDir::Files);
for (const QString& fn : files) {
QFile f(d.filePath(fn));
if (!f.open(QIODevice::ReadOnly | QIODevice::Text)) continue;
const QString title = fn.chopped(3); // il titolo della nota Γ¨ una sinapsi del file
const QStringList lines = QString::fromUtf8(f.readAll()).split('\n');
f.close();
for (const QString& raw : lines) {
if (!raw.trimmed().startsWith(QStringLiteral("- "))) continue;
const QString fact = stripFactMarkup(raw);
if (fact.size() < 3) continue;
QList<Ent> es = entities(fact + ' ' + title);
facts.append(fact);
factEnt.append(es);
}
}
if (facts.isEmpty()) return QString();
// 2) Indice inverso entitΓ β fatti (Γ¨ il grafo: due fatti che condividono
// un'entitΓ sono collegati da quella sinapsi).
QHash<QString, QList<int>> byEnt;
QHash<QString, int> entW;
for (int i = 0; i < factEnt.size(); ++i)
for (const Ent& e : factEnt[i]) { byEnt[e.key].append(i); entW[e.key] = e.weight; }
// 3) SEED: attiva i fatti che condividono entitΓ con la domanda (peso entitΓ ).
const QList<Ent> q = entities(query);
QVector<double> seed(facts.size(), 0.0);
QSet<QString> qkeys;
for (const Ent& e : q) qkeys.insert(e.key);
for (const Ent& e : q)
for (int fi : byEnt.value(e.key)) seed[fi] += e.weight;
bool anySeed = false;
for (double s : seed) if (s > 0) { anySeed = true; break; }
if (!anySeed) return QString(); // niente di pertinente: non iniettare nulla
// 4) SPREADING ACTIVATION (1 salto, decadimento 0.5): l'energia dei fatti
// accesi rifluisce nelle loro entità e da lì accende i fatti collegati,
// anche se NON combaciano direttamente con la domanda. Γ l'associazione.
const double decay = 0.5;
QHash<QString, double> entAct; // attivazione accumulata da ogni sinapsi
for (int i = 0; i < facts.size(); ++i)
if (seed[i] > 0)
for (const Ent& e : factEnt[i]) entAct[e.key] += seed[i];
QVector<double> total = seed;
for (int i = 0; i < facts.size(); ++i)
for (const Ent& e : factEnt[i]) {
// il fatto riceve dalle proprie sinapsi l'energia degli ALTRI fatti
double in = entAct.value(e.key) - seed[i]; // togli il proprio contributo
if (in > 0) total[i] += decay * in * (e.weight / 5.0);
}
// 5) Classifica e prendi i migliori entro il budget. Bonus leggero ai fatti
// che combaciano direttamente (per non farli scavalcare dai soli "vicini").
QVector<int> idx;
for (int i = 0; i < facts.size(); ++i) if (total[i] > 0) idx.append(i);
std::sort(idx.begin(), idx.end(), [&](int a, int b) { return total[a] > total[b]; });
QString out; int n = 0, chars = 0;
for (int i : idx) {
const QString line = hamCompact(facts[i]);
if (chars + line.size() + 2 > maxChars && n > 0) break;
out += "- " + line + '\n';
chars += line.size() + 2;
if (++n >= maxFacts) break;
}
return out.trimmed();
}
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// hamCompact β il "morse/alfabeto" che accorcia (in realtΓ Huffman/Q-code).
// Sostituisce frasi verbose con il loro codice ham che il modello giΓ conosce.
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
QString hamCompact(const QString& textIn) {
QString t = textIn;
// Dizionario phraseβcodice (ordinato: prima le frasi lunghe). Case-insensitive,
// confine di parola, così non si spezzano parole più grandi.
static const QList<QPair<QString, QString>> dict = {
{QStringLiteral("la mia posizione e"), QStringLiteral("QTH")},
{QStringLiteral("mia posizione"), QStringLiteral("QTH")},
{QStringLiteral("il mio locatore"), QStringLiteral("QTH")},
{QStringLiteral("rapporto segnale"), QStringLiteral("RST")},
{QStringLiteral("conferma del qso"), QStringLiteral("QSL")},
{QStringLiteral("confermo"), QStringLiteral("QSL")},
{QStringLiteral("ricevuto"), QStringLiteral("QSL")},
{QStringLiteral("nominativo"), QStringLiteral("call")},
{QStringLiteral("frequenza"), QStringLiteral("QRG")},
{QStringLiteral("interferenza"), QStringLiteral("QRM")},
{QStringLiteral("disturbi atmosferici"),QStringLiteral("QRN")},
{QStringLiteral("trasmissione"), QStringLiteral("TX")},
{QStringLiteral("ricezione"), QStringLiteral("RX")},
{QStringLiteral("collegamento"), QStringLiteral("QSO")},
{QStringLiteral("contatto"), QStringLiteral("QSO")},
{QStringLiteral("chiamata generale"), QStringLiteral("CQ")},
{QStringLiteral("propagazione"), QStringLiteral("prop")},
{QStringLiteral("condizioni"), QStringLiteral("cnd")},
{QStringLiteral("potenza"), QStringLiteral("PWR")},
{QStringLiteral("antenna"), QStringLiteral("ant")},
{QStringLiteral("saluti"), QStringLiteral("73")},
{QStringLiteral("per favore"), QStringLiteral("pse")},
{QStringLiteral("grazie"), QStringLiteral("tnx")},
};
for (const auto& kv : dict) {
QRegularExpression re(QStringLiteral("\\b") + QRegularExpression::escape(kv.first)
+ QStringLiteral("\\b"),
QRegularExpression::CaseInsensitiveOption);
t.replace(re, kv.second);
}
// Toglie il prefisso-data se presente e comprime gli spazi.
static const QRegularExpression reDate(QStringLiteral("\\[\\d{4}-\\d{2}-\\d{2}\\]\\s*"));
t.remove(reDate);
t.replace(QRegularExpression(QStringLiteral("\\s+")), QStringLiteral(" "));
return t.trimmed();
}
// Stima dei token: parole + simboli di punteggiatura (β tokenizer subword).
int approxTokens(const QString& text) {
static const QRegularExpression re(QStringLiteral("[A-Za-zΓ-ΓΏ0-9]+|[^\\sA-Za-zΓ-ΓΏ0-9]"));
int n = 0;
auto it = re.globalMatch(text);
while (it.hasNext()) { it.next(); ++n; }
return n;
}