Пример #1
0
def int2numeral_in_tree(f_int, f_numeral, tree):
    fun, args = tree.unpack()
    if fun == f_int and len(args) == 1:
        return pgf.Expr(f_numeral, [int2numeral_tree(args[0])])
    else:
        return pgf.Expr(
            fun, [int2numeral_in_tree(f_int, f_numeral, t) for t in args])
Пример #2
0
def insert_subtree(tree, location, subtree):
    # Returns a new tree with subtree inserted into tree at the given location.
    try:
        fun, children = tree.unpack()
        if not fun:
            return (False, tree)
    except:
        return (False, tree)

    rep_loc = location[0]
    rep_pos = location[1]
    if rep_loc == fun and node_is_empty(children[rep_pos]):
        # insert only if parent has empty child at correct location
        children[rep_pos] = subtree
        expr = pgf.Expr(fun, children)
        return (True, expr)
    else:
        n_children = []
        inserted = False
        for c in children:
            c_inserted, c_expr = insert_subtree(c, location, subtree)
            n_children.append(c_expr)
            inserted = c_inserted
        try:
            expr = pgf.Expr(fun, n_children)
        except TypeError:
            return (False, tree)
        return (inserted, expr)
Пример #3
0
def pname(win, just_family=False):
    name = win.winner
    if just_family:
        name = name.split()[-1]
    if win.sex == "female":
        return pgf.Expr('femalePName', [string_expr(name)])
    else:
        return pgf.Expr('malePName', [string_expr(name)])
Пример #4
0
def country_facts(c):
  object = pgf.Expr('NameObject', [name_expr(c['country'])])
  return [
    pgf.Expr('AttributeFact', [pgf.Expr(attr,[]), object, val])
      for (attr, val) in [
        ('capital_Attribute', name_value(c['capital'])),   
        ('area_Attribute', string_value(c['area'])),
        ('population_Attribute', string_value(c['population'])),
        ('continent_Attribute', name_value(c['continent'])),
        ('currency_Attribute', name_value(c['currency']))
        ]
    ]
Пример #5
0
def country_facts(c):
    object = pgf.Expr('StringObject', [string_expr(c.country)])
    return [
        pgf.Expr('AttributeFact', [
            pgf.Expr(attr, []), object,
            pgf.Expr('StringValue', [string_expr(val)])
        ]) for (attr, val) in [(
            'capital_Attribute', c.capital), (
                'area_Attribute',
                c.area), ('population_Attribute',
                          c.population), (
                              'continent_Attribute',
                              c.continent), ('currency_Attribute', c.currency)]
    ]
Пример #6
0
    def callback(lin_idx, sentence, start):
	moving_start, end, eot = start, len(sentence), True;
	isNewToken = (moving_start == 0) or (moving_start > 1 and sentence[moving_start-1].isspace()) # -- added to deal with segmentation errors like may => ma_N + Symb y
	if moving_start < end and (not sentence[moving_start].isupper()):
	    while moving_start < end:
		if sentence[moving_start] in string.whitespace:
		    end = moving_start;
		    break;
		moving_start += 1;
	    unknown_word = sentence[start:end].strip();
	    if unknown_word and isNewToken:
		count = 0;
		for analysis in grammar.languages[language].lookupMorpho(unknown_word):
		    count += 1;
		if not count:
		    expr = pgf.Expr("MkSymb", [pgf.Expr(unknown_word)]);
		    return (expr, 0, end);
	return None;
Пример #7
0
def simple_facts(factsys, data):
    "for each tuple in data, generate an attribute fact for each field"
    fields = factsys.fieldnames.split()
    facts = []
    for tuple in data:
        object = factsys.str2exp("Object", tuple[0])
        for (attr, val) in [(fields[i], tuple[i])
                            for i in range(1, len(fields))]:
            fact = pgf.Expr("AttributeFact", [
                factsys.str2exp("Attribute", attr), object,
                factsys.str2exp("Value", val)
            ])
            facts.append(fact)
    return facts
