alex.components.asr package

Submodules

alex.components.asr.autopath module

self cloning, automatic path configuration

copy this into any subdirectory of pypy from which scripts need to be run, typically all of the test subdirs. The idea is that any such script simply issues

import autopath

and this will make sure that the parent directory containing “pypy” is in sys.path.

If you modify the master “autopath.py” version (in pypy/tool/autopath.py) you can directly run it which will copy itself on all autopath.py files it finds under the pypy root directory.

This module always provides these attributes:

pypydir pypy root directory path this_dir directory where this autopath.py resides

alex.components.asr.base module

alex.components.asr.common module

alex.components.asr.common.asr_factory(cfg, asr_type=None)[source]

Returns instance of specified ASR decoder in asr_type.

The ASR decoders are imported on the fly, because they need external non Python libraries.

alex.components.asr.common.get_asr_type(cfg)[source]

Reads the ASR type from the configuration.

alex.components.asr.exceptions module

exception alex.components.asr.exceptions.ASRException[source]

Bases: alex.AlexException

exception alex.components.asr.exceptions.JuliusASRException[source]

Bases: alex.components.asr.exceptions.ASRException

exception alex.components.asr.exceptions.JuliusASRTimeoutException[source]

Bases: alex.components.asr.exceptions.ASRException

exception alex.components.asr.exceptions.KaldiASRException[source]

Bases: alex.components.asr.exceptions.ASRException

exception alex.components.asr.exceptions.KaldiSetupException[source]

Bases: alex.components.asr.exceptions.KaldiASRException

alex.components.asr.google module

alex.components.asr.pykaldi module

alex.components.asr.test_utterance module

class alex.components.asr.test_utterance.TestUttCNFeats(methodName='runTest')[source]

Bases: unittest.case.TestCase

Basic test for utterance confnet features.

test_empty_features()[source]
class alex.components.asr.test_utterance.TestUtterance(methodName='runTest')[source]

Bases: unittest.case.TestCase

Tests correct working of the Utterance class.

setUp()[source]
test_index()[source]
test_ngram_iterator()[source]
class alex.components.asr.test_utterance.TestUtteranceConfusionNetwork(methodName='runTest')[source]

Bases: unittest.case.TestCase

Tests correct working of the UtteranceConfusionNetwork class.

Test using
$ python -m unittest test_utterance
test_conversion_of_confnet_into_nblist()[source]
test_idx_zero()[source]
test_ngram_iterator()[source]
test_replace()[source]
test_repr_basic()[source]

alex.components.asr.utterance module

class alex.components.asr.utterance.ASRHypothesis[source]

Bases: alex.ml.hypothesis.Hypothesis

This is the base class for all forms of probabilistic ASR hypotheses representations.

class alex.components.asr.utterance.AbstractedUtterance(surface)[source]

Bases: alex.components.asr.utterance.Utterance, alex.ml.features.Abstracted

classmethod from_utterance(utterance)[source]

Constructs a new AbstractedUtterance from an existing Utterance.

iter_triples()[source]
iter_typeval()[source]
join_typeval(type_, val)[source]
classmethod make_other(type_)[source]
other_val = (u'[OTHER]',)
phrase2category_label(phrase, catlab)[source]

Replaces the phrase given by `phrase’ by a new phrase, given by `catlab’. Assumes `catlab’ is an abstraction for `phrase’.

replace(orig, replacement)[source]

Replaces the phrase given by `orig’ by a new phrase, given by `replacement’.

replace_typeval(orig, replacement)

Replaces the phrase given by `orig’ by a new phrase, given by `replacement’.

class alex.components.asr.utterance.Utterance(surface)[source]

Bases: object

find(phrase)[source]

Returns the word index of the start of first occurrence of `phrase’ within this utterance. If none is found, returns -1.

Arguments:
phrase – a list of words constituting the phrase sought
index(phrase)[source]

Returns the word index of the start of first occurrence of `phrase’ within this utterance. If none is found, ValueError is raised.

Arguments:
phrase – a list of words constituting the phrase sought
insert(idx, val)[source]
isempty()[source]
iter_ngrams(n, with_boundaries=False)[source]
iter_with_boundaries()[source]

