Skip to content

Commit 50bcd79

Browse files
add functions
1 parent 8cb67eb commit 50bcd79

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

code/logic/fossil/ai/jellyfish.h

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,38 @@ void fossil_jellyfish_dump(const fossil_jellyfish_chain *chain);
110110
*/
111111
void fossil_jellyfish_hash(const char *input, const char *output, uint8_t *hash_out);
112112

113+
/**
114+
* Save the jellyfish chain to a file.
115+
* This serializes the chain to a file for persistence.
116+
*
117+
* @param chain Pointer to the jellyfish chain to save.
118+
* @param filepath The path to the file where the chain will be saved.
119+
* @return 0 on success, non-zero on failure.
120+
*/
121+
int fossil_jellyfish_save(const fossil_jellyfish_chain *chain, const char *filepath);
122+
123+
/**
124+
* Load a jellyfish chain from a file.
125+
* This deserializes the chain from a file.
126+
*
127+
* @param chain Pointer to the jellyfish chain to load.
128+
* @param filepath The path to the file from which the chain will be loaded.
129+
* @return 0 on success, non-zero on failure.
130+
*/
131+
int fossil_jellyfish_load(fossil_jellyfish_chain *chain, const char *filepath);
132+
133+
/**
134+
* Fuzzy reasoning for jellyfish AI.
135+
* This function attempts to find a close match for the input string
136+
* and returns the corresponding output if found.
137+
*
138+
* @param chain Pointer to the jellyfish chain.
139+
* @param input The input string to reason about.
140+
* @return The output string if a close match is found, or "Unknown" if not found.
141+
*/
142+
const char* fossil_jellyfish_reason_fuzzy(fossil_jellyfish_chain *chain, const char *input);
143+
144+
113145
#ifdef __cplusplus
114146
}
115147
#include <stdexcept>
@@ -209,6 +241,41 @@ namespace ai {
209241
fossil_jellyfish_init(&chain);
210242
}
211243

244+
/**
245+
* Fuzzy reasoning for jellyfish AI.
246+
* This function attempts to find a close match for the input string
247+
* and returns the corresponding output if found.
248+
*
249+
* @param input The input string to reason about.
250+
* @return The output string if a close match is found, or "Unknown" if not found.
251+
*/
252+
std::string reason_fuzzy(const std::string &input) {
253+
const char *result = fossil_jellyfish_reason_fuzzy(&chain, input.c_str());
254+
return std::string(result);
255+
}
256+
257+
/**
258+
* Save the jellyfish chain to a file.
259+
* This serializes the chain to a file for persistence.
260+
*
261+
* @param filepath The path to the file where the chain will be saved.
262+
* @return 0 on success, non-zero on failure.
263+
*/
264+
int save(const std::string &filepath) const {
265+
return fossil_jellyfish_save(&chain, filepath.c_str());
266+
}
267+
268+
/**
269+
* Load a jellyfish chain from a file.
270+
* This deserializes the chain from a file.
271+
*
272+
* @param filepath The path to the file from which the chain will be loaded.
273+
* @return 0 on success, non-zero on failure.
274+
*/
275+
int load(const std::string &filepath) {
276+
return fossil_jellyfish_load(&chain, filepath.c_str());
277+
}
278+
212279
private:
213280
fossil_jellyfish_chain chain; // The jellyfish chain instance
214281
};

code/logic/jellyfish.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,51 @@ void fossil_jellyfish_dump(const fossil_jellyfish_chain *chain) {
9595
printf("\n");
9696
}
9797
}
98+
99+
int fossil_jellyfish_save(const fossil_jellyfish_chain *chain, const char *filepath) {
100+
FILE *fp = fopen(filepath, "wb");
101+
if (!fp) return 0;
102+
103+
fwrite(&chain->count, sizeof(size_t), 1, fp);
104+
fwrite(chain->memory, sizeof(fossil_jellyfish_block), chain->count, fp);
105+
fclose(fp);
106+
return 1;
107+
}
108+
109+
int fossil_jellyfish_load(fossil_jellyfish_chain *chain, const char *filepath) {
110+
FILE *fp = fopen(filepath, "rb");
111+
if (!fp) return 0;
112+
113+
fread(&chain->count, sizeof(size_t), 1, fp);
114+
if (chain->count > FOSSIL_JELLYFISH_MAX_MEM) chain->count = FOSSIL_JELLYFISH_MAX_MEM;
115+
116+
fread(chain->memory, sizeof(fossil_jellyfish_block), chain->count, fp);
117+
fclose(fp);
118+
return 1;
119+
}
120+
121+
// Simple Levenshtein-like distance (cost is # of mismatched chars)
122+
static int fossil_jellyfish_similarity(const char *a, const char *b) {
123+
int score = 0;
124+
for (int i = 0; a[i] && b[i]; ++i) {
125+
if (a[i] != b[i]) score++;
126+
}
127+
return score;
128+
}
129+
130+
const char* fossil_jellyfish_reason_fuzzy(fossil_jellyfish_chain *chain, const char *input) {
131+
int best_score = 1000;
132+
const char *best_output = "Unknown";
133+
134+
for (size_t i = 0; i < chain->count; ++i) {
135+
if (!chain->memory[i].valid) continue;
136+
137+
int score = fossil_jellyfish_similarity(input, chain->memory[i].input);
138+
if (score < best_score) {
139+
best_score = score;
140+
best_output = chain->memory[i].output;
141+
}
142+
}
143+
144+
return best_output;
145+
}

0 commit comments

Comments
 (0)