Exemplo n.º 1
0
 def _apply_general(self, term):
     out = []
     if type(term) == Term and term.op in self.over_ops:
         for dist_op in self.dist_ops:
             ct = {}
             for i in range(len(term.terms)):
                 subterm = term.terms[i]
                 if type(subterm) == Term and subterm.op == dist_op:
                     for subsubterm in subterm.terms:
                         if subsubterm not in ct:
                             ct[subsubterm] = []
                         if i not in ct[subsubterm]:
                             ct[subsubterm].append(i)
             for r_term in ct.keys():
                 if len(ct[r_term]) > 1:
                     paired_terms = []
                     other_terms = []
                     for i in range(len(term.terms)):
                         if i in ct[r_term]:
                             p_term = term.terms[i].__deepcopy__()
                             p_term.terms.remove(r_term)
                             if len(p_term.terms) == 1:
                                 p_term = p_term.terms[0]
                             paired_terms.append(p_term)
                         else:
                             other_terms.append(
                                 term.terms[i].__deepcopy__())
                     t_term = Term(
                         dist_op,
                         [r_term, Term(term.op, paired_terms)])
                     if other_terms:
                         out.append(Term(term.op, [t_term] + other_terms))
                     else:
                         out.append(t_term)
     return out + self.recurse_apply(term)
Exemplo n.º 2
0
def unflatten(term):
    if type(term) == Term and term.op in assoc_ops and len(
            term.terms) > 2:  #term.flattened
        return Term(term.op, [
            unflatten(term.terms[0].__deepcopy__()),
            unflatten(Term(term.op, term.terms[1:]))
        ])
    if type(term) == Term:
        # recurse
        return Term(term.op, [unflatten(subterm) for subterm in term.terms])
    return term.__deepcopy__()
Exemplo n.º 3
0
def all_unflatten(term):  # modulo state vars
    out = []
    if type(term) == Term:
        if term.op not in assoc_ops:
            for prod in product(
                    *[all_unflatten(subterm) for subterm in term.terms]):
                out.append(Term(term.op, prod))
            return out
        state_terms, raw_terms = [], []
        for i in range(len(term.terms)):
            if not term.terms[i].state_free():
                state_terms.append(term.terms[i].__deepcopy__())
            else:
                raw_terms.append(term.terms[i].__deepcopy__())
        for state_prod in product(
                *[all_unflatten(state_term) for state_term in state_terms]):
            state_prod = list(state_prod)
            if len(raw_terms) < 2:
                return [Term(term.op, state_prod + raw_terms)]
            if len(raw_terms) == 2:
                for left_unflatten in all_unflatten(raw_terms[0]):
                    for right_unflatten in all_unflatten(raw_terms[1]):
                        if state_prod:
                            out.append(
                                Term(
                                    term.op, state_prod + [
                                        Term(term.op,
                                             [left_unflatten, right_unflatten])
                                    ]))
                        else:
                            out.append(
                                Term(term.op,
                                     [left_unflatten, right_unflatten]))
            else:
                for perm in set(
                        permutations(raw_terms)
                ):  # TODO: identify permutations with same last two terms
                    for left_unflatten in all_unflatten(perm[0]):
                        for right_unflatten in all_unflatten(
                                Term(term.op, perm[1:])):
                            if state_prod:
                                out.append(
                                    Term(
                                        term.op, state_prod + [
                                            Term(term.op, [
                                                left_unflatten, right_unflatten
                                            ])
                                        ]))
                            else:
                                out.append(
                                    Term(term.op,
                                         [left_unflatten, right_unflatten]))
            return out
    return [term.__deepcopy__()]
Exemplo n.º 4
0
 def apply(self, term):
     if self.over_term_inds is None:
         return self._apply_general(term)
     out = []
     if type(term) == Term and term.op in self.over_ops:
         for dist_op in self.dist_ops:
             dist = True
             comm_terms = None
             for i in self.over_term_inds:
                 subterm = term.terms[i]
                 if type(subterm) == Term and subterm.op == dist_op:
                     if comm_terms == None:
                         comm_terms = set(subterm.terms)
                     else:
                         comm_terms.intersection_update(set(subterm.terms))
                 else:
                     dist = False
                     break
             if dist:
                 for comm_term in comm_terms:
                     p_term = term.__deepcopy__()
                     for i in self.over_term_inds:
                         p_term.terms[i].terms.remove(comm_term)
                         if len(p_term.terms[i].terms) == 1:
                             p_term.terms[i] = p_term.terms[i].terms[0]
                     out.append(Term(dist_op, [comm_term, p_term]))
     return out + self.recurse_apply(term)
