module documentation

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 http://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 Concept A Concept class, loosely based on SKOS (http://www.w3.org/TR/swbp-skos-core-guide/).
Function binary_concept Make a binary concept out of the primary key and another field in a record.
Function cities2table Convert a file of Prolog clauses into a database table.
Function clause2concepts Convert a file of Prolog clauses into a list of Concept objects.
Function concepts Build a list of concepts corresponding to the relation names in items.
Function label_indivs Assign individual constants to the individuals in the domain of a Valuation.
Function main Undocumented
Function make_lex Create lexical CFG rules for each individual symbol.
Function make_valuation Convert a list of Concept objects into a list of (label, extension) pairs; optionally create a Valuation object.
Function process_bundle Given a list of relation metadata bundles, make a corresponding dictionary of concepts, indexed by the relation name.
Function sql_demo Print out every row from the 'city.db' database.
Function sql_query Execute an SQL query over a database. :param dbname: filename of persistent store :type schema: str :param query: SQL query :type rel_name: str
Function unary_concept Make a unary concept out of the primary key in a record.
Function val_dump Make a Valuation from a list of relation metadata bundles and dump to persistent database.
Function val_load Load a Valuation from a persistent database.
Variable borders Undocumented
Variable circle_of_lat Undocumented
Variable circle_of_long Undocumented
Variable city Undocumented
Variable contains Undocumented
Variable continent Undocumented
Variable country Undocumented
Variable item_metadata Undocumented
Variable items Undocumented
Variable not_unary Undocumented
Variable ocean Undocumented
Variable region Undocumented
Variable rels Undocumented
Variable sea Undocumented
Function _str2records Read a file into memory and convert each relation clause into a list.
def 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:strthe base part of the preferred label for the concept
closures:listclosure properties for the extension of the concept
subj:intposition in the record of the subject of the predicate
obj:intposition in the record of the object of the predicate
records:list of listsa list of records
Returns
ConceptConcept of arity 2
def 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:strfilename containing the relations
rel_name:strname of the relation
dbnamefilename of persistent store
verboseUndocumented
setupUndocumented
schema:strUndocumented
def clause2concepts(filename, rel_name, schema, closures=[]): (source)

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

Parameters
filename:strfilename containing the relations
rel_name:strname of the relation
schema:listthe schema used in a set of relational tuples
closures:listclosure properties for the extension of the concept
Returns
lista list of Concept objects
def concepts(items=items): (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
list(Concept)the Concept objects which are extracted from the relations
def 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'}.

Parameters
valuation:ValuationUndocumented
lexiconUndocumented
Returns
ValuationUndocumented
def main(): (source)

Undocumented

def 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
Returns
list(str)Undocumented
def 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:boolif True, (symbol, set) pairs are read into a Valuation
lexiconUndocumented
Returns
list or ValuationUndocumented
def 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
dict(str): Concepta dictionary of concepts, indexed by the relation name.
def sql_demo(): (source)

Print out every row from the 'city.db' database.

def 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

def 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:stringthe preferred label for the concept
subj:intposition in the record of the subject of the predicate
records:list of listsa list of records
Returns
ConceptConcept of arity 1
def val_dump(rels, db): (source)

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

Parameters
rels:list of dictbundle of metadata needed for constructing a concept
db:strname of file to which data is written. The suffix '.db' will be automatically appended.
def val_load(db): (source)

Load a Valuation from a persistent database.

Parameters
db:strname of file from which data is read. The suffix '.db' should be omitted from the name.
borders: dict = (source)

Undocumented

circle_of_lat: dict = (source)

Undocumented

circle_of_long: dict = (source)

Undocumented

city: dict = (source)

Undocumented

contains: dict = (source)

Undocumented

continent: dict = (source)

Undocumented

country: dict = (source)

Undocumented

item_metadata = (source)

Undocumented

Undocumented

not_unary: list[str] = (source)

Undocumented

ocean: dict = (source)

Undocumented

region: dict = (source)

Undocumented

Undocumented

sea: dict = (source)

Undocumented

def _str2records(filename, rel): (source)

Read a file into memory and convert each relation clause into a list.