Skip to content

Commit 33eda62

Browse files
Update parser.c
1 parent 3b7864d commit 33eda62

File tree

1 file changed

+51
-12
lines changed

1 file changed

+51
-12
lines changed

code/logic/parser.c

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,27 @@ extern char *_custom_strdup(const char *str) {
3838
}
3939

4040
// ==================================================================
41-
// AI magic tricks
41+
// TI Reasoning Metadata (lightweight struct for audit/debug)
4242
// ==================================================================
43+
typedef struct {
44+
const char *input;
45+
const char *suggested;
46+
int edit_distance;
47+
float confidence_score; // 0.0 to 1.0
48+
const char *reason;
49+
} fossil_ti_reason_t;
4350

44-
// Function to calculate Levenshtein Distance
51+
// ==================================================================
52+
// Levenshtein Distance (Unchanged Core)
53+
// ==================================================================
4554
int levenshtein_distance(const char *s1, const char *s2) {
4655
int len1 = strlen(s1), len2 = strlen(s2);
4756
int i, j;
48-
49-
// Allocate the dp array dynamically
5057
int **dp = (int **)malloc((len1 + 1) * sizeof(int *));
5158
if (!dp) return INT_MAX;
5259
for (i = 0; i <= len1; i++) {
5360
dp[i] = (int *)malloc((len2 + 1) * sizeof(int));
5461
if (!dp[i]) {
55-
// Free previously allocated rows
5662
for (j = 0; j < i; j++) free(dp[j]);
5763
free(dp);
5864
return INT_MAX;
@@ -73,27 +79,47 @@ int levenshtein_distance(const char *s1, const char *s2) {
7379
dp[i][j] = min;
7480
}
7581
}
82+
7683
int result = dp[len1][len2];
7784
for (i = 0; i <= len1; i++) free(dp[i]);
7885
free(dp);
7986
return result;
8087
}
8188

82-
// Function to find the closest command match
83-
const char* suggest_command(const char *input, fossil_io_parser_palette_t *palette) {
89+
// ==================================================================
90+
// TI-Aware Command Suggestion (with traceable reasoning)
91+
// ==================================================================
92+
const char* suggest_command_ti(const char *input, fossil_io_parser_palette_t *palette, fossil_ti_reason_t *out_reason) {
8493
fossil_io_parser_command_t *current = palette->commands;
8594
const char *best_match = NULL;
8695
int min_distance = INT_MAX;
96+
int best_length = 1; // Avoid divide-by-zero
8797

8898
while (current) {
8999
int distance = levenshtein_distance(input, current->name);
90100
if (distance < min_distance) {
91101
min_distance = distance;
92102
best_match = current->name;
103+
best_length = strlen(current->name);
93104
}
94105
current = current->next;
95106
}
96-
return (min_distance <= 3) ? best_match : NULL; // Suggest only if close enough
107+
108+
// Compute confidence score based on edit distance
109+
float confidence = 1.0f - ((float)min_distance / (float)best_length);
110+
confidence = (confidence < 0.0f) ? 0.0f : (confidence > 1.0f) ? 1.0f : confidence;
111+
112+
// Optional reasoning trace
113+
if (out_reason) {
114+
out_reason->input = input;
115+
out_reason->suggested = best_match;
116+
out_reason->edit_distance = min_distance;
117+
out_reason->confidence_score = confidence;
118+
out_reason->reason = (confidence >= 0.7f) ? "Close semantic match" : "Low confidence match";
119+
}
120+
121+
// Only return suggestions above a minimum confidence threshold
122+
return (confidence >= 0.7f) ? best_match : NULL;
97123
}
98124

99125
// ==================================================================
@@ -443,12 +469,25 @@ void fossil_io_parser_parse(fossil_io_parser_palette_t *palette, int argc, char
443469
}
444470

445471
if (!command) {
446-
// Suggest a similar command or show an error
447-
const char *suggestion = suggest_command(command_name, palette);
472+
// Use TI-aware suggestion system
473+
fossil_ti_reason_t ti_reason = {0};
474+
const char *suggestion = suggest_command_ti(command_name, palette, &ti_reason);
475+
448476
if (suggestion) {
449-
fossil_io_fprintf(FOSSIL_STDERR, "{red}Unknown command: '%s'. Did you mean '%s'?{reset}\n", command_name, suggestion);
477+
fossil_io_fprintf(FOSSIL_STDERR,
478+
"{red}Unknown command: '%s'. Did you mean '%s'?{reset}\n"
479+
"{yellow}[TI] Suggestion confidence: %.2f | Distance: %d | Reason: %s{reset}\n",
480+
command_name,
481+
suggestion,
482+
ti_reason.confidence_score,
483+
ti_reason.edit_distance,
484+
ti_reason.reason
485+
);
450486
} else {
451-
fossil_io_fprintf(FOSSIL_STDERR, "{red}Unknown command: '%s'. Type '--help' to see available commands.{reset}\n", command_name);
487+
fossil_io_fprintf(FOSSIL_STDERR,
488+
"{red}Unknown command: '%s'. Type '--help' to see available commands.{reset}\n",
489+
command_name
490+
);
452491
}
453492
return;
454493
}

0 commit comments

Comments
 (0)