diff --git a/data/README.rst b/data/README.rst
index 4d2c022aaa..3a60ea5a17 100644
--- a/data/README.rst
+++ b/data/README.rst
@@ -3,9 +3,9 @@ Language-Specific Data
This directory contains language-specific data files. Most importantly, you will find here:
-1. A list of unique characters for the target language (e.g. English) in `data/alphabet.txt`
+1. A list of unique characters for the target language (e.g. English) in ``data/alphabet.txt``. After installing the training code, you can check ``python -m deepspeech_training.util.check_characters --help`` for a tool that creates an alphabet file from a list of training CSV files.
-2. A scorer package (`data/lm/kenlm.scorer`) generated with `data/lm/generate_package.py`. The scorer package includes a binary n-gram language model generated with `data/lm/generate_lm.py`.
+2. A scorer package (``data/lm/kenlm.scorer``) generated with ``generate_scorer_package`` (``native_client/generate_scorer_package.cpp``). The scorer package includes a binary n-gram language model generated with ``data/lm/generate_lm.py``.
For more information on how to build these resources from scratch, see the ``External scorer scripts`` section on `deepspeech.readthedocs.io `_.
diff --git a/data/lm/generate_package.py b/data/lm/generate_package.py
deleted file mode 100644
index 30a33fcc7e..0000000000
--- a/data/lm/generate_package.py
+++ /dev/null
@@ -1,157 +0,0 @@
-#!/usr/bin/env python
-from __future__ import absolute_import, division, print_function
-
-import argparse
-import shutil
-import sys
-
-import ds_ctcdecoder
-from deepspeech_training.util.text import Alphabet, UTF8Alphabet
-from ds_ctcdecoder import Scorer, Alphabet as NativeAlphabet
-
-
-def create_bundle(
- alphabet_path,
- lm_path,
- vocab_path,
- package_path,
- force_utf8,
- default_alpha,
- default_beta,
-):
- words = set()
- vocab_looks_char_based = True
- with open(vocab_path) as fin:
- for line in fin:
- for word in line.split():
- words.add(word.encode("utf-8"))
- if len(word) > 1:
- vocab_looks_char_based = False
- print("{} unique words read from vocabulary file.".format(len(words)))
-
- cbm = "Looks" if vocab_looks_char_based else "Doesn't look"
- print("{} like a character based model.".format(cbm))
-
- if force_utf8 != None: # pylint: disable=singleton-comparison
- use_utf8 = force_utf8.value
- else:
- use_utf8 = vocab_looks_char_based
- print("Using detected UTF-8 mode: {}".format(use_utf8))
-
- if use_utf8:
- serialized_alphabet = UTF8Alphabet().serialize()
- else:
- if not alphabet_path:
- raise RuntimeError("No --alphabet path specified, can't continue.")
- serialized_alphabet = Alphabet(alphabet_path).serialize()
-
- alphabet = NativeAlphabet()
- err = alphabet.deserialize(serialized_alphabet, len(serialized_alphabet))
- if err != 0:
- raise RuntimeError("Error loading alphabet: {}".format(err))
-
- scorer = Scorer()
- scorer.set_alphabet(alphabet)
- scorer.set_utf8_mode(use_utf8)
- scorer.reset_params(default_alpha, default_beta)
- err = scorer.load_lm(lm_path)
- if err != ds_ctcdecoder.DS_ERR_SCORER_NO_TRIE:
- print('Error loading language model file: 0x{:X}.'.format(err))
- print('See the error codes section in https://deepspeech.readthedocs.io for a description.')
- sys.exit(1)
- scorer.fill_dictionary(list(words))
- shutil.copy(lm_path, package_path)
- # append, not overwrite
- if scorer.save_dictionary(package_path, True):
- print("Package created in {}".format(package_path))
- else:
- print("Error when creating {}".format(package_path))
- sys.exit(1)
-
-
-class Tristate(object):
- def __init__(self, value=None):
- if any(value is v for v in (True, False, None)):
- self.value = value
- else:
- raise ValueError("Tristate value must be True, False, or None")
-
- def __eq__(self, other):
- return (
- self.value is other.value
- if isinstance(other, Tristate)
- else self.value is other
- )
-
- def __ne__(self, other):
- return not self == other
-
- def __bool__(self):
- raise TypeError("Tristate object may not be used as a Boolean")
-
- def __str__(self):
- return str(self.value)
-
- def __repr__(self):
- return "Tristate(%s)" % self.value
-
-
-def main():
- parser = argparse.ArgumentParser(
- description="Generate an external scorer package for DeepSpeech."
- )
- parser.add_argument(
- "--alphabet",
- help="Path of alphabet file to use for vocabulary construction. Words with characters not in the alphabet will not be included in the vocabulary. Optional if using UTF-8 mode.",
- )
- parser.add_argument(
- "--lm",
- required=True,
- help="Path of KenLM binary LM file. Must be built without including the vocabulary (use the -v flag). See generate_lm.py for how to create a binary LM.",
- )
- parser.add_argument(
- "--vocab",
- required=True,
- help="Path of vocabulary file. Must contain words separated by whitespace.",
- )
- parser.add_argument("--package", required=True, help="Path to save scorer package.")
- parser.add_argument(
- "--default_alpha",
- type=float,
- required=True,
- help="Default value of alpha hyperparameter.",
- )
- parser.add_argument(
- "--default_beta",
- type=float,
- required=True,
- help="Default value of beta hyperparameter.",
- )
- parser.add_argument(
- "--force_utf8",
- type=str,
- default="",
- help="Boolean flag, force set or unset UTF-8 mode in the scorer package. If not set, infers from the vocabulary. See for further explanation",
- )
- args = parser.parse_args()
-
- if args.force_utf8 in ("True", "1", "true", "yes", "y"):
- force_utf8 = Tristate(True)
- elif args.force_utf8 in ("False", "0", "false", "no", "n"):
- force_utf8 = Tristate(False)
- else:
- force_utf8 = Tristate(None)
-
- create_bundle(
- args.alphabet,
- args.lm,
- args.vocab,
- args.package,
- force_utf8,
- args.default_alpha,
- args.default_beta,
- )
-
-
-if __name__ == "__main__":
- main()
diff --git a/doc/Decoder.rst b/doc/Decoder.rst
index 63e3ac2da6..1115e38e61 100644
--- a/doc/Decoder.rst
+++ b/doc/Decoder.rst
@@ -56,9 +56,11 @@ At decoding time, the scorer is queried every time a Unicode codepoint is predic
**Acoustic models trained with ``--utf8`` MUST NOT be used with an alphabet based scorer. Conversely, acoustic models trained with an alphabet file MUST NOT be used with a UTF-8 scorer.**
-UTF-8 scorers can be built by using an input corpus with space separated codepoints. If your corpus only contains single codepoints separated by spaces, ``data/lm/generate_package.py`` should automatically enable UTF-8 mode, and it should print the message "Looks like a character based model."
+UTF-8 scorers can be built by using an input corpus with space separated codepoints. If your corpus only contains single codepoints separated by spaces, ``generate_scorer_package`` should automatically enable UTF-8 mode, and it should print the message "Looks like a character based model."
-If the message "Doesn't look like a character based model." is printed, you should double check your inputs to make sure it only contains single codepoints separated by spaces. UTF-8 mode can be forced by specifying the ``--force_utf8`` flag when running ``data/lm/generate_package.py``, but it is NOT RECOMMENDED.
+If the message "Doesn't look like a character based model." is printed, you should double check your inputs to make sure it only contains single codepoints separated by spaces. UTF-8 mode can be forced by specifying the ``--force_utf8`` flag when running ``generate_scorer_package``, but it is NOT RECOMMENDED.
+
+See :ref:`scorer-scripts` for more details on using ``generate_scorer_package``.
Because KenLM uses spaces as a word separator, the resulting language model will not include space characters in it. If you wish to use UTF-8 mode but still model spaces, you need to replace spaces in the input corpus with a different character **before** converting it to space separated codepoints. For example:
diff --git a/doc/Scorer.rst b/doc/Scorer.rst
index 8df94a74ce..04ce2d686b 100644
--- a/doc/Scorer.rst
+++ b/doc/Scorer.rst
@@ -5,7 +5,9 @@ External scorer scripts
DeepSpeech pre-trained models include an external scorer. This document explains how to reproduce our external scorer, as well as adapt the scripts to create your own.
-The scorer is composed of two sub-components, a KenLM language model and a trie data structure containing all words in the vocabulary. In order to create the scorer package, first we must create a KenLM language model (using ``data/lm/generate_lm.py``, and then use ``data/lm/generate_package.py`` to create the final package file including the trie data structure.
+The scorer is composed of two sub-components, a KenLM language model and a trie data structure containing all words in the vocabulary. In order to create the scorer package, first we must create a KenLM language model (using ``data/lm/generate_lm.py``, and then use ``generate_scorer_package`` to create the final package file including the trie data structure.
+
+The ``generate_scorer_package`` binary is part of the native client package that is included with official releases. You can find the appropriate archive for your platform in the `GitHub release downloads `_. The native client package is named ``native_client.{arch}.{config}.{plat}.tar.xz``, where ``{arch}`` is the architecture the binary was built for, for example ``amd64`` or ``arm64``, ``config`` is the build configuration, which for building decoder packages does not matter, and ``{plat}`` is the platform the binary was built-for, for example ``linux`` or ``osx``. If you wanted to run the ``generate_scorer_package`` binary on a Linux desktop, you would download ``native_client.amd64.cpu.linux.tar.xz``.
Reproducing our external scorer
-------------------------------
@@ -36,12 +38,15 @@ Else you have to build `KenLM `_ first and then pa
--binary_a_bits 255 --binary_q_bits 8 --binary_type trie
-Afterwards you can use ``generate_package.py`` to generate the scorer package using the ``lm.binary`` and ``vocab-500000.txt`` files:
+Afterwards you can use ``generate_scorer_package`` to generate the scorer package using the ``lm.binary`` and ``vocab-500000.txt`` files:
.. code-block:: bash
cd data/lm
- python3 generate_package.py --alphabet ../alphabet.txt --lm lm.binary --vocab vocab-500000.txt \
+ # Download and extract appropriate native_client package:
+ curl -LO http://github.com/mozilla/DeepSpeech/releases/...
+ tar xvf native_client.*.tar.xz
+ ./generate_scorer_package --alphabet ../alphabet.txt --lm lm.binary --vocab vocab-500000.txt \
--package kenlm.scorer --default_alpha 0.931289039105002 --default_beta 1.1834137581510284
Building your own scorer
@@ -51,7 +56,6 @@ Building your own scorer can be useful if you're using models in a narrow usage
The LibriSpeech LM training text used by our scorer is around 4GB uncompressed, which should give an idea of the size of a corpus needed for a reasonable language model for general speech recognition. For more constrained use cases with smaller vocabularies, you don't need as much data, but you should still try to gather as much as you can.
-With a text corpus in hand, you can then re-use the ``generate_lm.py`` and ``generate_package.py`` scripts to create your own scorer that is compatible with DeepSpeech clients and language bindings. Before building the language model, you must first familiarize yourself with the `KenLM toolkit `_. Most of the options exposed by the ``generate_lm.py`` script are simply forwarded to KenLM options of the same name, so you must read the KenLM documentation in order to fully understand their behavior.
+With a text corpus in hand, you can then re-use ``generate_lm.py`` and ``generate_scorer_package`` to create your own scorer that is compatible with DeepSpeech clients and language bindings. Before building the language model, you must first familiarize yourself with the `KenLM toolkit `_. Most of the options exposed by the ``generate_lm.py`` script are simply forwarded to KenLM options of the same name, so you must read the KenLM documentation in order to fully understand their behavior.
-After using ``generate_lm.py`` to create a KenLM language model binary file, you can use ``generate_package.py`` to create a scorer package as described in the previous section. Note that we have a :github:`lm_optimizer.py script ` which can be used to find good default values for alpha and beta. To use it, you must first
-generate a package with any value set for default alpha and beta flags. For this step, it doesn't matter what values you use, as they'll be overridden by ``lm_optimizer.py``. Then, use ``lm_optimizer.py`` with this scorer file to find good alpha and beta values. Finally, use ``generate_package.py`` again, this time with the new values.
+After using ``generate_lm.py`` to create a KenLM language model binary file, you can use ``generate_scorer_package`` to create a scorer package as described in the previous section. Note that we have a :github:`lm_optimizer.py script ` which can be used to find good default values for alpha and beta. To use it, you must first generate a package with any value set for default alpha and beta flags. For this step, it doesn't matter what values you use, as they'll be overridden by ``lm_optimizer.py`` later. Then, use ``lm_optimizer.py`` with this scorer file to find good alpha and beta values. Finally, use ``generate_scorer_package`` again, this time with the new values.
diff --git a/native_client/BUILD b/native_client/BUILD
index 53711dc2a6..232d99c776 100644
--- a/native_client/BUILD
+++ b/native_client/BUILD
@@ -2,6 +2,7 @@
load("@org_tensorflow//tensorflow:tensorflow.bzl", "tf_cc_shared_object")
load("@local_config_cuda//cuda:build_defs.bzl", "if_cuda")
+load("@com_github_nelhage_rules_boost//:boost/boost.bzl", "boost_deps")
load(
"@org_tensorflow//tensorflow/lite:build_def.bzl",
@@ -74,16 +75,24 @@ cc_library(
"ctcdecode/scorer.cpp",
"ctcdecode/path_trie.cpp",
"ctcdecode/path_trie.h",
+ "alphabet.cc",
] + OPENFST_SOURCES_PLATFORM,
hdrs = [
"ctcdecode/ctc_beam_search_decoder.h",
"ctcdecode/scorer.h",
+ "ctcdecode/decoder_utils.h",
+ "alphabet.h",
],
includes = [
".",
"ctcdecode/third_party/ThreadPool",
] + OPENFST_INCLUDES_PLATFORM,
- deps = [":kenlm"]
+ deps = [":kenlm"],
+ linkopts = [
+ "-lm",
+ "-ldl",
+ "-pthread",
+ ],
)
tf_cc_shared_object(
@@ -91,11 +100,11 @@ tf_cc_shared_object(
srcs = [
"deepspeech.cc",
"deepspeech.h",
- "alphabet.h",
- "modelstate.h",
+ "deepspeech_errors.cc",
"modelstate.cc",
- "workspace_status.h",
+ "modelstate.h",
"workspace_status.cc",
+ "workspace_status.h",
] + select({
"//native_client:tflite": [
"tflitemodelstate.h",
@@ -185,6 +194,27 @@ genrule(
cmd = "dsymutil $(location :libdeepspeech.so) -o $@"
)
+cc_binary(
+ name = "generate_scorer_package",
+ srcs = [
+ "generate_scorer_package.cpp",
+ "deepspeech_errors.cc",
+ ],
+ copts = ["-std=c++11"],
+ deps = [
+ ":decoder",
+ "@com_google_absl//absl/flags:flag",
+ "@com_google_absl//absl/flags:parse",
+ "@com_google_absl//absl/types:optional",
+ "@boost//:program_options",
+ ],
+ linkopts = [
+ "-lm",
+ "-ldl",
+ "-pthread",
+ ],
+)
+
cc_binary(
name = "enumerate_kenlm_vocabulary",
srcs = [
@@ -201,10 +231,5 @@ cc_binary(
"trie_load.cc",
],
copts = ["-std=c++11"],
- linkopts = [
- "-lm",
- "-ldl",
- "-pthread",
- ],
deps = [":decoder"],
)
diff --git a/native_client/alphabet.cc b/native_client/alphabet.cc
new file mode 100644
index 0000000000..1f0a8dbea2
--- /dev/null
+++ b/native_client/alphabet.cc
@@ -0,0 +1,189 @@
+#include "alphabet.h"
+#include "ctcdecode/decoder_utils.h"
+
+#include
+
+// std::getline, but handle newline conventions from multiple platforms instead
+// of just the platform this code was built for
+std::istream&
+getline_crossplatform(std::istream& is, std::string& t)
+{
+ t.clear();
+
+ // The characters in the stream are read one-by-one using a std::streambuf.
+ // That is faster than reading them one-by-one using the std::istream.
+ // Code that uses streambuf this way must be guarded by a sentry object.
+ // The sentry object performs various tasks,
+ // such as thread synchronization and updating the stream state.
+ std::istream::sentry se(is, true);
+ std::streambuf* sb = is.rdbuf();
+
+ while (true) {
+ int c = sb->sbumpc();
+ switch (c) {
+ case '\n':
+ return is;
+ case '\r':
+ if(sb->sgetc() == '\n')
+ sb->sbumpc();
+ return is;
+ case std::streambuf::traits_type::eof():
+ // Also handle the case when the last line has no line ending
+ if(t.empty())
+ is.setstate(std::ios::eofbit);
+ return is;
+ default:
+ t += (char)c;
+ }
+ }
+}
+
+int
+Alphabet::init(const char *config_file)
+{
+ std::ifstream in(config_file, std::ios::in);
+ if (!in) {
+ return 1;
+ }
+ unsigned int label = 0;
+ space_label_ = -2;
+ for (std::string line; getline_crossplatform(in, line);) {
+ if (line.size() == 2 && line[0] == '\\' && line[1] == '#') {
+ line = '#';
+ } else if (line[0] == '#') {
+ continue;
+ }
+ //TODO: we should probably do something more i18n-aware here
+ if (line == " ") {
+ space_label_ = label;
+ }
+ label_to_str_[label] = line;
+ str_to_label_[line] = label;
+ ++label;
+ }
+ size_ = label;
+ in.close();
+ return 0;
+}
+
+std::string
+Alphabet::Serialize()
+{
+ // Serialization format is a sequence of (key, value) pairs, where key is
+ // a uint16_t and value is a uint16_t length followed by `length` UTF-8
+ // encoded bytes with the label.
+ std::stringstream out;
+
+ // We start by writing the number of pairs in the buffer as uint16_t.
+ uint16_t size = size_;
+ out.write(reinterpret_cast(&size), sizeof(size));
+
+ for (auto it = label_to_str_.begin(); it != label_to_str_.end(); ++it) {
+ uint16_t key = it->first;
+ string str = it->second;
+ uint16_t len = str.length();
+ // Then we write the key as uint16_t, followed by the length of the value
+ // as uint16_t, followed by `length` bytes (the value itself).
+ out.write(reinterpret_cast(&key), sizeof(key));
+ out.write(reinterpret_cast(&len), sizeof(len));
+ out.write(str.data(), len);
+ }
+
+ return out.str();
+}
+
+int
+Alphabet::Deserialize(const char* buffer, const int buffer_size)
+{
+ // See util/text.py for an explanation of the serialization format.
+ int offset = 0;
+ if (buffer_size - offset < sizeof(uint16_t)) {
+ return 1;
+ }
+ uint16_t size = *(uint16_t*)(buffer + offset);
+ offset += sizeof(uint16_t);
+ size_ = size;
+
+ for (int i = 0; i < size; ++i) {
+ if (buffer_size - offset < sizeof(uint16_t)) {
+ return 1;
+ }
+ uint16_t label = *(uint16_t*)(buffer + offset);
+ offset += sizeof(uint16_t);
+
+ if (buffer_size - offset < sizeof(uint16_t)) {
+ return 1;
+ }
+ uint16_t val_len = *(uint16_t*)(buffer + offset);
+ offset += sizeof(uint16_t);
+
+ if (buffer_size - offset < val_len) {
+ return 1;
+ }
+ std::string val(buffer+offset, val_len);
+ offset += val_len;
+
+ label_to_str_[label] = val;
+ str_to_label_[val] = label;
+
+ if (val == " ") {
+ space_label_ = label;
+ }
+ }
+
+ return 0;
+}
+
+std::string
+Alphabet::DecodeSingle(unsigned int label) const
+{
+ auto it = label_to_str_.find(label);
+ if (it != label_to_str_.end()) {
+ return it->second;
+ } else {
+ std::cerr << "Invalid label " << label << std::endl;
+ abort();
+ }
+}
+
+unsigned int
+Alphabet::EncodeSingle(const std::string& string) const
+{
+ auto it = str_to_label_.find(string);
+ if (it != str_to_label_.end()) {
+ return it->second;
+ } else {
+ std::cerr << "Invalid string " << string << std::endl;
+ abort();
+ }
+}
+
+std::string
+Alphabet::Decode(const std::vector& input) const
+{
+ std::string word;
+ for (auto ind : input) {
+ word += DecodeSingle(ind);
+ }
+ return word;
+}
+
+std::string
+Alphabet::Decode(const unsigned int* input, int length) const
+{
+ std::string word;
+ for (int i = 0; i < length; ++i) {
+ word += DecodeSingle(input[i]);
+ }
+ return word;
+}
+
+std::vector
+Alphabet::Encode(const std::string& input) const
+{
+ std::vector result;
+ for (auto cp : split_into_codepoints(input)) {
+ result.push_back(EncodeSingle(cp));
+ }
+ return result;
+}
diff --git a/native_client/alphabet.h b/native_client/alphabet.h
index ace905ccde..45fc444e5c 100644
--- a/native_client/alphabet.h
+++ b/native_client/alphabet.h
@@ -1,9 +1,6 @@
#ifndef ALPHABET_H
#define ALPHABET_H
-#include
-#include
-#include
#include
#include
#include
@@ -18,92 +15,15 @@ class Alphabet {
Alphabet() = default;
Alphabet(const Alphabet&) = default;
Alphabet& operator=(const Alphabet&) = default;
+ virtual ~Alphabet() = default;
- int init(const char *config_file) {
- std::ifstream in(config_file, std::ios::in);
- if (!in) {
- return 1;
- }
- unsigned int label = 0;
- space_label_ = -2;
- for (std::string line; std::getline(in, line);) {
- if (line.size() == 2 && line[0] == '\\' && line[1] == '#') {
- line = '#';
- } else if (line[0] == '#') {
- continue;
- }
- //TODO: we should probably do something more i18n-aware here
- if (line == " ") {
- space_label_ = label;
- }
- label_to_str_[label] = line;
- str_to_label_[line] = label;
- ++label;
- }
- size_ = label;
- in.close();
- return 0;
- }
+ virtual int init(const char *config_file);
- int deserialize(const char* buffer, const int buffer_size) {
- // See util/text.py for an explanation of the serialization format.
- int offset = 0;
- if (buffer_size - offset < sizeof(uint16_t)) {
- return 1;
- }
- uint16_t size = *(uint16_t*)(buffer + offset);
- offset += sizeof(uint16_t);
- size_ = size;
-
- for (int i = 0; i < size; ++i) {
- if (buffer_size - offset < sizeof(uint16_t)) {
- return 1;
- }
- uint16_t label = *(uint16_t*)(buffer + offset);
- offset += sizeof(uint16_t);
-
- if (buffer_size - offset < sizeof(uint16_t)) {
- return 1;
- }
- uint16_t val_len = *(uint16_t*)(buffer + offset);
- offset += sizeof(uint16_t);
-
- if (buffer_size - offset < val_len) {
- return 1;
- }
- std::string val(buffer+offset, val_len);
- offset += val_len;
-
- label_to_str_[label] = val;
- str_to_label_[val] = label;
-
- if (val == " ") {
- space_label_ = label;
- }
- }
+ // Serialize alphabet into a binary buffer.
+ std::string Serialize();
- return 0;
- }
-
- const std::string& StringFromLabel(unsigned int label) const {
- auto it = label_to_str_.find(label);
- if (it != label_to_str_.end()) {
- return it->second;
- } else {
- std::cerr << "Invalid label " << label << std::endl;
- abort();
- }
- }
-
- unsigned int LabelFromString(const std::string& string) const {
- auto it = str_to_label_.find(string);
- if (it != str_to_label_.end()) {
- return it->second;
- } else {
- std::cerr << "Invalid string " << string << std::endl;
- abort();
- }
- }
+ // Deserialize alphabet from a binary buffer.
+ int Deserialize(const char* buffer, const int buffer_size);
size_t GetSize() const {
return size_;
@@ -117,20 +37,47 @@ class Alphabet {
return space_label_;
}
- template
- std::string LabelsToString(const std::vector& input) const {
- std::string word;
- for (auto ind : input) {
- word += StringFromLabel(ind);
- }
- return word;
- }
+ // Decode a single label into a string.
+ std::string DecodeSingle(unsigned int label) const;
+
+ // Encode a single character/output class into a label.
+ unsigned int EncodeSingle(const std::string& string) const;
+
+ // Decode a sequence of labels into a string.
+ std::string Decode(const std::vector& input) const;
+
+ // We provide a C-style overload for accepting NumPy arrays as input, since
+ // the NumPy library does not have built-in typemaps for std::vector.
+ std::string Decode(const unsigned int* input, int length) const;
-private:
+ // Encode a sequence of character/output classes into a sequence of labels.
+ // Characters are assumed to always take a single Unicode codepoint.
+ std::vector Encode(const std::string& input) const;
+
+protected:
size_t size_;
unsigned int space_label_;
std::unordered_map label_to_str_;
std::unordered_map str_to_label_;
};
+class UTF8Alphabet : public Alphabet
+{
+public:
+ UTF8Alphabet() {
+ size_ = 255;
+ space_label_ = ' ' - 1;
+ for (size_t i = 0; i < size_; ++i) {
+ std::string val(1, i+1);
+ label_to_str_[i] = val;
+ str_to_label_[val] = i;
+ }
+ }
+
+ int init(const char*) override {
+ return 0;
+ }
+};
+
+
#endif //ALPHABET_H
diff --git a/native_client/ctcdecode/__init__.py b/native_client/ctcdecode/__init__.py
index 7e3766bebf..ee5645d408 100644
--- a/native_client/ctcdecode/__init__.py
+++ b/native_client/ctcdecode/__init__.py
@@ -1,7 +1,7 @@
from __future__ import absolute_import, division, print_function
from . import swigwrapper # pylint: disable=import-self
-from .swigwrapper import Alphabet
+from .swigwrapper import UTF8Alphabet
__version__ = swigwrapper.__version__
@@ -30,24 +30,25 @@ def __init__(self, alpha=None, beta=None, scorer_path=None, alphabet=None):
assert beta is not None, 'beta parameter is required'
assert scorer_path, 'scorer_path parameter is required'
- serialized = alphabet.serialize()
- native_alphabet = swigwrapper.Alphabet()
- err = native_alphabet.deserialize(serialized, len(serialized))
+ err = self.init(scorer_path, alphabet)
if err != 0:
- raise ValueError('Error when deserializing alphabet.')
-
- err = self.init(scorer_path.encode('utf-8'),
- native_alphabet)
- if err != 0:
- raise ValueError('Scorer initialization failed with error code {}'.format(err))
+ raise ValueError('Scorer initialization failed with error code 0x{:X}'.format(err))
self.reset_params(alpha, beta)
- def load_lm(self, lm_path):
- return super(Scorer, self).load_lm(lm_path.encode('utf-8'))
- def save_dictionary(self, save_path, *args, **kwargs):
- return super(Scorer, self).save_dictionary(save_path.encode('utf-8'), *args, **kwargs)
+class Alphabet(swigwrapper.Alphabet):
+ """Convenience wrapper for Alphabet which calls init in the constructor"""
+ def __init__(self, config_path):
+ super(Alphabet, self).__init__()
+ err = self.init(config_path)
+ if err != 0:
+ raise ValueError('Alphabet initialization failed with error code 0x{:X}'.format(err))
+
+ def Encode(self, input):
+ """Convert SWIG's UnsignedIntVec to a Python list"""
+ res = super(Alphabet, self).Encode(input)
+ return [el for el in res]
def ctc_beam_search_decoder(probs_seq,
@@ -79,15 +80,10 @@ def ctc_beam_search_decoder(probs_seq,
results, in descending order of the confidence.
:rtype: list
"""
- serialized = alphabet.serialize()
- native_alphabet = swigwrapper.Alphabet()
- err = native_alphabet.deserialize(serialized, len(serialized))
- if err != 0:
- raise ValueError("Error when deserializing alphabet.")
beam_results = swigwrapper.ctc_beam_search_decoder(
- probs_seq, native_alphabet, beam_size, cutoff_prob, cutoff_top_n,
+ probs_seq, alphabet, beam_size, cutoff_prob, cutoff_top_n,
scorer)
- beam_results = [(res.confidence, alphabet.decode(res.tokens)) for res in beam_results]
+ beam_results = [(res.confidence, alphabet.Decode(res.tokens)) for res in beam_results]
return beam_results
@@ -126,14 +122,9 @@ def ctc_beam_search_decoder_batch(probs_seq,
results, in descending order of the confidence.
:rtype: list
"""
- serialized = alphabet.serialize()
- native_alphabet = swigwrapper.Alphabet()
- err = native_alphabet.deserialize(serialized, len(serialized))
- if err != 0:
- raise ValueError("Error when deserializing alphabet.")
- batch_beam_results = swigwrapper.ctc_beam_search_decoder_batch(probs_seq, seq_lengths, native_alphabet, beam_size, num_processes, cutoff_prob, cutoff_top_n, scorer)
+ batch_beam_results = swigwrapper.ctc_beam_search_decoder_batch(probs_seq, seq_lengths, alphabet, beam_size, num_processes, cutoff_prob, cutoff_top_n, scorer)
batch_beam_results = [
- [(res.confidence, alphabet.decode(res.tokens)) for res in beam_results]
+ [(res.confidence, alphabet.Decode(res.tokens)) for res in beam_results]
for beam_results in batch_beam_results
]
return batch_beam_results
diff --git a/native_client/ctcdecode/build_archive.py b/native_client/ctcdecode/build_archive.py
index c379d6b376..8a689ac0cd 100644
--- a/native_client/ctcdecode/build_archive.py
+++ b/native_client/ctcdecode/build_archive.py
@@ -46,7 +46,8 @@
'scorer.cpp',
'path_trie.cpp',
'decoder_utils.cpp',
- 'workspace_status.cc'
+ 'workspace_status.cc',
+ '../alphabet.cc',
]
def build_archive(srcs=[], out_name='', build_dir='temp_build/temp_build', debug=False, num_parallel=1):
diff --git a/native_client/ctcdecode/decoder_utils.cpp b/native_client/ctcdecode/decoder_utils.cpp
index ed244c3a7a..bb3e1c77a1 100644
--- a/native_client/ctcdecode/decoder_utils.cpp
+++ b/native_client/ctcdecode/decoder_utils.cpp
@@ -119,7 +119,7 @@ bool prefix_compare_external(const PathTrie *x, const PathTrie *y, const std::un
}
}
-void add_word_to_fst(const std::vector &word,
+void add_word_to_fst(const std::vector &word,
fst::StdVectorFst *dictionary) {
if (dictionary->NumStates() == 0) {
fst::StdVectorFst::StateId start = dictionary->AddState();
@@ -144,7 +144,7 @@ bool add_word_to_dictionary(
fst::StdVectorFst *dictionary) {
auto characters = utf8 ? split_into_bytes(word) : split_into_codepoints(word);
- std::vector int_word;
+ std::vector int_word;
for (auto &c : characters) {
auto int_c = char_map.find(c);
diff --git a/native_client/ctcdecode/decoder_utils.h b/native_client/ctcdecode/decoder_utils.h
index 3ba1d7e60d..c51ea046ac 100644
--- a/native_client/ctcdecode/decoder_utils.h
+++ b/native_client/ctcdecode/decoder_utils.h
@@ -86,7 +86,7 @@ std::vector split_into_codepoints(const std::string &str);
std::vector split_into_bytes(const std::string &str);
// Add a word in index to the dicionary of fst
-void add_word_to_fst(const std::vector &word,
+void add_word_to_fst(const std::vector &word,
fst::StdVectorFst *dictionary);
// Return whether a byte is a code point boundary (not a continuation byte).
diff --git a/native_client/ctcdecode/output.h b/native_client/ctcdecode/output.h
index 10eb4228a6..bdfc8ee9dd 100644
--- a/native_client/ctcdecode/output.h
+++ b/native_client/ctcdecode/output.h
@@ -8,8 +8,8 @@
*/
struct Output {
double confidence;
- std::vector tokens;
- std::vector timesteps;
+ std::vector tokens;
+ std::vector timesteps;
};
#endif // OUTPUT_H_
diff --git a/native_client/ctcdecode/path_trie.cpp b/native_client/ctcdecode/path_trie.cpp
index 0c0ee98cfb..7a04f693c5 100644
--- a/native_client/ctcdecode/path_trie.cpp
+++ b/native_client/ctcdecode/path_trie.cpp
@@ -35,7 +35,7 @@ PathTrie::~PathTrie() {
}
}
-PathTrie* PathTrie::get_path_trie(int new_char, int new_timestep, float cur_log_prob_c, bool reset) {
+PathTrie* PathTrie::get_path_trie(unsigned int new_char, unsigned int new_timestep, float cur_log_prob_c, bool reset) {
auto child = children_.begin();
for (; child != children_.end(); ++child) {
if (child->first == new_char) {
@@ -102,7 +102,7 @@ PathTrie* PathTrie::get_path_trie(int new_char, int new_timestep, float cur_log_
}
}
-void PathTrie::get_path_vec(std::vector& output, std::vector& timesteps) {
+void PathTrie::get_path_vec(std::vector& output, std::vector& timesteps) {
// Recursive call: recurse back until stop condition, then append data in
// correct order as we walk back down the stack in the lines below.
if (parent != nullptr) {
@@ -114,8 +114,8 @@ void PathTrie::get_path_vec(std::vector& output, std::vector& timestep
}
}
-PathTrie* PathTrie::get_prev_grapheme(std::vector& output,
- std::vector& timesteps,
+PathTrie* PathTrie::get_prev_grapheme(std::vector& output,
+ std::vector& timesteps,
const Alphabet& alphabet)
{
PathTrie* stop = this;
@@ -124,7 +124,7 @@ PathTrie* PathTrie::get_prev_grapheme(std::vector& output,
}
// Recursive call: recurse back until stop condition, then append data in
// correct order as we walk back down the stack in the lines below.
- if (!byte_is_codepoint_boundary(alphabet.StringFromLabel(character)[0])) {
+ if (!byte_is_codepoint_boundary(alphabet.DecodeSingle(character)[0])) {
stop = parent->get_prev_grapheme(output, timesteps, alphabet);
}
output.push_back(character);
@@ -135,7 +135,7 @@ PathTrie* PathTrie::get_prev_grapheme(std::vector& output,
int PathTrie::distance_to_codepoint_boundary(unsigned char *first_byte,
const Alphabet& alphabet)
{
- if (byte_is_codepoint_boundary(alphabet.StringFromLabel(character)[0])) {
+ if (byte_is_codepoint_boundary(alphabet.DecodeSingle(character)[0])) {
*first_byte = (unsigned char)character + 1;
return 1;
}
@@ -146,8 +146,8 @@ int PathTrie::distance_to_codepoint_boundary(unsigned char *first_byte,
return 0;
}
-PathTrie* PathTrie::get_prev_word(std::vector& output,
- std::vector& timesteps,
+PathTrie* PathTrie::get_prev_word(std::vector& output,
+ std::vector& timesteps,
const Alphabet& alphabet)
{
PathTrie* stop = this;
@@ -225,7 +225,7 @@ void PathTrie::print(const Alphabet& a) {
for (PathTrie* el : chain) {
printf("%X ", (unsigned char)(el->character));
if (el->character != ROOT_) {
- tr.append(a.StringFromLabel(el->character));
+ tr.append(a.DecodeSingle(el->character));
}
}
printf("\ntimesteps:\t ");
diff --git a/native_client/ctcdecode/path_trie.h b/native_client/ctcdecode/path_trie.h
index dbd8a2337a..0a4374fc56 100644
--- a/native_client/ctcdecode/path_trie.h
+++ b/native_client/ctcdecode/path_trie.h
@@ -21,22 +21,22 @@ class PathTrie {
~PathTrie();
// get new prefix after appending new char
- PathTrie* get_path_trie(int new_char, int new_timestep, float log_prob_c, bool reset = true);
+ PathTrie* get_path_trie(unsigned int new_char, unsigned int new_timestep, float log_prob_c, bool reset = true);
// get the prefix data in correct time order from root to current node
- void get_path_vec(std::vector& output, std::vector& timesteps);
+ void get_path_vec(std::vector& output, std::vector& timesteps);
// get the prefix data in correct time order from beginning of last grapheme to current node
- PathTrie* get_prev_grapheme(std::vector& output,
- std::vector& timesteps,
+ PathTrie* get_prev_grapheme(std::vector& output,
+ std::vector& timesteps,
const Alphabet& alphabet);
// get the distance from current node to the first codepoint boundary, and the byte value at the boundary
int distance_to_codepoint_boundary(unsigned char *first_byte, const Alphabet& alphabet);
// get the prefix data in correct time order from beginning of last word to current node
- PathTrie* get_prev_word(std::vector& output,
- std::vector& timesteps,
+ PathTrie* get_prev_word(std::vector& output,
+ std::vector& timesteps,
const Alphabet& alphabet);
// update log probs
@@ -64,8 +64,8 @@ class PathTrie {
float log_prob_c;
float score;
float approx_ctc;
- int character;
- int timestep;
+ unsigned int character;
+ unsigned int timestep;
PathTrie* parent;
private:
@@ -73,7 +73,7 @@ class PathTrie {
bool exists_;
bool has_dictionary_;
- std::vector> children_;
+ std::vector> children_;
// pointer to dictionary of FST
std::shared_ptr dictionary_;
diff --git a/native_client/ctcdecode/scorer.cpp b/native_client/ctcdecode/scorer.cpp
index ebf5522763..a6616e2178 100644
--- a/native_client/ctcdecode/scorer.cpp
+++ b/native_client/ctcdecode/scorer.cpp
@@ -65,7 +65,7 @@ void Scorer::setup_char_map()
// The initial state of FST is state 0, hence the index of chars in
// the FST should start from 1 to avoid the conflict with the initial
// state, otherwise wrong decoding results would be given.
- char_map_[alphabet_.StringFromLabel(i)] = i + 1;
+ char_map_[alphabet_.DecodeSingle(i)] = i + 1;
}
}
@@ -314,11 +314,11 @@ void Scorer::reset_params(float alpha, float beta)
this->beta = beta;
}
-std::vector Scorer::split_labels_into_scored_units(const std::vector& labels)
+std::vector Scorer::split_labels_into_scored_units(const std::vector& labels)
{
if (labels.empty()) return {};
- std::string s = alphabet_.LabelsToString(labels);
+ std::string s = alphabet_.Decode(labels);
std::vector words;
if (is_utf8_mode_) {
words = split_into_codepoints(s);
@@ -339,8 +339,8 @@ std::vector Scorer::make_ngram(PathTrie* prefix)
break;
}
- std::vector prefix_vec;
- std::vector prefix_steps;
+ std::vector prefix_vec;
+ std::vector prefix_steps;
if (is_utf8_mode_) {
new_node = current_node->get_prev_grapheme(prefix_vec, prefix_steps, alphabet_);
@@ -350,14 +350,14 @@ std::vector Scorer::make_ngram(PathTrie* prefix)
current_node = new_node->parent;
// reconstruct word
- std::string word = alphabet_.LabelsToString(prefix_vec);
+ std::string word = alphabet_.Decode(prefix_vec);
ngram.push_back(word);
}
std::reverse(ngram.begin(), ngram.end());
return ngram;
}
-void Scorer::fill_dictionary(const std::vector& vocabulary)
+void Scorer::fill_dictionary(const std::unordered_set& vocabulary)
{
// ConstFst is immutable, so we need to use a MutableFst to create the trie,
// and then we convert to a ConstFst for the decoder and for storing on disk.
diff --git a/native_client/ctcdecode/scorer.h b/native_client/ctcdecode/scorer.h
index d2a1c8b3be..13c2ef1f09 100644
--- a/native_client/ctcdecode/scorer.h
+++ b/native_client/ctcdecode/scorer.h
@@ -4,6 +4,7 @@
#include
#include
#include
+#include
#include
#include "lm/virtual_interface.hh"
@@ -72,7 +73,7 @@ class Scorer {
// trransform the labels in index to the vector of words (word based lm) or
// the vector of characters (character based lm)
- std::vector split_labels_into_scored_units(const std::vector &labels);
+ std::vector split_labels_into_scored_units(const std::vector &labels);
void set_alphabet(const Alphabet& alphabet);
@@ -83,7 +84,7 @@ class Scorer {
bool is_scoring_boundary(PathTrie* prefix, size_t new_label);
// fill dictionary FST from a vocabulary
- void fill_dictionary(const std::vector &vocabulary);
+ void fill_dictionary(const std::unordered_set &vocabulary);
// load language model from given path
int load_lm(const std::string &lm_path);
diff --git a/native_client/ctcdecode/swigwrapper.i b/native_client/ctcdecode/swigwrapper.i
index ab5675be32..ffe23c3a2e 100644
--- a/native_client/ctcdecode/swigwrapper.i
+++ b/native_client/ctcdecode/swigwrapper.i
@@ -3,7 +3,6 @@
%{
#include "ctc_beam_search_decoder.h"
#define SWIG_FILE_WITH_INIT
-#define SWIG_PYTHON_STRICT_BYTE_CHAR
#include "workspace_status.h"
%}
@@ -19,6 +18,9 @@ import_array();
namespace std {
%template(StringVector) vector;
+ %template(UnsignedIntVector) vector;
+ %template(OutputVector) vector