Get a Customized Website SEO Audit and Quantum SEO Marketing Strategy and Action Plan
This project introduces a practical and scalable technique that enhances document relevance and contextual understanding by combining sentiment polarity scoring with semantic similarity. Designed for real-world applications in SEO and content discovery, it analyzes content blocks from webpages, scores them using transformer-based sentiment models, and retrieves top-matching segments based on their alignment with the user query — both in meaning and sentiment.
The final output reveals not only the semantically similar sections but also their emotional tone (positive, neutral, or negative), which can inform SEO strategy, content refinement, and client-focused reporting. The modular and production-ready implementation is optimized for plug-and-play usage in SEO platforms.
Project Purpose
The primary purpose of this project is to improve the quality of information retrieval in long-form web content by going beyond traditional lexical or semantic similarity. While transformer embeddings provide a strong foundation for relevance matching, they often overlook emotional nuance and polarity — a critical factor in evaluating how well content aligns with user intent or brand tone.
By embedding sentiment polarity into the retrieval process, this project allows content strategists and SEO professionals to:
- Retrieve content segments that are not only topically aligned but emotionally resonant.
- Identify tone mismatches between user queries and existing webpage content.
- Gain deeper visibility into how specific parts of a page emotionally respond to different queries.
This approach refines both user experience and editorial decisions — benefiting clients in high-intent, competitive search verticals.
Project’s Key Topics Explanation and Understanding
This section explains the key concepts in the project “Polarity and Sentiment Embedding — Uses polarity and sentiment scores to refine the context and relevance of retrieved documents.” Understanding these concepts is essential for appreciating the value this project delivers in enhancing SEO and content targeting.
Polarity and Sentiment: What They Mean in SEO Context
Sentiment
Sentiment refers to the emotional tone expressed in a piece of content. It is generally categorized as:
- Positive
- Negative
- Neutral
In the context of SEO, sentiment plays a crucial role in identifying whether a section of content carries persuasive, optimistic, critical, or neutral messaging — helping align user intent with the tone of the content.
Polarity
Polarity is a numerical representation of sentiment, typically on a continuous scale:
- Negative polarity (< 0) for negative sentiment
- Positive polarity (> 0) for positive sentiment
- Zero or close to zero for neutral sentiment
While sentiment is categorical, polarity provides granularity — allowing prioritization or ranking of content blocks based on how strongly they express a sentiment.
SEO Relevance
- Positive sentiment content tends to perform better for intent-driven or purchase-oriented queries.
- Negative sentiment may be more useful in reviews or critical comparisons.
- Neutral sentiment is often preferred in objective, informational content.
Embedding-Based Context Understanding
What Are Embeddings?
Embeddings are numerical representations of text derived using advanced transformer-based language models. They capture not just the meaning of a word or sentence but also its context.
For example, the word “bank” will have different embeddings in:
- “river bank”
- “financial bank”
This capability makes embeddings highly effective for semantic-level document understanding.
Why Embeddings Matter in Retrieval
In SEO and content retrieval tasks:
- Embeddings help compare the semantic similarity between a user’s query and a section of content.
- Unlike keyword matching, embeddings can capture meaningful relevance even if no exact keywords overlap.
Sentiment-Aware Retrieval
By integrating sentiment embeddings with content retrieval:
- Content that matches both semantic meaning and emotional tone can be prioritized.
- Retrieval becomes more aligned with user expectations and engagement triggers.
Relevance Refinement Using Polarity and Sentiment
Traditional retrieval systems may score content only based on keyword or vector similarity. This project enhances that by:
- Adding a sentiment-based filter or weight to narrow down or rank content sections.
- Making sure the emotional tone of content aligns with the user’s search intent, especially in commercial, product-focused, or review-based domains.
This dual-scoring approach — combining contextual embedding similarity and sentiment polarity — leads to significantly more precise, intent-aligned retrieval.
Practical SEO Use Cases of These Concepts
· Sentiment Matching for Search Queries Helps retrieve content blocks with a sentiment that matches the emotional tone of user queries — improving bounce rates and session depth.
· Content Evaluation for SERP Optimization Allows SEOs to understand which parts of a page carry persuasive sentiment that can be surfaced or repurposed for better search snippets.
· Polarity-Based Block Prioritization Prioritizes or recommends blocks based on how strongly they support or criticize a concept, ideal for comparison or review-based articles.
· CTR Optimization Through Tone-Tuned Content Matching tone and polarity can improve Click-Through Rates when content tone aligns with ad or snippet messaging.
Q&A on Project Value
Why does sentiment and polarity scoring matter in SEO-driven content strategies?
In modern SEO, it’s no longer sufficient to simply target the right keywords — the tone and emotional resonance of your content now plays a major role in user engagement, retention, and conversions. Search engines like Google have moved toward intent-based ranking, where the content that best satisfies the user’s informational or emotional need is surfaced higher.
This project allows clients to quantify sentiment and emotional tone of individual content blocks on their pages. With this data, SEO strategists can:
- Identify sections that may be too neutral or off-tone for emotionally charged or persuasive queries.
- Improve targeting by making sure high-ranking content matches the tone of user search behavior.
- Repurpose or rewrite content blocks with strong positive sentiment for promotional use or call-to-action snippets.
Ultimately, sentiment and polarity provide new optimization levers beyond keywords — helping businesses deliver content that feels more relevant, trustworthy, and aligned with the user mindset.
How does this project help us identify the most impactful content sections on a page?
The project doesn’t just score entire pages — it breaks down each web page into individual content blocks, evaluates them separately, and returns detailed polarity and sentiment scores. This level of granularity helps clients:
- Spot which content blocks carry the strongest positive tone, often key in driving action (e.g., signups, purchases).
- Detect and revise negatively worded or dull blocks that may be hurting trust or conversions.
- Compare sentiment distribution across multiple pages or URLs, revealing which ones are most emotionally engaging.
By combining contextual relevance (embedding similarity) with sentiment tone, the system ensures that clients can target and showcase only the most meaningful and sentiment-aligned sections, reducing content noise and focusing user attention on high-performing text.
Can this project improve how our content ranks or appears in search results?
Yes, indirectly — but meaningfully. Search engines are increasingly tuned to engagement signals, such as bounce rate, dwell time, and user interaction. When a user clicks on a page and the content feels emotionally aligned with their intent, they are far more likely to:
- Stay longer on the site
- Click internal links
- Read full sections or complete CTAs
By using this system to identify and elevate content with positive or persuasive sentiment, businesses can:
- Make their content match searcher expectations better
- Use high-sentiment blocks in meta descriptions or snippets to increase CTR (Click Through Rate)
- Reduce mismatch bounce rates by ensuring the tone and message align with what users expect
These benefits indirectly influence search engine rankings, especially in competitive niches where small improvements in engagement metrics can make a big difference.
How can the project insights help guide future content strategy or editorial planning?
The insights generated from this project serve as a diagnostic tool for content tone. Editorial teams often write content with a particular intention, but over time, tone drifts or becomes inconsistent across a site. This system enables:
- Sentiment benchmarking across existing pages — identifying where tone is strong and where it needs improvement.
- Planning future content with target sentiment goals in mind. For example, a product page should lean more positive and persuasive, while an investigative article may lean neutral or critical.
- Comparing tone across competitors, if similar analysis is run on competitor pages.
Ultimately, this project helps editorial and SEO teams align content production with intent and emotional tone, ensuring that what they write is not just informative, but also contextually and emotionally appropriate for their audience.
What kind of business decisions can we make using the sentiment and polarity data from this system?
The system produces structured, page-level and block-level data that can be used to:
- Decide which content blocks should be highlighted in search snippets or internal landing pages.
- Guide CRO (Conversion Rate Optimization) teams to rework or A/B test content blocks with weak sentiment.
- Inform design and layout decisions, e.g., placing highly positive blocks in prominent positions on a landing page.
- Build personalization systems that surface different blocks to users based on their engagement history or sentiment preferences.
For multi-page or multi-URL scenarios, this data can be aggregated and compared, providing leadership and marketing teams with insights into which content types and tones are performing better — enabling better prioritization in content production and marketing investment.
Libraries Used
requests
The requests library is a widely used Python module for sending HTTP/1.1 requests. In this project, it serves as the foundational tool to fetch HTML content from client-provided URLs. By issuing GET requests, the library helps retrieve page data in a robust and simple manner, facilitating the initial step of content extraction. Its reliability, error-handling capabilities, and wide adoption in production environments make it a trusted choice for real-world data ingestion tasks such as those required in this SEO-oriented project.
BeautifulSoup (from bs4) and Comment
BeautifulSoup is used to parse and navigate HTML or XML content. In the context of this project, it plays a central role in extracting meaningful textual content from raw HTML while removing elements irrelevant to SEO and document analysis—such as scripts, styles, and hidden sections. Additionally, the Comment class is used to identify and exclude comment blocks from the final content. Its intuitive syntax and powerful DOM traversal capabilities make it highly effective for content segmentation in web-based data science applications.
re, html, and unicodedata
These three Python standard libraries are essential for content cleaning and normalization:
- re (Regular Expressions) is used to define and apply text patterns for tasks such as removing special characters, formatting anomalies, or HTML entities not cleaned through initial parsing.
- html helps in unescaping HTML entities (e.g., converting & to &), ensuring cleaner, human-readable text.
- unicodedata standardizes Unicode character encoding, particularly useful for filtering out non-ASCII characters and normalizing content across different web encodings.
Together, they support robust, real-world text preprocessing to ensure the extracted content is ready for model inference and downstream analysis.
transformers from HuggingFace
This library provides access to pre-trained transformer models, which are integral to this project. Specifically, it is used to load a sentiment analysis model via AutoTokenizer, AutoModelForSequenceClassification, and AutoConfig. The utils submodule is employed to suppress unnecessary logging and progress bars, ensuring a clean, silent runtime—especially important in client-facing applications. This library empowers the polarity and sentiment scoring mechanism that refines contextual relevance of content blocks and aligns them with user queries.
torch and torch.nn.functional
PyTorch is the backend framework for running transformer models. It provides tensor operations, GPU support, and model execution capabilities. In this project, torch executes the forward pass of the sentiment classifier and manages model-device compatibility (CPU/GPU). Additionally, F.softmax from torch.nn.functional converts raw model outputs (logits) into interpretable probabilities across sentiment classes. These probabilities are then used to determine both polarity (positive/negative) and sentiment confidence levels, enabling the system to rank and filter content blocks with high contextual precision.
sentence_transformers
The sentence_transformers library is used to generate contextual embeddings for content blocks and user queries. It loads a transformer-based model (e.g., all-mpnet-base-v2) to encode textual segments into high-dimensional vectors. These embeddings are critical for computing semantic similarity scores between queries and content blocks using cosine similarity. This library abstracts away much of the complexity of tokenization and model inference, offering a clean interface optimized for sentence-level representation learning in retrieval and ranking tasks.
numpy and sklearn.metrics.pairwise.cosine_similarity
numpy supports efficient array and matrix operations, which are frequently used in embedding and similarity calculations. Combined with cosine_similarity from sklearn.metrics.pairwise, it enables computation of semantic closeness between query and content vectors. This similarity score is central to determining the relevance of a block to a query, and subsequently, ranking them for display or decision-making. The use of these libraries ensures fast, reliable, and scalable similarity evaluation.
matplotlib.pyplot and seaborn
These libraries are used for visualizing the results and analytical outputs. matplotlib.pyplot provides foundational plotting capabilities, while seaborn enhances the aesthetic and readability of visual plots such as sentiment distribution, similarity rankings, and comparative block analysis across URLs and queries. Together, they help stakeholders intuitively interpret model decisions and understand how sentiment and context influence content relevance—a key factor in client reporting and SEO impact analysis.
pandas
pandas offers structured data manipulation tools. In this project, it plays a supporting role in formatting and transforming the content block data, sentiment labels, and similarity scores into tabular formats suitable for plotting or exporting. Its compatibility with other libraries like seaborn and its ease of use for grouping, filtering, and pivoting makes it essential for any production-grade reporting and visualization workflow.
Function extract_blocks()
Overview
The function extract_blocks() is responsible for retrieving and preparing high-quality textual content from a client-provided webpage URL. It acts as the foundation of the project pipeline by collecting the raw data that is later enriched with polarity and sentiment information. Its design ensures that only meaningful, human-visible, and non-duplicate content is included—making it highly valuable for downstream SEO analysis.
Key Code Explanations
The following are important lines of code within this function that define its robustness and quality:
· response = requests.get(url, headers=headers, timeout=timeout)
This line makes an HTTP GET request to the input URL with a defined timeout and custom headers. The headers simulate a request from a real browser to avoid being blocked by the website. If the request fails, it raises a clear exception with the cause, ensuring that calling processes can handle the error cleanly.
· soup = BeautifulSoup(html_text, “lxml”)
This line initializes a BeautifulSoup object using the lxml parser, which is one of the fastest and most robust parsing engines. It converts the raw HTML into a structured parse tree that can be easily navigated to extract and manipulate elements.
· for tag in soup([“script”, “style”, …]): tag.decompose()
This loop removes all elements that typically do not contain main textual content (scripts, stylesheets, navigation, forms, etc.). Removing these early ensures that the content blocks later extracted are not polluted by irrelevant or noisy text.
· if “display:none” in tag[“style”].replace(” “, “”).lower(): tag.decompose()
This line handles inline styles by checking if an element is hidden using the CSS property display:none. It ensures that even if a paragraph or header tag is technically in the HTML, but not visible to users, it is excluded from analysis.
· text = tag.get_text(separator=” “, strip=True)
This extracts the text from each tag using a space separator, preserving the word boundaries while removing unnecessary whitespace. It’s essential for turning HTML into plain, analyzable text.
· if len(text.split()) < min_words: continue
Short, low-value content blocks are filtered out based on a minimum word count threshold. This improves quality by removing things like button text, single-word list items, or irrelevant fragments.
· ascii_ratio = sum(ord(c) < 128 for c in text) / len(text)
This calculates the proportion of ASCII characters in a block of text. Blocks that are mostly non-English or made up of special characters (e.g., symbols, code, or icon text) are excluded if they fall below a quality threshold.
· digest = hash(text.lower()); if digest in seen: continue
Deduplication is achieved using hashed versions of each content block. If a block has already been processed (e.g., the same paragraph repeated in different page sections), it is skipped to maintain uniqueness across blocks.
Function: filter_blocks() — Cleans and Filters Raw Content Blocks
Overview
The filter_blocks() function ensures that the raw content extracted from web pages is further refined and cleaned before being passed to polarity and sentiment scoring stages. This step eliminates boilerplate phrases, unwanted URLs, and common formatting issues, producing a list of concise, relevant, and readable blocks suitable for contextual scoring.
It takes a list of raw content blocks (strings) as input and returns a filtered list of high-quality blocks after cleaning and validation.
Key Code Explanations
· boilerplate_patterns = re.compile(…)
This regular expression targets typical non-informative phrases often found in footers, subscription prompts, disclaimers, and copyright notices. Removing such boilerplate content prevents noise and improves result quality for SEO and NLP applications.
· url_pattern = re.compile(r’https?://\S+|www\.\S+’)
This pattern removes embedded URLs from blocks. URLs are usually irrelevant for contextual analysis and sentiment scoring, and their removal simplifies the text while reducing distraction.
· substitutions = {…}
This dictionary maps typographic characters (e.g., curly quotes, long dashes, non-breaking spaces) to their plain ASCII equivalents. It ensures consistency and prevents formatting irregularities from affecting model inputs.
· def clean_block(text: str) -> str:
This inner function applies all cleaning steps to a single text block. The process includes:
- HTML entity decoding (html.unescape)
- Unicode normalization (unicodedata.normalize(“NFKC”))
- Regex-based removal of boilerplate phrases and URLs
- Substitution of non-standard characters with standard ones
- Whitespace normalization and trimming
This encapsulated design allows easy reuse and testing of the cleaning logic.
· if len(cleaned.split()) >= min_words:
After cleaning, this check filters out any blocks that fall below a minimum word threshold (default: 5 words). This prevents short, non-contextual fragments from entering further stages.
This function is essential in enhancing the quality of textual input by enforcing a consistent and noise-free structure across all content. Its integration ensures that only meaningful text is passed through for semantic analysis, making the downstream results more actionable and relevant for SEO clients.
Function: load_sentiment_model()
Overview
This function loads a transformer-based sentiment classification model along with its tokenizer and configuration. The selected model is cardiffnlp/twitter-roberta-base-sentiment-latest, a version of RoBERTa fine-tuned for sentiment analysis on social and general text. The function ensures the model is ready for inference and optimized to use available GPU resources if present.
By modularizing the model loading logic, this function supports flexibility in model selection while maintaining reproducibility and deployment-readiness. It returns the model, tokenizer, and configuration object needed for subsequent inference and label decoding tasks.
Key Code Explanations
· AutoTokenizer.from_pretrained(model_name)
Initializes a tokenizer compatible with the selected transformer model. The tokenizer is responsible for converting raw text into token IDs required for model input. This ensures consistency with the model’s pretraining vocabulary and tokenization rules.
· AutoConfig.from_pretrained(model_name)
Loads the configuration object that contains model-specific metadata, such as label mappings and architecture details. This is especially useful when decoding predicted labels or inspecting the model’s classification heads.
· AutoModelForSequenceClassification.from_pretrained(model_name)
Loads the sentiment classification model with pretrained weights. The function ensures that the exact architecture and checkpoint corresponding to the model name are retrieved.
· torch.device(“cuda” if torch.cuda.is_available() else “cpu”)
Dynamically selects the device for inference, preferring GPU if available for performance benefits. This enables the function to run efficiently in both local and cloud environments.
· model.to(device) and model.eval()
Transfers the model to the selected device and sets it to evaluation mode. The evaluation mode disables training-specific behaviors like dropout, ensuring deterministic predictions during inference.
This function forms the foundation of the polarity and sentiment scoring pipeline by making a robust, high-performance sentiment model available in a flexible and ready-to-use form.
Sentiment Analysis Model
Model Name: cardiffnlp/twitter-roberta-base-sentiment-latest
This model is a transformer-based sentiment classifier developed and released by Cardiff NLP. It is based on the RoBERTa architecture and fine-tuned specifically for sentiment analysis tasks.
Architecture Overview
The model architecture follows the RobertaForSequenceClassification structure, consisting of the following key components:
· Embedding Layer Includes word, position, and token type embeddings of dimension 768, combined with layer normalization and dropout. This layer encodes the input tokens into dense representations.
· Encoder Layers (12 Layers) Each encoder layer includes:
- Multi-Head Self Attention: Uses the RobertaSdpaSelfAttention module with query, key, and value projections to capture token-to-token relationships in the input sequence.
- Feed-Forward Network: Applies a two-layer dense network with GELU activation to transform the attention outputs.
- Residual Connections & LayerNorm: Used between subcomponents for stable training and better convergence.
· Classification Head A dense layer followed by dropout and an output projection layer. It maps the final [CLS] token embedding to one of three sentiment classes:
- Positive
- Neutral
- Negative
Why This Model Was Used in This Project
This specific model was selected for several practical reasons aligned with the goals of refining document-level retrieval using sentiment and polarity signals:
· Context Sensitivity The RoBERTa backbone enables the model to analyze sentiment in a context-aware manner. This is essential for technical or SEO content, where sentiment is often implicit and context-dependent.
· Optimized for Real-World Text Although trained on Twitter data, the model generalizes well to short-to-medium form content, such as sentence blocks in SEO documents. This property is important for analyzing individual content blocks rather than full pages.
· Balanced Sentiment Classes The model produces neutral sentiment predictions with high confidence where appropriate, helping avoid false polarities in objective or instructional content—common in documentation and technical pages.
· High Confidence Scores The model outputs probability scores (via softmax) for each sentiment class, which are used as confidence values in this project to aid in ranking and filtering content.
· Lightweight and Fast With 12 transformer layers and a 768-dimensional hidden size, it offers a balance between accuracy and computational efficiency—critical for scaling across multiple documents.
Role in the Project
The model plays a central role in generating sentiment polarity signals that help evaluate and refine the relevance of document content in response to user queries. Specifically:
- Sentiment scores are combined with semantic similarity scores.
- Sentences with neutral or appropriately polarized sentiment and high confidence are considered more relevant for informational queries.
- Helps differentiate between instructional, promotional, or misleading blocks in a page.
This model’s output is integrated into the final ranked results that guide retrieval and relevance assessments.
Function: score_sentiment_blocks()
Overview
The score_sentiment_blocks() function takes a list of textual content blocks and applies a transformer-based sentiment model to assign each block a sentiment label and a confidence score. This is a core component of the project’s goal—to analyze and embed sentiment signals from web content for enhancing relevance and document understanding.
By processing each content block independently and using softmax-normalized logits from the model, the function generates structured outputs that capture the polarity and intensity of sentiment for each section. These outputs are later used to enrich document representation for SEO-focused retrieval and analysis tasks.
Key Code Explanations
· device = next(model.parameters()).device
Retrieves the device (CPU or GPU) on which the model has been loaded. This ensures that all inputs are correctly mapped to the same device, avoiding runtime errors and enabling hardware acceleration.
· text = block.strip(); if not text: continue
Removes whitespace and skips any empty blocks. This ensures that only valid content is passed through the sentiment model, maintaining data quality.
· tokenizer(text, return_tensors=”pt”, truncation=True, max_length=max_length)
Encodes each text block into token IDs and attention masks expected by the transformer model. The truncation=True parameter ensures that overly long blocks are safely truncated without causing errors.
· inputs = {k: v.to(device) for k, v in encoded.items()}
Transfers the encoded input tensors (e.g., input_ids, attention_mask) to the appropriate device (GPU or CPU), keeping model and data colocated for performance and correctness.
· with torch.no_grad(): output = model(**inputs)
Disables gradient tracking for inference, reducing memory usage and increasing speed. The model is then called with the input to produce raw logits for each sentiment class.
· probs = F.softmax(output.logits, dim=1)[0]
Applies the softmax function to the model’s raw logits to convert them into probabilities for each sentiment label (e.g., positive, neutral, negative). Only the first (and only) item in the batch is used.
· label_id = int(probs.argmax().cpu().item())
Identifies the index of the highest-probability class, which corresponds to the predicted sentiment.
· label = config.id2label[label_id]
Converts the class index into a human-readable label (e.g., “Positive”) using the model’s configuration object.
· confidence = round(probs[label_id].cpu().item(), 4)
Extracts and rounds the probability of the predicted label. This score indicates how confident the model is in its sentiment prediction.
· results.append({…})
Appends a dictionary containing the original block text, the predicted label (converted to lowercase), and the associated confidence score to the results list.
This function plays a critical role in quantifying the emotional tone of web content at a fine-grained level. The resulting sentiment embeddings can help clients better understand user perception, adjust content strategy, and improve the contextual matching of pages to user queries.
Function: load_embedding_model()
Overview
This function loads a high-performance sentence embedding model from the Sentence-Transformers library, specifically optimized for semantic understanding of text. It checks for GPU availability and ensures that the model is loaded on the appropriate device for efficient computation.
This embedding model is a foundational component in the project, enabling the transformation of textual content into dense vector representations that encode semantic meaning. These embeddings are later used in downstream tasks like document similarity, sentiment alignment, and retrieval refinement.
Key Code Explanations
· model_name: str = ‘all-mpnet-base-v2’
The default model used is all-mpnet-base-v2, a state-of-the-art sentence embedding model that provides strong performance on a wide range of semantic similarity tasks. This parameter can be overridden to use alternative models depending on specific client needs or use cases.
· torch.cuda.is_available()
Checks whether a compatible GPU is available on the system. If true, the model will be loaded on the GPU to leverage faster matrix operations and reduce inference time. Otherwise, it defaults to CPU execution.
· SentenceTransformer(model_name, device=…)
Instantiates the model from the Sentence-Transformers framework. The device parameter ensures that the model is loaded onto the correct hardware backend (‘cuda’ or ‘cpu’) automatically.
The loaded embedding model serves as the engine behind semantic scoring and contextual representation, which are critical for enhancing content relevance and aligning document sections with user intent. This abstraction also keeps the main pipeline modular and flexible, allowing for easy upgrades or model switches in the future.
Contextual Embedding Model
Model Name: all-mpnet-base-v2
This model is part of the Sentence-Transformers library and is built upon Microsoft’s MPNet architecture. It is optimized for generating dense semantic embeddings for text segments and is widely used in tasks involving semantic similarity, clustering, ranking, and retrieval.
Architecture Overview
The all-mpnet-base-v2 model is implemented using the SentenceTransformer framework and comprises the following components:
· Transformer Layer Based on the MPNet backbone (MPNetModel), configured with a maximum sequence length of 384 tokens. This layer encodes input tokens into contextual embeddings.
· Pooling Layer Uses mean pooling across token embeddings to generate a single vector representation per content block. This approach captures the overall semantic meaning of the segment.
· Normalization Layer Applies L2 normalization to ensure unit-length embeddings, enabling accurate cosine similarity comparisons.
This architecture produces 768-dimensional sentence embeddings optimized for semantic similarity tasks, ranking, and retrieval.
Why This Model Was Used in This Project
The selection of all-mpnet-base-v2 was based on its performance, reliability, and compatibility with context-sensitive tasks required in content analysis and document-level retrieval:
· High Performance on Semantic Tasks It consistently ranks among the top models on the MTEB (Massive Text Embedding Benchmark), making it suitable for sentence-level and paragraph-level semantic retrieval.
· Rich Context Representation MPNet outperforms traditional BERT-based encoders in preserving nuanced context, especially important when analyzing subtle semantic signals in SEO content, such as instructional tone, informational specificity, or sentiment carryover.
· Embedding Quality for Short Segments This model excels in producing high-quality embeddings for short to medium-length text blocks—ideal for the use case where page content is split into logical, filtered blocks.
· Efficient Similarity Computation The output embeddings are normalized and designed for cosine similarity calculations, directly feeding into the sentence-ranking and scoring modules in this project.
· Integration-Ready in Retrieval Systems The standardized output format and the SentenceTransformer compatibility make it straightforward to scale across large document sets with minimal pre- or post-processing.
Role in the Project
The model serves as the foundation for computing contextual semantic embeddings of individual content blocks. These embeddings are used to:
- Measure similarity between user queries and document segments.
- Rank content blocks based on semantic relevance.
- Support retrieval decisions in combination with polarity/sentiment scores.
By using all-mpnet-base-v2, the system gains both fine-grained contextual understanding and scalable performance for real-world document filtering and relevance scoring tasks.
Function: get_sentiment_embeddings()
Overview
This function generates high-quality, sentiment-aware embeddings for content blocks by combining contextual information with sentiment polarity. Each content block is prefixed with its corresponding sentiment label (e.g., [POSITIVE], [NEGATIVE]) to inject additional context into the embedding process. The embeddings are then computed using a pre-loaded sentence transformer model, which encodes the text into dense numerical vectors suitable for similarity computations or clustering.
This approach ensures that sentiment is not just a parallel metric, but is embedded directly into the semantic representation of the content. Such embeddings improve the alignment between user intent and content retrieval by emphasizing emotional tone alongside lexical meaning.
Key Code Explanations
· sentiment_scored_blocks: list[dict] | dict
This input expects a list of dictionaries, each containing a “text” and “label” key. These represent content segments and their corresponding sentiment classifications (e.g., positive, negative, or neutral). A single dictionary input is also supported for flexibility in future use cases or one-off evaluations.
· texts = [entry.get(“text”, []) …] and labels = [entry.get(“label”, []) …]
These lines extract the raw text content and associated sentiment label from each content block. Defaulting to an empty list ensures the function fails gracefully in case of missing fields.
· sentiment_texts = [f”[{label.upper()}] {text}” …]
This step is the core of sentiment injection. Each content block is prefixed with a capitalized sentiment tag in square brackets. This token influences the transformer model to consider emotional tone during encoding, enabling more context-sensitive embeddings.
· model.encode(…, convert_to_numpy=True)
Uses the sentence transformer model to convert the prefixed texts into embeddings. The output is a NumPy array for each block, making it efficient to use in subsequent vector-based operations such as cosine similarity or clustering.
By incorporating sentiment directly into the embedding representation, the function enables more nuanced document matching and retrieval, particularly useful in domains like SEO, product reviews, and customer experience analysis, where emotional tone matters as much as content relevance.
Function: get_sentiment_embeddings()
Overview
This function transforms content blocks into sentiment-enriched embeddings by combining their semantic meaning with their emotional tone. It does this by prefixing each content block with a sentiment label (e.g., [POSITIVE], [NEGATIVE], [NEUTRAL]) before passing it to a transformer-based embedding model. The resulting vector representations are context-sensitive, making them well-suited for downstream tasks like relevance ranking, clustering, or document retrieval with sentiment awareness.
This embedding method is critical in refining content understanding for use cases such as content quality evaluation, audience sentiment analysis, or emotional tone-based search optimization.
Key Steps Explained
Inputs
· sentiment_scored_blocks: This is a list of dictionaries, each containing two keys:
- “text” — the actual content block to be encoded
- “label” — the sentiment label associated with the block (e.g., “positive”, “negative”)
The function also gracefully supports a single dictionary input, making it flexible for both bulk and single-content evaluation scenarios.
· model: A preloaded SentenceTransformer model used to convert text into numerical vector embeddings. This model captures deep contextual semantics from the input text.
Text and Sentiment Extraction
· Each block’s text and associated sentiment label are extracted in parallel. If any of these lists are empty (due to malformed input or missing fields), the function safely returns an empty result — maintaining robustness.
Sentiment-Aware Text Composition
· sentiment_texts = [f”[{label.upper()}] {text}” for label, text in zip(labels, texts)]
This step constructs a new version of each text block by prefixing the sentiment label in uppercase within square brackets. For example:
- Original: “The service was fast and friendly.” with label “positive”
- Modified: “[POSITIVE] The service was fast and friendly.”
This prefix acts as a semantic cue for the embedding model, allowing it to interpret the emotional context of the sentence during encoding.
Embedding Generation
· embeddings = model.encode(sentiment_texts, show_progress_bar=False, convert_to_numpy=True)
The modified texts are passed to the sentence transformer model. The encode() function returns dense vector representations for each block. These embeddings:
- Encode both the lexical meaning and the sentiment tone
- Are output as NumPy arrays for efficient processing in downstream similarity or clustering tasks
By embedding sentiment directly into semantic space, this function makes the entire system more sensitive to real human tone — a key differentiator in modern AI-powered search and recommendation systems.
Function: display_sentiment_retrieval_results()
This function presents sentiment-aware content retrieval results in a clear, readable format. For each of the top retrieved content blocks, it displays the sentiment label, confidence score, similarity score, and a shortened version of the content text. This summary helps clients quickly understand how their content blocks align with user sentiment and intent.
The function is primarily used to inspect and validate output relevance for a given query, showing whether the retrieved blocks are emotionally and contextually appropriate. It’s a useful diagnostic or presentation tool in practical SEO analysis, content audits, or stakeholder reporting.
Result Analysis and Explanation
URL Analyzed: https://thatware.co/handling-different-document-urls-using-http-headers/
Query Asked: “how to manage multiple document versions with HTTP headers”
This section explains the output produced from your page when matched with the above query. It helps you understand what the result means, how your content responds to the search intent, and what improvements or actions may be considered.
Understanding Result
The system has extracted specific content blocks from your page that are most contextually aligned with the user query. Each block is scored by:
- Sentiment: Tone of the passage (e.g., informative, negative, etc.)
- Confidence: Model’s certainty in the sentiment classification
- Similarity: How contextually close the block is to the query
These are not keyword matches — they’re contextual relevance matches based on how well your content answers or aligns with the meaning of the query.
How This Content Answers the Query
The blocks retrieved all relate to technical implementation of HTTP headers for managing documents. Specifically, they focus on:
- Configuring servers like Nginx
- Using .htaccess for canonical tags
- Structuring headers for non-HTML files like PDFs or images
These directly support the query intent, which seeks practical ways to manage multiple versions of documents via headers. This shows your content not only mentions the topic but also provides implementation-level details, which search engines may favor for informational queries.
Tone and Trustworthiness
All retrieved blocks are tagged neutral, with high confidence (avg. ~0.89), which is ideal for how-to, instructional, or reference queries. This suggests your content maintains a helpful, unbiased tone — exactly what users expect when searching for technical guidance.
This neutral tone can:
- Improve trust in your content
- Help prevent misunderstandings or confusion
- Support clarity in instructional contexts
Key SEO Strengths in the Content
From an SEO and retrieval perspective, your page demonstrates several strong characteristics:
- High contextual alignment: All blocks have similarity scores > 0.43, indicating good coverage of the query topic
- Varied implementations covered: Multiple server setups and header strategies are discussed
- Information density: Content blocks are direct, technical, and task-focused, which improves scannability
These qualities improve your chances of visibility for similar long-tail or implementation-oriented queries.
Recommended Actions
To strengthen your page further:
- Ensure key blocks are indexable: Make sure these high-relevance paragraphs are not hidden by CSS or JavaScript.
- Optimize structural clarity: Use subheadings like “Managing Document Versions with Headers” or “Canonical Headers for PDFs” to guide both users and crawlers.
- Add semantic reinforcement: Consider adding short introductory lines or summaries to these blocks to reinforce their purpose.
- Link internally to these sections if possible, to increase navigability and signal importance.
Result Analysis and Explanation
This section explains how to understand and interpret the results generated by the Polarity and Sentiment Embedding model. The results consist of content segments extracted from the URLs, scored and ranked based on both their semantic relevance to the input query and their emotional alignment, i.e., the sentiment polarity and its strength.
The output aims to highlight which parts of your existing content are most aligned with user intent—specifically, what users are searching for and how positively your content addresses those needs.
Structure of the Output
Each result entry includes the following components:
- Sentiment Polarity: Whether the tone of the content is Positive, Negative, or Neutral in the context of the query.
- Sentiment Confidence Score: A numerical score (between 0 and 1) indicating how strong or certain the sentiment prediction is.
- Semantic Similarity Score: A score (also between 0 and 1) measuring how closely the block of content semantically matches the query.
- Content Block Text: A meaningful segment extracted from your web page content, selected based on structural, semantic, and contextual cues.
These outputs are repeated across all provided URL-query pairs, allowing for a comparative and comprehensive view of how each page responds to each target search intent.
Understanding the Sentiment and Confidence Scores
Sentiment Types
- Positive content reflects supportive, encouraging, or beneficial statements related to the query.
- Negative content may indicate gaps, drawbacks, or misalignments with the query.
- Neutral content is typically factual, informative, or neither strongly supportive nor dismissive of the query.
In most SEO and marketing contexts, positive sentiment is ideal, particularly when aligned with high relevance.
Confidence Score Bins (Generalized)
The sentiment confidence score is grouped into qualitative bins for easier interpretation:
- Very High Confidence (≥ 0.85): The sentiment is very clear and reliable. These are the strongest indicators of how the content emotionally connects with the query.
- High Confidence (0.75 – 0.84): The sentiment is strong and dependable, offering meaningful insight into user perception.
- Moderate Confidence (0.65 – 0.74): The content expresses the sentiment clearly, but might include mixed language or less assertive phrasing.
- Low Confidence (< 0.65): Sentiment is uncertain or weak. These blocks may contain vague or overly generic phrasing, or the emotional tone might be mixed or ambiguous.
Note: Higher confidence sentiment blocks are more actionable. If relevant and aligned with the query, they can be used as anchors for CTA (Call-To-Action) placement, internal linking, or content amplification.
Understanding Semantic Similarity Scores
Semantic similarity is measured by how well the meaning of a content block aligns with the intent behind the query. It’s not just about keyword matching; it’s about contextual relevance.
eral Interpretation of Similarity Scores:
- 0.40 and above: High semantic relevance. The content is topically and contextually aligned with the query. These are often the best content matches.
- 0.30 to 0.39: Moderate to high relevance. The content is contextually relevant but might not directly answer the query or use less precise phrasing.
- 0.20 to 0.29: Moderate relevance. These may need optimization or enrichment to better target the query intent.
- Below 0.20: Low relevance. The content might be loosely related or off-topic and may require major revision to support the query.
Visual Analysis of Multi-Query Results Across URLs
After processing multiple queries across multiple URLs, the final result includes not just the most relevant content blocks per query but also accompanying sentiment and similarity scores. To make these results digestible and interpretable at scale, we use a dedicated visualization pipeline with three plots. Each plot is designed to help stakeholders (content teams, SEO strategists, or clients) quickly identify actionable insights.
Sentiment Distribution of All Content Blocks per URL
What It Shows: This grouped bar plot displays how content blocks across different URLs are distributed by sentiment category (positive, negative, neutral).
Purpose and Interpretation:
- Helps detect whether a site’s content tone is consistent with the intended brand voice.
- If most blocks are negative or neutral in sentiment, that may signal a need to adjust tone or structure for reader engagement.
- By using short_url on the x-axis, the plot is compact even with long domain names.
User Actions Based on This Plot:
- If most blocks under a query show negative sentiment, evaluate the tone of content under that topic.
- If one URL shows a highly positive skew, assess how that content structure differs and replicate it.
Similarity Score Distribution by Sentiment and URL
What It Shows: A boxplot comparing similarity scores (relevance) of retrieved blocks by their sentiment label across different URLs.
Purpose and Interpretation:
- Reveals how sentiment correlates with relevance.
- Clients can see whether positive content is actually less relevant (i.e., lower similarity) or vice versa.
- This plot differentiates between highly emotional content and relevant content.
User Actions Based on This Plot:
- If positive blocks are consistently lower in similarity, content strategy may need balancing tone with topical depth.
- If neutral blocks are more relevant, consider making them more engaging without reducing their relevance.
Average Similarity Score per Query Grouped by URL
What It Shows: A grouped bar plot that shows the average similarity score for top-k blocks per query, grouped by URL.
Purpose and Interpretation:
- Directly reflects which URL performs best for each query in terms of semantic match.
- This is critical for competitive comparison: seeing which page handles which intent better.
User Actions Based on This Plot:
- Identify which URL outperforms others for a query — and analyze what’s working on that page.
- For queries where a client’s own URL underperforms, pinpoint optimization areas (content structure, keyword integration, contextual coverage).
Key Takeaways
- Multiple relevant segments per URL: The tool surfaces several high-relevance, high-sentiment sections per page. This offers flexibility in deciding where to focus optimization efforts.
- Cross-query insights: If a URL performs well across different but related queries, it reflects content robustness and multi-intent coverage.
- Content mismatch indicators: Low similarity + low sentiment blocks can highlight mismatches or neglected sections that fail to support the user’s needs.
- Sentiment clarity matters: Even content that’s topically relevant might be overlooked by users if it’s not framed positively or confidently.
How to Interpret the Results Effectively
- Prioritize blocks with both high similarity and high sentiment confidence for content optimization, internal linking, or marketing focus.
- Flag content with low similarity but on important URLs or queries as potential areas for content enrichment.
- Identify underutilized positive content on secondary pages that could be leveraged more prominently.
- Review repeated blocks across queries to ensure that they’re not overused or generic—query-specific tailoring may be required.
How to Benefit from These Results
To make the most of this analysis:
- Enhance content using high-performing blocks: Use insights from top segments as models for rewriting or expanding weaker areas.
- Improve internal linking: Link from weaker pages to stronger sentiment/relevance blocks to guide users toward more impactful content.
- Focus on top-performing sentiment: Promote content with the clearest positive sentiment and alignment with business goals.
- Plan future content strategy: Identify recurring themes that yield high sentiment and similarity, and use them to inform blog topics, landing page updates, or FAQ content.
- Address content gaps: Where results show low sentiment or off-topic content, consider editorial updates, structural revisions, or creating new dedicated sections.
This result framework empowers to take specific, informed actions to strengthen SEO performance—not just at a technical level, but also in terms of user trust, relevance, and emotional engagement.
What do the sentiment and similarity scores in the result actually mean for my SEO strategy?
The sentiment score reflects the overall emotional tone of the matched content—whether it is positive, negative, or neutral in tone. In the context of SEO, a positive sentiment in content closely related to your query signals that the tone of the document supports or aligns with your topic—something especially important for trust-building and brand perception.
The similarity score measures how semantically close a content block is to your query after it has been expanded with sentiment and polarity-aware embeddings. A higher similarity score means the content is contextually relevant to the search intent behind the query. This allows users to identify the most impactful sections of their website for that specific search scenario.
Users should use this information to:
- Highlight high-similarity, high-sentiment content in on-page SEO strategies.
- Reassess low-similarity blocks even if sentiment is high—they may need content rewrites for contextual alignment.
How do I identify high-performing content blocks from the result?
Content blocks with both high similarity and high sentiment scores are top performers. These are the blocks most aligned with the expanded query intent and present a favorable tone. They represent the most valuable content from a search relevance and user experience perspective.
For example, if your page contains multiple blocks of content, this analysis will point you to the exact paragraphs that contribute most toward SEO performance for a specific query. Prioritize these in:
- Internal linking strategies
- Featured snippets targeting
- Meta descriptions
- Section-level A/B testing
What actions should I take for blocks with moderate or low scores?
When scores fall in the moderate range (e.g., similarity between 0.30–0.40 or sentiment confidence between 0.60–0.75), those blocks may still be valuable, but might need contextual fine-tuning. This could include:
- Optimizing sentence structures to better align with the query’s meaning.
- Updating keywords using query-specific terminology.
- Refining emotional tone where it doesn’t reflect positivity, especially for commercial or informational intent.
Low-scoring content doesn’t necessarily need to be removed, but it should be:
- Monitored for traffic performance.
- Considered for rewriting or repositioning on the page.
- Marked for follow-up testing after updates are applied.
Can these results help me restructure my content layout for SEO benefit?
Yes. This type of analysis helps uncover which sections truly carry the most SEO weight for specific queries. With that knowledge, you can:
- Reorder blocks on the page to place high-performing content earlier (above the fold).
- Design new content modules around high-similarity themes uncovered in the analysis.
- Restructure pages for ###ry-aligned storytelling, improving both relevance and dwell time.**
This leads to more effective information architecture, which not only improves rankings but also user satisfaction.
How can this project be integrated into my ongoing SEO workflow?
This result layer becomes a part of your continuous content audit and optimization cycle. By running this periodically across key landing pages and updated queries, you will:
- Track how content evolves with algorithm updates.
- Detect opportunities to repurpose existing content for new semantic angles.
- Train your writing or content team to create better-targeted content right from the start.
For users working with multiple domains or large sites, this analysis can scale to prioritize which URLs or topics to tackle first—based on actual impact rather than guesswork.
Yes — adding more Q&A pairs specifically to highlight the project’s unique features based on actual output is not only practical but highly effective in real-world client reports. These questions help:
- Translate technical output into value-focused insights,
- Reinforce why the project’s methodology matters, and
- Show how each feature solves a real SEO pain point.
How does the use of sentiment-aware embeddings give this project an edge over regular content matching?
Traditional relevance scoring often fails to consider how the content says something—not just what it says. Our sentiment-aware embeddings factor in both context and emotional tone, which ensures:
- A match that reflects the intent behind the query, not just term overlap.
- Better alignment with search engine preference for high-quality, trust-inspiring content.
- Identification of content that’s not just topically correct but user-experience optimized.
In the results, this is visible when content with slightly lower term similarity but high positive sentiment ranks higher due to richer, more confident messaging—highlighting better SEO performance potential.
How does this system handle ambiguity in user queries?
Ambiguous or emotionally loaded queries (e.g., “is this worth it”, “why people avoid this”) are hard to match using keyword-based logic alone.
This approach interprets both the tone and contextual polarity of such queries and expands them using advanced embeddings. This enables matching against content that addresses the real concern or motivation behind the query.
As a result, users get visibility into which parts of their content truly resolve user hesitation or align with user intent, even when queries don’t have clear-cut keywords.
What is the role of polarity scoring in these results, and how does it help SEO decisions?
Polarity scoring helps differentiate positive vs. negative sentiment in the most granular way, which is crucial in SEO for:
- Deciding which blocks to promote or demote.
- Identifying if content conveys trust, fear, excitement, or dissatisfaction, even if the topic is correct.
- Creating landing pages that feel balanced, credible, and aligned with commercial or informational intent.
In the results, polarity helps separate blocks that are “on-topic but risky” from those that are “on-topic and persuasive”—a distinction that’s critical for search trust and conversion optimization.
What makes this system more reliable than general-purpose sentiment or matching tools?
Unlike generic tools that analyze full pages or headlines in isolation, this system:
- Works at the block level — allowing fine-grained recommendations.
- Uses contextual embeddings — not just keyword overlap or static lexicons.
- Outputs results that can drive action: rewriting, reordering, or reinforcing content at the block level.
This level of precision is reflected in the results structure, where each block is independently scored, making it possible to surgically optimize content rather than relying on broad, page-level changes.
Final Thoughts
This project successfully demonstrates a scalable, real-world solution for refining document relevance using contextual sentiment and polarity cues. By integrating advanced transformer-based sentiment scoring with high-quality contextual embeddings, the system intelligently elevates or de-emphasizes content based on nuanced emotional and polarity signals. This directly supports broader SEO objectives, such as increasing user engagement, improving semantic relevance, and enhancing long-tail visibility in organic search.
The result output format is designed for immediate inspection, review, or downstream integration — allowing SEO professionals to analyze how content blocks are perceived emotionally and contextually across multiple pages. The flexibility of sentiment and polarity thresholds provides control over what type of content surfaces for different strategic needs, such as promoting positive messaging or identifying polarizing segments.
The implementation remains modular, extensible, and suitable for integration into larger content analysis, retrieval, or ranking pipelines. The ability to interpret sentiment not in isolation but in conjunction with contextual relevance makes this solution uniquely powerful for real-world SEO use cases. This approach enables smarter content decisions that go beyond traditional keyword-based or surface-level relevance metrics.
Overall, the project delivers a robust foundation for sentiment-aware contextual retrieval, aligning technical depth with clear, beneficial outcomes.
Thatware | Founder & CEO
Tuhin is recognized across the globe for his vision to revolutionize digital transformation industry with the help of cutting-edge technology. He won bronze for India at the Stevie Awards USA as well as winning the India Business Awards, India Technology Award, Top 100 influential tech leaders from Analytics Insights, Clutch Global Front runner in digital marketing, founder of the fastest growing company in Asia by The CEO Magazine and is a TEDx speaker and BrightonSEO speaker.