Iterates the sequence [SENTENCE_START, word1, ..., wordlast, SENTENCE_END].

lower()[source]

Lowercases words of this utterance.

BEWARE, this method is destructive, it lowercases self.

replace(orig, replacement, return_startidx=False)[source]

Analogous to the `str.replace’ method. If the original phrase is not found in this utterance, this instance is returned. If it is found, only the first match is replaced.

Arguments:

orig – the phrase to replace, as a sequence of words replacement – the replacement in the same form return_startidx – if set to True, the tuple (replaced, orig_pos)

is returned where `replaced’ is the new utterance and `orig_pos’ is the index of the word where `orig’ was found in the original utterance. If set to False (the default), only the resulting utterance is returned.
replace2(start, end, replacement)[source]

Replace the words from start to end with the replacement.

Parameters:
  • start – the start position of replaced word sequence
  • end – the end position of replaced word sequence
  • replacement – a replacement
Returns:

return a new Utterance instance with the word sequence replaced with the replacement

replace_all(orig, replacement)[source]

Replace all occurrences of the given words with the replacement. Only replaces at word boundaries.

Parameters:
  • orig – the original string to be replaced (as string or list of words)
  • replacement – the replacement (as string or list of words)
Return type:

Utterance

utterance
class alex.components.asr.utterance.UtteranceConfusionNetwork(rep=None)[source]

Bases: alex.components.asr.utterance.ASRHypothesis, alex.ml.features.Abstracted

Word confusion network

Attributes:
cn: a list of alternatives of the following signature
[word_index-> [ alternative ]]

XXX Are the alternatives always sorted wrt their probability in decreasing order?

TODO Define a lightweight class SimpleHypothesis as a tuple (probability, fact) with easy-to-read indexing. namedtuple might be the best choice.

class Index(is_long_link, word_idx, alt_idx, link_widx)

Bases: tuple

unique index into the confnet

Attributes:

is_long_link – indexing to a long link? word_idx – first index either to self.cn or self._long_links alt_idx – second index ditto link_widx – if is_long_link, this indexes the word within a phrase of

the long link
alt_idx

Alias for field number 2

Alias for field number 0

Alias for field number 3

word_idx

Alias for field number 1

Bases: object

attrs = (u'end', u'orig_probs', u'hyp', u'normalise')

Represents a long link in a word confusion network.

Attributes:

end – end index of the link (exclusive) orig_probs – list of probabilities associated with the ordinary

words this link corresponds to
hyp – a (probability, phrase) tuple, the label of this link.
`phrase’ itself is a sequence of words (list of strings).
normalise – boolean; whether this link’s probability should be
taken into account when normalising probabilities for alternatives in the confnet
UtteranceConfusionNetwork.add(hyps)[source]

Adds a new arc to the confnet with alternatives as specified.

Arguments:
  • hyps: an iterable of simple hypotheses – (probability, word)

    tuples

UtteranceConfusionNetwork.cn
UtteranceConfusionNetwork.find(phrase, start=0, end=None)[source]
UtteranceConfusionNetwork.find_unaware(phrase, start=0, end=None)[source]
UtteranceConfusionNetwork.get_best_hyp()[source]
UtteranceConfusionNetwork.get_best_utterance()[source]
UtteranceConfusionNetwork.get_hyp_index_utterance(hyp_index)[source]
UtteranceConfusionNetwork.get_next_worse_candidates(hyp_index)[source]

Returns such hypotheses that will have lower probability. It assumes that the confusion network is sorted.

UtteranceConfusionNetwork.get_phrase_idxs(phrase, start=0, end=None, start_in_midlinks=True, immediate=False)[source]

Returns indices to words constituting the given phrase within this confnet. It looks only for the first occurrence of the phrase in the interval specified.

Arguments:

phrase: the phrase to look for, specified as a list of words start: the index where to start searching end: the index after which to stop searching start_in_midlinks: whether a phrase starting in the middle of

a long link should be considered too
immediate: whether the phrase has to start immediately at the start
index (intervening empty words are allowed)
Returns:
  • an empty list in case that phrase was not found
  • a list of indices to words (UtteranceConfusionNetwork.Index) that constitute that phrase within this confnet
UtteranceConfusionNetwork.get_prob(hyp_index)[source]

Returns a probability of the given hypothesis.

UtteranceConfusionNetwork.get_utterance_nblist(n=10, prune_prob=0.005)[source]

Parses the confusion network and generates n best hypotheses.

The result is a list of utterance hypotheses each with a with assigned probability. The list also includes the utterance “_other_” for not having the correct utterance in the list.

Generation of hypotheses will stop when the probability of the hypotheses is smaller then the prune_prob.

UtteranceConfusionNetwork.index(phrase, start=0, end=None)[source]
UtteranceConfusionNetwork.isempty()[source]
UtteranceConfusionNetwork.iter_ngrams(n, with_boundaries=False, start=None)[source]

Iterates n-gram hypotheses of the length specified. This is the interface method. It is aware of multi-word phrases (“long links”) that were substituted into the confnet.

Arguments:
n: size of the n-grams with_boundaries: whether to include special sentence boundary marks start: at which word index the n-grams have to start (exactly)
UtteranceConfusionNetwork.iter_ngrams_fromto(from_=None, to=None)[source]

Iterates n-gram hypotheses between the indices `from_‘ and `to_‘. This method does not consider phrases longer than 1 that were substituted into the confnet.

UtteranceConfusionNetwork.iter_ngrams_unaware(n, with_boundaries=False)[source]

Iterates n-gram hypotheses of the length specified. This is the interface method, and uses `iter_ngrams_fromto’ internally. This method does not consider phrases longer than 1 that were substituted into the confnet.

