Skip to content

Commit

Permalink
presentation renderers
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddoret committed Nov 30, 2024
1 parent 9db4c0a commit 8952177
Show file tree
Hide file tree
Showing 14 changed files with 125 additions and 91 deletions.
112 changes: 58 additions & 54 deletions .idea/workspace.xml

Large diffs are not rendered by default.

17 changes: 9 additions & 8 deletions src/punctilious/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import foundations
import presentation
from punctilious.data.representations import \
greek_alphabet_lowercase_serif_italic as _greek_alphabet_lowercase_serif_italic, \
greek_alphabet_uppercase_serif_italic as _greek_alphabet_uppercase_serif_italic, \
Expand All @@ -7,27 +8,27 @@
from punctilious.data.connectors import operators_1 as _operators1
from punctilious.data.theorems import propositional_logic_1 as _propositional_logic1

preferences = foundations.get_preferences()
packages = foundations.get_packages()
# preferences = foundations.get_preferences()
# packages = foundations.get_packages()

greek_alphabet_lowercase_serif_italic = _greek_alphabet_lowercase_serif_italic.GreekAlphabetLowercaseSerifItalic()
# greek_alphabet_lowercase_serif_italic = _greek_alphabet_lowercase_serif_italic.GreekAlphabetLowercaseSerifItalic()
# print(greek_alphabet_lowercase_serif_italic.alpha.configurations[0])
# print(greek_alphabet_lowercase_serif_italic.phi.configurations[1])
# print(greek_alphabet_lowercase_serif_italic.psi.configurations[2])

greek_alphabet_uppercase_serif_italic = _greek_alphabet_uppercase_serif_italic.GreekAlphabetUppercaseSerifItalic()
# greek_alphabet_uppercase_serif_italic = _greek_alphabet_uppercase_serif_italic.GreekAlphabetUppercaseSerifItalic()
# print(greek_alphabet_uppercase_serif_italic.alpha.configurations[0])
# print(greek_alphabet_uppercase_serif_italic.phi.configurations[1])
# print(greek_alphabet_uppercase_serif_italic.psi.configurations[2])

latin_alphabet_lowercase_serif_italic = _latin_alphabet_lowercase_serif_italic.LatinAlphabetLowercaseSerifItalic()
latin_alphabet_lowercase_serif_roman = _latin_alphabet_lowercase_serif_roman.LatinAlphabetLowercaseSerifRoman()
# latin_alphabet_lowercase_serif_italic = _latin_alphabet_lowercase_serif_italic.LatinAlphabetLowercaseSerifItalic()
# latin_alphabet_lowercase_serif_roman = _latin_alphabet_lowercase_serif_roman.LatinAlphabetLowercaseSerifRoman()

operators_1 = _operators1.Operators1()
# operators_1 = _operators1.Operators1()
# print(operators_1.conjunction.representation.configurations[0])
# print(operators_1.entailment.representation.configurations[1])
# print(operators_1.entailment.representation.configurations[2])

pl1 = _propositional_logic1.PropositionalLogic1()
# pl1 = _propositional_logic1.PropositionalLogic1()

pass
Binary file modified src/punctilious/__pycache__/__init__.cpython-312.pyc
Binary file not shown.
Binary file modified src/punctilious/__pycache__/foundations.cpython-312.pyc
Binary file not shown.
Binary file not shown.
59 changes: 30 additions & 29 deletions src/punctilious/sandbox_2.py → src/punctilious/presentation.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from __future__ import annotations

import abc
import collections
import collections.abc

Expand All @@ -16,7 +18,7 @@ def optimize_renderer(self, prefs: TagsPreferences):
best_score = 0
optimal_renderer = self.renderers[0]
for current_renderer in self.renderers:
current_score = prefs.score_tags_assignment(tags=current_renderer.tags)
current_score = prefs.score_tags(tags=current_renderer.tags)
if current_score > best_score:
optimal_renderer = current_renderer
best_score = current_score
Expand All @@ -32,21 +34,29 @@ def rep(self, *args, prefs: TagsPreferences, **kwargs):
return renderer.rep()


