nltk.sem.chat80 module

Overview

Chat-80 was a natural language system which allowed the user to interrogate a Prolog knowledge base in the domain of world geography. It was developed in the early ’80s by Warren and Pereira; see https://www.aclweb.org/anthology/J82-3002.pdf for a description and http://www.cis.upenn.edu/~pereira/oldies.html for the source files.

This module contains functions to extract data from the Chat-80 relation files (‘the world database’), and convert then into a format that can be incorporated in the FOL models of nltk.sem.evaluate. The code assumes that the Prolog input files are available in the NLTK corpora directory.

The Chat-80 World Database consists of the following files:

world0.pl
rivers.pl
cities.pl
countries.pl
contain.pl
borders.pl

This module uses a slightly modified version of world0.pl, in which a set of Prolog rules have been omitted. The modified file is named world1.pl. Currently, the file rivers.pl is not read in, since it uses a list rather than a string in the second field.

Reading Chat-80 Files

Chat-80 relations are like tables in a relational database. The relation acts as the name of the table; the first argument acts as the ‘primary key’; and subsequent arguments are further fields in the table. In general, the name of the table provides a label for a unary predicate whose extension is all the primary keys. For example, relations in cities.pl are of the following form:

'city(athens,greece,1368).'

Here, 'athens' is the key, and will be mapped to a member of the unary predicate city.

The fields in the table are mapped to binary predicates. The first argument of the predicate is the primary key, while the second argument is the data in the relevant field. Thus, in the above example, the third field is mapped to the binary predicate population_of, whose extension is a set of pairs such as '(athens, 1368)'.

An exception to this general framework is required by the relations in the files borders.pl and contains.pl. These contain facts of the following form:

'borders(albania,greece).'

'contains0(africa,central_africa).'

We do not want to form a unary concept out the element in the first field of these records, and we want the label of the binary relation just to be 'border'/'contain' respectively.

In order to drive the extraction process, we use ‘relation metadata bundles’ which are Python dictionaries such as the following:

city = {'label': 'city',
        'closures': [],
        'schema': ['city', 'country', 'population'],
        'filename': 'cities.pl'}

According to this, the file city['filename'] contains a list of relational tuples (or more accurately, the corresponding strings in Prolog form) whose predicate symbol is city['label'] and whose relational schema is city['schema']. The notion of a closure is discussed in the next section.

Concepts

In order to encapsulate the results of the extraction, a class of Concept objects is introduced. A Concept object has a number of attributes, in particular a prefLabel and extension, which make it easier to inspect the output of the extraction. In addition, the extension can be further processed: in the case of the 'border' relation, we check that the relation is symmetric, and in the case of the 'contain' relation, we carry out the transitive closure. The closure properties associated with a concept is indicated in the relation metadata, as indicated earlier.

The extension of a Concept object is then incorporated into a Valuation object.

Persistence

The functions val_dump and val_load are provided to allow a valuation to be stored in a persistent database and re-loaded, rather than having to be re-computed each time.

Individuals and Lexical Items

As well as deriving relations from the Chat-80 data, we also create a set of individual constants, one for each entity in the domain. The individual constants are string-identical to the entities. For example, given a data item such as 'zloty', we add to the valuation a pair ('zloty', 'zloty'). In order to parse English sentences that refer to these entities, we also create a lexical item such as the following for each individual constant:

PropN[num=sg, sem=<\P.(P zloty)>] -> 'Zloty'

The set of rules is written to the file chat_pnames.cfg in the current directory.

class nltk.sem.chat80.Concept[source]

Bases: object

