Skip to content

Commit 046f78e

Browse files
committed
Update .gitignore and fix filter condition in compile-quotes.js
1 parent 89b171e commit 046f78e

4 files changed

+86
-2
lines changed

.DS_Store

0 Bytes
Binary file not shown.

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ kindle-highlights
88
kindle-export-supabase.js
99
ocr
1010
note_exports
11-
alfred-searches
11+
alfred-searches
12+
*.csv

compile-quotes.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ const main = async () => {
240240
const assignments = kmeans(
241241
quotes
242242
.map((quote) => JSON.parse(quote.embedding))
243-
.filter((quote) => quote.length === 1536),
243+
.filter((embedding) => embedding.length === 1536),
244244
k
245245
);
246246

get-random-highlights-pl-spec.js

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// get random highlights of arg value number and put them in format { data , metadata }
2+
import { fetchRandomHighlight } from "./get-random-highlight.js";
3+
import fs from "fs";
4+
5+
const convertToDataMetadata = async (amount, embeddings = false) => {
6+
const highlights = await fetchRandomHighlight(amount, true);
7+
const data = highlights.map((highlight) => highlight.text);
8+
const metadata = highlights.map((highlight) => {
9+
return {
10+
title: highlight.book.title,
11+
author: highlight.book.author,
12+
book_id: highlight.book.book_id,
13+
cover_image_url: highlight.book.cover_image_url,
14+
readwise_url: highlight.readwise_url,
15+
question: highlight.question,
16+
thoughts: highlight.thoughts,
17+
};
18+
});
19+
20+
if (embeddings) {
21+
const embeddings = highlights.map((highlight) => highlight.embedding);
22+
return data.map((data, index) => {
23+
return { data: data, metadata: metadata[index], embedding: embeddings[index] };
24+
});
25+
}
26+
27+
// zip data and metadata into an object and return it as an array
28+
const result = data.map((data, index) => {
29+
return { data: data, metadata: metadata[index] };
30+
});
31+
32+
return result;
33+
};
34+
35+
// write to csv file with headers: data, metadata
36+
const saveAsCSV = async (amount, embeddings = false) => {
37+
const dataMetadata = await convertToDataMetadata(amount, embeddings);
38+
39+
if (embeddings) {
40+
const csv = dataMetadata.map((row) => {
41+
const cleanData = row.data.replace(/,/g, "");
42+
const cleanMetadata = JSON.stringify(row.metadata).replace(/,/g, "");
43+
return `${cleanData},${JSON.stringify(cleanMetadata)},${row.embedding}`;
44+
});
45+
46+
// add headers
47+
csv.unshift("data,metadata,embedding");
48+
49+
const csvString = csv.join("\n");
50+
51+
fs.writeFile("highlights.csv", csvString, (err) => {
52+
if (err) {
53+
console.error(err);
54+
return;
55+
}
56+
console.log("File has been created");
57+
});
58+
59+
return;
60+
}
61+
62+
const csv = dataMetadata.map((row) => {
63+
const cleanData = row.data.replace(/,/g, "");
64+
const cleanMetadata = JSON.stringify(row.metadata).replace(/,/g, "");
65+
return `${cleanData},${JSON.stringify(cleanMetadata)}`;
66+
});
67+
68+
// add headers
69+
csv.unshift("data,metadata");
70+
71+
const csvString = csv.join("\n");
72+
73+
fs.writeFile("highlights.csv", csvString, (err) => {
74+
if (err) {
75+
console.error(err);
76+
return;
77+
}
78+
console.log("File has been created");
79+
});
80+
};
81+
82+
const amount = 10;
83+
saveAsCSV(amount);

0 commit comments

Comments
 (0)