Utility Modules

ContextLogger.py - wrapper for Python logging API

Copyright CUED Dialogue Systems Group 2015 - 2017

Relevant Config variables [Default values]:

[logging]
screen_level=info
file_level=debug
file=logFileName.txt
usecolor = False
Basic Usage:
>>> from utils import ContextLogger
>>> ContextLogger.createLoggingHandlers()
>>> logger = ContextLogger.getLogger('Name')

then within any script issue debug, info, warning and error messages, eg

>>> logger.warning("String too long [%d]", 100)

issuing an error message generates ExceptionRaisedByLogger.

Logger can if required be configured via a config section. Then pass config info to createLoggingHandlers >>> ContextLogger.createLoggingHandlers(config)


class utils.ContextLogger.ConsoleFormatter(*args, **kwargs)

Class to format logger output to console.

format(record)

Format the specified record as text.

The record’s attribute dictionary is used as the operand to a string formatting operation which yields the returned string. Before formatting the dictionary, a couple of preparatory steps are carried out. The message attribute of the record is computed using LogRecord.getMessage(). If the formatting string uses the time (as determined by a call to usesTime(), formatTime() is called to format the event time. If there is exception information, it is formatted using formatException() and appended to the message.

class utils.ContextLogger.ContextLogger(module_name=None, *args)

Wrapper for Python logging class.

addContext(*args)

Create a nested named context for use in a with statement.

Parameters

args – list of one or more context names (str)

Returns

ContextManager

Example:
>>> with mylogger.addContext("Session 1") :
...    mylogger.warning("Warn Message from Session 1")
addTimedContext(*args)

Create a timed nested named context for use in a with statement.

Parameters

args – list of one or more context names (str)

Returns

ContextManager

Example:
>>> with mylogger.addContext("Session 1") :
...    Dostuff()

On exit from the with statement, the elapsed time is logged.

debug(msg, *args, **kwargs)

Log a DEBUG message.

Parameters
  • msg (formatted-str) – message string

  • args – args to formatted message string if any

Returns

None

error(msg, *args, **kwargs)

Log an ERROR message.

Parameters
  • msg (formatted-str) – message string

  • args – args to formatted message string if any

Returns

None

Note

Issuing an error message also raises exception ExceptionRaisedByLogger

info(msg, *args, **kwargs)

Log an INFO message.

Parameters
  • msg (formatted-str) – message string

  • args – args to formatted message string if any

Returns

None

setLevel(level)

Set the logging level of this logger.

Parameters

level (str) – default screen logging level if no config

Returns

None

warning(msg, *args, **kwargs)

Log a WARNING message.

Parameters
  • msg (formatted-str) – message string

  • args – args to formatted message string if any

Returns

None

exception utils.ContextLogger.ExceptionRaisedByLogger
class utils.ContextLogger.NOcolors

ASCII escape chars just print junk when dumping logger output to file. Can use the config setting usecolor.

class utils.ContextLogger.bcolors

Color specification for logger output.

utils.ContextLogger.createLoggingHandlers(config=None, screen_level='INFO', log_file=None, file_level='DEBUG', use_color=True)

Create a top level logger and configure logging handlers

Parameters
  • config – a config structure as returned by the std ConfigParser

  • screen_level (str) – default screen logging level if no config

  • log_file (str) – default log file if no config

  • file_level (str) – default file logging level if no config

Returns

None

Note

Valid logging levels are “DEBUG”, “INFO”, “WARNING”, “ERROR”

utils.ContextLogger.getLogger(name)

Retrieve or if necessary create a context logger with specified name.

Parameters

name (str) – name of logger to create or retrieve

Returns

logger (ContextLogger.ContextLogger)

Note

Use only this function to create instances of the ContextLogger class

dact.py - dialogue act specification

Copyright CUED Dialogue Systems Group 2015 - 2017

Basic Usage:
>>> import dact

Note

Copied from dstc-ii code

See also

CUED Imports/Dependencies:

import utils.ContextLogger


class utils.dact.DactItem(slot, op, val)

Dialogue act specification

Parameters
  • slot (str) – slot name

  • op (str) – comparative operation, e.g. ‘=’ or ‘!=’

match(other)

Commutative operation for comparing two items. Note that “self” is the goal constraint, and “other” is from the system action. The item in “other” must be more specific. For example, the system action confirm(food=dontcare) doesn’t match the goal with food=chinese, but confirm(food=chinese) matches the goal food=dontcare.

If slots are different, return True.

If slots are the same, (possible values are x, y, dontcare, !x, !y, !dontcare)s

x, x = True x, y = False dontcare, x = True x, dontcare = False dontcare, dontcare = True

x, !x = False x, !y = True x, !dontcare = True dontcare, !x = False dontcare, !dontcare = False

!x, !x = True !x, !y = True !x, !dontcare = True !dontcare, !dontcare = True

Parameters

other

Returns

utils.dact.inferSlotsForAct(uacts, ontology=None)

Works out the slot from the ontology and value

Parameters
  • uacts

  • ontology

Returns

user’s dialogue acts

DiaAct.py - dialogue act specification that extends dact.py

Copyright CUED Dialogue Systems Group 2015 - 2017

Basic Usage:
>>> import DiaAct   

See also

CUED Imports/Dependencies:

import utils.ContextLogger
import utils.dact


class utils.DiaAct.DiaAct(act)

Dialogue act class.

self.dact = {'act': acttype,'slots': [(slot1, op1, value1), ..])}

Parameters

act – dialogue act in string

Todo

SummaryAction is not implemented.

append(slot, value, negate=False)

Add item to this act avoiding duplication

Parameters
  • slot (str) – None

  • value (str) – None

  • negate (bool [Default=False]) – semantic operation is negation or not

Returns

