コード例 #1
0
class Relativizers(object):

    def __init__(self,model=None):
        self.features=RelFeatures()
        self.model=model

    def learn(self,tree,outfile):
        for rel in tree.rels:
            features=self.features.create(rel.gov,rel.dep,tree)
            types=is_dep(rel.gov,rel.dep,tree)
            if len(types)<2: continue # post-processed dependency, do not use in training
            for dtype in types:
                if dtype==u"rel": continue
                klass=dtype.split(u"&",1)[1]
                writeData(outfile,klass,features)
                break


    def post_process_rel(self,g,d,t,tree):
        # move dependency if iccomp and it does not have this dtype already
        for dep in tree.deps:
            if dep.gov==g and dep.dtype==u"iccomp":
                deps=set()
                for dep2 in tree.childs[dep.dep]: # dependents of iccomp
                    deps.add(dep2.dtype) # collect all dependents of iccomp
                if t not in deps:
                    g=dep.dep
                    break
        return g,d,t

    def predict(self,tree):
        if self.model is None:
            print >> sys.stderr, u"no model found"
            sys.exit(1)
        for rel in tree.rels:
            features=self.features.create(rel.gov,rel.dep,tree)
            klass=self.model.predict_one(features)
            klass_str=u"rel&"+self.model.number2klass[klass]
            g,d,t=self.post_process_rel(rel.gov,rel.dep,klass_str,tree)
            dependency=Dep(g,d,t,flag=u"REL")
            #print dependency
            tree.add_dep(dependency)
コード例 #2
0
 def __init__(self,model=None):
     self.features=RelFeatures()
     self.model=model