Ejemplo n.º 1
0
def make_valuation(concepts, read=False, lexicon=False):
    """
    Convert a list of ``Concept`` objects into a list of (label, extension) pairs;
    optionally create a ``Valuation`` object.

    :param concepts: concepts
    :type concepts: list(Concept)
    :param read: if ``True``, ``(symbol, set)`` pairs are read into a ``Valuation``
    :type read: bool
    :rtype: list or Valuation
    """
    vals = []

    for c in concepts:
        vals.append((c.prefLabel, c.extension))
    if lexicon:
        read = True
    if read:
        from nltk.sem import Valuation

        val = Valuation({})
        val.update(vals)
        # add labels for individuals
        val = label_indivs(val, lexicon=lexicon)
        return val
    else:
        return vals
Ejemplo n.º 2
0
def make_valuation(concepts, read=False, lexicon=False):
    """
    Convert a list of ``Concept`` objects into a list of (label, extension) pairs;
    optionally create a ``Valuation`` object.

    :param concepts: concepts
    :type concepts: list(Concept)
    :param read: if ``True``, ``(symbol, set)`` pairs are read into a ``Valuation``
    :type read: bool
    :rtype: list or Valuation
    """
    vals = []

    for c in concepts:
        vals.append((c.prefLabel, c.extension))
    if lexicon:
        read = True
    if read:
        from nltk.sem import Valuation

        val = Valuation({})
        val.update(vals)
        # add labels for individuals
        val = label_indivs(val, lexicon=lexicon)
        return val
    else:
        return vals
def make_valuation(concepts, read=False, lexicon=False):
    """
    Convert a list of C{Concept}s into a list of (label, extension) pairs;
    optionally create a C{Valuation} object.

    @param concepts: concepts
    @type concepts: list of L{Concept}s
    @param read: if C{True}, C{(symbol, set)} pairs are read into a C{Valuation}
    @type read: C{bool}
    @rtype: C{list} or a L{Valuation}
    """
    vals = []

    for c in concepts:
        vals.append((c.prefLabel, c.extension))
    if lexicon: read = True
    if read:
        from nltk.sem import Valuation
        val = Valuation({})
        val.update(vals)
        # add labels for individuals
        val = label_indivs(val, lexicon=lexicon)
        return val
    else:
        return vals
Ejemplo n.º 4
0
def make_valuation(concepts, read=False, lexicon=False):
    """
    Convert a list of C{Concept}s into a list of (label, extension) pairs;
    optionally create a C{Valuation} object.

    :param concepts: concepts
    :type concepts: list of L{Concept}s
    :param read: if C{True}, C{(symbol, set)} pairs are read into a C{Valuation}
    :type read: bool
    :rtype: list or a L{Valuation}
    """
    vals = []
    
    for c in concepts:
        vals.append((c.prefLabel, c.extension))
    if lexicon: read = True
    if read:
        from nltk.sem import Valuation
        val = Valuation({})
        val.update(vals)
        # add labels for individuals
        val = label_indivs(val, lexicon=lexicon)
        return val
    else: return vals
Ejemplo n.º 5
0
    def _convert2val(self, valuation_str):
        """
        Transform the output file into an NLTK-style Valuation.

        :return: A model if one is generated; None otherwise.
        :rtype: sem.Valuation
        """
        valuation_standard_format = self._transform_output(
            valuation_str, 'standard')

        val = []
        for line in valuation_standard_format.splitlines(False):
            l = line.strip()

            if l.startswith('interpretation'):
                # find the number of entities in the model
                num_entities = int(l[l.index('(') + 1:l.index(',')].strip())

            elif l.startswith('function') and l.find('_') == -1:
                # replace the integer identifier with a corresponding alphabetic character
                name = l[l.index('(') + 1:l.index(',')].strip()
                if is_indvar(name):
                    name = name.upper()
                value = int(l[l.index('[') + 1:l.index(']')].strip())
                val.append((name, MaceCommand._make_model_var(value)))

            elif l.startswith('relation'):
                l = l[l.index('(') + 1:]
                if '(' in l:
                    # relation is not nullary
                    name = l[:l.index('(')].strip()
                    values = [
                        int(v.strip())
                        for v in l[l.index('[') + 1:l.index(']')].split(',')
                    ]
                    val.append(
                        (name,
                         MaceCommand._make_relation_set(num_entities, values)))
                else:
                    # relation is nullary
                    name = l[:l.index(',')].strip()
                    value = int(l[l.index('[') + 1:l.index(']')].strip())
                    val.append((name, value == 1))

        return Valuation(val)
