@bendyline/squisq 0.1.1 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +85 -0
- package/dist/{themeLibrary-DySHPcgj.d.ts → Doc-1hmT1LZl.d.ts} +1 -42
- package/dist/__tests__/animationUtils.test.d.ts +2 -0
- package/dist/__tests__/animationUtils.test.d.ts.map +1 -0
- package/dist/__tests__/animationUtils.test.js +60 -0
- package/dist/__tests__/animationUtils.test.js.map +1 -0
- package/dist/__tests__/contentExtractor.test.d.ts +2 -0
- package/dist/__tests__/contentExtractor.test.d.ts.map +1 -0
- package/dist/__tests__/contentExtractor.test.js +151 -0
- package/dist/__tests__/contentExtractor.test.js.map +1 -0
- package/dist/__tests__/geohash.test.d.ts +2 -0
- package/dist/__tests__/geohash.test.d.ts.map +1 -0
- package/dist/__tests__/geohash.test.js +87 -0
- package/dist/__tests__/geohash.test.js.map +1 -0
- package/dist/__tests__/getLayers.test.d.ts +2 -0
- package/dist/__tests__/getLayers.test.d.ts.map +1 -0
- package/dist/__tests__/getLayers.test.js +167 -0
- package/dist/__tests__/getLayers.test.js.map +1 -0
- package/dist/__tests__/haversine.test.d.ts +2 -0
- package/dist/__tests__/haversine.test.d.ts.map +1 -0
- package/dist/__tests__/haversine.test.js +56 -0
- package/dist/__tests__/haversine.test.js.map +1 -0
- package/dist/__tests__/localForageAdapter.test.d.ts +8 -0
- package/dist/__tests__/localForageAdapter.test.d.ts.map +1 -0
- package/dist/__tests__/localForageAdapter.test.js +130 -0
- package/dist/__tests__/localForageAdapter.test.js.map +1 -0
- package/dist/__tests__/markdown.test.d.ts +2 -0
- package/dist/__tests__/markdown.test.d.ts.map +1 -0
- package/dist/__tests__/markdown.test.js +654 -0
- package/dist/__tests__/markdown.test.js.map +1 -0
- package/dist/__tests__/markdownToDoc.test.d.ts +2 -0
- package/dist/__tests__/markdownToDoc.test.d.ts.map +1 -0
- package/dist/__tests__/markdownToDoc.test.js +313 -0
- package/dist/__tests__/markdownToDoc.test.js.map +1 -0
- package/dist/__tests__/seededRandom.test.d.ts +2 -0
- package/dist/__tests__/seededRandom.test.d.ts.map +1 -0
- package/dist/__tests__/seededRandom.test.js +125 -0
- package/dist/__tests__/seededRandom.test.js.map +1 -0
- package/dist/__tests__/slideshowGenerator.test.d.ts +2 -0
- package/dist/__tests__/slideshowGenerator.test.d.ts.map +1 -0
- package/dist/__tests__/slideshowGenerator.test.js +114 -0
- package/dist/__tests__/slideshowGenerator.test.js.map +1 -0
- package/dist/__tests__/storage.test.d.ts +2 -0
- package/dist/__tests__/storage.test.d.ts.map +1 -0
- package/dist/__tests__/storage.test.js +51 -0
- package/dist/__tests__/storage.test.js.map +1 -0
- package/dist/__tests__/templates.test.d.ts +2 -0
- package/dist/__tests__/templates.test.d.ts.map +1 -0
- package/dist/__tests__/templates.test.js +217 -0
- package/dist/__tests__/templates.test.js.map +1 -0
- package/dist/__tests__/timing.test.d.ts +2 -0
- package/dist/__tests__/timing.test.d.ts.map +1 -0
- package/dist/__tests__/timing.test.js +131 -0
- package/dist/__tests__/timing.test.js.map +1 -0
- package/dist/chunk-3E5F2XMR.js +129 -0
- package/dist/chunk-3E5F2XMR.js.map +1 -0
- package/dist/chunk-6YJPI6VI.js +35 -0
- package/dist/chunk-6YJPI6VI.js.map +1 -0
- package/dist/chunk-FVTQSNR7.js +100 -0
- package/dist/chunk-FVTQSNR7.js.map +1 -0
- package/dist/chunk-V543AI2F.js +1 -0
- package/dist/chunk-V543AI2F.js.map +1 -0
- package/dist/chunk-VP5SZTZ2.js +792 -0
- package/dist/chunk-VP5SZTZ2.js.map +1 -0
- package/dist/doc/docToMarkdown.d.ts +41 -0
- package/dist/doc/docToMarkdown.d.ts.map +1 -0
- package/dist/doc/docToMarkdown.js +88 -0
- package/dist/doc/docToMarkdown.js.map +1 -0
- package/dist/doc/getLayers.d.ts +65 -0
- package/dist/doc/getLayers.d.ts.map +1 -0
- package/dist/doc/getLayers.js +103 -0
- package/dist/doc/getLayers.js.map +1 -0
- package/dist/doc/index.d.ts +9 -3
- package/dist/doc/index.d.ts.map +1 -0
- package/dist/doc/index.js +6 -125
- package/dist/doc/index.js.map +1 -1
- package/dist/doc/markdownToDoc.d.ts +68 -0
- package/dist/doc/markdownToDoc.d.ts.map +1 -0
- package/dist/doc/markdownToDoc.js +225 -0
- package/dist/doc/markdownToDoc.js.map +1 -0
- package/dist/doc/templates/accentImage.d.ts +59 -0
- package/dist/doc/templates/accentImage.d.ts.map +1 -0
- package/dist/doc/templates/accentImage.js +275 -0
- package/dist/doc/templates/accentImage.js.map +1 -0
- package/dist/doc/templates/captionUtils.d.ts +20 -0
- package/dist/doc/templates/captionUtils.d.ts.map +1 -0
- package/dist/doc/templates/captionUtils.js +29 -0
- package/dist/doc/templates/captionUtils.js.map +1 -0
- package/dist/doc/templates/comparisonBar.d.ts +13 -0
- package/dist/doc/templates/comparisonBar.d.ts.map +1 -0
- package/dist/doc/templates/comparisonBar.js +127 -0
- package/dist/doc/templates/comparisonBar.js.map +1 -0
- package/dist/doc/templates/coverBlock.d.ts +51 -0
- package/dist/doc/templates/coverBlock.d.ts.map +1 -0
- package/dist/doc/templates/coverBlock.js +125 -0
- package/dist/doc/templates/coverBlock.js.map +1 -0
- package/dist/doc/templates/dateEvent.d.ts +15 -0
- package/dist/doc/templates/dateEvent.d.ts.map +1 -0
- package/dist/doc/templates/dateEvent.js +110 -0
- package/dist/doc/templates/dateEvent.js.map +1 -0
- package/dist/doc/templates/definitionCard.d.ts +13 -0
- package/dist/doc/templates/definitionCard.d.ts.map +1 -0
- package/dist/doc/templates/definitionCard.js +114 -0
- package/dist/doc/templates/definitionCard.js.map +1 -0
- package/dist/doc/templates/factCard.d.ts +15 -0
- package/dist/doc/templates/factCard.d.ts.map +1 -0
- package/dist/doc/templates/factCard.js +100 -0
- package/dist/doc/templates/factCard.js.map +1 -0
- package/dist/doc/templates/fullBleedQuote.d.ts +13 -0
- package/dist/doc/templates/fullBleedQuote.d.ts.map +1 -0
- package/dist/doc/templates/fullBleedQuote.js +54 -0
- package/dist/doc/templates/fullBleedQuote.js.map +1 -0
- package/dist/doc/templates/imageWithCaption.d.ts +13 -0
- package/dist/doc/templates/imageWithCaption.d.ts.map +1 -0
- package/dist/doc/templates/imageWithCaption.js +142 -0
- package/dist/doc/templates/imageWithCaption.js.map +1 -0
- package/dist/doc/templates/index.d.ts +104 -0
- package/dist/doc/templates/index.d.ts.map +1 -0
- package/dist/doc/templates/index.js +442 -0
- package/dist/doc/templates/index.js.map +1 -0
- package/dist/doc/templates/listBlock.d.ts +13 -0
- package/dist/doc/templates/listBlock.d.ts.map +1 -0
- package/dist/doc/templates/listBlock.js +89 -0
- package/dist/doc/templates/listBlock.js.map +1 -0
- package/dist/doc/templates/mapBlock.d.ts +17 -0
- package/dist/doc/templates/mapBlock.d.ts.map +1 -0
- package/dist/doc/templates/mapBlock.js +112 -0
- package/dist/doc/templates/mapBlock.js.map +1 -0
- package/dist/doc/templates/persistentLayers.d.ts +39 -0
- package/dist/doc/templates/persistentLayers.d.ts.map +1 -0
- package/dist/doc/templates/persistentLayers.js +447 -0
- package/dist/doc/templates/persistentLayers.js.map +1 -0
- package/dist/doc/templates/photoGrid.d.ts +20 -0
- package/dist/doc/templates/photoGrid.d.ts.map +1 -0
- package/dist/doc/templates/photoGrid.js +172 -0
- package/dist/doc/templates/photoGrid.js.map +1 -0
- package/dist/doc/templates/pullQuote.d.ts +13 -0
- package/dist/doc/templates/pullQuote.d.ts.map +1 -0
- package/dist/doc/templates/pullQuote.js +109 -0
- package/dist/doc/templates/pullQuote.js.map +1 -0
- package/dist/doc/templates/quoteBlock.d.ts +15 -0
- package/dist/doc/templates/quoteBlock.d.ts.map +1 -0
- package/dist/doc/templates/quoteBlock.js +97 -0
- package/dist/doc/templates/quoteBlock.js.map +1 -0
- package/dist/doc/templates/sectionHeader.d.ts +14 -0
- package/dist/doc/templates/sectionHeader.d.ts.map +1 -0
- package/dist/doc/templates/sectionHeader.js +96 -0
- package/dist/doc/templates/sectionHeader.js.map +1 -0
- package/dist/doc/templates/statHighlight.d.ts +15 -0
- package/dist/doc/templates/statHighlight.d.ts.map +1 -0
- package/dist/doc/templates/statHighlight.js +99 -0
- package/dist/doc/templates/statHighlight.js.map +1 -0
- package/dist/doc/templates/titleBlock.d.ts +13 -0
- package/dist/doc/templates/titleBlock.d.ts.map +1 -0
- package/dist/doc/templates/titleBlock.js +92 -0
- package/dist/doc/templates/titleBlock.js.map +1 -0
- package/dist/doc/templates/twoColumn.d.ts +15 -0
- package/dist/doc/templates/twoColumn.d.ts.map +1 -0
- package/dist/doc/templates/twoColumn.js +219 -0
- package/dist/doc/templates/twoColumn.js.map +1 -0
- package/dist/doc/templates/videoPullQuote.d.ts +20 -0
- package/dist/doc/templates/videoPullQuote.d.ts.map +1 -0
- package/dist/doc/templates/videoPullQuote.js +118 -0
- package/dist/doc/templates/videoPullQuote.js.map +1 -0
- package/dist/doc/templates/videoWithCaption.d.ts +20 -0
- package/dist/doc/templates/videoWithCaption.d.ts.map +1 -0
- package/dist/doc/templates/videoWithCaption.js +104 -0
- package/dist/doc/templates/videoWithCaption.js.map +1 -0
- package/dist/doc/utils/animationUtils.d.ts +34 -0
- package/dist/doc/utils/animationUtils.d.ts.map +1 -0
- package/dist/doc/utils/animationUtils.js +110 -0
- package/dist/doc/utils/animationUtils.js.map +1 -0
- package/dist/doc/utils/themeUtils.d.ts +55 -0
- package/dist/doc/utils/themeUtils.d.ts.map +1 -0
- package/dist/doc/utils/themeUtils.js +109 -0
- package/dist/doc/utils/themeUtils.js.map +1 -0
- package/dist/generate/contentExtractor.d.ts +112 -0
- package/dist/generate/contentExtractor.d.ts.map +1 -0
- package/dist/generate/contentExtractor.js +638 -0
- package/dist/generate/contentExtractor.js.map +1 -0
- package/dist/generate/index.d.ts +7 -0
- package/dist/generate/index.d.ts.map +1 -0
- package/dist/generate/index.js +4 -0
- package/dist/generate/index.js.map +1 -0
- package/dist/generate/slideshowGenerator.d.ts +72 -0
- package/dist/generate/slideshowGenerator.d.ts.map +1 -0
- package/dist/generate/slideshowGenerator.js +159 -0
- package/dist/generate/slideshowGenerator.js.map +1 -0
- package/dist/generate/templateMapper.d.ts +23 -0
- package/dist/generate/templateMapper.d.ts.map +1 -0
- package/dist/generate/templateMapper.js +114 -0
- package/dist/generate/templateMapper.js.map +1 -0
- package/dist/index.d.ts +9 -8
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +8 -216
- package/dist/index.js.map +1 -1
- package/dist/markdown/convert.d.ts +71 -0
- package/dist/markdown/convert.d.ts.map +1 -0
- package/dist/markdown/convert.js +746 -0
- package/dist/markdown/convert.js.map +1 -0
- package/dist/markdown/htmlParse.d.ts +26 -0
- package/dist/markdown/htmlParse.d.ts.map +1 -0
- package/dist/markdown/htmlParse.js +195 -0
- package/dist/markdown/htmlParse.js.map +1 -0
- package/dist/markdown/index.d.ts +19 -225
- package/dist/markdown/index.d.ts.map +1 -0
- package/dist/markdown/index.js +33 -32
- package/dist/markdown/index.js.map +1 -1
- package/dist/markdown/parse.d.ts +27 -0
- package/dist/markdown/parse.d.ts.map +1 -0
- package/dist/markdown/parse.js +90 -0
- package/dist/markdown/parse.js.map +1 -0
- package/dist/markdown/stringify.d.ts +32 -0
- package/dist/markdown/stringify.d.ts.map +1 -0
- package/dist/markdown/stringify.js +108 -0
- package/dist/markdown/stringify.js.map +1 -0
- package/dist/markdown/types.d.ts +496 -0
- package/dist/markdown/types.d.ts.map +1 -0
- package/dist/markdown/types.js +17 -0
- package/dist/markdown/types.js.map +1 -0
- package/dist/markdown/utils.d.ts +78 -0
- package/dist/markdown/utils.d.ts.map +1 -0
- package/dist/markdown/utils.js +149 -0
- package/dist/markdown/utils.js.map +1 -0
- package/dist/random/SeededRandom.d.ts +50 -0
- package/dist/random/SeededRandom.d.ts.map +1 -0
- package/dist/random/SeededRandom.js +115 -0
- package/dist/random/SeededRandom.js.map +1 -0
- package/dist/random/index.d.ts +2 -0
- package/dist/random/index.d.ts.map +1 -0
- package/dist/random/index.js +2 -0
- package/dist/random/index.js.map +1 -0
- package/dist/schemas/BlockTemplates.d.ts +583 -0
- package/dist/schemas/BlockTemplates.d.ts.map +1 -0
- package/dist/schemas/BlockTemplates.js +72 -0
- package/dist/schemas/BlockTemplates.js.map +1 -0
- package/dist/schemas/Doc.d.ts +411 -0
- package/dist/schemas/Doc.d.ts.map +1 -0
- package/dist/schemas/Doc.js +90 -0
- package/dist/schemas/Doc.js.map +1 -0
- package/dist/schemas/LayoutStrategy.d.ts +84 -0
- package/dist/schemas/LayoutStrategy.d.ts.map +1 -0
- package/dist/schemas/LayoutStrategy.js +123 -0
- package/dist/schemas/LayoutStrategy.js.map +1 -0
- package/dist/schemas/MediaProvider.d.ts +65 -0
- package/dist/schemas/MediaProvider.d.ts.map +1 -0
- package/dist/schemas/MediaProvider.js +13 -0
- package/dist/schemas/MediaProvider.js.map +1 -0
- package/dist/schemas/Theme.d.ts +163 -0
- package/dist/schemas/Theme.d.ts.map +1 -0
- package/dist/schemas/Theme.js +62 -0
- package/dist/schemas/Theme.js.map +1 -0
- package/dist/schemas/Types.d.ts +14 -0
- package/dist/schemas/Types.d.ts.map +1 -0
- package/dist/schemas/Types.js +5 -0
- package/dist/schemas/Types.js.map +1 -0
- package/dist/schemas/Viewport.d.ts +74 -0
- package/dist/schemas/Viewport.d.ts.map +1 -0
- package/dist/schemas/Viewport.js +63 -0
- package/dist/schemas/Viewport.js.map +1 -0
- package/dist/schemas/index.d.ts +10 -70
- package/dist/schemas/index.d.ts.map +1 -0
- package/dist/schemas/index.js +7 -60
- package/dist/schemas/index.js.map +1 -1
- package/dist/schemas/themeLibrary.d.ts +41 -0
- package/dist/schemas/themeLibrary.d.ts.map +1 -0
- package/dist/schemas/themeLibrary.js +466 -0
- package/dist/schemas/themeLibrary.js.map +1 -0
- package/dist/spatial/Geohash.d.ts +85 -0
- package/dist/spatial/Geohash.d.ts.map +1 -0
- package/dist/spatial/Geohash.js +142 -0
- package/dist/spatial/Geohash.js.map +1 -0
- package/dist/spatial/Haversine.d.ts +24 -0
- package/dist/spatial/Haversine.d.ts.map +1 -0
- package/dist/spatial/Haversine.js +48 -0
- package/dist/spatial/Haversine.js.map +1 -0
- package/dist/spatial/index.d.ts +3 -111
- package/dist/spatial/index.d.ts.map +1 -0
- package/dist/spatial/index.js +2 -24
- package/dist/spatial/index.js.map +1 -1
- package/dist/storage/LocalForageAdapter.d.ts +36 -0
- package/dist/storage/LocalForageAdapter.d.ts.map +1 -0
- package/dist/storage/LocalForageAdapter.js +82 -0
- package/dist/storage/LocalForageAdapter.js.map +1 -0
- package/dist/storage/LocalStorageAdapter.d.ts +16 -0
- package/dist/storage/LocalStorageAdapter.d.ts.map +1 -0
- package/dist/storage/LocalStorageAdapter.js +50 -0
- package/dist/storage/LocalStorageAdapter.js.map +1 -0
- package/dist/storage/MemoryStorageAdapter.d.ts +15 -0
- package/dist/storage/MemoryStorageAdapter.d.ts.map +1 -0
- package/dist/storage/MemoryStorageAdapter.js +34 -0
- package/dist/storage/MemoryStorageAdapter.js.map +1 -0
- package/dist/storage/Storage.d.ts +23 -0
- package/dist/storage/Storage.d.ts.map +1 -0
- package/dist/storage/Storage.js +9 -0
- package/dist/storage/Storage.js.map +1 -0
- package/dist/storage/index.d.ts +6 -91
- package/dist/storage/index.d.ts.map +1 -0
- package/dist/storage/index.js +3 -10
- package/dist/storage/index.js.map +1 -1
- package/dist/story/index.d.ts +3 -2
- package/dist/themeLibrary-DR1VRo-8.d.ts +44 -0
- package/dist/timing/index.d.ts +4 -0
- package/dist/timing/index.d.ts.map +1 -0
- package/dist/timing/index.js +3 -0
- package/dist/timing/index.js.map +1 -0
- package/dist/timing/narrationTiming.d.ts +54 -0
- package/dist/timing/narrationTiming.d.ts.map +1 -0
- package/dist/timing/narrationTiming.js +190 -0
- package/dist/timing/narrationTiming.js.map +1 -0
- package/dist/timing/readingTime.d.ts +54 -0
- package/dist/timing/readingTime.d.ts.map +1 -0
- package/dist/timing/readingTime.js +49 -0
- package/dist/timing/readingTime.js.map +1 -0
- package/package.json +19 -5
- package/src/__tests__/contentExtractor.test.ts +183 -0
- package/src/__tests__/seededRandom.test.ts +142 -0
- package/src/__tests__/slideshowGenerator.test.ts +128 -0
- package/src/__tests__/timing.test.ts +167 -0
- package/src/generate/contentExtractor.ts +808 -0
- package/src/generate/index.ts +21 -0
- package/src/generate/slideshowGenerator.ts +273 -0
- package/src/generate/templateMapper.ts +170 -0
- package/src/index.ts +3 -0
- package/src/random/SeededRandom.ts +127 -0
- package/src/random/index.ts +1 -0
- package/src/timing/index.ts +17 -0
- package/src/timing/narrationTiming.ts +220 -0
- package/src/timing/readingTime.ts +94 -0
|
@@ -0,0 +1,808 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Content Extractor
|
|
3
|
+
*
|
|
4
|
+
* Pattern-based extraction of compelling content from plain text.
|
|
5
|
+
* Identifies statistics, dates, quotes, comparisons, facts, impact lines,
|
|
6
|
+
* lists, and definitions that can be turned into slideshow slides.
|
|
7
|
+
*
|
|
8
|
+
* Extraction is purely regex-based — no AI calls, no runtime costs,
|
|
9
|
+
* deterministic and reproducible.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
// ── Types ──────────────────────────────────────────────────────────
|
|
13
|
+
|
|
14
|
+
/** Kinds of content the extractor recognises. */
|
|
15
|
+
export type ExtractionType =
|
|
16
|
+
| 'stat'
|
|
17
|
+
| 'date'
|
|
18
|
+
| 'quote'
|
|
19
|
+
| 'comparison'
|
|
20
|
+
| 'fact'
|
|
21
|
+
| 'impactLine'
|
|
22
|
+
| 'list'
|
|
23
|
+
| 'definition';
|
|
24
|
+
|
|
25
|
+
/** An extracted content element with source position. */
|
|
26
|
+
export interface ExtractedElement {
|
|
27
|
+
type: ExtractionType;
|
|
28
|
+
/** Full extracted text (usually the surrounding sentence). */
|
|
29
|
+
text: string;
|
|
30
|
+
/** Confidence score 0–1. */
|
|
31
|
+
confidence: number;
|
|
32
|
+
/** Character offset in the source text. */
|
|
33
|
+
sourcePosition: number;
|
|
34
|
+
/** Character offset where element ends. */
|
|
35
|
+
endPosition: number;
|
|
36
|
+
/** Type-specific structured payload. */
|
|
37
|
+
data:
|
|
38
|
+
| StatData
|
|
39
|
+
| DateData
|
|
40
|
+
| QuoteData
|
|
41
|
+
| ComparisonData
|
|
42
|
+
| FactData
|
|
43
|
+
| ImpactLineData
|
|
44
|
+
| ListData
|
|
45
|
+
| DefinitionData;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface StatData {
|
|
49
|
+
type: 'stat';
|
|
50
|
+
value: string;
|
|
51
|
+
unit?: string;
|
|
52
|
+
description: string;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface DateData {
|
|
56
|
+
type: 'date';
|
|
57
|
+
date: string;
|
|
58
|
+
description: string;
|
|
59
|
+
mood?: 'neutral' | 'somber' | 'celebratory';
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export interface QuoteData {
|
|
63
|
+
type: 'quote';
|
|
64
|
+
quote: string;
|
|
65
|
+
attribution?: string;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export interface ComparisonData {
|
|
69
|
+
type: 'comparison';
|
|
70
|
+
left: { label: string; sublabel?: string };
|
|
71
|
+
right: { label: string; sublabel?: string };
|
|
72
|
+
header?: string;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export interface FactData {
|
|
76
|
+
type: 'fact';
|
|
77
|
+
fact: string;
|
|
78
|
+
explanation?: string;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export interface ImpactLineData {
|
|
82
|
+
type: 'impactLine';
|
|
83
|
+
text: string;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export interface ListData {
|
|
87
|
+
type: 'list';
|
|
88
|
+
items: string[];
|
|
89
|
+
title?: string;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export interface DefinitionData {
|
|
93
|
+
type: 'definition';
|
|
94
|
+
term: string;
|
|
95
|
+
definition: string;
|
|
96
|
+
origin?: string;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** Options for `extractContent`. */
|
|
100
|
+
export interface ExtractionOptions {
|
|
101
|
+
/** Minimum confidence threshold (default: 0.3). */
|
|
102
|
+
minConfidence?: number;
|
|
103
|
+
/** Which types to extract (default: all). */
|
|
104
|
+
types?: ExtractionType[];
|
|
105
|
+
/** Maximum extractions per type (default: unlimited). */
|
|
106
|
+
maxPerType?: number;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** Result of `extractContent`. */
|
|
110
|
+
export interface ExtractionResult {
|
|
111
|
+
elements: ExtractedElement[];
|
|
112
|
+
sourceLength: number;
|
|
113
|
+
stats: {
|
|
114
|
+
statCount: number;
|
|
115
|
+
dateCount: number;
|
|
116
|
+
quoteCount: number;
|
|
117
|
+
comparisonCount: number;
|
|
118
|
+
factCount: number;
|
|
119
|
+
impactLineCount: number;
|
|
120
|
+
listCount: number;
|
|
121
|
+
definitionCount: number;
|
|
122
|
+
totalCount: number;
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// ── Patterns ───────────────────────────────────────────────────────
|
|
127
|
+
|
|
128
|
+
const STAT_PATTERNS = [
|
|
129
|
+
/\b(roughly |about |approximately |nearly |over |under |more than |less than )?([\d,]+(?:\.\d+)?)\s*%/gi,
|
|
130
|
+
/\b([\d,]+(?:\.\d+)?)\s*(million|billion|trillion|thousand|hundred)\b/gi,
|
|
131
|
+
/\b([\d,]+(?:\.\d+)?)\s*(miles?|feet|ft|meters?|m|kilometers?|km|acres?|square miles?|sq mi|hectares?)\b/gi,
|
|
132
|
+
/\b([\d,]+(?:\.\d+)?)\s*(x|times)\b/gi,
|
|
133
|
+
/\b([$€£¥])\s*([\d,]+(?:\.\d+)?)\s*(million|billion|thousand)?\b/gi,
|
|
134
|
+
/\b(\d+)(st|nd|rd|th)\s+(largest|smallest|oldest|newest|most|least)/gi,
|
|
135
|
+
];
|
|
136
|
+
|
|
137
|
+
const DATE_PATTERNS = [
|
|
138
|
+
/\b(January|February|March|April|May|June|July|August|September|October|November|December)\s+(\d{1,2},?\s+)?(\d{4})\b/gi,
|
|
139
|
+
/\b(\d{4})s\b|\b(\d{1,2})(st|nd|rd|th)\s+century\b/gi,
|
|
140
|
+
/\b(from\s+)?(\d{4})\s*(to|–|-|through)\s*(\d{4})\b/gi,
|
|
141
|
+
/\b(in|by|since|after|before|during)\s+(\d{4})\b/gi,
|
|
142
|
+
];
|
|
143
|
+
|
|
144
|
+
const QUOTE_PATTERNS = [
|
|
145
|
+
/"([^"]{15,200})"/g,
|
|
146
|
+
/\u201c([^\u201d]{15,200})\u201d/g,
|
|
147
|
+
/(\w+(?:\s+\w+)?)\s+(?:said|stated|wrote|noted|observed|commented),?\s*"([^"]+)"/gi,
|
|
148
|
+
];
|
|
149
|
+
|
|
150
|
+
const COMPARISON_PATTERNS = [
|
|
151
|
+
/from\s+(.{2,30})\s+to\s+(.{2,30})/gi,
|
|
152
|
+
/(.{2,30})\s+(?:vs\.?|versus)\s+(.{2,30})/gi,
|
|
153
|
+
/(.{2,30})\s+compared\s+to\s+(.{2,30})/gi,
|
|
154
|
+
];
|
|
155
|
+
|
|
156
|
+
const FACT_INDICATORS = [
|
|
157
|
+
'largest',
|
|
158
|
+
'smallest',
|
|
159
|
+
'oldest',
|
|
160
|
+
'newest',
|
|
161
|
+
'first',
|
|
162
|
+
'last',
|
|
163
|
+
'most',
|
|
164
|
+
'least',
|
|
165
|
+
'only',
|
|
166
|
+
'unique',
|
|
167
|
+
'rare',
|
|
168
|
+
'famous',
|
|
169
|
+
'important',
|
|
170
|
+
'significant',
|
|
171
|
+
'notable',
|
|
172
|
+
'remarkable',
|
|
173
|
+
'extraordinary',
|
|
174
|
+
'crucial',
|
|
175
|
+
'essential',
|
|
176
|
+
'critical',
|
|
177
|
+
'major',
|
|
178
|
+
'key',
|
|
179
|
+
'discovered',
|
|
180
|
+
'founded',
|
|
181
|
+
'established',
|
|
182
|
+
'created',
|
|
183
|
+
'built',
|
|
184
|
+
'invented',
|
|
185
|
+
'developed',
|
|
186
|
+
'introduced',
|
|
187
|
+
'pioneered',
|
|
188
|
+
'transformed',
|
|
189
|
+
'revolutionized',
|
|
190
|
+
'changed',
|
|
191
|
+
'shaped',
|
|
192
|
+
];
|
|
193
|
+
|
|
194
|
+
const SOMBER_INDICATORS = [
|
|
195
|
+
'disaster',
|
|
196
|
+
'tragedy',
|
|
197
|
+
'death',
|
|
198
|
+
'died',
|
|
199
|
+
'killed',
|
|
200
|
+
'destroyed',
|
|
201
|
+
'devastated',
|
|
202
|
+
'collapsed',
|
|
203
|
+
'lost',
|
|
204
|
+
'abandoned',
|
|
205
|
+
'declined',
|
|
206
|
+
'war',
|
|
207
|
+
'battle',
|
|
208
|
+
'conflict',
|
|
209
|
+
'earthquake',
|
|
210
|
+
'fire',
|
|
211
|
+
'flood',
|
|
212
|
+
];
|
|
213
|
+
|
|
214
|
+
const CELEBRATORY_INDICATORS = [
|
|
215
|
+
'celebration',
|
|
216
|
+
'victory',
|
|
217
|
+
'success',
|
|
218
|
+
'achievement',
|
|
219
|
+
'triumph',
|
|
220
|
+
'opened',
|
|
221
|
+
'completed',
|
|
222
|
+
'inaugurated',
|
|
223
|
+
'launched',
|
|
224
|
+
'won',
|
|
225
|
+
'record',
|
|
226
|
+
'breakthrough',
|
|
227
|
+
'milestone',
|
|
228
|
+
'golden',
|
|
229
|
+
'grand',
|
|
230
|
+
];
|
|
231
|
+
|
|
232
|
+
const IMPACT_VERBS = [
|
|
233
|
+
'check',
|
|
234
|
+
'watch',
|
|
235
|
+
'stop',
|
|
236
|
+
'prepare',
|
|
237
|
+
'imagine',
|
|
238
|
+
'consider',
|
|
239
|
+
'look',
|
|
240
|
+
'listen',
|
|
241
|
+
'notice',
|
|
242
|
+
'remember',
|
|
243
|
+
'discover',
|
|
244
|
+
'explore',
|
|
245
|
+
'visit',
|
|
246
|
+
'try',
|
|
247
|
+
'experience',
|
|
248
|
+
'avoid',
|
|
249
|
+
'beware',
|
|
250
|
+
'enjoy',
|
|
251
|
+
];
|
|
252
|
+
|
|
253
|
+
const LIST_PATTERNS = [
|
|
254
|
+
/(?:including|such as|namely|like|features?|offers?)\s+([^.]{10,150})/gi,
|
|
255
|
+
/(?:three|four|five|six|several|many|various)\s+(?:main|key|primary|notable|popular|important)?\s*\w+(?:\s+\w+)?:\s*([^.]{10,150})/gi,
|
|
256
|
+
];
|
|
257
|
+
|
|
258
|
+
const DEFINITION_PATTERNS = [
|
|
259
|
+
/\b([A-Z][a-z]+(?:\s+[A-Z]?[a-z]+){0,3})\s+is\s+(?:a|an|the)\s+([^.]{10,120})/g,
|
|
260
|
+
/known\s+as\s+([A-Z][a-z]+(?:\s+[A-Z]?[a-z]+){0,3}),\s+([^.]{10,120})/gi,
|
|
261
|
+
/\b([A-Z][a-z]+(?:\s+[A-Z]?[a-z]+){0,3})\s+refers?\s+to\s+([^.]{10,120})/g,
|
|
262
|
+
/called\s+([A-Z][a-z]+(?:\s+[A-Z]?[a-z]+){0,3}),?\s+(?:which|meaning|a)\s+([^.]{10,120})/gi,
|
|
263
|
+
];
|
|
264
|
+
|
|
265
|
+
// ── Main ───────────────────────────────────────────────────────────
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* Extract compelling content from plain text.
|
|
269
|
+
*
|
|
270
|
+
* Call `stripMarkdown(text)` first if the input contains markdown.
|
|
271
|
+
*/
|
|
272
|
+
export function extractContent(text: string, options: ExtractionOptions = {}): ExtractionResult {
|
|
273
|
+
const {
|
|
274
|
+
minConfidence = 0.3,
|
|
275
|
+
types = ['stat', 'date', 'quote', 'comparison', 'fact', 'impactLine', 'list', 'definition'],
|
|
276
|
+
maxPerType,
|
|
277
|
+
} = options;
|
|
278
|
+
|
|
279
|
+
const elements: ExtractedElement[] = [];
|
|
280
|
+
|
|
281
|
+
if (types.includes('stat')) elements.push(...extractStats(text));
|
|
282
|
+
if (types.includes('date')) elements.push(...extractDates(text));
|
|
283
|
+
if (types.includes('quote')) elements.push(...extractQuotes(text));
|
|
284
|
+
if (types.includes('comparison')) elements.push(...extractComparisons(text));
|
|
285
|
+
if (types.includes('fact')) elements.push(...extractFacts(text));
|
|
286
|
+
if (types.includes('impactLine')) elements.push(...extractImpactLines(text));
|
|
287
|
+
if (types.includes('list')) elements.push(...extractLists(text));
|
|
288
|
+
if (types.includes('definition')) elements.push(...extractDefinitions(text));
|
|
289
|
+
|
|
290
|
+
let filtered = elements.filter((e) => e.confidence >= minConfidence);
|
|
291
|
+
|
|
292
|
+
if (maxPerType !== undefined) {
|
|
293
|
+
const byType = new Map<ExtractionType, ExtractedElement[]>();
|
|
294
|
+
for (const elem of filtered) {
|
|
295
|
+
const list = byType.get(elem.type) || [];
|
|
296
|
+
list.push(elem);
|
|
297
|
+
byType.set(elem.type, list);
|
|
298
|
+
}
|
|
299
|
+
filtered = [];
|
|
300
|
+
for (const [, list] of byType) {
|
|
301
|
+
list.sort((a, b) => b.confidence - a.confidence);
|
|
302
|
+
filtered.push(...list.slice(0, maxPerType));
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
filtered.sort((a, b) => a.sourcePosition - b.sourcePosition);
|
|
307
|
+
filtered = deduplicateOverlapping(filtered);
|
|
308
|
+
|
|
309
|
+
return {
|
|
310
|
+
elements: filtered,
|
|
311
|
+
sourceLength: text.length,
|
|
312
|
+
stats: {
|
|
313
|
+
statCount: filtered.filter((e) => e.type === 'stat').length,
|
|
314
|
+
dateCount: filtered.filter((e) => e.type === 'date').length,
|
|
315
|
+
quoteCount: filtered.filter((e) => e.type === 'quote').length,
|
|
316
|
+
comparisonCount: filtered.filter((e) => e.type === 'comparison').length,
|
|
317
|
+
factCount: filtered.filter((e) => e.type === 'fact').length,
|
|
318
|
+
impactLineCount: filtered.filter((e) => e.type === 'impactLine').length,
|
|
319
|
+
listCount: filtered.filter((e) => e.type === 'list').length,
|
|
320
|
+
definitionCount: filtered.filter((e) => e.type === 'definition').length,
|
|
321
|
+
totalCount: filtered.length,
|
|
322
|
+
},
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
// ── Per-type extractors ────────────────────────────────────────────
|
|
327
|
+
|
|
328
|
+
function extractStats(text: string): ExtractedElement[] {
|
|
329
|
+
const elements: ExtractedElement[] = [];
|
|
330
|
+
const seen = new Set<number>();
|
|
331
|
+
|
|
332
|
+
for (const pattern of STAT_PATTERNS) {
|
|
333
|
+
pattern.lastIndex = 0;
|
|
334
|
+
let match;
|
|
335
|
+
while ((match = pattern.exec(text)) !== null) {
|
|
336
|
+
const position = match.index;
|
|
337
|
+
if (seen.has(position)) continue;
|
|
338
|
+
seen.add(position);
|
|
339
|
+
|
|
340
|
+
const context = getSentenceContext(text, position, match[0].length);
|
|
341
|
+
const value = match[0].trim();
|
|
342
|
+
|
|
343
|
+
elements.push({
|
|
344
|
+
type: 'stat',
|
|
345
|
+
text: context.sentence,
|
|
346
|
+
confidence: calculateStatConfidence(value, context.sentence),
|
|
347
|
+
sourcePosition: context.start,
|
|
348
|
+
endPosition: context.end,
|
|
349
|
+
data: { type: 'stat', value, description: cleanDescription(context.sentence, value) },
|
|
350
|
+
});
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
return elements;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
function extractDates(text: string): ExtractedElement[] {
|
|
357
|
+
const elements: ExtractedElement[] = [];
|
|
358
|
+
const seen = new Set<number>();
|
|
359
|
+
|
|
360
|
+
for (const pattern of DATE_PATTERNS) {
|
|
361
|
+
pattern.lastIndex = 0;
|
|
362
|
+
let match;
|
|
363
|
+
while ((match = pattern.exec(text)) !== null) {
|
|
364
|
+
const position = match.index;
|
|
365
|
+
if (seen.has(position)) continue;
|
|
366
|
+
seen.add(position);
|
|
367
|
+
|
|
368
|
+
const context = getSentenceContext(text, position, match[0].length);
|
|
369
|
+
const dateStr = match[0].trim();
|
|
370
|
+
|
|
371
|
+
elements.push({
|
|
372
|
+
type: 'date',
|
|
373
|
+
text: context.sentence,
|
|
374
|
+
confidence: calculateDateConfidence(dateStr, context.sentence),
|
|
375
|
+
sourcePosition: context.start,
|
|
376
|
+
endPosition: context.end,
|
|
377
|
+
data: {
|
|
378
|
+
type: 'date',
|
|
379
|
+
date: dateStr,
|
|
380
|
+
description: cleanDescription(context.sentence, dateStr),
|
|
381
|
+
mood: detectMood(context.sentence),
|
|
382
|
+
},
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
return elements;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
function extractQuotes(text: string): ExtractedElement[] {
|
|
390
|
+
const elements: ExtractedElement[] = [];
|
|
391
|
+
const seen = new Set<number>();
|
|
392
|
+
|
|
393
|
+
for (const pattern of QUOTE_PATTERNS) {
|
|
394
|
+
pattern.lastIndex = 0;
|
|
395
|
+
let match;
|
|
396
|
+
while ((match = pattern.exec(text)) !== null) {
|
|
397
|
+
const position = match.index;
|
|
398
|
+
if (seen.has(position)) continue;
|
|
399
|
+
seen.add(position);
|
|
400
|
+
|
|
401
|
+
const quote = match.length > 2 ? match[2] : match[1];
|
|
402
|
+
const attribution = match.length > 2 ? match[1] : undefined;
|
|
403
|
+
if (quote.length < 15 || quote.length > 200) continue;
|
|
404
|
+
|
|
405
|
+
elements.push({
|
|
406
|
+
type: 'quote',
|
|
407
|
+
text: match[0],
|
|
408
|
+
confidence: calculateQuoteConfidence(quote),
|
|
409
|
+
sourcePosition: position,
|
|
410
|
+
endPosition: position + match[0].length,
|
|
411
|
+
data: { type: 'quote', quote: quote.trim(), attribution: attribution?.trim() },
|
|
412
|
+
});
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
return elements;
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
function extractComparisons(text: string): ExtractedElement[] {
|
|
419
|
+
const elements: ExtractedElement[] = [];
|
|
420
|
+
const seen = new Set<number>();
|
|
421
|
+
|
|
422
|
+
for (const pattern of COMPARISON_PATTERNS) {
|
|
423
|
+
pattern.lastIndex = 0;
|
|
424
|
+
let match;
|
|
425
|
+
while ((match = pattern.exec(text)) !== null) {
|
|
426
|
+
const position = match.index;
|
|
427
|
+
if (seen.has(position)) continue;
|
|
428
|
+
seen.add(position);
|
|
429
|
+
|
|
430
|
+
const left = match[1].trim();
|
|
431
|
+
const right = match[2].trim();
|
|
432
|
+
if (left.length < 2 || left.length > 30) continue;
|
|
433
|
+
if (right.length < 2 || right.length > 30) continue;
|
|
434
|
+
|
|
435
|
+
const context = getSentenceContext(text, position, match[0].length);
|
|
436
|
+
|
|
437
|
+
elements.push({
|
|
438
|
+
type: 'comparison',
|
|
439
|
+
text: context.sentence,
|
|
440
|
+
confidence: 0.6,
|
|
441
|
+
sourcePosition: context.start,
|
|
442
|
+
endPosition: context.end,
|
|
443
|
+
data: { type: 'comparison', left: { label: left }, right: { label: right } },
|
|
444
|
+
});
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
return elements;
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
/**
|
|
451
|
+
* Iterate over sentence-like spans in the text, yielding trimmed text and
|
|
452
|
+
* accurate start/end indices within the original string.
|
|
453
|
+
*
|
|
454
|
+
* Sentences are approximated as runs of non-[.!?] characters optionally
|
|
455
|
+
* followed by [.!?] delimiters. Leading/trailing whitespace is excluded.
|
|
456
|
+
*/
|
|
457
|
+
function forEachSentenceSpan(
|
|
458
|
+
text: string,
|
|
459
|
+
callback: (trimmed: string, start: number, end: number) => void,
|
|
460
|
+
): void {
|
|
461
|
+
const sentenceRegex = /[^.!?]+[.!?]*/g;
|
|
462
|
+
let match: RegExpExecArray | null;
|
|
463
|
+
|
|
464
|
+
while ((match = sentenceRegex.exec(text)) !== null) {
|
|
465
|
+
const raw = match[0];
|
|
466
|
+
const rawStart = match.index;
|
|
467
|
+
const rawEnd = rawStart + raw.length;
|
|
468
|
+
|
|
469
|
+
// Compute leading whitespace to exclude
|
|
470
|
+
let leading = 0;
|
|
471
|
+
while (leading < raw.length && /\s/.test(raw[leading])) {
|
|
472
|
+
leading++;
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
// Compute trailing whitespace or sentence-ending punctuation to exclude
|
|
476
|
+
let trailing = 0;
|
|
477
|
+
while (trailing < raw.length - leading && /[\s.!?]/.test(raw[raw.length - 1 - trailing])) {
|
|
478
|
+
trailing++;
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
const start = rawStart + leading;
|
|
482
|
+
const end = rawEnd - trailing;
|
|
483
|
+
if (end <= start) continue;
|
|
484
|
+
|
|
485
|
+
const trimmed = text.slice(start, end);
|
|
486
|
+
if (!trimmed) continue;
|
|
487
|
+
|
|
488
|
+
callback(trimmed, start, end);
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
function extractFacts(text: string): ExtractedElement[] {
|
|
493
|
+
const elements: ExtractedElement[] = [];
|
|
494
|
+
|
|
495
|
+
forEachSentenceSpan(text, (trimmed, start, end) => {
|
|
496
|
+
if (trimmed.length <= 20) return;
|
|
497
|
+
|
|
498
|
+
const score = scoreSentenceAsFact(trimmed);
|
|
499
|
+
if (score < 0.4) return;
|
|
500
|
+
|
|
501
|
+
elements.push({
|
|
502
|
+
type: 'fact',
|
|
503
|
+
text: trimmed,
|
|
504
|
+
confidence: score,
|
|
505
|
+
sourcePosition: start,
|
|
506
|
+
endPosition: end,
|
|
507
|
+
data: { type: 'fact', fact: trimmed },
|
|
508
|
+
});
|
|
509
|
+
});
|
|
510
|
+
|
|
511
|
+
return elements;
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
function extractImpactLines(text: string): ExtractedElement[] {
|
|
515
|
+
const elements: ExtractedElement[] = [];
|
|
516
|
+
|
|
517
|
+
forEachSentenceSpan(text, (trimmed, start, end) => {
|
|
518
|
+
if (trimmed.length < 8 || trimmed.length > 60) return;
|
|
519
|
+
|
|
520
|
+
let score = 0.3;
|
|
521
|
+
const lower = trimmed.toLowerCase();
|
|
522
|
+
|
|
523
|
+
for (const verb of IMPACT_VERBS) {
|
|
524
|
+
if (lower.startsWith(verb)) {
|
|
525
|
+
score += 0.25;
|
|
526
|
+
break;
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
if (end < text.length && text[end] === '!') score += 0.15;
|
|
531
|
+
if (trimmed.length < 30) score += 0.1;
|
|
532
|
+
if (trimmed.includes(',')) score -= 0.1;
|
|
533
|
+
if (/^(and|but|or|the|a|an)\b/i.test(trimmed)) score -= 0.15;
|
|
534
|
+
if (score < 0.5) return;
|
|
535
|
+
|
|
536
|
+
elements.push({
|
|
537
|
+
type: 'impactLine',
|
|
538
|
+
text: trimmed,
|
|
539
|
+
confidence: Math.min(1, score),
|
|
540
|
+
sourcePosition: start,
|
|
541
|
+
endPosition: end,
|
|
542
|
+
data: { type: 'impactLine', text: trimmed },
|
|
543
|
+
});
|
|
544
|
+
});
|
|
545
|
+
|
|
546
|
+
return elements;
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
function extractLists(text: string): ExtractedElement[] {
|
|
550
|
+
const elements: ExtractedElement[] = [];
|
|
551
|
+
const seen = new Set<number>();
|
|
552
|
+
|
|
553
|
+
for (const pattern of LIST_PATTERNS) {
|
|
554
|
+
pattern.lastIndex = 0;
|
|
555
|
+
let match;
|
|
556
|
+
while ((match = pattern.exec(text)) !== null) {
|
|
557
|
+
const position = match.index;
|
|
558
|
+
if (seen.has(position)) continue;
|
|
559
|
+
seen.add(position);
|
|
560
|
+
|
|
561
|
+
const listContent = match[1].trim();
|
|
562
|
+
const items = listContent
|
|
563
|
+
.split(/,\s*(?:and|or)\s+|,\s+|\s+and\s+|\s+or\s+/)
|
|
564
|
+
.map((item) => item.trim())
|
|
565
|
+
.filter((item) => item.length >= 2 && item.length <= 60);
|
|
566
|
+
|
|
567
|
+
if (items.length < 3 || items.length > 5) continue;
|
|
568
|
+
|
|
569
|
+
const context = getSentenceContext(text, position, match[0].length);
|
|
570
|
+
|
|
571
|
+
elements.push({
|
|
572
|
+
type: 'list',
|
|
573
|
+
text: context.sentence,
|
|
574
|
+
confidence: 0.65,
|
|
575
|
+
sourcePosition: context.start,
|
|
576
|
+
endPosition: context.end,
|
|
577
|
+
data: { type: 'list', items },
|
|
578
|
+
});
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
return elements;
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
function extractDefinitions(text: string): ExtractedElement[] {
|
|
585
|
+
const elements: ExtractedElement[] = [];
|
|
586
|
+
const seen = new Set<number>();
|
|
587
|
+
|
|
588
|
+
for (const pattern of DEFINITION_PATTERNS) {
|
|
589
|
+
pattern.lastIndex = 0;
|
|
590
|
+
let match;
|
|
591
|
+
while ((match = pattern.exec(text)) !== null) {
|
|
592
|
+
const position = match.index;
|
|
593
|
+
if (seen.has(position)) continue;
|
|
594
|
+
seen.add(position);
|
|
595
|
+
|
|
596
|
+
const term = match[1].trim();
|
|
597
|
+
const definition = match[2].trim();
|
|
598
|
+
if (term.length < 3 || term.length > 30) continue;
|
|
599
|
+
if (definition.length < 10 || definition.length > 120) continue;
|
|
600
|
+
|
|
601
|
+
const context = getSentenceContext(text, position, match[0].length);
|
|
602
|
+
|
|
603
|
+
elements.push({
|
|
604
|
+
type: 'definition',
|
|
605
|
+
text: context.sentence,
|
|
606
|
+
confidence: 0.6,
|
|
607
|
+
sourcePosition: context.start,
|
|
608
|
+
endPosition: context.end,
|
|
609
|
+
data: { type: 'definition', term, definition },
|
|
610
|
+
});
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
return elements;
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
// ── Helpers ────────────────────────────────────────────────────────
|
|
617
|
+
|
|
618
|
+
function getSentenceContext(
|
|
619
|
+
text: string,
|
|
620
|
+
matchStart: number,
|
|
621
|
+
matchLength: number,
|
|
622
|
+
): { sentence: string; start: number; end: number } {
|
|
623
|
+
let start = matchStart;
|
|
624
|
+
while (start > 0 && !/[.!?]/.test(text[start - 1])) start--;
|
|
625
|
+
while (start < matchStart && /\s/.test(text[start])) start++;
|
|
626
|
+
|
|
627
|
+
let end = matchStart + matchLength;
|
|
628
|
+
while (end < text.length && !/[.!?]/.test(text[end])) end++;
|
|
629
|
+
if (end < text.length) end++;
|
|
630
|
+
|
|
631
|
+
return { sentence: text.slice(start, end).trim(), start, end };
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
function calculateStatConfidence(value: string, context: string): number {
|
|
635
|
+
let score = 0.5;
|
|
636
|
+
if (value.includes('%')) score += 0.2;
|
|
637
|
+
if (/million|billion|trillion/i.test(value)) score += 0.15;
|
|
638
|
+
if (/increased|decreased|grew|dropped|rose|fell/i.test(context)) score += 0.15;
|
|
639
|
+
if (context.length < 30) score -= 0.2;
|
|
640
|
+
return Math.min(1, Math.max(0, score));
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
function calculateDateConfidence(date: string, context: string): number {
|
|
644
|
+
let score = 0.5;
|
|
645
|
+
if (
|
|
646
|
+
/January|February|March|April|May|June|July|August|September|October|November|December/i.test(
|
|
647
|
+
date,
|
|
648
|
+
)
|
|
649
|
+
)
|
|
650
|
+
score += 0.2;
|
|
651
|
+
if (/founded|established|built|opened|discovered|created/i.test(context)) score += 0.2;
|
|
652
|
+
if (/happened|occurred|took place|began|ended/i.test(context)) score += 0.15;
|
|
653
|
+
return Math.min(1, Math.max(0, score));
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
function calculateQuoteConfidence(quote: string): number {
|
|
657
|
+
let score = 0.5;
|
|
658
|
+
if (quote.length >= 30 && quote.length <= 150) score += 0.2;
|
|
659
|
+
if (/\b(I|we|our|my)\b/i.test(quote)) score += 0.1;
|
|
660
|
+
if (!/[.!?]$/.test(quote)) score -= 0.1;
|
|
661
|
+
return Math.min(1, Math.max(0, score));
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
function scoreSentenceAsFact(sentence: string): number {
|
|
665
|
+
let score = 0.3;
|
|
666
|
+
const lower = sentence.toLowerCase();
|
|
667
|
+
for (const indicator of FACT_INDICATORS) {
|
|
668
|
+
if (lower.includes(indicator)) {
|
|
669
|
+
score += 0.1;
|
|
670
|
+
break;
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
const properNouns = sentence.match(/\b[A-Z][a-z]+\b/g);
|
|
674
|
+
if (properNouns && properNouns.length >= 2) score += 0.15;
|
|
675
|
+
if (/\d/.test(sentence)) score += 0.1;
|
|
676
|
+
if (sentence.endsWith('?')) score -= 0.3;
|
|
677
|
+
if (sentence.length > 200) score -= 0.15;
|
|
678
|
+
return Math.min(1, Math.max(0, score));
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
function detectMood(sentence: string): 'neutral' | 'somber' | 'celebratory' {
|
|
682
|
+
const lower = sentence.toLowerCase();
|
|
683
|
+
for (const word of SOMBER_INDICATORS) {
|
|
684
|
+
if (lower.includes(word)) return 'somber';
|
|
685
|
+
}
|
|
686
|
+
for (const word of CELEBRATORY_INDICATORS) {
|
|
687
|
+
if (lower.includes(word)) return 'celebratory';
|
|
688
|
+
}
|
|
689
|
+
return 'neutral';
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
function cleanDescription(sentence: string, value: string): string {
|
|
693
|
+
const prepositionPatterns = [
|
|
694
|
+
new RegExp(`\\b[Ii]n\\s+${escapeRegex(value)}[,.]?\\s*`, 'g'),
|
|
695
|
+
new RegExp(`\\b[Oo]n\\s+${escapeRegex(value)}[,.]?\\s*`, 'g'),
|
|
696
|
+
new RegExp(`\\b[Dd]uring\\s+${escapeRegex(value)}[,.]?\\s*`, 'g'),
|
|
697
|
+
new RegExp(`\\b[Bb]y\\s+${escapeRegex(value)}[,.]?\\s*`, 'g'),
|
|
698
|
+
new RegExp(`\\b[Ss]ince\\s+${escapeRegex(value)}[,.]?\\s*`, 'g'),
|
|
699
|
+
new RegExp(`\\b[Ff]rom\\s+${escapeRegex(value)}[,.]?\\s*`, 'g'),
|
|
700
|
+
];
|
|
701
|
+
|
|
702
|
+
let desc = sentence;
|
|
703
|
+
for (const pattern of prepositionPatterns) {
|
|
704
|
+
desc = desc.replace(pattern, '');
|
|
705
|
+
}
|
|
706
|
+
if (desc === sentence) {
|
|
707
|
+
desc = sentence.replace(value, '').trim();
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
desc = desc.replace(/^[,:\-–—]+\s*/, '').replace(/\s*[,:\-–—]+$/, '');
|
|
711
|
+
desc = desc.replace(
|
|
712
|
+
/\s+(?:of|on|in|at|by|to|for|from|with|about|over|under|between|than|into|onto|upon)\s*([,.])/gi,
|
|
713
|
+
'$1',
|
|
714
|
+
);
|
|
715
|
+
desc = desc.replace(
|
|
716
|
+
/\s+(?:for|of|to|in|at|by|with|from|about|over|under|between|than|into|onto|upon)\s*[.!?]?\s*$/i,
|
|
717
|
+
'.',
|
|
718
|
+
);
|
|
719
|
+
desc = desc.replace(/\s{2,}/g, ' ').replace(/\s+([,.])/g, '$1');
|
|
720
|
+
desc = desc.charAt(0).toUpperCase() + desc.slice(1);
|
|
721
|
+
return desc.trim();
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
function escapeRegex(str: string): string {
|
|
725
|
+
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
const TYPE_SPECIFICITY: Record<ExtractionType, number> = {
|
|
729
|
+
stat: 5,
|
|
730
|
+
date: 4,
|
|
731
|
+
quote: 3,
|
|
732
|
+
definition: 3,
|
|
733
|
+
comparison: 2,
|
|
734
|
+
list: 2,
|
|
735
|
+
impactLine: 2,
|
|
736
|
+
fact: 1,
|
|
737
|
+
};
|
|
738
|
+
|
|
739
|
+
function deduplicateOverlapping(elements: ExtractedElement[]): ExtractedElement[] {
|
|
740
|
+
if (elements.length <= 1) return elements;
|
|
741
|
+
|
|
742
|
+
const toRemove = new Set<number>();
|
|
743
|
+
|
|
744
|
+
for (let i = 0; i < elements.length; i++) {
|
|
745
|
+
if (toRemove.has(i)) continue;
|
|
746
|
+
const a = elements[i];
|
|
747
|
+
|
|
748
|
+
for (let j = i + 1; j < elements.length; j++) {
|
|
749
|
+
if (toRemove.has(j)) continue;
|
|
750
|
+
const b = elements[j];
|
|
751
|
+
|
|
752
|
+
if (elementsOverlap(a, b)) {
|
|
753
|
+
const aSpec = TYPE_SPECIFICITY[a.type];
|
|
754
|
+
const bSpec = TYPE_SPECIFICITY[b.type];
|
|
755
|
+
if (aSpec >= bSpec) {
|
|
756
|
+
toRemove.add(j);
|
|
757
|
+
} else {
|
|
758
|
+
toRemove.add(i);
|
|
759
|
+
break;
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
}
|
|
764
|
+
|
|
765
|
+
return elements.filter((_, index) => !toRemove.has(index));
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
function elementsOverlap(a: ExtractedElement, b: ExtractedElement): boolean {
|
|
769
|
+
if (a.sourcePosition === b.sourcePosition) return true;
|
|
770
|
+
if (a.sourcePosition <= b.sourcePosition && a.endPosition >= b.endPosition) return true;
|
|
771
|
+
if (b.sourcePosition <= a.sourcePosition && b.endPosition >= a.endPosition) return true;
|
|
772
|
+
|
|
773
|
+
const overlapStart = Math.max(a.sourcePosition, b.sourcePosition);
|
|
774
|
+
const overlapEnd = Math.min(a.endPosition, b.endPosition);
|
|
775
|
+
if (overlapEnd > overlapStart) {
|
|
776
|
+
const overlapLength = overlapEnd - overlapStart;
|
|
777
|
+
const minLength = Math.min(a.endPosition - a.sourcePosition, b.endPosition - b.sourcePosition);
|
|
778
|
+
if (overlapLength > minLength * 0.5) return true;
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
return false;
|
|
782
|
+
}
|
|
783
|
+
|
|
784
|
+
/**
|
|
785
|
+
* Strip markdown formatting from text, returning plain prose.
|
|
786
|
+
* Call this before `extractContent` if the input is markdown.
|
|
787
|
+
*/
|
|
788
|
+
export function stripMarkdown(markdown: string): string {
|
|
789
|
+
return (
|
|
790
|
+
markdown
|
|
791
|
+
// Remove code blocks (before inline code)
|
|
792
|
+
.replace(/```[\s\S]*?```/g, '')
|
|
793
|
+
// Remove images (before links — `![` prefix distinguishes them)
|
|
794
|
+
.replace(/!\[([^\]]*)\]\([^)]+\)/g, '')
|
|
795
|
+
// Remove links, keeping text
|
|
796
|
+
.replace(/\[([^\]]+)\]\([^)]+\)/g, '$1')
|
|
797
|
+
.replace(/^#{1,6}\s+/gm, '')
|
|
798
|
+
.replace(/\*\*([^*]+)\*\*/g, '$1')
|
|
799
|
+
.replace(/\*([^*]+)\*/g, '$1')
|
|
800
|
+
.replace(/__([^_]+)__/g, '$1')
|
|
801
|
+
.replace(/_([^_]+)_/g, '$1')
|
|
802
|
+
.replace(/`([^`]+)`/g, '$1')
|
|
803
|
+
.replace(/^>\s+/gm, '')
|
|
804
|
+
.replace(/^[-*_]{3,}$/gm, '')
|
|
805
|
+
.replace(/\n{3,}/g, '\n\n')
|
|
806
|
+
.trim()
|
|
807
|
+
);
|
|
808
|
+
}
|