示例#1
0
    def _end_node(self):
        # TODO: make error msg more specific
        if not self._comp_attrs.get("label"):
            raise DaesoError("node must have a 'label' attribute")
        if not self._comp_attrs.get("tokens"):
            raise DaesoError("node must have a 'tokens' attribute")

        self._node = None
        self._comp_attrs = None
示例#2
0
    def _end_graph(self):
        if len(self._graph) == 0:
            msg = "encountered empty graph (id=%s) while parsing graphbank" % self._graph.id
            raise DaesoError(msg)

        self._update_tokens()
        self._graph = None
示例#3
0
 def _check_for_unresolved_graphs(self, inf):
     unresolved = [ '"{0}"'.format(id) 
                    for (id, graph) in self._id2graph.items() 
                    if graph is None ]
 
     if unresolved:
         msg = ( "graphs with the following id's were not found"
                 ' in graphbank file "{0}": {1}'.format(
                     inf.name, 
                     ", ".join(unresolved) ))
         raise DaesoError(msg)
示例#4
0
    def _end_graph(self):
        if self._graph_wanted():
            if self._graph.tokens is None:
                raise DaesoError("graph must have a 'tokens' attribute")

            # set the root node, defined as a graph attribute like
            # <data key="root">n0</data>
            self._graph.root = self._graph.graph.get("graph")

            self._graph = None
            self._comp_attrs = None
示例#5
0
 def _get_node_label(self, attrs):
     """
     return node label, which is the 'pos', 'cat' or 'index' attribute, in
     that order
     """
     for key in ("pos", "cat", "index"):
         try:
             return attrs[key]
         except KeyError:
             pass
     else:
         raise DaesoError("Alpino <node> element must have "
                          "'pos', 'cat' or 'index' attribute")
示例#6
0
    def _lookup_attr_name(self):
        # lookup the name of the component attribute for the current key id in
        # self._data_key, which is the value of the "key" attribute on the
        # current <data> element

        # the tag of the parent of <data>
        key_for = self._context[-2]

        try:
            return self._key_table[key_for][self._data_key]["name"]
        except KeyError:
            pass

        # if the name is not in the key table specifically for this component,
        # then it _must_ be in the general property table
        try:
            return self._key_table["all"][self._data_key]["name"]
        except:
            raise DaesoError("encountered undefined key " +
                             repr(self._data_key))
示例#7
0
    def __init__(self,
                 function=ff_none,
                 type="i",
                 metric="O",
                 name=None,
                 pp_graph_hooks=[],
                 pp_node_hooks=[]):
        """
        @keyword function: feature function which is called to compute the
        feature's value in a particular instance
                
        @keyword type: Numpy data type
        
        @keyword metric: Timbl feature metric (e.g. "O","N", ...).
        
        @keyword name: alternative feature name; by default the feature name
        is derived from the name of the feature function by striping the
        initial "ff_" string        
        
        @keyword pp_graph_hooks: a list op preprocessing hooks, where each
        preprocesing function takes a pair of source and target graphs as
        input
        
        @keyword pp_node_hooks: a list op preprocessing hooks, where each
        preprocesing function takes a pair of source and target nodes as
        input
        """
        self.type = type
        self.function = function
        self.init_metric = self.metric = metric
        self.pp_graph_hooks = pp_graph_hooks
        self.pp_node_hooks = pp_node_hooks

        if name:
            self.name = name
        elif function.__name__.startswith("ff_"):
            self.name = function.__name__[3:]
        else:
            raise DaesoError("cannot infer feature name from function named " +
                             function.__name__)