Exemplo n.º 1
0
 def flush(self):
     self.it = None
     self.they = None # third person pronoun? plural third person pronoun?
     self.he = None
     self.she = None
     self.things = LruCache(self.cache_size) # the most recently encountered thing of a given class
     self.others = LruCache(self.cache_size) # other things, if they've been displaced from primacy in things
     self.encounters = LruCache(self.cache_size) # encountered things in order by name
     self.names = {} # LruCache(7) # object to name
     self.genders = {} # objects to their known genders
Exemplo n.º 2
0
import os

CACHE_SIZE_FACTOR = float(os.environ.get("SYNAPSE_CACHE_FACTOR", 0.1))

DEBUG_CACHES = False

metrics = synapse.metrics.get_metrics_for("synapse.util.caches")

caches_by_name = {}
cache_counter = metrics.register_cache(
    "cache",
    lambda: {(name,): len(caches_by_name[name]) for name in caches_by_name.keys()},
    labels=["name"],
)

_string_cache = LruCache(int(5000 * CACHE_SIZE_FACTOR))
caches_by_name["string_cache"] = _string_cache


KNOWN_KEYS = {
    key: key for key in
    (
        "auth_events",
        "content",
        "depth",
        "event_id",
        "hashes",
        "origin",
        "origin_server_ts",
        "prev_events",
        "room_id",
Exemplo n.º 3
0
class Narrative(object):

    cache_size = 7

    def __init__(self, narrator, audience):
        self.narrator = narrator
        self.audience = audience
        self.flush()

    def flush(self):
        self.it = None
        self.they = None # third person pronoun? plural third person pronoun?
        self.he = None
        self.she = None
        self.things = LruCache(self.cache_size) # the most recently encountered thing of a given class
        self.others = LruCache(self.cache_size) # other things, if they've been displaced from primacy in things
        self.encounters = LruCache(self.cache_size) # encountered things in order by name
        self.names = {} # LruCache(7) # object to name
        self.genders = {} # objects to their known genders

    def meet(self, person):
        self.names[person] = person.name
        self.he = None
        self.she = None
        self.they = None

    def knows(self, person):
        return isinstance(person, Person) and person in self.names

    def lookup(self, name):
        thing = None
        if name == 'him': thing = self.he
        if name == 'her': thing =  self.she
        if name == 'it': thing = self.it
        if name == 'them': thing = self.they
        if name == 'myself': thing = self.audience
        if name == 'you': thing = self.narrator
        if name in self.things: thing = self.things[name]
        self.noun(thing)
        return thing

    def __call__(self, story):
        if story is None:
            return
        elif isinstance(story, Event):
            return self.event(story)
        else:
            return self.events(story)

    def events(self, events):
        if events is None:
            return
        return '  '.join(
            story for story in (
                self.event(event)
                for event in events
            ) if story is not None
        )

    def event(self, event):
        return self.event_(event.subject, event, event.object, *event.modifiers, **event.transitives)

    def event_(self, subject, verb, object = None, *modifiers, **transitives):
        parts = []
        parts.append(self.verb(subject, verb))
        parts.insert(0, self.noun(subject))
        if object is not None:
            parts.append(self.noun(object, subject))
        for modifier in modifiers:
            parts.append(modifier)
        for modifier, object in transitives.items():
            parts.append([lower(modifier, ' '), self.noun(object, subject)])
        return sentential(parts)

    def verb(self, subject, verb):
        if (
            subject is self.audience or
            subject is self.narrator or
            subject is self.they
        ):
            return verb.nominative # You say, I say, they say, (we say)
        return verb.present # He says, she says, it says, Joe says

    def noun(self, object, subject = None):
        # find out what to call something, either in the subject or the predicate
        #  of a sentence, based on what the narrative already describes.

        if object is None:
            return 'nothing'

        if isinstance(object, basestring):
            return '"%s"' % object.replace('"', "'")

        a = False
        the = False
        other = False

        # recognize

        if object in self.names:
            name = self.names[object]
        elif object is self.narrator:
            if subject is object: name = 'myself'
            elif subject: name = 'me'
            else: name = 'I'
        elif object is self.audience:
            if subject is object: name = 'yourself'
            else: name = 'you'
        elif object is self.it:
            if subject is object: name = 'itself'
            else: name = 'it'
        elif object is self.she:
            if object is subject: name = 'herself'
            elif subject: name = 'her'
            else: name = 'she'
        elif object is self.he:
            if object is subject: name = 'himself'
            elif subject: name = 'him'
            else: name ='he'
        elif object is self.they:
            if subject is object: name = 'themself'
            elif subject: name = 'them'
            else: name = 'they'
        elif self.things.get(object.singular) == object:
            name = object.singular
            the = True
        elif self.others.get(object.singular) == object:
            name = object.singular
            the = True
            other = True
        elif object in self.encounters.get(object.singular, []):
            encounters = self.encounters[object.singular]
            name = (ordinals[encounters.index(object)], object.singular)
            the = True
        elif isinstance(object, Unique):
            name = object.singular
            the = True
        else:
            # describe
            # distinguish
            # generalize
            name = object.singular
            a = True

        # learn
        if object is not self.audience:
            if object in self.genders:
                gender = self.genders[object]
                if gender is Male:
                    self.he = object
                if gender is Female:
                    self.she = object
                self.it = None
            elif isinstance(object, Person):
                self.they = object
                self.he = None
                self.she = None
                self.it = None
            elif isinstance(object, Creature):
                self.it = None
            else:
                self.it = object

        encounters = self.encounters.setdefault(object.singular, [])
        if len(encounters) < len(ordinals) and object not in encounters:
            encounters.append(object)
        if self.others.get(object.singular) != self.things.get(object.singular):
            self.others[object.singular] = self.things.get(object.singular)
        self.things[object.singular] = object

        # add articles
        if a and name == 'person':
            name = 'someone'
        elif other:
            if a:
                name = ('another', name)
            elif the:
                name = ('the', 'other', name)
            else:
                name = ('some', 'other', name)
        elif a:
            name = (object.singular_a, name)
        elif the:
            name = ('the', name)

        return name