A Concept class, loosely based on SKOS (https://www.w3.org/TR/swbp-skos-core-guide/).

__init__(prefLabel, arity, altLabels=[], closures=[], extension={})[source]
Parameters
  • prefLabel (str) – the preferred label for the concept

  • arity (int) – the arity of the concept

  • altLabels (list) – other (related) labels

  • closures (list) – closure properties of the extension (list items can be symmetric, reflexive, transitive)

  • extension (set) – the extensional value of the concept

augment(data)[source]

Add more data to the Concept’s extension set.

Parameters

data (string or pair of strings) – a new semantic value

Return type

set

close()[source]

Close a binary relation in the Concept’s extension set.

Returns

a new extension for the Concept in which the relation is closed under a given property

nltk.sem.chat80.binary_concept(label, closures, subj, obj, records)[source]

Make a binary concept out of the primary key and another field in a record.

A record is a list of entities in some relation, such as ['france', 'paris'], where 'france' is acting as the primary key, and 'paris' stands in the 'capital_of' relation to 'france'.

More generally, given a record such as ['a', 'b', 'c'], where label is bound to 'B', and obj bound to 1, the derived binary concept will have label 'B_of', and its extension will be a set of pairs such as ('a', 'b').

Parameters
  • label (str) – the base part of the preferred label for the concept

  • closures (list) – closure properties for the extension of the concept

  • subj (int) – position in the record of the subject of the predicate

  • obj (int) – position in the record of the object of the predicate

  • records (list of lists) – a list of records

Returns

Concept of arity 2

Return type

Concept

nltk.sem.chat80.cities2table(filename, rel_name, dbname, verbose=False, setup=False)[source]

Convert a file of Prolog clauses into a database table.

This is not generic, since it doesn’t allow arbitrary schemas to be set as a parameter.

Intended usage:

cities2table('cities.pl', 'city', 'city.db', verbose=True, setup=True)
Parameters
  • filename (str) – filename containing the relations

  • rel_name (str) – name of the relation

  • dbname – filename of persistent store

nltk.sem.chat80.clause2concepts(filename, rel_name, schema, closures=[])[source]

Convert a file of Prolog clauses into a list of Concept objects.

Parameters
  • filename (str) – filename containing the relations

  • rel_name (str) – name of the relation

  • schema (list) – the schema used in a set of relational tuples

  • closures (list) – closure properties for the extension of the concept

Returns

a list of Concept objects

Return type

list

nltk.sem.chat80.concepts(items=('borders', 'circle_of_lat', 'circle_of_long', 'city', 'contains', 'continent', 'country', 'ocean', 'region', 'sea'))[source]

Build a list of concepts corresponding to the relation names in items.

Parameters

items (list(str)) – names of the Chat-80 relations to extract

Returns

the Concept objects which are extracted from the relations

Return type

list(Concept)

nltk.sem.chat80.label_indivs(valuation, lexicon=False)[source]

Assign individual constants to the individuals in the domain of a Valuation.

Given a valuation with an entry of the form {'rel': {'a': True}}, add a new entry {'a': 'a'}.

Return type

Valuation

nltk.sem.chat80.main()[source]
nltk.sem.chat80.make_lex(symbols)[source]

Create lexical CFG rules for each individual symbol.

Given a valuation with an entry of the form {'zloty': 'zloty'}, create a lexical rule for the proper name ‘Zloty’.

Parameters

symbols (sequence -- set(str)) – a list of individual constants in the semantic representation

Return type

list(str)

nltk.sem.chat80.make_valuation(concepts, read=False, lexicon=False)[source]

Convert a list of Concept objects into a list of (label, extension) pairs; optionally create a Valuation object.

Parameters
  • concepts (list(Concept)) – concepts

  • read (bool) – if True, (symbol, set) pairs are read into a Valuation

Return type

list or Valuation

nltk.sem.chat80.process_bundle(rels)[source]

Given a list of relation metadata bundles, make a corresponding dictionary of concepts, indexed by the relation name.

Parameters

rels (list(dict)) – bundle of metadata needed for constructing a concept

Returns

a dictionary of concepts, indexed by the relation name.

Return type

dict(str): Concept

nltk.sem.chat80.sql_demo()[source]

Print out every row from the ‘city.db’ database.

nltk.sem.chat80.sql_query(dbname, query)[source]

Execute an SQL query over a database. :param dbname: filename of persistent store :type schema: str :param query: SQL query :type rel_name: str

nltk.sem.chat80.unary_concept(label, subj, records)[source]

Make a unary concept out of the primary key in a record.

A record is a list of entities in some relation, such as ['france', 'paris'], where 'france' is acting as the primary key.

Parameters
  • label (string) – the preferred label for the concept

  • subj (int) – position in the record of the subject of the predicate

  • records (list of lists) – a list of records

Returns

Concept of arity 1

Return type

Concept

nltk.sem.chat80.val_dump(rels, db)[source]

Make a Valuation from a list of relation metadata bundles and dump to persistent database.

Parameters
  • rels (list of dict) – bundle of metadata needed for constructing a concept

  • db (str) – name of file to which data is written. The suffix ‘.db’ will be automatically appended.

nltk.sem.chat80.val_load(db)[source]

Load a Valuation from a persistent database.

Parameters

db (str) – name of file from which data is read. The suffix ‘.db’ should be omitted from the name.