Пример #8
0
 def callback(lin_idx, start):
     moving_start, end, eot = start, len(sentence), True
     if moving_start < end and (not sentence[moving_start].isupper()):
         return None
     while moving_start < end:
         if sentence[moving_start] in string.whitespace:
             eot = True
         elif eot and sentence[moving_start].isupper():
             eot = False
         elif eot and (not sentence[moving_start].isupper()):
             end = moving_start - 1
             break
         moving_start += 1
     possible_name = sentence[start:end].strip()
     if possible_name:
         if language.endswith('Eng') and \
             (possible_name == "I" or possible_name == "I'm"):
             return None
         elif language.endswith('Eng') and possible_name.endswith("'s"):
             end_idx = possible_name.rfind("'s")
             if end_idx != -1:
                 possible_name = possible_name[:end_idx].strip()
                 end -= 2
                 if not possible_name:
                     return None
         expr, prob = None, None
         for analysis in grammar.languages[language].lookupMorpho(
                 possible_name):
             category = grammar.functionType(analysis[0]).cat
             if prob < analysis[-1]:
                 if category == "PN":
                     expr, prob = pgf.Expr(analysis[0], []), analysis[-1]
                 elif category == "Weekday":
                     expr, prob = pgf.Expr("weekdayPN", \
                         [pgf.Expr(analysis[0], [])]), analysis[-1]
                 elif category == "Month":
                     expr, prob = pgf.Expr("monthPN", \
                         [pgf.Expr(analysis[0], [])]), analysis[-1]
                 elif category == "Language":
                     return None
         # generic named entity
         if expr == None:
             expr = pgf.Expr(possible_name)
             expr = pgf.Expr("MkSymb", [expr])
             expr = pgf.Expr("SymbPN", [expr])
         return (expr, 0, end)
     return None
Пример #9
0
print('>>> import pgf')
import pgf

print(">>> gr = pgf.readPGF('Facts.pgf')")
gr = pgf.readPGF('Facts.pgf')

print('>>> print(list(gr.languages.keys()))')
print(list(gr.languages.keys()))

print(">>> eng = gr.languages['FactsEng']")
eng = gr.languages['FactsEng']

print(">>> attr = pgf.Expr('area_Attribute',[])")
attr = pgf.Expr('area_Attribute', [])

print(">>> eng.linearize(attr)")
print(eng.linearize(attr))

print(">>> obj = pgf.readExpr('NameObject (StringName \"France\")')")
obj = pgf.readExpr('NameObject (StringName "France")')

print(">>> val = pgf.readExpr('123')")
val = pgf.readExpr('123')

print(">>> fact = pgf.Expr('AttributeFact',[attr,obj,val])")
fact = pgf.Expr('AttributeFact', [attr, obj, val])

print(">>> print(fact)")
print(fact)

print(">>> print(eng.linearize(fact))")
Пример #10
0
def app(f, xs):
    if f == "id":
        return xs[0]
    else:
        return pgf.Expr(f, xs)
Пример #11
0
def mkApp(f, xs):
    return pgf.Expr(f, xs)
Пример #12
0
def cname(factsys, s):
    try:
        return factsys.str2exp('CName', s)
    except:
        return pgf.Expr('stringCName', [string_expr(s)])
Пример #13
0
def string_year(s):
    return pgf.Expr('inYearDate', [pgf.readExpr(s[:4])])  # just the year part
Пример #14
0
def string_value(s):
    return pgf.Expr('StringValue', [pgf.readExpr(str('"' + s + '"'))])
Пример #15
0
    def callback(lin_idx, sentence, start):
	if start < len(sentence):
	    return (pgf.Expr(sentence[start]), 0, start+1);
	return None;
Пример #16
0
def name_value(s):
    return pgf.Expr('NameValue', [name_expr(s)])
Пример #17
0
def float_tree(float_value):
    fe = pgf.Expr(str(float_value))
    return pgf.Expr("FNum",[fe])
Пример #18
0
def name_expr(name):
    return pgf.Expr(abbreviate(name) + '_Name', [])
Пример #19
0
                        continue
                    if tag == "nn" and an == "s Pl Nom":
                        continue
                    if tag == "nns" and an == "s Sg Nom":
                        continue

                    ans2.append((f, an, p))

                if len(ans2) == 0:
                    ans = ans1
                else:
                    ans = ans2

                ans2 = []
                for f, an, p in ans:
                    tbl = eng.tabularLinearize(pgf.Expr(f, []))
                    if "p" in tbl and tbl["p"] != "" and tbl["p"] not in ws:
                        continue
                    if "c2" in tbl and tbl["c2"] != "" and tbl["c2"] not in ws:
                        continue
                    if "c3" in tbl and tbl["c3"] != "" and tbl["c3"] not in ws:
                        continue
                    ans2.append((f, an, p))
                ans = ans2
            else:
                ans = []

                if tag == "at" and w.lower() in ["a", "an"]:
                    ans.append(("IndefArt", "_", 0))

                for f, an, p in eng.lookupMorpho(w):