append_front(slot, value, negate=False)

Add item to this act avoiding duplication

Parameters
  • slot (str) – None

  • value (str) – None

  • negate (bool) – operation is ‘=’ or not? False by default.

Returns

contains(slot, value, negate=False)
Parameters
  • slot (str) – None

  • value (str) – None

  • negate (bool - default False) – None

Returns

(bool) is full semantic act in self.items?

contains_slot(slot)
Parameters

slot (str) – slot name

Returns

(bool) answering whether self.items mentions slot

getDiaItemFormat()
Parameters

None (based on self.items) – None

Returns

the number of arguments of this diaact type. e.g. act: 0, act(slot): 1, act(slot,value): 2

get_value(slot, negate=False)
Parameters
  • slot (str) – slot name

  • negate (bool - default False) – relating to semantic operation, i.e slot = or slot !=.

Returns

(str) value

get_values(slot, negate=False)
Parameters
  • slot (str) – slot name

  • negate (bool - default False) –

    • semantic operation

Returns

(list) values in self.items

has_conflicting_value(constraints)
Parameters

constraints (list) – as [(slot, op, value), …]

Returns

(bool) True if this DiaAct has values which conflict with the given constraints. Note that consider only non-name slots.

to_string()
Parameters

None

Returns

(str) semantic act

class utils.DiaAct.DiaActWithProb(act)

self.dact = {'act': acttype,'slots': set([(slot1, value1), ..])}

Parameters

act (str) – dialogue act

Todo

Parser is not complete. Attached probability P_Au_O cannot be parsed. SummaryAction is not implemented. Emphasis and operator are not implemented.

utils.DiaAct.known_format = {'affirm': 2, 'badact': 0, 'bye': 0, 'confirm': 2, 'confreq': 1, 'deny': 3, 'hello': 0, 'inform': 2, 'negate': 2, 'null': 0, 'repeat': 0, 'reqalts': 2, 'request': 1, 'select': 3, 'thankyou': 0}

within class dictionary

0 signals no dia item, eg hello() 1 signals slot, but no value, eg request(bar) 2 signals slot value pair, eg inform(food=”Italian”) 3 signals 2 slot value pairs, eg deny(food=”Italian”, food=”Chinese”)

DialogueState.py - dialogue state object specification

Copyright CUED Dialogue Systems Group 2017

Basic Usage:
>>> import DialogueState   

See also

CUED Imports/Dependencies:

import utils.ContextLogger


class utils.DialogueState.DialogueState

The encapsulation of the dialogue state with a definition of the main interface.

check_user_ending()

Utility function to check whether the user has said good bye.

getDomainState(dstring)

Access to the dialogue state (belief state) of the specified domain. Returns None if there is no state yet.

Parameters

dstring (str) – the string identifier of the domain the state should be retreived from

Returns

the state dict or None

getLastSystemAct(dstring)

Retreives the last system act of domain dstring.

Parameters

dstring (str) – the string identifier of the domain of which the last system act should be retreived from

Returns

the last system act of domain dstring or None

printUserActs(dstring)

Utility function to print the user acts stored in the belief state of domain dstring.

Parameters

dstring (str) – the string identifier of the domain of which the user act should be printed

setLastSystemAct(sysAct)

Sets the last system act of the current domain. Note that currentdomain needs to be set first, otherwise it does not work.

Parameters

sysAct (str) – string representation of the last system action

dummyDialogueServerClient.py - script for talking to the dialogue server via cmd line, i.e. without using any voice client.

Copyright CUED Dialogue Systems Group 2015 - 2017

Basic Usage:

Also can be used to simulate multiple concurrent calls to a running DialogueServer() - each of these is run in it’s own process via python’s multiprocessing module. >> python dummyDialogueServerClient.py fake number_of_fake_clients

— where number_of_fake_clients is an integer.

NB: This was just a tool I wrote for developing DialogueServer, but it is actually pretty useful so i’ve put it in the repo and it is also used by nosetests now to execute some basic testing of DialogueServer.


class utils.dummyDialogueServerClient.fake_client(user_in, hostname='localhost', port=8082, numDialogs=2)

Text based client for interacting with DialogueServer via the cmd line

reset()

Resets dialogue state.

Scanner.py - string scanning utility

Copyright CUED Dialogue Systems Group 2015 - 2017

See also

CUED Imports/Dependencies:

none


class utils.Scanner.Scanner(string)

Class to maintain tokenized string.

utils.Scanner.remove_comments(src)

This reads tokens using tokenize.generate_tokens and recombines them using tokenize.untokenize, and skipping comment/docstring tokens in between

Settings.py - global variables: config, random num generator

Copyright CUED Dialogue Systems Group 2015 - 2017

Creates and makes accessible system wide a config parser and random number generator. Also contains hardcodings of the paths to the ontologies.

[GENERAL]

root = ‘’ - which is the path to root directory of python system. Use when running grid jobs.

Globals:

config:       python ConfigParser.ConfigParser(allow_no_value=True) object
random:       numpy.random.RandomState() random number generator
root:         location of root directory of repository - for running on the grid

See also

CUED Imports/Dependencies:

none


utils.Settings.init(config_file, seed=None)

Called by dialog programs (simulate, texthub, dialogueserver) to init Settings globals

utils.Settings.load_config(config_file)

Loads the passed config file into a python ConfigParser().

Parameters

config_file (str) – path to config

utils.Settings.load_root(rootIn=None)

Root is the location (ie full path) of the cued-python repository. This is used when running things on the grid (non local machines in general).

utils.Settings.locate_file(filename)

Locate file either as given or relative to root

Parameters

filename – file to check

Returns

filename possibly prepended with root

utils.Settings.set_seed(seed)

Intialise np random num generator

Parameters

seed (int) – None