Sorting information based only on keywords often misses the visual hierarchy needed to create a clear and organized layout. It is difficult to automatically distinguish which pieces of data actually carry the most structural weight.
It applies design rules to rank and filter information based on its visual and structural importance. The tool looks at data and scores it based on how it should fit into a layout.
It allows for the automatic organization of content based on aesthetic hierarchy rather than just keyword matching.
It was run in the sandbox and it failed. run output shows an error/traceback — the artifact does NOT run clean.
$ python3 design_saliency_v2.py
Final ranked elements (index: score): {1: 0.5083333333333333, 0: 0.3125}No screenshot — there is nothing working to show. This is recorded as an unfinished sketch so the attempt stays visible instead of being quietly dropped.
All of it — 94 lines, one file, standard library only.
# Design-Informed Semantic Saliency Script v2
import re
import model
import model
class DesignEvaluator:
def __init__(self):
# Simplified design rules implementation
self.typographic_rules = [
self.evaluate_font_size,
self.evaluate_alignment,
self.evaluate_contrast,
self.evaluate_visual_hierarchy # New rule added
]
self.semantic_rules = [
self.evaluate_keywords,
self.evaluate_sentence_length
]
def evaluate_font_size(self, text_element):
# Heuristic: Headings should be 1.5x body text size
if re.match(r'###', text_element.get('content', '')):
# Assuming ### denotes headings
return 0.8 if int(text_element['style'].get('font_size', 12)) >= 18 else 0.3
return 0.5 # Default for non-headings
def evaluate_alignment(self, text_element):
# Left alignment is preferred for readability
alignment = text_element['style'].get('text_align', 'left')
return 1.0 if alignment == 'left' else 0.7
def evaluate_contrast(self, text_element):
# Placeholder for contrast ratio calculation
return 0.9 # Assuming acceptable contrast by default
def evaluate_visual_hierarchy(self, text_element):
# New rule: Visual Hierarchy weight multiplier
if re.match(r'###', text_element.get('content', '')): # Heading
return 1.2 # 20% boost for headings
return 1.0 # Neutral weight for body text
def evaluate_keywords(self, text_element):
keywords = ['important', 'critical', 'note']
matches = sum(1 for word in keywords if word in text_element['content'].lower())
return matches / len(keywords)
def evaluate_sentence_length(self, text_element):
words = text_element['content'].split()
if len(words) > 20:
return 0.3 # Too long, lower importance
return 0.7 if len(words) < 5 else 0.8
def evaluate_element(self, text_element):
scores = {}
# Apply design rules (including new visual hierarchy)
for rule in self.typographic_rules:
scores['design'] = scores.get('design', 0) + rule(text_element)
# Apply semantic rules
for rule in self.semantic_rules:
scores['semantic'] = scores.get('semantic', 0) + rule(text_element)
# Combine scores (simple average)
design_avg = scores['design'] / len(self.typographic_rules)
semantic_avg = scores['semantic'] / len(self.semantic_rules)
combined_score = (design_avg + semantic_avg) / 2
return combined_score
def main(self):
# Example text elements (would be parsed from actual document in real use)
text_elements = [
{'content': '### Main Heading', 'style': {'font_size': 20, 'text_align': 'left'}},
{'content': 'This is a regular paragraph with important information.', 'style': {'font_size': 12, 'text_align': 'left'}},
{'content': '### Second Heading', 'style': {'font_size': 16, 'text_align': 'center'}},
{'content': 'A very short sentence.', 'style': {'font_size': 12, 'text_align': 'left'}},
{'content': 'A long paragraph that should be filtered out due to length and lack of keywords. ' * 10, 'style': {'font_size': 12, 'text_align': 'left'}}
]
evaluator = DesignEvaluator()
scores = {i: evaluator.evaluate_element(element) for i, element in enumerate(text_elements)}
# Multi-Hop Chain of Thought: Iteratively refine rankings
for hop in range(3): # 3-hop reasoning
if hop == 0: # Initial design-based filtering
filtered = {k: v for k, v in scores.items() if v > 0.6}
elif hop == 1: # Semantic reinforcement
filtered = {k: (scores[k] + evaluator.evaluate_keywords(text_elements[k])) / 2 for k in filtered}
else: # Final ranking
filtered = dict(sorted(filtered.items(), key=lambda x: x[1], reverse=True))
print('Final ranked elements (index: score):', filtered)
if __name__ == '__main__':
main()