Exemplo n.º 5
0
def define(name, expr):
    """
    Take an expression of 't' (possibly complicated)
    and make it a '%s(t)' % name, such that
    when it evaluates it has the right values.

    Parameters
    ----------
    expr : sympy expression, with only 't' as a Symbol
    name : str

    Returns
    -------
    nexpr: sympy expression

    Examples
    --------
    >>> t = Term('t')
    >>> expr = t**2 + 3*t
    >>> print expr
    3*t + t**2
    >>> newexpr = define('f', expr)
    >>> print newexpr
    f(t)
    >>> import aliased
    >>> f = aliased.lambdify(t, newexpr)
    >>> f(4)
    28
    >>> 3*4+4**2
    28
    """
    v = vectorize(expr)
    return aliased_function(name, v)(Term('t'))
Exemplo n.º 6
0
 def apply(self, term):
     out = []
     if type(term) == Term and term.op in self.dist_ops:
         for over_op in self.over_ops:
             term_inds = []
             other_terms = []
             for i in range(len(term.terms)):
                 if type(term.terms[i]
                         ) == Term and term.terms[i].op == over_op:
                     term_inds.append(i)
                 else:
                     other_terms.append(term.terms[i])
             for i in term_inds:
                 for other_term in other_terms:
                     new_term = term.__deepcopy__()
                     for j in range(len(new_term.terms[i].terms)):
                         if self.over_term_inds is None or j in self.over_term_inds:
                             if type(new_term.terms[i].terms[j]) == Term and \
                                     new_term.terms[i].terms[j].op == term.op:
                                 new_term.terms[i].terms[j].terms.append(
                                     other_term.__deepcopy__())
                             else:
                                 new_term.terms[i].terms[j] = \
                                     Term(term.op, [other_term.__deepcopy__(),
                                                    new_term.terms[i].terms[j]])
                     new_term.terms.remove(other_term)
                     if len(new_term.terms) == 1:
                         new_term = new_term.terms[0]
                     out.append(new_term)
     return out + self.recurse_apply(term)
Exemplo n.º 7
0
    def test_returns_object_terms_with_one_field_if_string_with_only_constant_passed(
            self):
        #ARRANGE
        term_string = '2'

        #ACT
        first = Term(term_string)

        #ASSERT
        self.assertEqual(first.constant, 2)
Exemplo n.º 8
0
 def recurse_apply(self, term):
     if type(term) != Term:
         return []
     out = []
     for i in range(len(term.terms)):
         for rew in self.apply(term.terms[i]):
             new_terms = [subterm.__deepcopy__() for subterm in term.terms]
             new_terms[i] = rew
             out.append(Term(term.op, new_terms))
     return out
Exemplo n.º 9
0
def collect_terms(path, filename):
    terms = []

    if os.path.exists(path + filename):
        f = open(path + filename)
        for line in f:
            words = [x.strip() for x in line.split("\t")]
            words = [x for x in words if len(x) > 0]

            if len(words) >= 2:
                fro = words[0].strip()
                types = [t for t in words[1].strip()]
                term = Term(fro, types)

                if len(words) >= 3:
                    term.to = words[2].strip()

                terms.append(term)
        f.close()
    return terms
Exemplo n.º 10
0
    def test_returns_object_terms_with_two_fields_if_string_with_x_on_power_passed(
            self):
        #ARRANGE
        term_string = '2x^3'

        #ACT
        first = Term(term_string)

        #ASSERT
        self.assertEqual(first.constant, 2)
        self.assertEqual(first.power, 3)
Exemplo n.º 11
0
    def extract_terms(self):

        definitions = self.tree.findall('.//{eregs}def')
        self.terms = []

        for defn in definitions:
            defined_in = defn.find('../..').get('label')
            term = defn.get('term')
            self.terms.append(Term(term, defined_in))

        self.terms.sort(key=lambda x: x.term)
Exemplo n.º 12
0
 def apply(self, term):
     out = []
     if term == self.greater_term:
         out.append(
             Term('max', [
                 self.greater_term.__deepcopy__(),
                 self.lesser_term.__deepcopy__()
             ]))
     if type(term) == Term and term.op == 'max':
         if self.greater_term in term.terms and self.lesser_term not in term.terms:
             new_term = term.__deepcopy__()
             new_term.terms.append(self.lesser_term.__deepcopy__())
             out.append(new_term)
     return out + self.recurse_apply(term)