Arguments:
n: size of the n-grams with_boundaries: whether to include special sentence boundary marks
UtteranceConfusionNetwork.iter_triples()[source]
UtteranceConfusionNetwork.iter_typeval()[source]
UtteranceConfusionNetwork.join_typeval(type_, val)[source]
UtteranceConfusionNetwork.lower()[source]

Lowercases words of this confnet.

BEWARE, this method is destructive, it lowercases self.

classmethod UtteranceConfusionNetwork.make_other(type_)[source]
UtteranceConfusionNetwork.merge()[source]

Adds up probabilities for the same hypotheses.

TODO: not implemented yet

UtteranceConfusionNetwork.normalise(end=None)[source]

Makes sure that all probabilities add up to one. There should be no need of calling this from outside, since this invariant is ensured between calls to this class’ methods.

UtteranceConfusionNetwork.other_val = (u'[OTHER]',)
UtteranceConfusionNetwork.phrase2category_label(phrase, catlab)[source]

Replaces the phrase given by `phrase’ by a new phrase, given by `catlab’. Assumes `catlab’ is an abstraction for `phrase’.

UtteranceConfusionNetwork.prune(prune_prob=0.001)[source]
UtteranceConfusionNetwork.replace(phrase, replacement)[source]
UtteranceConfusionNetwork.replace_typeval(combined, replacement)[source]
UtteranceConfusionNetwork.repr_escer = <alex.utils.text.Escaper object>
UtteranceConfusionNetwork.repr_spec_chars = u'():,;|[]"\\'
UtteranceConfusionNetwork.sort()[source]

Sort the alternatives for each word according their probability.

UtteranceConfusionNetwork.str_escer = <alex.utils.text.Escaper object>
exception alex.components.asr.utterance.UtteranceConfusionNetworkException[source]

Bases: alex.components.slu.exceptions.SLUException

class alex.components.asr.utterance.UtteranceConfusionNetworkFeatures(type=u'ngram', size=3, confnet=None)[source]

Bases: alex.ml.features.Features

Represents features extracted from an utterance hypothesis in the form of a confusion network. These are simply a probabilistic generalisation of simple utterance features. Only n-gram (incl. skip n-gram) features are currently implemented.

parse(confnet)[source]

Extracts the features from `confnet’.

exception alex.components.asr.utterance.UtteranceException[source]

Bases: alex.components.slu.exceptions.SLUException

class alex.components.asr.utterance.UtteranceFeatures(type=u'ngram', size=3, utterance=None)[source]

Bases: alex.ml.features.Features

Represents the vector of features for an utterance.

The class also provides methods for manipulation of the feature vector, including extracting features from an utterance.

Currently, only n-gram (including skip n-grams) features are implemented.

Attributes:
type: type of features (‘ngram’) size: size of features (an integer) features: mapping { feature : value of feature (# occs) }
parse(utterance, with_boundaries=True)[source]

Extracts the features from `utterance’.