class Renderer:
class Renderer(abc.ABC):
"""A renderer is an object that has the capability to generate the output of a presentation."""

def __init__(self, tags: TagsAssignment | collections.abc.Iterable):
"""
:param tags: The tags for which this renderer is optimal.
"""
if not isinstance(tags, TagsAssignment):
tags: TagsAssignment = TagsAssignment(*tags)
self._tags: TagsAssignment = tags

def rep(self):
pass
def rep(self, *args, **kwargs):
raise NotImplementedError('This method is abstract.')

@property
def tags(self):
return self._tags


class RendererForStringConstant(Renderer):
"""A renderer that generates a string from a constant."""

def __init__(self, string_constant: str, tags: TagsAssignment | collections.abc.Iterable):
super().__init__(tags)
self._string_constant = string_constant
Expand All @@ -60,6 +70,10 @@ def rep(self, *args, **kwargs):


class RendererForStringTemplate(Renderer):
"""A renderer that generates a string from a template.
"""

def __init__(self, string_template: str, tags: TagsAssignment | collections.abc.Iterable):
super().__init__(tags)
self._string_template = string_template
Expand Down Expand Up @@ -144,6 +158,10 @@ def values(self) -> collections.abc.Iterable:


class TagsPreferences(dict):
"""User preferences for tags.
"""

def __init__(self):
super().__init__()

Expand All @@ -162,29 +180,12 @@ def _validate_key_value(self, key, value):
if not isinstance(value, int):
raise TypeError(f"Value must be of type int, got {type(value).__name__} instead.")

def score_tags_assignment(self, tags: TagsAssignment):
return sum(self.get(tag, 0) for tag in tags)
def score_tags(self, tags: TagsAssignment | collections.abc.Iterable):
"""Returns the preference score of a collection of tags.

en = Tag('language', 'en')
fr = Tag('language', 'fr')
symbol = Tag('connector_representation', 'symbol')
word = Tag('connector_representation', 'word')
print(en)

x = RendererForStringConstant(string_constant='and', tags=(en, word,))
y = RendererForStringConstant(string_constant='et', tags=(fr, word,))
z = RendererForStringConstant(string_constant='∧', tags=(symbol,))
rep = Representation(renderers=(x, y, z,))

prefs = TagsPreferences()
prefs[en] = 6
prefs[fr] = 9
prefs[symbol] = 100
prefs[word] = 1

print(rep.rep(prefs=prefs))
prefs[word] = 100
print(rep.rep(prefs=prefs))
prefs[en] = 500
print(rep.rep(prefs=prefs))
:param tags:
:return:
"""
if not isinstance(tags, TagsAssignment):
tags: TagsAssignment = TagsAssignment(*tags)
return sum(self.get(tag, 0) for tag in tags)
Binary file not shown.
28 changes: 28 additions & 0 deletions tests/test_presentation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import pytest
import punctilious as pu


class TestRepresentation:
def test_representation(self):
en = pu.presentation.Tag('language', 'en')
fr = pu.presentation.Tag('language', 'fr')
symbol = pu.presentation.Tag('connector_representation', 'symbol')
word = pu.presentation.Tag('connector_representation', 'word')

x = pu.presentation.RendererForStringConstant(string_constant='and', tags=(en, word,))
y = pu.presentation.RendererForStringConstant(string_constant='et', tags=(fr, word,))
z = pu.presentation.RendererForStringConstant(string_constant='∧', tags=(symbol,))
rep = pu.presentation.Representation(renderers=(x, y, z,))

prefs = pu.presentation.TagsPreferences()
prefs[en] = 6
prefs[fr] = 9
prefs[symbol] = 100
prefs[word] = 1
assert (rep.rep(prefs=prefs) == '∧')

prefs[word] = 100
assert (rep.rep(prefs=prefs) == 'et')

prefs[en] = 500
assert (rep.rep(prefs=prefs) == 'and')
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 8952177

Please sign in to comment.