DocumentEntity is a deep and sophisticated system that connects documents to content pages (Characters, Locations, Items, etc.) with emotional analysis and sentiment tracking. It's part of the document analysis feature and goes way beyond simple linking.
DocumentEntity < ApplicationRecord
belongs_to :entity, polymorphic: true # Links to any content type
belongs_to :document_analysis # Part of document analysis- Document → has_many DocumentAnalysis → has_many DocumentEntities
- DocumentEntities can be linked to any content type (polymorphic)
- Each entity tracks emotional scores (joy, sadness, fear, disgust, anger)
- User types
@and selects a page → inserts[[Type-ID]]token DocumentMentionJobprocesses these tokens on save- Creates DocumentEntity records linked to the mentioned pages
- When user clicks "Analyze this document"
- IBM Watson NLP service extracts entities from text
- Creates DocumentEntities for detected entities (people, places, things)
- Attempts to auto-match to existing pages via
match_notebook_page!
- In document analysis view, users can manually link detected entities
- "Link existing" button opens modal to select from user's pages
- Can also create new pages from detected entities
Each DocumentEntity tracks:
- Sentiment scores: joy, sadness, fear, disgust, anger (0-100)
- Overall sentiment: positive/negative/neutral with confidence score
- Relevance score: How important the entity is to the document
- Cards for each entity with:
- Image (if linked page has one)
- Relevance score with progress bar
- Emotional range radar chart
- Sentiment breakdown
- Links to view/edit the page
- Option to remove from analysis
- "Entities" button shows linked pages
- Quick reference with expandable previews
- Shows key attributes of linked pages
- Shows "Mentioned in Documents" section
- Lists all documents that reference this page
- View: Navigate to the content page
- Edit: Edit the content page
- Remove: Unlink from document analysis
- Create New: Create a new page for this entity
- Link Existing: Select from your existing pages
- Remove: Delete from analysis entirely
- Triggered on document save when body changes
- Scans for
[[Type-ID]]tokens - Creates DocumentEntities for @mentions
- Triggered when entity is added manually
- Calls IBM Watson to analyze sentiment
- Updates emotional scores
link_entity: Links detected entity to existing page or creates newdestroy_document_entity: Removes entity from analysisunlink_entity: Removes link between document and page
documents do
member do
get :analysis
post :queue_analysis
delete 'destroy_entity/:id' => 'documents#destroy_document_entity'
post :link_entity
end
end- Only document owner can link/unlink entities
- Linked pages must belong to document owner
- Analysis features require premium plan
- @mentions only work with user's own content
- Processes
[[Type-ID]]tokens in document body - Renders them as styled, clickable links
- Creates placeholder analysis when needed
- Manages relationship between documents and entities
- Extracts entities from text
- Provides sentiment analysis
- Calculates relevance scores
- User writes document with @mentions →
[[Character-123]]tokens - On save,
DocumentMentionJobcreates DocumentEntities - Optional: User clicks "Analyze" → Watson extracts more entities
- Entities appear in analysis view with emotions/sentiment
- User can:
- Link unmatched entities to existing pages
- Create new pages from entities
- Remove entities they don't want
- Linked pages show up in:
- Document's entity sidebar
- Page's "Mentioned in Documents" section
- Analysis view with full emotional data
This isn't just "linking pages" - it's:
- Contextual understanding: How entities relate emotionally to the document
- Bi-directional references: Documents know their entities, pages know their mentions
- Sentiment tracking: Understanding the emotional tone around each entity
- Auto-matching: AI tries to match extracted text to existing pages
- Quick reference: Instant access to linked page data while editing
- Analysis insights: Visual representation of entity emotions and relevance
The system essentially creates a knowledge graph of your world-building content with emotional and contextual metadata.