Looking at the original communication density formula, the challenge is that LSA vectors have a natural norm (||d_i||) that represents semantic richness, while cosine similarity normalizes away the magnitude information that's crucial for this metric.
Here are several approaches to adapt this metric for your embedding-based analysis:
Approach 1: TF-IDF Based Semantic Density
Since TF-IDF vectors preserve magnitude information that correlates with semantic content:
pythonfrom sklearn.feature_extraction.text import TfidfVectorizer
import numpy as np
def calculate_communication_density_tfidf(contributions):
"""
Calculate communication density using TF-IDF vectors
"""
# Create TF-IDF vectors for all contributions
vectorizer = TfidfVectorizer(max_features=1000, stop_words='english')
tfidf_vectors = vectorizer.fit_transform(contributions)
densities = []
for i, contribution in enumerate(contributions):
# Get TF-IDF vector norm (semantic richness)
vector_norm = np.linalg.norm(tfidf_vectors[i].toarray())
# Get word count
word_count = len(contribution.split())
# Calculate density
if word_count > 0:
density = vector_norm / word_count
else:
density = 0
densities.append(density)
return densities
Approach 2: Content Word Ratio
Focus on semantically meaningful words:
pythonimport nltk
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from nltk.tag import pos_tag
def calculate_semantic_word_ratio(text):
"""
Calculate ratio of semantic words to total words
"""
# Content-bearing POS tags
content_tags = {'NN', 'NNS', 'NNP', 'NNPS', # Nouns
'VB', 'VBD', 'VBG', 'VBN', 'VBP', 'VBZ', # Verbs
'JJ', 'JJR', 'JJS', # Adjectives
'RB', 'RBR', 'RBS'} # Adverbs
tokens = word_tokenize(text.lower())
pos_tags = pos_tag(tokens)
content_words = [word for word, tag in pos_tags if tag in content_tags]
total_words = len(tokens)
return len(content_words) / total_words if total_words > 0 else 0
def calculate_communication_density_semantic_ratio(contributions):
return [calculate_semantic_word_ratio(contrib) for contrib in contributions]
Approach 3: Hybrid Embedding + Lexical Approach
Combine embedding information with lexical diversity:
pythonfrom sentence_transformers import SentenceTransformer
import numpy as np
def calculate_communication_density_hybrid(contributions, model_name='all-MiniLM-L6-v2'):
"""
Hybrid approach using embeddings and lexical features
"""
model = SentenceTransformer(model_name)
densities = []
for contribution in contributions:
# Get embedding
embedding = model.encode([contribution])[0]
# Calculate lexical diversity (unique words / total words)
words = contribution.lower().split()
lexical_diversity = len(set(words)) / len(words) if len(words) > 0 else 0
# Calculate semantic density proxy
# Use embedding norm as proxy for semantic richness
embedding_norm = np.linalg.norm(embedding)
word_count = len(words)
# Combine embedding norm with lexical diversity
if word_count > 0:
density = (embedding_norm * lexical_diversity) / word_count
else:
density = 0
densities.append(density)
return densities
Approach 4: Entropy-Based Semantic Density
Use information theory to measure semantic content:
pythonfrom collections import Counter
import math
def calculate_entropy_density(text):
"""
Calculate semantic density using word entropy
"""
words = text.lower().split()
if len(words) == 0:
return 0
# Calculate word frequency distribution
word_counts = Counter(words)
total_words = len(words)
# Calculate entropy
entropy = 0
for count in word_counts.values():
probability = count / total_words
if probability > 0:
entropy -= probability * math.log2(probability)
# Normalize by word count to get density
return entropy / len(set(words)) if len(set(words)) > 0 else 0
def calculate_communication_density_entropy(contributions):
return [calculate_entropy_density(contrib) for contrib in contributions]
Recommended Implementation
I'd suggest starting with Approach 1 (TF-IDF) because:
It's most analogous to the original LSA approach
TF-IDF vectors naturally encode semantic importance through term weighting
The vector norm provides a meaningful measure of semantic content density
It's computationally efficient and interpretable
Here's a complete implementation for participant-level analysis:Communication Density AnalysisCode import pandas as pd
import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer
from collections import defaultdict
import matplotlib.pyplot as plt
import seaborn as sns
class CommunicationDensityAnalyzer:
"""
Analyze commThis implementation provides a robust way to calculate communication density that's analogous to the original LSA-based approach. The key insight is using TF-IDF vector norms as a proxy for semantic richness, which captures the "amount of semantically meaningful information" per word.
Key advantages of this approach:
Theoretically grounded: TF-IDF vectors, like LSA, capture semantic importance through weighting
Maintains the original formula structure: Still calculates density as semantic_richness/word_count
Participant-level aggregation: Properly implements the averaging across all participant contributions
Scalable: Works efficiently with large datasets
Usage tips:
Adjust max_features in the TfidfVectorizer based on your corpus size
Consider domain-specific stop words if analyzing technical discussions
The density values will be on a different scale than LSA, but relative comparisons between participants remain meaningful
Would you like me to modify any aspect of this implementation or explain how to adapt it for your specific dataset structure?