-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtask-placement.ts
More file actions
158 lines (141 loc) · 4.64 KB
/
Copy pathtask-placement.ts
File metadata and controls
158 lines (141 loc) · 4.64 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
import { normalizeHeadingName } from "./headings.js";
type ItemType = "to-do" | "project" | "heading";
type ItemStatus = "incomplete" | "canceled" | "completed";
type StartBucket = "Inbox" | "Anytime" | "Someday" | "";
type ChecklistItem = {
id: string;
title: string;
status: ItemStatus;
stopDate: string | null;
};
type TaskLike = {
id: string;
type: ItemType;
title: string;
status: ItemStatus;
trashed: boolean;
notes: string;
start: StartBucket;
startDate: string | null;
deadline: string | null;
deadlineSuppressed: boolean;
stopDate: string | null;
created: string | null;
modified: string | null;
areaId: string | null;
areaTitle: string | null;
projectId: string | null;
projectTitle: string | null;
headingId: string | null;
headingTitle: string | null;
tags: string[];
checklist: ChecklistItem[];
index: number;
todayIndex: number;
};
const STOPWORDS = new Set([
"de", "la", "el", "los", "las", "y", "en", "para", "con", "por", "del",
"the", "and", "for", "with", "to", "a", "an", "of", "on", "or"
]);
function tokenize(value: string) {
return value
.toLowerCase()
.normalize("NFD")
.replace(/[\u0300-\u036f]/g, "")
.replace(/[^a-z0-9\s]/g, " ")
.split(/\s+/)
.filter((token) => token.length > 1 && !STOPWORDS.has(token));
}
function buildHeadingCorpus(headings: TaskLike[], todos: TaskLike[]) {
return headings.map((heading) => {
const ownTodos = todos.filter((todo) => todo.headingId === heading.id);
const tokens = new Set<string>([
...tokenize(heading.title),
...tokenize(normalizeHeadingName(heading.title)),
]);
for (const todo of ownTodos) {
for (const token of tokenize(todo.title)) {
tokens.add(token);
}
}
return {
headingId: heading.id,
headingTitle: heading.title,
ownTodoCount: ownTodos.length,
tokens,
};
});
}
export function suggestTaskPlacement(input: {
projectTitle: string;
headings: TaskLike[];
todos: TaskLike[];
taskTitles: string[];
}) {
const corpora = buildHeadingCorpus(input.headings, input.todos);
const placements = input.taskTitles.map((taskTitle) => {
const taskTokens = tokenize(taskTitle);
const scored = corpora
.map((heading) => {
let score = 0;
const matches: string[] = [];
for (const token of taskTokens) {
if (heading.tokens.has(token)) {
score += 3;
matches.push(token);
}
}
const normalizedHeading = normalizeHeadingName(heading.headingTitle);
if (taskTitle.toLowerCase().includes(normalizedHeading)) {
score += 4;
matches.push(heading.headingTitle);
}
if (heading.ownTodoCount > 0 && score > 0) {
score += 1;
}
return {
headingId: heading.headingId,
headingTitle: heading.headingTitle,
score,
matches: [...new Set(matches)],
};
})
.sort((a, b) => b.score - a.score || a.headingTitle.localeCompare(b.headingTitle));
const best = scored[0];
const second = scored[1];
const ambiguous = Boolean(best && second && best.score > 0 && best.score === second.score);
const confident = Boolean(best && best.score >= 3 && !ambiguous);
return {
taskTitle,
suggestedHeadingId: confident ? best.headingId : null,
suggestedHeadingTitle: confident ? best.headingTitle : null,
confidence: confident ? (best.score >= 6 ? "high" : "medium") : "low",
ambiguous,
reason: confident
? `Coincide mejor con ${best.headingTitle}${best.matches.length ? ` por: ${best.matches.join(", ")}` : ""}.`
: ambiguous
? "La tarea podría encajar en más de un heading existente."
: "No hay una coincidencia semántica suficientemente clara con los headings existentes.",
alternatives: scored
.filter((entry) => entry.score > 0)
.slice(0, 3)
.map(({ headingId, headingTitle, score }) => ({ headingId, headingTitle, score })),
};
});
return {
projectTitle: input.projectTitle,
headings: corpora.map(({ headingId, headingTitle, ownTodoCount }) => ({
headingId,
headingTitle,
ownTodoCount,
})),
placements,
summary: {
totalTasks: placements.length,
confidentlyPlaced: placements.filter((entry) => entry.suggestedHeadingId).length,
needsReview: placements.filter((entry) => !entry.suggestedHeadingId).length,
},
guidance:
"Usa estas sugerencias para reutilizar los headings existentes del proyecto. Si una tarea queda con confianza baja o ambigua, conviene confirmar con el usuario antes de moverla.",
};
}