Exemplo n.º 13
0
 def apply(self, term):
     out = []
     if type(term) == Var or type(term) == Const:
         if self.target_var and term != self.target_var:
             return []
         if term.type != term_types[self.op].get_ret_type(
         ):  # TODO: be careful here and elsewhere
             return []
         out.append(Term(self.op, [term.__deepcopy__(), self.id]))
     if type(term) == Term and term.op == self.op:
         if self.target_var is None or self.target_var in term.terms:
             new_term = term.__deepcopy__()
             new_term.terms.append(self.id)
             out.append(new_term)
     return out + self.recurse_apply(term)
Exemplo n.º 14
0
def flatten(term):
    if type(term) == Const:
        return Const(term.value)
    elif type(term) == Var:
        return Var(term.vclass, term.name, term.index, term.type)
    else:
        new_terms = []
        for subterm in term.terms:
            if type(
                    subterm
            ) == Term and subterm.op in assoc_ops and subterm.op == term.op:
                new_terms.extend(flatten(subterm).terms)
            else:
                new_terms.append(flatten(subterm))
        return Term(term.op, new_terms)
Exemplo n.º 15
0
 def apply(self, term):
     out = []
     if term == self.id:
         for var in self.vars:
             out.append(
                 Term(self.op, [var.__deepcopy__(),
                                self.inv_func(var)]))
     if type(term) == Term and term.op == self.op:
         if self.id in term.terms:
             for var in self.vars:
                 new_term = term.__deepcopy__()
                 new_term.terms.remove(self.id)
                 new_term.terms.append(var.__deepcopy__())
                 new_term.terms.append(self.inv_func(var))
                 out.append(new_term)
     return out + self.recurse_apply(term)
Exemplo n.º 16
0
 def apply(self, term):
     out = []
     if type(term) == Var or type(term) == Const:
         if term.type != term_types[self.op].get_ret_type():
             return []
         out.append(
             Term(self.op, [term.__deepcopy__(),
                            term.__deepcopy__()]))
     if type(term) == Term and term.op == self.op:
         for subterm in set(term.terms):
             if type(subterm) == Var or type(
                     subterm) == Const:  # remember to remove this condition
                 new_term = term.__deepcopy__()
                 new_term.terms.append(subterm)
                 out.append(new_term)
     return out + self.recurse_apply(term)
Exemplo n.º 17
0
def merge(loop, op, joins):
    new_loop = loop.__deepcopy__()
    new_terms = []
    for i in range(len(joins)):
        new_term = joins[i].term.__deepcopy__()
        r = 0
        reindex = []
        for j in range(joins[i].loop.get_num_states()):
            r = new_loop.add_state(joins[i].loop.state_init[j],
                                   joins[i].loop.state_terms[j], j)
            reindex.append(r)
        for j in reversed(range(len(reindex))):
            r = reindex[j]
            new_term = new_term.reindex(Var("SV", "s", j+1), r+1).\
                reindex(Var("RSV", "s", j+1), r+1)
        new_terms.append(new_term)
    return Join(new_loop, Term(op, new_terms))
Exemplo n.º 18
0
def terms_from_rec(rec, keep=[], drop=[]):
    """ Construct terms from recarray

    For fields with a string-dtype, it is assumed that these are
    qualtiatitve regressors, i.e. Factors.

    Parameters
    ----------
    rec: recarray
        Recarray whose field names will be used to create a formula.
    keep: []
        Field names to explicitly keep, dropping all others.
    drop: []
        Field names to drop.
    """
    spec = {}
    for n in rec.dtype.names:
        if rec[n].dtype.kind == 'S':
            spec[n] = Factor.fromcol(rec[n], n)
        else:
            spec[n] = Term(n).formula
    for d in drop:
        del (spec[d])
    return spec
Exemplo n.º 19
0
    def get_term_obj_list_from_database(self, list_id):
        master_list = vocabulary_list.objects.get(title = self.title, id = list_id)
        vocabs = vocabulary.objects.filter(master_list = master_list ).order_by('vocabulary')

        term_obj_list = []
        for vocab in vocabs:
            term = Term(vocab.vocabulary, vocab.definition)
            
            # add extra definition
            defis = vocabulary_extra_definition.objects.filter(master_vocabulary=vocab).order_by('extra_def')
            for defi in defis:
                term.add_definitions(defi)
            # add synonyms
            syns = vocabulary_synonyms.objects.filter(master_vocabulary=vocab).order_by('synonyms')
            for syn in syns:
                term.add_synonym(syn)
            # add example sentences
            exps = vocabulary_example_sentence.objects.filter(master_vocabulary=vocab).order_by('example_sentences')
            for exp in exps:
                term.add_example(exp)

            term_obj_list.append(term)
        return term_obj_list
