interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / kernel / svc / allen_notes.c

164 lines · 5297 bytes Raw Blame History
1/* allen_notes.c -- Allen's notes; see allen_notes.h. */
2#include "allen_notes.h"
3
4#include <stddef.h>
5
6/* Small words that say nothing about what a note is about. */
7static const char *const small_words[] = {
8 "the", "an", "and", "or", "but", "is", "are", "was", "were", "be", "been", "am", "me", "my", "mine", "you",
9 "your", "yours", "we", "our", "it", "its", "this", "that", "these", "those", "of", "in", "on", "at", "to",
10 "for", "from", "with", "about", "as", "by", "what", "whats", "where", "when", "who", "which", "why", "how",
11 "do", "does", "did", "have", "has", "had", "can", "could", "would", "should", "will", "there", "here", "then",
12 "than", "so", "not", "no", "yes", "just", "also", "please", "show", "tell", "give", "find", "search", "look",
13 "up", "get", "got", "any", "anything", "something", "thing", "things", "note", "notes", "saved", "save",
14 "remember", "know", "all", "some", "again", "more", "ok", "okay", "hey", "hi", "hello", "allen", "want",
15 "need", "let", "lets", "remind", "pull", "down", "write", "jot", "keep", 0};
16
17static int same_word(const char *a, size_t n, const char *w)
18{
19 size_t i = 0;
20 while (i < n && w[i] && a[i] == w[i])
21 i++;
22 return i == n && !w[i];
23}
24
25static int is_small(const char *w, size_t n)
26{
27 for (const char *const *p = small_words; *p; p++)
28 if (same_word(w, n, *p))
29 return 1;
30 return 0;
31}
32
33static uint32_t hash(const char *w, size_t n)
34{
35 uint32_t h = 2166136261u;
36 for (size_t i = 0; i < n; i++)
37 h = (h ^ (uint8_t)w[i]) * 16777619u;
38 return h ? h : 1;
39}
40
41unsigned ck_allen_key_words(const char *text, uint32_t *word, unsigned max)
42{
43 unsigned k = 0;
44 const char *s = text;
45 while (*s && k < max) {
46 char w[32];
47 size_t n = 0;
48 int digits = 1;
49 /* one word: a run of letters and digits, lowercased; anything else
50 * (apostrophes too: "what's" is "what" "s") ends it */
51 while (*s && !((*s >= 'a' && *s <= 'z') || (*s >= 'A' && *s <= 'Z') || (*s >= '0' && *s <= '9')))
52 s++;
53 while ((*s >= 'a' && *s <= 'z') || (*s >= 'A' && *s <= 'Z') || (*s >= '0' && *s <= '9')) {
54 char c = *s++;
55 if (c >= 'A' && c <= 'Z')
56 c = (char)(c - 'A' + 'a');
57 if (c < '0' || c > '9')
58 digits = 0;
59 if (n < sizeof w)
60 w[n++] = c;
61 }
62 if (!n || n == sizeof w)
63 continue;
64 if (!digits && (n < 3 || is_small(w, n)))
65 continue;
66 if (!digits && n > 3 && w[n - 1] == 's' && w[n - 2] != 's') /* "codes" is "code" */
67 n--;
68 uint32_t h = hash(w, n);
69 unsigned dup = 0;
70 for (unsigned i = 0; i < k; i++)
71 dup |= word[i] == h;
72 if (!dup)
73 word[k++] = h;
74 }
75 return k;
76}
77
78void ck_allen_note_index(ck_allen_note *n)
79{
80 n->nword = (uint8_t)ck_allen_key_words(n->text, n->word, CK_ALLEN_NOTE_WORDS);
81}
82
83int ck_allen_note_set(ck_allen_note *n, const char *text, uint32_t start)
84{
85 while (*text == ' ')
86 text++;
87 size_t len = 0;
88 while (text[len]) {
89 if ((uint8_t)text[len] < 0x20 || (uint8_t)text[len] > 0x7e)
90 return -1;
91 len++;
92 }
93 while (len && text[len - 1] == ' ')
94 len--;
95 if (!len || len >= CK_ALLEN_NOTE_MAX)
96 return -1;
97 char *d = (char *)n;
98 for (size_t i = 0; i < sizeof *n; i++)
99 d[i] = 0;
100 for (size_t i = 0; i < len; i++)
101 n->text[i] = text[i];
102 n->len = (uint16_t)len;
103 n->start = start;
104 ck_allen_note_index(n);
105 return 0;
106}
107
108unsigned ck_allen_note_links(const ck_allen_note *a, const ck_allen_note *b)
109{
110 unsigned k = 0;
111 for (unsigned i = 0; i < a->nword; i++)
112 for (unsigned j = 0; j < b->nword; j++)
113 k += a->word[i] == b->word[j];
114 return k;
115}
116
117unsigned ck_allen_note_degree(const ck_allen_notes *ns, uint32_t i)
118{
119 unsigned d = 0;
120 for (uint32_t j = 0; j < ns->n; j++)
121 d += j != i && !ns->note[j].gone && ck_allen_note_links(&ns->note[i], &ns->note[j]) > 0;
122 return d;
123}
124
125unsigned ck_allen_notes_live(const ck_allen_notes *ns)
126{
127 unsigned k = 0;
128 for (uint32_t i = 0; i < ns->n; i++)
129 k += !ns->note[i].gone;
130 return k;
131}
132
133int ck_allen_notes_best(const ck_allen_notes *ns, const char *question, int strict, unsigned *also)
134{
135 uint32_t q[CK_ALLEN_NOTE_WORDS];
136 unsigned nq = ck_allen_key_words(question, q, CK_ALLEN_NOTE_WORDS), best_score = 0, count = 0;
137 int best = -1;
138 if (also)
139 *also = 0;
140 if (!ns || !nq)
141 return -1;
142 for (uint32_t i = 0; i < ns->n; i++) {
143 const ck_allen_note *n = &ns->note[i];
144 if (n->gone)
145 continue;
146 unsigned score = 0;
147 for (unsigned a = 0; a < nq; a++)
148 for (unsigned b = 0; b < n->nword; b++)
149 if (q[a] == n->word[b]) {
150 score++;
151 break;
152 }
153 if (!score || (strict ? score < nq : score * 2 < nq))
154 continue;
155 count++;
156 if (score >= best_score) { /* >=: the newest wins a tie */
157 best_score = score;
158 best = (int)i;
159 }
160 }
161 if (also && count)
162 *also = count - 1;
163 return best;
164}