Skip to content

Commit

Permalink
refa: quote with context
Browse files Browse the repository at this point in the history
  • Loading branch information
Myllaume committed Oct 1, 2024
1 parent 957e3ee commit 41a5f22
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 45 deletions.
17 changes: 1 addition & 16 deletions core/models/bibliography.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,28 +62,13 @@ class Bibliography {

/**
* @param {string[]} quotesId
* @returns {BibliographicRecord[]}
*/

static getBibliographicRecordsFromList(quotesId = []) {
return quotesId.map((quoteId, index) => {
return {
quotesExtract: {
citationItems: [
{
prefix: '',
suffix: '',
id: quoteId,
label: 'page',
locator: '',
'suppress-author': false,
},
],
properties: { noteIndex: index + 1 },
},
text: '',
contexts: [],
ids: new Set([quoteId]),
id: quoteId,
};
});
}
Expand Down
42 changes: 13 additions & 29 deletions core/models/cosmoscope.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import Bibliography from './bibliography.js';
import Report from './report.js';
import { read as readYmlFm } from '../utils/yamlfrontmatter.js';
import { ReadCsvFileNodesError, ReadCsvFileLinksError, ReadCsvLinesLinksError } from './errors.js';
import quoteIdsWithContexts from '../utils/quoteIdsWithContexts.js';

/**
* @typedef File
Expand Down Expand Up @@ -313,31 +314,23 @@ class Cosmoscope extends Graph {

for (const file of files) {
const bibliographicRecords = [
...Bibliography.getBibliographicRecordsFromText(file.content),
...quoteIdsWithContexts(file.content),
...Bibliography.getBibliographicRecordsFromList(file.metas.references),
];

const fileId = file.metas['id'] || file.metas['title'].toLowerCase();

bibliographicRecords.forEach(({ ids, contexts }) => {
for (const id of ids) {
if (!bibliography.library[id]) continue;
bibliographicRecords.forEach(({ id, contexts }) => {
if (!bibliography.library[id]) return;

if (referenceRecords.has(id)) {
const ref = referenceRecords.get(id);
ref.targets.add(fileId);

if (ref.contexts.has(fileId)) {
ref.contexts.get(fileId).push(...contexts);
} else {
ref.contexts.set(fileId, contexts);
}
} else {
referenceRecords.set(id, {
contexts: new Map([[fileId, contexts]]),
targets: new Set([fileId]),
});
}
if (referenceRecords.has(id)) {
const ref = referenceRecords.get(id);
ref.targets.add(fileId);
} else {
referenceRecords.set(id, {
contexts,
targets: new Set([fileId]),
});
}
});
}
Expand All @@ -349,16 +342,7 @@ class Cosmoscope extends Graph {
);
Array.from(targets).forEach((id) =>
links.push(
new Link(
undefined,
Array.from(new Set(contexts.get(id))),
'undefined',
undefined,
undefined,
undefined,
id,
key,
),
new Link(undefined, contexts, 'undefined', undefined, undefined, undefined, id, key),
),
);
});
Expand Down
32 changes: 32 additions & 0 deletions core/utils/quoteIdsWithContexts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import extractCitations from './citeExtractor';
import extractParaphs from './paraphExtractor';

/**
*
* @param {string} markdown
* @returns {any[]}
*/

export default function quoteIdsWithContexts(markdown) {
let quotes = {};

extractParaphs(markdown).forEach((paraph) => {
extractCitations(paraph).forEach((result) => {
result.citations.forEach(({ id }) => {
if (quotes[id]) {
quotes[id].contexts.add(paraph);
} else {
quotes[id] = {
contexts: new Set([paraph]),
id,
};
}
});
});
});

return Object.values(quotes).map((quote) => ({
...quote,
contexts: Array.from(quote.contexts),
}));
}
44 changes: 44 additions & 0 deletions core/utils/quoteIdsWithContexts.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import quotesWithContexts from './quotesWithContexts';

describe('quotesWithContexts', () => {
it('should parse one quote per paragraph', () => {
const text = `First paragraph quoting @smith04.
Second paragraph quoting @doe99.`;

const result = quotesWithContexts(text);

expect(result).toEqual([
expect.objectContaining({
contexts: ['First paragraph quoting @smith04.'],
id: 'smith04',
}),
expect.objectContaining({
contexts: ['Second paragraph quoting @doe99.'],
id: 'doe99',
}),
]);
});

it('should parse many quotes per paragraph', () => {
const text = `First paragraph quoting @smith04.
Second paragraph quoting @smith04 and @doe99. @doe99 is quoted once.`;

const result = quotesWithContexts(text);

expect(result).toEqual([
expect.objectContaining({
contexts: [
'First paragraph quoting @smith04.',
'Second paragraph quoting @smith04 and @doe99. @doe99 is quoted once.',
],
id: 'smith04',
}),
expect.objectContaining({
contexts: ['Second paragraph quoting @smith04 and @doe99. @doe99 is quoted once.'],
id: 'doe99',
}),
]);
});
});

0 comments on commit 41a5f22

Please sign in to comment.