Ejemplo n.º 6
0
Archivo: chat80.py Proyecto: sp00/nltk
def val_load(db):
    """
    Load a ``Valuation`` from a persistent database.

    :param db: name of file from which data is read.
               The suffix '.db' should be omitted from the name.
    :type db: string
    """
    dbname = db + ".db"

    if not os.access(dbname, os.R_OK):
        sys.exit("Cannot read file: %s" % dbname)
    else:
        db_in = shelve.open(db)
        from nltk.sem import Valuation
        val = Valuation(db_in)
        #        val.read(db_in.items())
        return val
Ejemplo n.º 7
0
    def convert2val(self):
        """
        Transform the output file into an NLTK-style Valuation. 
        
        @return: A model if one is generated; None otherwise.
        @rtype: L{nltk.sem.Valuation} 
        """
        from nltk.sem import Valuation
        valuation = None
        if self.model_found():
            self._transform_output('standard')

            d = {}
            for line in open(os.path.join(self._p9_dir, 'mace.standard.out')):
                l = line.strip()
                # find the number of entities in the model
                if l.startswith('interpretation'):
                    num_entities = int(l[l.index('(') +
                                         1:l.index(',')].strip())
                # replace the integer identifier with a corresponding alphabetic character
                elif l.startswith('function') and l.find('_') == -1:
                    name = l[l.index('(') + 1:l.index(',')].strip()
                    d[name] = Mace._make_model_var(
                        int(l[l.index('[') + 1:l.index(']')].strip()))

                elif l.startswith('relation'):
                    name = l[l.index('(') + 1:l.index('(',
                                                      l.index('(') +
                                                      1)].strip()
                    values = [
                        int(v.strip())
                        for v in l[l.index('[') + 1:l.index(']')].split(',')
                    ]
                    d[name] = Mace._make_model_dict(num_entities, values)

            valuation = Valuation(d)
        return valuation
Ejemplo n.º 8
0
        for name, g in groupby(sorted(val0), itemgetter(0)):
          e_len1 = []
          e_len2 = []
          for name1, entities1 in g:
            if len(entities1[0]) == 1:
              e_len1.extend(entities1)
            elif len(entities1[0]) == 2:
              e_len2.extend(entities1)
          if len(list(set(e_len1))) > len(list(set(e_len2))):
            e_set = set(e_len1)
            data[name] = e_len1
          else:
            e_set = set(e_len2)
            data[name] = e_len2
          val2.append((name, e_set))
        models[i][key] = Model(dom, Valuation(val2))  
        word_data[key] = data

  #save model
  #with open(VTE + '/work/structure_g_obj.pkl', 'wb') as f:
  #  pickle.dump(models[0], f, protocol=2)

  #save model
  #with open(VTE + '/work/structure_g_obj_att.pkl', 'wb') as f:
  #  pickle.dump(models[1], f, protocol=2)

  #save model
  with open(VTE + '/work/structure_graph_200.pkl', 'wb') as f:
    pickle.dump(models[2], f, protocol=2)

  # save words
Ejemplo n.º 9
0
    v = n_lst + a_lst + s_lst
    data.update(data_n)
    data.update(data_a)
    data.update(data_s)
  return(v, data)

if __name__ == '__main__':

  model_data = {}
  word_data = {}
  for model_id in grim_lst:
    v, data = model_parser(GRIM + '/' + model_id + '.mod')
    model_data[model_id] = v
    word_data[model_id] = data
  
  # construct a new Model.
  model = {}
  for model_id in grim_lst:
    val = Valuation(model_data[model_id])
    dom = val.domain
    m = Model(dom, val)
    model[model_id] = m
  
  # save model
  with open(VTE + '/work/structure.pkl', 'wb') as f:
    pickle.dump(model, f, protocol=2)
  
  # save words
  with open(VTE + '/work/words_elst.pkl', 'wb') as f:
    pickle.dump(word_data, f, protocol=2)
Ejemplo n.º 10
0
#!/usr/bin/python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from nltk.sem import Valuation, Model, Assignment, evaluate_sents

if __name__ == '__main__':
    valuation_describe = [('ember', 'e'), ('asztal', 'a'), (['elo',
                                                             set('e')]),
                          (['!elo', set('a')]), (['cselekvo',
                                                  set('e')]),
                          (['!cselekvo', set('a')])]

    valuation = Valuation(valuation_describe)
    valuation_province = Assignment(valuation.domain)
    model = Model(valuation.domain, valuation)
    sentence = 'egy asztal fut'

    res = evaluate_sents([sentence], 'file:data/semantic.fcfg', model,
                         valuation_province)

    for i in res:
        print("The sentence: '{}'".format(sentence))
        print("The parsed tree: '{}'".format(i[0][0]))
        print("The semantic formula: '{}'".format(i[0][1]))
        print("The semantic value: '{}'".format(i[0][2]))