class alex.components.asr.utterance.UtteranceHyp(prob=None, utterance=None)[source]

Bases: alex.components.asr.utterance.ASRHypothesis

Provide an interface for 1-best hypothesis from the ASR component.

get_best_utterance()[source]
class alex.components.asr.utterance.UtteranceNBList(rep=None)[source]

Bases: alex.components.asr.utterance.ASRHypothesis, alex.ml.hypothesis.NBList

Provides functionality of n-best lists for utterances.

When updating the n-best list, one should do the following.

  1. add utterances or parse a confusion network
  2. merge and normalise, in either order
Attributes:
n_best: the list containing pairs [prob, utterance] sorted from the
most probable to the least probable ones
add_other()[source]
deserialise(rep)[source]
get_best()[source]
get_best_utterance()[source]

Returns the most probable utterance.

DEPRECATED. Use get_best instead.

normalise()[source]

The N-best list is extended to include the “_other_” utterance to represent those utterance hypotheses which are not included in the N-best list.

DEPRECATED. Use add_other instead.

scale()[source]

Scales the n-best list to sum to one.

serialise()[source]
sort()[source]

DEPRECATED, going to be removed.

exception alex.components.asr.utterance.UtteranceNBListException[source]

Bases: alex.components.slu.exceptions.SLUException

class alex.components.asr.utterance.UtteranceNBListFeatures(type=u'ngram', size=3, utt_nblist=None)[source]

Bases: alex.ml.features.Features

parse(utt_nblist)[source]

This should be called only once during the object’s lifetime, preferably from within the initialiser.

alex.components.asr.utterance.load_utt_confnets(fname, limit=None, encoding=u'UTF-8')[source]

Loads a dictionary of utterance confusion networks from a given file.

The file is assumed to contain lines of the following form:

[whitespace..]<key>[whitespace..]=>[whitespace..]<utt_cn>[whitespace..]

or just (without keys):

[whitespace..]<utt_cn>[whitespace..]

where <utt_cn> is obtained as repr() of an UtteranceConfusionNetwork object.

Arguments:
fname – path towards the file to read the utterance confusion networks
from

limit – limit on the number of confusion networks to read encoding – the file encoding

Returns a dictionary with confnets (instances of Utterance) as values.

alex.components.asr.utterance.load_utt_nblists(fname, limit=None, n=40, encoding=u'UTF-8')[source]

Loads a dictionary of utterance n-best lists from a file with confnets.

The n-best lists are obtained simply from the confnets.

The file is assumed to contain lines of the following form:

[whitespace..]<key>[whitespace..]=>[whitespace..]<utt_cn>[whitespace..]

or just (without keys):

[whitespace..]<utt_cn>[whitespace..]

where <utt_cn> is obtained as repr() of an UtteranceConfusionNetwork object.

Arguments:
fname – path towards the file to read the utterance confusion networks
from

limit – limit on the number of n-best lists to read n – depth of n-best lists encoding – the file encoding

Returns a dictionary with n-best lists (instances of UtteranceNBList) as values.

alex.components.asr.utterance.load_utterances(fname, limit=None, encoding=u'UTF-8')[source]

Loads a dictionary of utterances from a given file.

The file is assumed to contain lines of the following form:

[whitespace..]<key>[whitespace..]=>[whitespace..]<utterance>[whitespace..]

or just (without keys):

[whitespace..]<utterance>[whitespace..]
Arguments:
fname – path towards the file to read the utterances from limit – limit on the number of utterances to read encoding – the file encoding

Returns a dictionary with utterances (instances of Utterance) as values.

alex.components.asr.utterance.save_utterances(file_name, utt, encoding=u'UTF-8')[source]

Saves a dictionary of utterances in the wave as key format into a file.

Parameters:
  • file_name – name of the target file
  • utt – a dictionary with the utterances where the keys are the names of the corresponding wave files
Returns:

None

Module contents