Exemplo n.º 20
0
 def __init__(self, arguments_as_string):
     arguments_as_string_as_parts = parse_string_to_list_of_terms(
         arguments_as_string)
     self.terms = [Term(x) for x in arguments_as_string_as_parts]
Exemplo n.º 21
0
from os import path
from random import randint, choice, seed

from terms import Term
T = Term()

def get_bot_data():
    import json
    data = {}
    with open(path.join('res', 'config.json'),"r") as f:
        data = json.loads(f.read())
    return data[T.b_token], data[T.b_owner], data[T.b_prefix], data[T.b_seed]



def clamp(n, minv, maxv):
    return max(min(maxv, n), minv)

def get_index(x, y, w):
    return y * w + x

def coords(x, y):
    return '{}-{}'.format(x, y)



def get_drops(m):
    data = {}
    choices = []
    count = 0
    for drop in T.drops[m]:
Exemplo n.º 22
0
def parseEntry(tag):
    """Given the first <td> in the row containing a term and a definition, 
    parse out the term, the definition, and the various optional attributes
    (synonyms, sources, see alsos, etc.) from the definition."""

    # parse out the term name. Check for abbreviations
    term_name = tag.a.text.strip()
    term_name, abbreviation = parseNameForAbbrev(term_name)
    if abbreviation is not None:
        abbreviation = [abbreviation]

    # start parsing (possibly multiple) definitions
    description = tag.next_sibling
    definition_tag = description.li
    def_list = []
    see_also_list = []
    synonym_list = []
    source_text = None
    source_link = None

    while definition_tag is not None:
        # Get the definition text. We don't want recursion because that will
        # eat up the subordinant list full of "extras", parsed out below.
        definition = definition_tag.find(text=True, recursive=False)
        # Sometimes (rarely) the definition is encapsulated by a <p> Tag.
        if definition is None and definition_tag.p is not None:
            definition = definition_tag.p.find(text=True, recursive=False)
        # Sometimes (rarely) the definition is encapsulated by an <i> Tag.
        if definition is None and definition_tag.i is not None:
            definition = definition_tag.i.find(text=True, recursive=False)

        see_also = []
        synonym = []

        # check to see if the description contains a Source
        desc_html = str(description)
        pos = desc_html.rfind('Source:')
        if pos != -1:
            # If there's a source, remove the "source" bits from the description
            source_text = desc_html[pos:]
            source_soup = BeautifulSoup(source_text, 'html.parser')

            source_text = source_soup.text[7:].strip()

            # if there's a subordinate ul element (see also or synonyms)
            # then get rid of it out of the source text.
            other_stuff = definition_tag.ul
            if other_stuff is not None:
                source_text = source_text[:-(len(other_stuff.text) + 1)]

            if source_soup.a is not None:
                source_link = source_soup.a['href']

            #chop out the source bits
            dpos = definition.rfind('Source')
            definition = definition[:dpos]

        # check to see if there's "more"
        extras = definition_tag.li
        while extras is not None:
            # references flagged with "see also" sometimes "See Also" sometimes "See also"
            see_also_pos = extras.text.lower().find('see also')
            if see_also_pos != -1:
                see_also_text = extras.text[9:].strip()
                see_also_terms = [
                    generateKey(s.strip()) for s in see_also_text.split(';')
                ]
                see_also = see_also + see_also_terms
            else:
                # references flagged with "See"
                see_also_pos = extras.text.find('See')
                if see_also_pos != -1:
                    see_also_text = extras.text[3:].strip()
                    see_also_terms = [
                        generateKey(s.strip())
                        for s in see_also_text.split(';')
                    ]
                    see_also = see_also + see_also_terms

            synonym_pos = extras.text.find('synonym')
            if synonym_pos != -1:
                synonym.append(generateKey(extras.text[9:].strip()))

            extension_pos = extras.text.find('Definition Extension')
            if extension_pos != -1:
                definition = definition + " " + extras.text.strip()

            extras = extras.next_sibling

        # accumulate the "features" of this definition into the term's properties
        if definition is not None:
            def_list.append(definition.strip())
        else:
            print "%s lacks a definition..." % term_name
        synonym_list = synonym_list + synonym
        see_also_list = see_also_list + see_also

        definition_tag = definition_tag.next_sibling

    # If we don't have any of these things, set them to None
    if not see_also_list:
        see_also_list = None
    if not synonym_list:
        synonym_list = None

    return Term(generateKey(term_name),
                term_name,
                def_list,
                see_also_list,
                synonym_list,
                source_text=source_text,
                source_link=source_link,
                abbrev=abbreviation)