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.exceptions module¶
-
exception
alex.components.asr.exceptions.
ASRException
[source]¶ Bases:
alex.AlexException
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.
-
class
alex.components.asr.test_utterance.
TestUtterance
(methodName='runTest')[source]¶ Bases:
unittest.case.TestCase
Tests correct working of the Utterance class.
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.
-
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’.
-
classmethod
-
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
-
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)
-
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
¶
-
-
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
-
is_long_link
¶ Alias for field number 0
-
link_widx
¶ Alias for field number 3
-
word_idx
¶ Alias for field number 1
-
class
UtteranceConfusionNetwork.
LongLink
(end, orig_probs, hyp, normalise=False)[source]¶ 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.
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.
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.
lower
()[source]¶ Lowercases words of this confnet.
BEWARE, this method is destructive, it lowercases self.
-
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.
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>¶
-
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.
-
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) }
-
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.
-
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.
- add utterances or parse a confusion network
- 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
-
get_best_utterance
()[source]¶ Returns the most probable utterance.
DEPRECATED. Use get_best instead.
-
class
alex.components.asr.utterance.
UtteranceNBListFeatures
(type=u'ngram', size=3, utt_nblist=None)[source]¶ Bases:
alex.ml.features.Features
-
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