Ejemplo n.º 1
0
        sample_tree(t, m)
    else:
        if t.c in m:
            ch = m[t.c].rvs()
            if len(ch) == 2:
                lc = tree.Tree(ch[0][0])
                rc = tree.Tree(ch[1][0])
                sample_tree(lc, m)
                sample_tree(rc, m)
                t.ch = [lc, rc]
            elif len(ch) == 1:
                lc = tree.Tree(ch[0])
                t.ch = [lc]


R = model.CondModel('R')

for line in sys.stdin:
    if line.strip != '':
        R.read(line)

m = {}
for c in R:
    vals = [None, None]
    vals[0] = list(R[c].keys())
    vals[1] = np.asarray(list(R[c].values()))
    m[c] = rv_discrete_label(values=vals)

for i in range(n):
    t = tree.Tree()
    sample_tree(t, m)
Ejemplo n.º 2
0
        return (words.index(t) + 1)
    heads[t] = max(t.ch, key=lambda x: head_model[t.c][x])
    if args.debug:
        heads[t].c = 'HEAD:' + heads[t].c + '->' + str(ix)
    children = t.ch[:]
    head = children.pop(children.index(heads[t]))
    headix = get_deps(head, ix, words)
    for ch in children:
        get_deps(ch, headix, words)
        if args.debug:
            ch.c += '->' + str(headix)
    return (headix)


with open(args.model[0], 'r') as m:
    head_model = model.CondModel('R')
    for line in m:
        head_model.read(line)

t = tree.Tree()

for line in sys.stdin:
    heads = {}
    deps = {}
    t.read(line)
    preterminals = preterms(t)
    get_deps(t, 0, preterminals)
    preterminals.insert(0, tree.Tree('X', [tree.Tree('ROOT', [])]))
    if args.debug:
        print(t)
    for i in range(1, len(preterminals)):
Ejemplo n.º 3
0
#### create branch- and depth-specific pcfg

import sys, os
sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', 'resource-gcg', 'scripts'))
import model

MAX_ITER = 20

#### 0) read model from stdin
mCr = model.Model('Cr')
mCC = model.CondModel('CC')
mCu = model.CondModel('Cu')
mX  = model.CondModel('X')
mPc = model.CondModel('Pc')
mPw = model.CondModel('Pw')
mP = model.Model('P')
mW = model.Model('W')
for s in sys.stdin:
    mCr.read(s)
    mCC.read(s)
    mCu.read(s)
    mX.read(s)
    mPc.read(s)
    mPw.read(s)
    mP.read(s)
    mW.read(s)
## # add terminal rules (comment out if terminal and nonterminal categories intersect)
## for c in mX:
##     mCC[c]['-','-'] = 1.0
Ejemplo n.º 4
0
import model

input = [
    "G : who = .1", "G : whom = .9", "P who : who = 1", "P who : whom = 0",
    "P whom : who = .2", "P whom : whom = .8", "C who : who = 1",
    "C who : whom = 0", "C whom : who = .5", "C whom : whom = .5"
]

G = model.Model('G')
#PgivG
P = model.CondModel('P')
#CgivP
C = model.CondModel('C')

for line in input:
    G.read(line)
    P.read(line)
    C.read(line)
    print line

PgivNone = model.Model("PgivNone")
for g in P:
    for p in P[g]:
        PgivNone[p] += P[g][p] / len(P[p])

GCP = model.Model("GCP")
PC = model.Model("PC")
for g in G:
    for p in C:
        for c in C[p]:
            GCP[g, p, c] = G[g] * P[g][p] * C[p][c]
Ejemplo n.º 5
0
    os.path.join(os.path.dirname(__file__), '..', '..', 'resource-gcg',
                 'scripts'))
import model
import argparse

argparser = argparse.ArgumentParser(description='''
Counts and divides model-formatted files.
''')
argparser.add_argument('-n',
                       '--no-normalization',
                       dest='nonorm',
                       action='store_true',
                       help='Do not normalize')
args = argparser.parse_args()

counts = model.CondModel()
for line in sys.stdin:
    predictor, _, response = line.strip('\n').rpartition(' : ')
    response, count = response.split(' = ') if ' = ' in response else (
        response, '1')
    counts[predictor][response] += float(count)

if not args.nonorm:
    for predictor in sorted(counts):
        denominator = sum(
            [counts[predictor][val] for val in counts[predictor]])
        for response in sorted(counts[predictor]):
            print(predictor + ' : ' + response + ' = ' +
                  str(counts[predictor][response] / denominator))
else:
    for predictor in sorted(counts):
Ejemplo n.º 6
0
import model

input = ["R : ohio = .5", "R : phil = .5", "W : /nek/ = .6", "W : /naek/ = .4",
"O ohio /nek/ : [nek] = 1", "O phil /nek/ : [nek] = .667", "O phil /nek/ : [naek] = .333",
"O ohio /naek/ : [naek] = 1", "O phil /naek/ : [naek] = 1"]

R = model.Model("R")
W = model.CondModel('W')
O = model.CondModel("O")

OgivR = model.CondModel('OR')

for line in input:
	R.read(line)
	W.read(line)
	O.read(line)
	
OgivNone = model.Model("Onone")

Idata = "I [naek] [nek] [naek]"
IdataSplit = Idata.split()
	
#w values aren't given for each time step, so condition out
for r, w in O:
	for o in O[r,w]:
		OgivR[r][o] += O[r,w][o] / 2
		OgivNone[o] += O[r,w][o] / 4

#for o in OgivNone:
	#print(o, OgivNone[o])