diff --git a/Dockerfile b/Dockerfile index 8c593e1..e88eaa3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,16 +1,16 @@ -FROM python:3 as build +FROM python:3 AS build WORKDIR /build RUN apt-get -y update && apt-get -y install git g++ autoconf-archive make libtool gfortran tar gawk -RUN wget http://www.openfst.org/twiki/pub/FST/FstDownload/openfst-1.6.2.tar.gz && \ - tar -xvzf openfst-1.6.2.tar.gz && \ - cd openfst-1.6.2 && \ - ./configure --enable-static --enable-shared --enable-far --enable-ngram-fsts && \ - make -j $(nproc) && \ - make install && \ - ldconfig +RUN wget http://www.openfst.org/twiki/pub/FST/FstDownload/openfst-1.8.4.tar.gz && \ + tar -xvzf openfst-1.8.4.tar.gz && \ + cd openfst-1.8.4 && \ + ./configure --enable-static --enable-shared --enable-far --enable-ngram-fsts --enable-lookahead-fsts --with-pic && \ + make -j $(nproc) && \ + make install && \ + ldconfig RUN git clone https://github.com/mitlm/mitlm && \ cd mitlm && \ @@ -23,15 +23,16 @@ WORKDIR /build/phonetisaurus COPY . ./ -RUN pip3 install pybindgen +RUN pip3 install pybindgen setuptools RUN ./configure --enable-python && \ - make -j $(nproc) && \ - make install + make -j $(nproc) && \ + make install FROM python:3-slim -RUN apt-get -y update && apt-get -y install gfortran && apt-get -y clean && apt-get -y autoclean +RUN apt-get -y update && apt-get -y install gfortran && apt-get -y clean && apt-get -y autoclean && \ + pip3 install setuptools WORKDIR /setup diff --git a/Makefile.am b/Makefile.am index 8575ca4..3098f9e 100644 --- a/Makefile.am +++ b/Makefile.am @@ -71,22 +71,22 @@ bin_PROGRAMS = \ phonetisaurus-g2prnn \ rnnlm -AM_CPPFLAGS = -I$(top_srcdir)/src -DGIT_REVISION=\"$(GIT_REVISION)\" -std=c++0x -Wall -Wno-sign-compare -Wno-unused-local-typedefs +AM_CPPFLAGS = -I$(top_srcdir)/src/3rdparty/utfcpp -I$(top_srcdir)/src -DGIT_REVISION=\"$(GIT_REVISION)\" -std=c++17 -Wall -Wno-sign-compare -Wno-unused-local-typedefs phonetisaurus_align_SOURCES = src/bin/phonetisaurus-align.cc src/include/PhonetisaurusRex.h src/lib/util.cc src/include/util.h src/lib/LatticePruner.cc src/include/LatticePruner.h src/lib/M2MFstAligner.cc src/include/M2MFstAligner.h phonetisaurus_align_CXXFLAGS = $(OPENFST_CXXFLAGS) $(UTFCPP_CXXFLAGS) phonetisaurus_align_LDADD = $(OPENFST_LDFLAGS) phonetisaurus_arpa2wfst_SOURCES = src/bin/phonetisaurus-arpa2wfst.cc src/include/ARPA2WFST.h src/lib/util.cc src/include/util.h -phonetisaurus_arpa2wfst_CXXFLAGS = $(OPENFST_CXXFLAGS) $(UTFCPP_CXXFLAGS) +phonetisaurus_arpa2wfst_CXXFLAGS = $(OPENFST_CXXFLAGS) $(UTFCPP_CXXFLAGS) phonetisaurus_arpa2wfst_LDADD = $(OPENFST_LDFLAGS) phonetisaurus_g2pfst_SOURCES = src/bin/phonetisaurus-g2pfst.cc src/include/PhonetisaurusScript.h src/include/PhonetisaurusRex.h src/lib/util.cc src/include/util.h -phonetisaurus_g2pfst_CXXFLAGS = $(OPENFST_CXXFLAGS) $(UTFCPP_CXXFLAGS) -funroll-loops -ffast-math +phonetisaurus_g2pfst_CXXFLAGS = $(OPENFST_CXXFLAGS) $(UTFCPP_CXXFLAGS) -funroll-loops phonetisaurus_g2pfst_LDADD = $(OPENFST_LDFLAGS) phonetisaurus_g2prnn_SOURCES = src/bin/phonetisaurus-g2prnn.cc src/include/LegacyRnnLMHash.h src/include/LegacyRnnLMDecodable.h src/include/LegacyRnnLMReader.h src/include/RnnLMDecoder.h src/lib/util.cc src/include/util.h src/3rdparty/rnnlm/rnnlmlib.cpp src/3rdparty/rnnlm/rnnlmlib.h -phonetisaurus_g2prnn_CXXFLAGS = $(OPENFST_CXXFLAGS) $(UTFCPP_CXXFLAGS) -I$(top_srcdir)/src/3rdparty/rnnlm -funroll-loops -ffast-math +phonetisaurus_g2prnn_CXXFLAGS = $(OPENFST_CXXFLAGS) $(UTFCPP_CXXFLAGS) -I$(top_srcdir)/src/3rdparty/rnnlm -funroll-loops phonetisaurus_g2prnn_LDADD = $(OPENFST_LDFLAGS) if WANT_OPENMP if OPENMP diff --git a/Makefile.in b/Makefile.in index 29843ea..c162dfc 100644 --- a/Makefile.in +++ b/Makefile.in @@ -532,7 +532,7 @@ dist_bin_SCRIPTS = \ src/scripts/phonetisaurus-apply \ src/scripts/phonetisaurus-train -AM_CPPFLAGS = -I$(top_srcdir)/src -DGIT_REVISION=\"$(GIT_REVISION)\" -std=c++0x -Wall -Wno-sign-compare -Wno-unused-local-typedefs +AM_CPPFLAGS = -I$(top_srcdir)/src/3rdparty/utfcpp -I$(top_srcdir)/src -DGIT_REVISION=\"$(GIT_REVISION)\" -std=c++17 -Wall -Wno-sign-compare -Wno-unused-local-typedefs phonetisaurus_align_SOURCES = src/bin/phonetisaurus-align.cc src/include/PhonetisaurusRex.h src/lib/util.cc src/include/util.h src/lib/LatticePruner.cc src/include/LatticePruner.h src/lib/M2MFstAligner.cc src/include/M2MFstAligner.h phonetisaurus_align_CXXFLAGS = $(OPENFST_CXXFLAGS) $(UTFCPP_CXXFLAGS) phonetisaurus_align_LDADD = $(OPENFST_LDFLAGS) @@ -540,15 +540,15 @@ phonetisaurus_arpa2wfst_SOURCES = src/bin/phonetisaurus-arpa2wfst.cc src/include phonetisaurus_arpa2wfst_CXXFLAGS = $(OPENFST_CXXFLAGS) $(UTFCPP_CXXFLAGS) phonetisaurus_arpa2wfst_LDADD = $(OPENFST_LDFLAGS) phonetisaurus_g2pfst_SOURCES = src/bin/phonetisaurus-g2pfst.cc src/include/PhonetisaurusScript.h src/include/PhonetisaurusRex.h src/lib/util.cc src/include/util.h -phonetisaurus_g2pfst_CXXFLAGS = $(OPENFST_CXXFLAGS) $(UTFCPP_CXXFLAGS) -funroll-loops -ffast-math +phonetisaurus_g2pfst_CXXFLAGS = $(OPENFST_CXXFLAGS) $(UTFCPP_CXXFLAGS) -funroll-loops phonetisaurus_g2pfst_LDADD = $(OPENFST_LDFLAGS) phonetisaurus_g2prnn_SOURCES = src/bin/phonetisaurus-g2prnn.cc src/include/LegacyRnnLMHash.h src/include/LegacyRnnLMDecodable.h src/include/LegacyRnnLMReader.h src/include/RnnLMDecoder.h src/lib/util.cc src/include/util.h src/3rdparty/rnnlm/rnnlmlib.cpp src/3rdparty/rnnlm/rnnlmlib.h phonetisaurus_g2prnn_CXXFLAGS = $(OPENFST_CXXFLAGS) $(UTFCPP_CXXFLAGS) \ - -I$(top_srcdir)/src/3rdparty/rnnlm -funroll-loops -ffast-math \ + -I$(top_srcdir)/src/3rdparty/rnnlm \ $(am__append_1) phonetisaurus_g2prnn_LDADD = $(OPENFST_LDFLAGS) $(am__append_2) rnnlm_SOURCES = src/bin/rnnlm.cc src/3rdparty/rnnlm/rnnlmlib.cpp src/3rdparty/rnnlm/rnnlmlib.h -rnnlm_CXXFLAGS = $(OPENFST_CXXFLAGS) -I$(top_srcdir)/src/3rdparty/rnnlm -funroll-loops -ffast-math +rnnlm_CXXFLAGS = $(OPENFST_CXXFLAGS) -I$(top_srcdir)/src/3rdparty/rnnlm -funroll-loops rnnlm_LDADD = $(OPENFST_LDFLAGS) @HAVE_PYTHON_TRUE@@WANT_PYTHON_TRUE@CLEANFILES = python/Phonetisaurus-binding.cc @HAVE_PYTHON_DEV_TRUE@@HAVE_PYTHON_TRUE@@WANT_PYTHON_TRUE@pyexec_LTLIBRARIES = Phonetisaurus.la diff --git a/README.md b/README.md index d4a48ea..580d62b 100644 --- a/README.md +++ b/README.md @@ -1,26 +1,27 @@ ## Phonetisaurus G2P ## -[![Build Status](https://travis-ci.org/AdolfVonKleist/Phonetisaurus.svg?branch=master)](https://travis-ci.org/AdolfVonKleist/Phonetisaurus) + +### Note on this fork + +The original project hasn't been updated in a while, so this fork was created. The main motivation for +this fork is to keep it synchronized with the latest versions of OpenFST as they are being updated +in Kaldi. Another reason is to make it compile-able in modern versions of GCC. This for was created +in late 2024 and is being updated ever since. + +### Original Description This repository contains scripts suitable for training, evaluating and using grapheme-to-phoneme models for speech recognition using the OpenFst framework. The current build requires OpenFst -version 1.6.0 or later, and the examples below use version 1.7.2. +version 1.6.0 or later, and the examples below use version 1.8.4. The repository includes C++ binaries suitable for training, compiling, and evaluating G2P models. It also some simple python bindings which may be used to extract individual multigram scores, alignments, and to dump the raw lattices in .fst format for each word. -The python scripts and bindings were tested most recently with python v3.8.5. +The python scripts and bindings were tested most recently with python v3.13. -Standalone distributions related to previous INTERSPEECH papers, as well as the complete, exported -final version of the old google-code repository are available via ```git-lfs``` in a separate -repository: - * https://github.com/AdolfVonKleist/phonetisaurus-downloads +#### Scratch Build for OpenFst v1.8.4 #### -#### Contact: #### - * phonetisaurus@gmail.com - -#### Scratch Build for OpenFst v1.7.2 and Ubuntu 20.04 #### -This build was tested via AWS EC2 with a fresh Ubuntu 20.04 base, and m4.large instance. +This build was tested in Docker using the Dockerfile included with the project. ``` $ sudo apt-get update @@ -38,33 +39,29 @@ $ mkdir g2p $ cd g2p/ ``` -Next grab and install OpenFst-1.7.2: +Next grab and install OpenFst-1.8.4: ``` -$ wget http://www.openfst.org/twiki/pub/FST/FstDownload/openfst-1.7.2.tar.gz -$ tar -xvzf openfst-1.7.2.tar.gz -$ cd openfst-1.7.2 +$ wget http://www.openfst.org/twiki/pub/FST/FstDownload/openfst-1.8.4.tar.gz +$ tar -xvzf openfst-1.8.4.tar.gz +$ cd openfst-1.8.4 # Minimal configure, compatible with current defaults for Kaldi $ ./configure --enable-static --enable-shared --enable-far --enable-ngram-fsts $ make -j # Now wait a while... $ sudo make install -# Extend your LD_LIBRARY_PATH .bashrc (assumes OpenFst installed to default location): -$ echo 'export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/usr/local/lib:/usr/local/lib/fst' \ - >> ~/.bashrc -$ source ~/.bashrc $ cd .. ``` Checkout the latest Phonetisaurus from master and compile without bindings: ``` -$ git clone https://github.com/AdolfVonKleist/Phonetisaurus.git +$ git clone https://github.com/danijel3/Phonetisaurus.git $ cd Phonetisaurus # if OpenFst is installed in the default location: $ ./configure # if OpenFst is installed in a special location: $ ./configure \ - --with-openfst-includes=${OFST_PATH}/openfst-1.7.2/include \ - --with-openfst-libs=${OFST_PATH}/openfst-1.7.2/lib + --with-openfst-includes=${OFST_PATH}/openfst-1.8.4/include \ + --with-openfst-libs=${OFST_PATH}/openfst-1.8.4/lib $ make $ sudo make install $ cd .. @@ -72,15 +69,15 @@ $ cd .. Checkout the latest Phonetisaurus from master and compile with python3 bindings: ``` -$ git clone https://github.com/AdolfVonKleist/Phonetisaurus.git +$ git clone https://github.com/danijel3/Phonetisaurus.git $ cd Phonetisaurus -$ sudo pip3 install pybindgen +$ sudo pip3 install pybindgen setuptools # if OpenFst is installed in the default location: $ PYTHON=python3 ./configure --enable-python # if OpenFst is installed in a special location: $ PYTHON=python3 ./configure \ - --with-openfst-includes=${OFST_PATH}/openfst-1.7.2/include \ - --with-openfst-libs=${OFST_PATH}/openfst-1.7.2/lib \ + --with-openfst-includes=${OFST_PATH}/openfst-1.8.4/include \ + --with-openfst-libs=${OFST_PATH}/openfst-1.8.4/lib \ --enable-python $ make $ sudo make install @@ -228,22 +225,6 @@ junkify JH AH1 NG K AH0 F AY2 junkify JH AH1 NG K IH0 F AY2 ``` -Use a special location for OpenFst, parallel build with 2 cores -``` - $ ./configure --with-openfst-libs=/home/ubuntu/openfst-1.6.2/lib \ - --with-openfst-includes=/home/ubuntu/openfst-1.6.2/include - $ make -j 2 all -``` - -Use custom g++ under OSX (Note: OpenFst must also be compiled with this -custom g++ alternative [untested with v1.6.2]) -``` - $ ./configure --with-openfst-libs=/home/osx/openfst-1.6.2gcc/lib \ - --with-openfst-includes=/home/osx/openfst-1.6.2gcc/include \ - CXX=g++-4.9 - $ make -j 2 all -``` - #### Rebuild configure #### If you need to rebuild the configure script you can do so: ``` @@ -280,7 +261,7 @@ If you need to rebuild the configure script you can do so: ### Docker: ### -Docker images are hosted on: https://hub.docker.com/r/phonetisaurus/phonetisaurus +You can build a docker image using the command `docker build -t phonetisaurus .`. The images can be used in one of 3 ways: @@ -288,17 +269,10 @@ The images can be used in one of 3 ways: * as a base image for another project (using the `FROM` statement) * to copy portions of the binaries or libraries into a new image (using the `COPY --from=` statement) - most of the files are in `/usr/local/bin` and `/usr/local/lib` -To use the program directly, you need to mount the local folder with the required files (eg. models, word lists, etc) into the Docker container under the `/work` path, as this is the default workdir in the image. Then you can call the programs directly after the name of the image, for example: +To use the program directly, you need to mount a local folder with the required files (eg. models, word lists, etc) into the Docker container under the `/work` path, as this is the default workdir in the image. Then you can call the programs directly after the name of the image, for example: ``` -docker run --rm -it -v $PWD:/work phonetisaurus/phonetisaurus "phonetisaurus-apply -m model.fst -wl test.wlist" +docker run --rm -it -v $PWD:/work phonetisaurus "phonetisaurus-apply -m model.fst -wl test.wlist" ``` You can also use the `bash` program to simply enter the interactive shell and run everything from there. -### Misc: ### -cpplint command: -``` - $ ./cpplint.py --filter=-whitespace/parens,-whitespace/braces,\ - -legal/copyright,-build/namespaces,-runtime/references\ - src/include/util.h -``` diff --git a/src/3rdparty/utfcpp/utf8.h b/src/3rdparty/utfcpp/utf8.h index 4e44514..b513530 100644 --- a/src/3rdparty/utfcpp/utf8.h +++ b/src/3rdparty/utfcpp/utf8.h @@ -1,34 +1,46 @@ -// Copyright 2006 Nemanja Trifunovic - -/* -Permission is hereby granted, free of charge, to any person or organization -obtaining a copy of the software and accompanying documentation covered by -this license (the "Software") to use, reproduce, display, distribute, -execute, and transmit the Software, and to prepare derivative works of the -Software, and to permit third-parties to whom the Software is furnished to -do so, all subject to the following: - -The copyright notices in the Software and this entire statement, including -the above license grant, this restriction and the following disclaimer, -must be included in all copies of the Software, in whole or in part, and -all derivative works of the Software, unless such copies or derivative -works are solely in the form of machine-executable object code generated by -a source language processor. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT -SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE -FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, -ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -DEALINGS IN THE SOFTWARE. -*/ - - -#ifndef UTF8_FOR_CPP_2675DCD0_9480_4c0c_B92A_CC14C027B731 -#define UTF8_FOR_CPP_2675DCD0_9480_4c0c_B92A_CC14C027B731 - -#include "utf8/checked.h" -#include "utf8/unchecked.h" - -#endif // header guard +// Copyright 2006 Nemanja Trifunovic + +/* +Permission is hereby granted, free of charge, to any person or organization +obtaining a copy of the software and accompanying documentation covered by +this license (the "Software") to use, reproduce, display, distribute, +execute, and transmit the Software, and to prepare derivative works of the +Software, and to permit third-parties to whom the Software is furnished to +do so, all subject to the following: + +The copyright notices in the Software and this entire statement, including +the above license grant, this restriction and the following disclaimer, +must be included in all copies of the Software, in whole or in part, and +all derivative works of the Software, unless such copies or derivative +works are solely in the form of machine-executable object code generated by +a source language processor. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. +*/ + + +#ifndef UTF8_FOR_CPP_2675DCD0_9480_4c0c_B92A_CC14C027B731 +#define UTF8_FOR_CPP_2675DCD0_9480_4c0c_B92A_CC14C027B731 + +/* +To control the C++ language version used by the library, you can define UTF_CPP_CPLUSPLUS macro +and set it to one of the values used by the __cplusplus predefined macro. + +For instance, + #define UTF_CPP_CPLUSPLUS 199711L +will cause the UTF-8 CPP library to use only types and language features available in the C++ 98 standard. +Some library features will be disabled. + +If you leave UTF_CPP_CPLUSPLUS undefined, it will be internally assigned to __cplusplus. +*/ + +#include "utf8/checked.h" +#include "utf8/unchecked.h" + +#endif // header guard diff --git a/src/3rdparty/utfcpp/utf8/checked.h b/src/3rdparty/utfcpp/utf8/checked.h index 9cb8d2c..96ceb4d 100644 --- a/src/3rdparty/utfcpp/utf8/checked.h +++ b/src/3rdparty/utfcpp/utf8/checked.h @@ -1,4 +1,4 @@ -// Copyright 2006 Nemanja Trifunovic +// Copyright 2006-2016 Nemanja Trifunovic /* Permission is hereby granted, free of charge, to any person or organization @@ -34,65 +34,91 @@ DEALINGS IN THE SOFTWARE. namespace utf8 { // Base for the exceptions that may be thrown from the library - class exception : public std::exception { + class exception : public ::std::exception { }; // Exceptions that may be thrown from the library functions. class invalid_code_point : public exception { - uint32_t cp; + utfchar32_t cp; public: - invalid_code_point(uint32_t cp) : cp(cp) {} - virtual const char* what() const throw() { return "Invalid code point"; } - uint32_t code_point() const {return cp;} + invalid_code_point(utfchar32_t codepoint) : cp(codepoint) {} + virtual const char* what() const UTF_CPP_NOEXCEPT UTF_CPP_OVERRIDE { return "Invalid code point"; } + utfchar32_t code_point() const {return cp;} }; class invalid_utf8 : public exception { - uint8_t u8; + utfchar8_t u8; public: - invalid_utf8 (uint8_t u) : u8(u) {} - virtual const char* what() const throw() { return "Invalid UTF-8"; } - uint8_t utf8_octet() const {return u8;} + invalid_utf8 (utfchar8_t u) : u8(u) {} + invalid_utf8 (char c) : u8(static_cast(c)) {} + virtual const char* what() const UTF_CPP_NOEXCEPT UTF_CPP_OVERRIDE { return "Invalid UTF-8"; } + utfchar8_t utf8_octet() const {return u8;} }; class invalid_utf16 : public exception { - uint16_t u16; + utfchar16_t u16; public: - invalid_utf16 (uint16_t u) : u16(u) {} - virtual const char* what() const throw() { return "Invalid UTF-16"; } - uint16_t utf16_word() const {return u16;} + invalid_utf16 (utfchar16_t u) : u16(u) {} + virtual const char* what() const UTF_CPP_NOEXCEPT UTF_CPP_OVERRIDE { return "Invalid UTF-16"; } + utfchar16_t utf16_word() const {return u16;} }; class not_enough_room : public exception { public: - virtual const char* what() const throw() { return "Not enough space"; } + virtual const char* what() const UTF_CPP_NOEXCEPT UTF_CPP_OVERRIDE { return "Not enough space"; } }; /// The library API - functions intended to be called by the users + template + octet_iterator append(utfchar32_t cp, octet_iterator result) + { + if (!utf8::internal::is_code_point_valid(cp)) + throw invalid_code_point(cp); + + return internal::append(cp, result); + } + + inline void append(utfchar32_t cp, std::string& s) + { + append(cp, std::back_inserter(s)); + } + + template + word_iterator append16(utfchar32_t cp, word_iterator result) + { + if (!utf8::internal::is_code_point_valid(cp)) + throw invalid_code_point(cp); + + return internal::append16(cp, result); + } + template - output_iterator replace_invalid(octet_iterator start, octet_iterator end, output_iterator out, uint32_t replacement) + output_iterator replace_invalid(octet_iterator start, octet_iterator end, output_iterator out, utfchar32_t replacement) { while (start != end) { octet_iterator sequence_start = start; - internal::utf_error err_code = internal::validate_next(start, end); + internal::utf_error err_code = utf8::internal::validate_next(start, end); switch (err_code) { case internal::UTF8_OK : for (octet_iterator it = sequence_start; it != start; ++it) *out++ = *it; break; case internal::NOT_ENOUGH_ROOM: - throw not_enough_room(); + out = utf8::append (replacement, out); + start = end; + break; case internal::INVALID_LEAD: - append (replacement, out); + out = utf8::append (replacement, out); ++start; break; case internal::INCOMPLETE_SEQUENCE: case internal::OVERLONG_SEQUENCE: case internal::INVALID_CODE_POINT: - append (replacement, out); + out = utf8::append (replacement, out); ++start; // just one replacement mark for the sequence - while (internal::is_trail(*start) && start != end) + while (start != end && utf8::internal::is_trail(*start)) ++start; break; } @@ -103,41 +129,29 @@ namespace utf8 template inline output_iterator replace_invalid(octet_iterator start, octet_iterator end, output_iterator out) { - static const uint32_t replacement_marker = internal::mask16(0xfffd); - return replace_invalid(start, end, out, replacement_marker); + static const utfchar32_t replacement_marker = static_cast(utf8::internal::mask16(0xfffd)); + return utf8::replace_invalid(start, end, out, replacement_marker); } - template - octet_iterator append(uint32_t cp, octet_iterator result) + inline std::string replace_invalid(const std::string& s, utfchar32_t replacement) { - if (!internal::is_code_point_valid(cp)) - throw invalid_code_point(cp); + std::string result; + replace_invalid(s.begin(), s.end(), std::back_inserter(result), replacement); + return result; + } - if (cp < 0x80) // one octet - *(result++) = static_cast(cp); - else if (cp < 0x800) { // two octets - *(result++) = static_cast((cp >> 6) | 0xc0); - *(result++) = static_cast((cp & 0x3f) | 0x80); - } - else if (cp < 0x10000) { // three octets - *(result++) = static_cast((cp >> 12) | 0xe0); - *(result++) = static_cast(((cp >> 6) & 0x3f) | 0x80); - *(result++) = static_cast((cp & 0x3f) | 0x80); - } - else { // four octets - *(result++) = static_cast((cp >> 18) | 0xf0); - *(result++) = static_cast(((cp >> 12) & 0x3f) | 0x80); - *(result++) = static_cast(((cp >> 6) & 0x3f) | 0x80); - *(result++) = static_cast((cp & 0x3f) | 0x80); - } + inline std::string replace_invalid(const std::string& s) + { + std::string result; + replace_invalid(s.begin(), s.end(), std::back_inserter(result)); return result; } template - uint32_t next(octet_iterator& it, octet_iterator end) + utfchar32_t next(octet_iterator& it, octet_iterator end) { - uint32_t cp = 0; - internal::utf_error err_code = internal::validate_next(it, end, &cp); + utfchar32_t cp = 0; + internal::utf_error err_code = utf8::internal::validate_next(it, end, cp); switch (err_code) { case internal::UTF8_OK : break; @@ -146,51 +160,57 @@ namespace utf8 case internal::INVALID_LEAD : case internal::INCOMPLETE_SEQUENCE : case internal::OVERLONG_SEQUENCE : - throw invalid_utf8(*it); + throw invalid_utf8(static_cast(*it)); case internal::INVALID_CODE_POINT : throw invalid_code_point(cp); } return cp; } + template + utfchar32_t next16(word_iterator& it, word_iterator end) + { + utfchar32_t cp = 0; + internal::utf_error err_code = utf8::internal::validate_next16(it, end, cp); + if (err_code == internal::NOT_ENOUGH_ROOM) + throw not_enough_room(); + return cp; + } + template - uint32_t peek_next(octet_iterator it, octet_iterator end) + utfchar32_t peek_next(octet_iterator it, octet_iterator end) { - return next(it, end); + return utf8::next(it, end); } template - uint32_t prior(octet_iterator& it, octet_iterator start) - { + utfchar32_t prior(octet_iterator& it, octet_iterator start) + { // can't do much if it == start - if (it == start) + if (it == start) throw not_enough_room(); - octet_iterator end = it; + octet_iterator end = it; // Go back until we hit either a lead octet or start - while (internal::is_trail(*(--it))) + while (utf8::internal::is_trail(*(--it))) if (it == start) throw invalid_utf8(*it); // error - no lead byte in the sequence - return peek_next(it, end); - } - - /// Deprecated in versions that include "prior" - template - uint32_t previous(octet_iterator& it, octet_iterator pass_start) - { - octet_iterator end = it; - while (internal::is_trail(*(--it))) - if (it == pass_start) - throw invalid_utf8(*it); // error - no lead byte in the sequence - octet_iterator temp = it; - return next(temp, end); + return utf8::peek_next(it, end); } template void advance (octet_iterator& it, distance_type n, octet_iterator end) { - for (distance_type i = 0; i < n; ++i) - next(it, end); + const distance_type zero(0); + if (n < zero) { + // backward + for (distance_type i = n; i < zero; ++i) + utf8::prior(it, end); + } else { + // forward + for (distance_type i = zero; i < n; ++i) + utf8::next(it, end); + } } template @@ -199,7 +219,7 @@ namespace utf8 { typename std::iterator_traits::difference_type dist; for (dist = 0; first < last; ++dist) - next(first, last); + utf8::next(first, last); return dist; } @@ -207,25 +227,25 @@ namespace utf8 octet_iterator utf16to8 (u16bit_iterator start, u16bit_iterator end, octet_iterator result) { while (start != end) { - uint32_t cp = internal::mask16(*start++); + utfchar32_t cp = static_cast(utf8::internal::mask16(*start++)); // Take care of surrogate pairs first - if (internal::is_lead_surrogate(cp)) { + if (utf8::internal::is_lead_surrogate(cp)) { if (start != end) { - uint32_t trail_surrogate = internal::mask16(*start++); - if (internal::is_trail_surrogate(trail_surrogate)) + const utfchar32_t trail_surrogate = static_cast(utf8::internal::mask16(*start++)); + if (utf8::internal::is_trail_surrogate(trail_surrogate)) cp = (cp << 10) + trail_surrogate + internal::SURROGATE_OFFSET; else - throw invalid_utf16(static_cast(trail_surrogate)); + throw invalid_utf16(static_cast(trail_surrogate)); } else - throw invalid_utf16(static_cast(cp)); + throw invalid_utf16(static_cast(cp)); } // Lone trail surrogate - else if (internal::is_trail_surrogate(cp)) - throw invalid_utf16(static_cast(cp)); + else if (utf8::internal::is_trail_surrogate(cp)) + throw invalid_utf16(static_cast(cp)); - result = append(cp, result); + result = utf8::append(cp, result); } return result; } @@ -233,14 +253,14 @@ namespace utf8 template u16bit_iterator utf8to16 (octet_iterator start, octet_iterator end, u16bit_iterator result) { - while (start != end) { - uint32_t cp = next(start, end); + while (start < end) { + const utfchar32_t cp = utf8::next(start, end); if (cp > 0xffff) { //make a surrogate pair - *result++ = static_cast((cp >> 10) + internal::LEAD_OFFSET); - *result++ = static_cast((cp & 0x3ff) + internal::TRAIL_SURROGATE_MIN); + *result++ = static_cast((cp >> 10) + internal::LEAD_OFFSET); + *result++ = static_cast((cp & 0x3ff) + internal::TRAIL_SURROGATE_MIN); } else - *result++ = static_cast(cp); + *result++ = static_cast(cp); } return result; } @@ -249,7 +269,7 @@ namespace utf8 octet_iterator utf32to8 (u32bit_iterator start, u32bit_iterator end, octet_iterator result) { while (start != end) - result = append(*(start++), result); + result = utf8::append(*(start++), result); return result; } @@ -257,34 +277,39 @@ namespace utf8 template u32bit_iterator utf8to32 (octet_iterator start, octet_iterator end, u32bit_iterator result) { - while (start != end) - (*result++) = next(start, end); + while (start < end) + (*result++) = utf8::next(start, end); return result; } // The iterator class template - class iterator : public std::iterator { + class iterator { octet_iterator it; octet_iterator range_start; octet_iterator range_end; public: - iterator () {}; + typedef utfchar32_t value_type; + typedef utfchar32_t* pointer; + typedef utfchar32_t& reference; + typedef std::ptrdiff_t difference_type; + typedef std::bidirectional_iterator_tag iterator_category; + iterator () {} explicit iterator (const octet_iterator& octet_it, - const octet_iterator& range_start, - const octet_iterator& range_end) : - it(octet_it), range_start(range_start), range_end(range_end) + const octet_iterator& rangestart, + const octet_iterator& rangeend) : + it(octet_it), range_start(rangestart), range_end(rangeend) { if (it < range_start || it > range_end) throw std::out_of_range("Invalid utf-8 iterator position"); } // the default "big three" are OK octet_iterator base () const { return it; } - uint32_t operator * () const + utfchar32_t operator * () const { octet_iterator temp = it; - return next(temp, range_end); + return utf8::next(temp, range_end); } bool operator == (const iterator& rhs) const { @@ -298,30 +323,37 @@ namespace utf8 } iterator& operator ++ () { - next(it, range_end); + utf8::next(it, range_end); return *this; } iterator operator ++ (int) { iterator temp = *this; - next(it, range_end); + utf8::next(it, range_end); return temp; } iterator& operator -- () { - prior(it, range_start); + utf8::prior(it, range_start); return *this; } iterator operator -- (int) { iterator temp = *this; - prior(it, range_start); + utf8::prior(it, range_start); return temp; } }; // class iterator } // namespace utf8 -#endif //header guard +#if UTF_CPP_CPLUSPLUS >= 202002L // C++ 20 or later +#include "cpp20.h" +#elif UTF_CPP_CPLUSPLUS >= 201703L // C++ 17 or later +#include "cpp17.h" +#elif UTF_CPP_CPLUSPLUS >= 201103L // C++ 11 or later +#include "cpp11.h" +#endif // C++ 11 or later +#endif //header guard diff --git a/src/3rdparty/utfcpp/utf8/core.h b/src/3rdparty/utfcpp/utf8/core.h index 268cf7c..26af0dc 100755 --- a/src/3rdparty/utfcpp/utf8/core.h +++ b/src/3rdparty/utfcpp/utf8/core.h @@ -29,15 +29,47 @@ DEALINGS IN THE SOFTWARE. #define UTF8_FOR_CPP_CORE_H_2675DCD0_9480_4c0c_B92A_CC14C027B731 #include +#include +#include + +// Determine the C++ standard version. +// If the user defines UTF_CPP_CPLUSPLUS, use that. +// Otherwise, trust the unreliable predefined macro __cplusplus + +#if !defined UTF_CPP_CPLUSPLUS + #define UTF_CPP_CPLUSPLUS __cplusplus +#endif + +#if UTF_CPP_CPLUSPLUS >= 201103L // C++ 11 or later + #define UTF_CPP_OVERRIDE override + #define UTF_CPP_NOEXCEPT noexcept + #define UTF_CPP_STATIC_ASSERT(condition) static_assert(condition, "UTFCPP static assert"); +#else // C++ 98/03 + #define UTF_CPP_OVERRIDE + #define UTF_CPP_NOEXCEPT throw() + // Simulate static_assert: + template struct UtfCppCompileTimeAssert; + template<> struct UtfCppCompileTimeAssert { }; + #define UTF_CPP_STATIC_ASSERT(condition) (UtfCppCompileTimeAssert <(condition) != 0>()) +#endif // C++ 11 or later + namespace utf8 { - // The typedefs for 8-bit, 16-bit and 32-bit unsigned integers - // You may need to change them to match your system. - // These typedefs have the same names as ones from cstdint, or boost/cstdint - typedef unsigned char uint8_t; - typedef unsigned short uint16_t; - typedef unsigned int uint32_t; +// The typedefs for 8-bit, 16-bit and 32-bit code units +#if UTF_CPP_CPLUSPLUS >= 201103L // C++ 11 or later + #if UTF_CPP_CPLUSPLUS >= 202002L // C++ 20 or later + typedef char8_t utfchar8_t; + #else // C++ 11/14/17 + typedef unsigned char utfchar8_t; + #endif + typedef char16_t utfchar16_t; + typedef char32_t utfchar32_t; +#else // C++ 98/03 + typedef unsigned char utfchar8_t; + typedef unsigned short utfchar16_t; + typedef unsigned int utfchar32_t; +#endif // C++ 11 or later // Helper code - not intended to be directly called by the library users. May be changed at any time namespace internal @@ -45,61 +77,63 @@ namespace internal // Unicode constants // Leading (high) surrogates: 0xd800 - 0xdbff // Trailing (low) surrogates: 0xdc00 - 0xdfff - const uint16_t LEAD_SURROGATE_MIN = 0xd800u; - const uint16_t LEAD_SURROGATE_MAX = 0xdbffu; - const uint16_t TRAIL_SURROGATE_MIN = 0xdc00u; - const uint16_t TRAIL_SURROGATE_MAX = 0xdfffu; - const uint16_t LEAD_OFFSET = LEAD_SURROGATE_MIN - (0x10000 >> 10); - const uint32_t SURROGATE_OFFSET = 0x10000u - (LEAD_SURROGATE_MIN << 10) - TRAIL_SURROGATE_MIN; + const utfchar16_t LEAD_SURROGATE_MIN = 0xd800u; + const utfchar16_t LEAD_SURROGATE_MAX = 0xdbffu; + const utfchar16_t TRAIL_SURROGATE_MIN = 0xdc00u; + const utfchar16_t TRAIL_SURROGATE_MAX = 0xdfffu; + const utfchar16_t LEAD_OFFSET = 0xd7c0u; // LEAD_SURROGATE_MIN - (0x10000 >> 10) + const utfchar32_t SURROGATE_OFFSET = 0xfca02400u; // 0x10000u - (LEAD_SURROGATE_MIN << 10) - TRAIL_SURROGATE_MIN // Maximum valid value for a Unicode code point - const uint32_t CODE_POINT_MAX = 0x0010ffffu; + const utfchar32_t CODE_POINT_MAX = 0x0010ffffu; template - inline uint8_t mask8(octet_type oc) + inline utfchar8_t mask8(octet_type oc) { - return static_cast(0xff & oc); + return static_cast(0xff & oc); } + template - inline uint16_t mask16(u16_type oc) + inline utfchar16_t mask16(u16_type oc) { - return static_cast(0xffff & oc); + return static_cast(0xffff & oc); } + template inline bool is_trail(octet_type oc) { - return ((mask8(oc) >> 6) == 0x2); + return ((utf8::internal::mask8(oc) >> 6) == 0x2); } - template - inline bool is_lead_surrogate(u16 cp) + inline bool is_lead_surrogate(utfchar32_t cp) { - return (cp >= LEAD_SURROGATE_MIN && cp <= LEAD_SURROGATE_MAX); + return (cp >= static_cast(LEAD_SURROGATE_MIN) && cp <= static_cast(LEAD_SURROGATE_MAX)); } - template - inline bool is_trail_surrogate(u16 cp) + inline bool is_trail_surrogate(utfchar32_t cp) { - return (cp >= TRAIL_SURROGATE_MIN && cp <= TRAIL_SURROGATE_MAX); + return (cp >= static_cast(TRAIL_SURROGATE_MIN) && cp <= static_cast(TRAIL_SURROGATE_MAX)); } - template - inline bool is_surrogate(u16 cp) + inline bool is_surrogate(utfchar32_t cp) { - return (cp >= LEAD_SURROGATE_MIN && cp <= TRAIL_SURROGATE_MAX); + return (cp >= static_cast(LEAD_SURROGATE_MIN) && cp <= static_cast(TRAIL_SURROGATE_MAX)); } - template - inline bool is_code_point_valid(u32 cp) + inline bool is_code_point_valid(utfchar32_t cp) { - return (cp <= CODE_POINT_MAX && !is_surrogate(cp)); + return (cp <= CODE_POINT_MAX && !utf8::internal::is_surrogate(cp)); + } + + inline bool is_in_bmp(utfchar32_t cp) + { + return cp < utfchar32_t(0x10000); } template - inline typename std::iterator_traits::difference_type - sequence_length(octet_iterator lead_it) + int sequence_length(octet_iterator lead_it) { - uint8_t lead = mask8(*lead_it); + const utfchar8_t lead = utf8::internal::mask8(*lead_it); if (lead < 0x80) return 1; else if ((lead >> 5) == 0x6) @@ -112,187 +146,157 @@ namespace internal return 0; } - template - inline bool is_overlong_sequence(uint32_t cp, octet_difference_type length) + inline bool is_overlong_sequence(utfchar32_t cp, int length) { if (cp < 0x80) { - if (length != 1) + if (length != 1) return true; } else if (cp < 0x800) { - if (length != 2) + if (length != 2) return true; } else if (cp < 0x10000) { - if (length != 3) + if (length != 3) return true; } - return false; } enum utf_error {UTF8_OK, NOT_ENOUGH_ROOM, INVALID_LEAD, INCOMPLETE_SEQUENCE, OVERLONG_SEQUENCE, INVALID_CODE_POINT}; - /// get_sequence_x functions decode utf-8 sequences of the length x + /// Helper for get_sequence_x + template + utf_error increase_safely(octet_iterator& it, const octet_iterator end) + { + if (++it == end) + return NOT_ENOUGH_ROOM; + + if (!utf8::internal::is_trail(*it)) + return INCOMPLETE_SEQUENCE; + + return UTF8_OK; + } + + #define UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(IT, END) {utf_error ret = increase_safely(IT, END); if (ret != UTF8_OK) return ret;} + /// get_sequence_x functions decode utf-8 sequences of the length x template - utf_error get_sequence_1(octet_iterator& it, octet_iterator end, uint32_t* code_point) + utf_error get_sequence_1(octet_iterator& it, octet_iterator end, utfchar32_t& code_point) { - if (it != end) { - if (code_point) - *code_point = mask8(*it); - return UTF8_OK; - } - return NOT_ENOUGH_ROOM; + if (it == end) + return NOT_ENOUGH_ROOM; + + code_point = static_cast(utf8::internal::mask8(*it)); + + return UTF8_OK; } template - utf_error get_sequence_2(octet_iterator& it, octet_iterator end, uint32_t* code_point) + utf_error get_sequence_2(octet_iterator& it, octet_iterator end, utfchar32_t& code_point) { - utf_error ret_code = NOT_ENOUGH_ROOM; + if (it == end) + return NOT_ENOUGH_ROOM; - if (it != end) { - uint32_t cp = mask8(*it); - if (++it != end) { - if (is_trail(*it)) { - cp = ((cp << 6) & 0x7ff) + ((*it) & 0x3f); + code_point = static_cast(utf8::internal::mask8(*it)); - if (code_point) - *code_point = cp; - ret_code = UTF8_OK; - } - else - ret_code = INCOMPLETE_SEQUENCE; - } - else - ret_code = NOT_ENOUGH_ROOM; - } + UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end) - return ret_code; + code_point = ((code_point << 6) & 0x7ff) + ((*it) & 0x3f); + + return UTF8_OK; } template - utf_error get_sequence_3(octet_iterator& it, octet_iterator end, uint32_t* code_point) + utf_error get_sequence_3(octet_iterator& it, octet_iterator end, utfchar32_t& code_point) { - utf_error ret_code = NOT_ENOUGH_ROOM; - - if (it != end) { - uint32_t cp = mask8(*it); - if (++it != end) { - if (is_trail(*it)) { - cp = ((cp << 12) & 0xffff) + ((mask8(*it) << 6) & 0xfff); - if (++it != end) { - if (is_trail(*it)) { - cp += (*it) & 0x3f; - - if (code_point) - *code_point = cp; - ret_code = UTF8_OK; - } - else - ret_code = INCOMPLETE_SEQUENCE; - } - else - ret_code = NOT_ENOUGH_ROOM; - } - else - ret_code = INCOMPLETE_SEQUENCE; - } - else - ret_code = NOT_ENOUGH_ROOM; - } + if (it == end) + return NOT_ENOUGH_ROOM; + + code_point = static_cast(utf8::internal::mask8(*it)); + + UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end) + + code_point = ((code_point << 12) & 0xffff) + ((utf8::internal::mask8(*it) << 6) & 0xfff); + + UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end) - return ret_code; + code_point = static_cast(code_point + ((*it) & 0x3f)); + + return UTF8_OK; } template - utf_error get_sequence_4(octet_iterator& it, octet_iterator end, uint32_t* code_point) + utf_error get_sequence_4(octet_iterator& it, octet_iterator end, utfchar32_t& code_point) { - utf_error ret_code = NOT_ENOUGH_ROOM; - - if (it != end) { - uint32_t cp = mask8(*it); - if (++it != end) { - if (is_trail(*it)) { - cp = ((cp << 18) & 0x1fffff) + ((mask8(*it) << 12) & 0x3ffff); - if (++it != end) { - if (is_trail(*it)) { - cp += (mask8(*it) << 6) & 0xfff; - if (++it != end) { - if (is_trail(*it)) { - cp += (*it) & 0x3f; - - if (code_point) - *code_point = cp; - ret_code = UTF8_OK; - } - else - ret_code = INCOMPLETE_SEQUENCE; - } - else - ret_code = NOT_ENOUGH_ROOM; - } - else - ret_code = INCOMPLETE_SEQUENCE; - } - else - ret_code = NOT_ENOUGH_ROOM; - } - else - ret_code = INCOMPLETE_SEQUENCE; - } - else - ret_code = NOT_ENOUGH_ROOM; - } + if (it == end) + return NOT_ENOUGH_ROOM; + + code_point = static_cast(utf8::internal::mask8(*it)); - return ret_code; + UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end) + + code_point = ((code_point << 18) & 0x1fffff) + ((utf8::internal::mask8(*it) << 12) & 0x3ffff); + + UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end) + + code_point = static_cast(code_point + ((utf8::internal::mask8(*it) << 6) & 0xfff)); + + UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end) + + code_point = static_cast(code_point + ((*it) & 0x3f)); + + return UTF8_OK; } + #undef UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR + template - utf_error validate_next(octet_iterator& it, octet_iterator end, uint32_t* code_point) + utf_error validate_next(octet_iterator& it, octet_iterator end, utfchar32_t& code_point) { + if (it == end) + return NOT_ENOUGH_ROOM; + // Save the original value of it so we can go back in case of failure // Of course, it does not make much sense with i.e. stream iterators octet_iterator original_it = it; - uint32_t cp = 0; + utfchar32_t cp = 0; // Determine the sequence length based on the lead octet - typedef typename std::iterator_traits::difference_type octet_difference_type; - octet_difference_type length = sequence_length(it); - if (length == 0) - return INVALID_LEAD; + const int length = utf8::internal::sequence_length(it); - // Now that we have a valid sequence length, get trail octets and calculate the code point + // Get trail octets and calculate the code point utf_error err = UTF8_OK; switch (length) { + case 0: + return INVALID_LEAD; case 1: - err = get_sequence_1(it, end, &cp); + err = utf8::internal::get_sequence_1(it, end, cp); break; case 2: - err = get_sequence_2(it, end, &cp); + err = utf8::internal::get_sequence_2(it, end, cp); break; case 3: - err = get_sequence_3(it, end, &cp); + err = utf8::internal::get_sequence_3(it, end, cp); break; case 4: - err = get_sequence_4(it, end, &cp); + err = utf8::internal::get_sequence_4(it, end, cp); break; } if (err == UTF8_OK) { // Decoding succeeded. Now, security checks... - if (is_code_point_valid(cp)) { - if (!is_overlong_sequence(cp, length)){ + if (utf8::internal::is_code_point_valid(cp)) { + if (!utf8::internal::is_overlong_sequence(cp, length)){ // Passed! Return here. - if (code_point) - *code_point = cp; + code_point = cp; ++it; return UTF8_OK; } else err = OVERLONG_SEQUENCE; } - else + else err = INVALID_CODE_POINT; } @@ -303,7 +307,130 @@ namespace internal template inline utf_error validate_next(octet_iterator& it, octet_iterator end) { - return validate_next(it, end, 0); + utfchar32_t ignored; + return utf8::internal::validate_next(it, end, ignored); + } + + template + utf_error validate_next16(word_iterator& it, word_iterator end, utfchar32_t& code_point) + { + // Make sure the iterator dereferences a large enough type + typedef typename std::iterator_traits::value_type word_type; + UTF_CPP_STATIC_ASSERT(sizeof(word_type) >= sizeof(utfchar16_t)); + // Check the edge case: + if (it == end) + return NOT_ENOUGH_ROOM; + // Save the original value of it so we can go back in case of failure + // Of course, it does not make much sense with i.e. stream iterators + word_iterator original_it = it; + + utf_error err = UTF8_OK; + + const utfchar16_t first_word = *it++; + if (!is_surrogate(first_word)) { + code_point = first_word; + return UTF8_OK; + } + else { + if (it == end) + err = NOT_ENOUGH_ROOM; + else if (is_lead_surrogate(first_word)) { + const utfchar16_t second_word = *it++; + if (is_trail_surrogate(static_cast(second_word))) { + code_point = static_cast(first_word << 10) + static_cast(second_word) + SURROGATE_OFFSET; + return UTF8_OK; + } else + err = INCOMPLETE_SEQUENCE; + + } else { + err = INVALID_LEAD; + } + } + // error branch + it = original_it; + return err; + } + + // Internal implementation of both checked and unchecked append() function + // This function will be invoked by the overloads below, as they will know + // the octet_type. + template + octet_iterator append(utfchar32_t cp, octet_iterator result) { + if (cp < 0x80) // one octet + *(result++) = static_cast(cp); + else if (cp < 0x800) { // two octets + *(result++) = static_cast((cp >> 6) | 0xc0); + *(result++) = static_cast((cp & 0x3f) | 0x80); + } + else if (cp < 0x10000) { // three octets + *(result++) = static_cast((cp >> 12) | 0xe0); + *(result++) = static_cast(((cp >> 6) & 0x3f) | 0x80); + *(result++) = static_cast((cp & 0x3f) | 0x80); + } + else { // four octets + *(result++) = static_cast((cp >> 18) | 0xf0); + *(result++) = static_cast(((cp >> 12) & 0x3f)| 0x80); + *(result++) = static_cast(((cp >> 6) & 0x3f) | 0x80); + *(result++) = static_cast((cp & 0x3f) | 0x80); + } + return result; + } + + // One of the following overloads will be invoked from the API calls + + // A simple (but dangerous) case: the caller appends byte(s) to a char array + inline char* append(utfchar32_t cp, char* result) { + return append(cp, result); + } + + // Hopefully, most common case: the caller uses back_inserter + // i.e. append(cp, std::back_inserter(str)); + template + std::back_insert_iterator append + (utfchar32_t cp, std::back_insert_iterator result) { + return append, + typename container_type::value_type>(cp, result); + } + + // The caller uses some other kind of output operator - not covered above + // Note that in this case we are not able to determine octet_type + // so we assume it's utfchar8_t; that can cause a conversion warning if we are wrong. + template + octet_iterator append(utfchar32_t cp, octet_iterator result) { + return append(cp, result); + } + + // Internal implementation of both checked and unchecked append16() function + // This function will be invoked by the overloads below, as they will know + // the word_type. + template + word_iterator append16(utfchar32_t cp, word_iterator result) { + UTF_CPP_STATIC_ASSERT(sizeof(word_type) >= sizeof(utfchar16_t)); + if (is_in_bmp(cp)) + *(result++) = static_cast(cp); + else { + // Code points from the supplementary planes are encoded via surrogate pairs + *(result++) = static_cast(LEAD_OFFSET + (cp >> 10)); + *(result++) = static_cast(TRAIL_SURROGATE_MIN + (cp & 0x3FF)); + } + return result; + } + + // Hopefully, most common case: the caller uses back_inserter + // i.e. append16(cp, std::back_inserter(str)); + template + std::back_insert_iterator append16 + (utfchar32_t cp, std::back_insert_iterator result) { + return append16, + typename container_type::value_type>(cp, result); + } + + // The caller uses some other kind of output operator - not covered above + // Note that in this case we are not able to determine word_type + // so we assume it's utfchar16_t; that can cause a conversion warning if we are wrong. + template + word_iterator append16(utfchar32_t cp, word_iterator result) { + return append16(cp, result); } } // namespace internal @@ -311,48 +438,65 @@ namespace internal /// The library API - functions intended to be called by the users // Byte order mark - const uint8_t bom[] = {0xef, 0xbb, 0xbf}; + const utfchar8_t bom[] = {0xef, 0xbb, 0xbf}; template octet_iterator find_invalid(octet_iterator start, octet_iterator end) { octet_iterator result = start; while (result != end) { - internal::utf_error err_code = internal::validate_next(result, end); + utf8::internal::utf_error err_code = utf8::internal::validate_next(result, end); if (err_code != internal::UTF8_OK) return result; } return result; } + inline const char* find_invalid(const char* str) + { + const char* end = str + std::strlen(str); + return find_invalid(str, end); + } + + inline std::size_t find_invalid(const std::string& s) + { + std::string::const_iterator invalid = find_invalid(s.begin(), s.end()); + return (invalid == s.end()) ? std::string::npos : static_cast(invalid - s.begin()); + } + template inline bool is_valid(octet_iterator start, octet_iterator end) { - return (find_invalid(start, end) == end); + return (utf8::find_invalid(start, end) == end); } + inline bool is_valid(const char* str) + { + return (*(utf8::find_invalid(str)) == '\0'); + } + + inline bool is_valid(const std::string& s) + { + return is_valid(s.begin(), s.end()); + } + + + template inline bool starts_with_bom (octet_iterator it, octet_iterator end) { return ( - ((it != end) && (internal::mask8(*it++)) == bom[0]) && - ((it != end) && (internal::mask8(*it++)) == bom[1]) && - ((it != end) && (internal::mask8(*it)) == bom[2]) + ((it != end) && (utf8::internal::mask8(*it++)) == bom[0]) && + ((it != end) && (utf8::internal::mask8(*it++)) == bom[1]) && + ((it != end) && (utf8::internal::mask8(*it)) == bom[2]) ); } - - //Deprecated in release 2.3 - template - inline bool is_bom (octet_iterator it) + + inline bool starts_with_bom(const std::string& s) { - return ( - (internal::mask8(*it++)) == bom[0] && - (internal::mask8(*it++)) == bom[1] && - (internal::mask8(*it)) == bom[2] - ); + return starts_with_bom(s.begin(), s.end()); } } // namespace utf8 #endif // header guard - diff --git a/src/3rdparty/utfcpp/utf8/cpp11.h b/src/3rdparty/utfcpp/utf8/cpp11.h new file mode 100644 index 0000000..691633c --- /dev/null +++ b/src/3rdparty/utfcpp/utf8/cpp11.h @@ -0,0 +1,70 @@ +// Copyright 2018 Nemanja Trifunovic + +/* +Permission is hereby granted, free of charge, to any person or organization +obtaining a copy of the software and accompanying documentation covered by +this license (the "Software") to use, reproduce, display, distribute, +execute, and transmit the Software, and to prepare derivative works of the +Software, and to permit third-parties to whom the Software is furnished to +do so, all subject to the following: + +The copyright notices in the Software and this entire statement, including +the above license grant, this restriction and the following disclaimer, +must be included in all copies of the Software, in whole or in part, and +all derivative works of the Software, unless such copies or derivative +works are solely in the form of machine-executable object code generated by +a source language processor. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. +*/ + + +#ifndef UTF8_FOR_CPP_a184c22c_d012_11e8_a8d5_f2801f1b9fd1 +#define UTF8_FOR_CPP_a184c22c_d012_11e8_a8d5_f2801f1b9fd1 + +#include "checked.h" + +namespace utf8 +{ + inline void append16(utfchar32_t cp, std::u16string& s) + { + append16(cp, std::back_inserter(s)); + } + + inline std::string utf16to8(const std::u16string& s) + { + std::string result; + utf16to8(s.begin(), s.end(), std::back_inserter(result)); + return result; + } + + inline std::u16string utf8to16(const std::string& s) + { + std::u16string result; + utf8to16(s.begin(), s.end(), std::back_inserter(result)); + return result; + } + + inline std::string utf32to8(const std::u32string& s) + { + std::string result; + utf32to8(s.begin(), s.end(), std::back_inserter(result)); + return result; + } + + inline std::u32string utf8to32(const std::string& s) + { + std::u32string result; + utf8to32(s.begin(), s.end(), std::back_inserter(result)); + return result; + } +} // namespace utf8 + +#endif // header guard + diff --git a/src/3rdparty/utfcpp/utf8/cpp17.h b/src/3rdparty/utfcpp/utf8/cpp17.h new file mode 100644 index 0000000..0758730 --- /dev/null +++ b/src/3rdparty/utfcpp/utf8/cpp17.h @@ -0,0 +1,96 @@ +// Copyright 2018 Nemanja Trifunovic + +/* +Permission is hereby granted, free of charge, to any person or organization +obtaining a copy of the software and accompanying documentation covered by +this license (the "Software") to use, reproduce, display, distribute, +execute, and transmit the Software, and to prepare derivative works of the +Software, and to permit third-parties to whom the Software is furnished to +do so, all subject to the following: + +The copyright notices in the Software and this entire statement, including +the above license grant, this restriction and the following disclaimer, +must be included in all copies of the Software, in whole or in part, and +all derivative works of the Software, unless such copies or derivative +works are solely in the form of machine-executable object code generated by +a source language processor. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. +*/ + + +#ifndef UTF8_FOR_CPP_7e906c01_03a3_4daf_b420_ea7ea952b3c9 +#define UTF8_FOR_CPP_7e906c01_03a3_4daf_b420_ea7ea952b3c9 + +#include "cpp11.h" + +namespace utf8 +{ + inline std::string utf16to8(std::u16string_view s) + { + std::string result; + utf16to8(s.begin(), s.end(), std::back_inserter(result)); + return result; + } + + inline std::u16string utf8to16(std::string_view s) + { + std::u16string result; + utf8to16(s.begin(), s.end(), std::back_inserter(result)); + return result; + } + + inline std::string utf32to8(std::u32string_view s) + { + std::string result; + utf32to8(s.begin(), s.end(), std::back_inserter(result)); + return result; + } + + inline std::u32string utf8to32(std::string_view s) + { + std::u32string result; + utf8to32(s.begin(), s.end(), std::back_inserter(result)); + return result; + } + + inline std::size_t find_invalid(std::string_view s) + { + std::string_view::const_iterator invalid = find_invalid(s.begin(), s.end()); + return (invalid == s.end()) ? std::string_view::npos : static_cast(invalid - s.begin()); + } + + inline bool is_valid(std::string_view s) + { + return is_valid(s.begin(), s.end()); + } + + inline std::string replace_invalid(std::string_view s, char32_t replacement) + { + std::string result; + replace_invalid(s.begin(), s.end(), std::back_inserter(result), replacement); + return result; + } + + inline std::string replace_invalid(std::string_view s) + { + std::string result; + replace_invalid(s.begin(), s.end(), std::back_inserter(result)); + return result; + } + + inline bool starts_with_bom(std::string_view s) + { + return starts_with_bom(s.begin(), s.end()); + } + +} // namespace utf8 + +#endif // header guard + diff --git a/src/3rdparty/utfcpp/utf8/cpp20.h b/src/3rdparty/utfcpp/utf8/cpp20.h new file mode 100644 index 0000000..07b61d0 --- /dev/null +++ b/src/3rdparty/utfcpp/utf8/cpp20.h @@ -0,0 +1,124 @@ +// Copyright 2022 Nemanja Trifunovic + +/* +Permission is hereby granted, free of charge, to any person or organization +obtaining a copy of the software and accompanying documentation covered by +this license (the "Software") to use, reproduce, display, distribute, +execute, and transmit the Software, and to prepare derivative works of the +Software, and to permit third-parties to whom the Software is furnished to +do so, all subject to the following: + +The copyright notices in the Software and this entire statement, including +the above license grant, this restriction and the following disclaimer, +must be included in all copies of the Software, in whole or in part, and +all derivative works of the Software, unless such copies or derivative +works are solely in the form of machine-executable object code generated by +a source language processor. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. +*/ + + +#ifndef UTF8_FOR_CPP_207e906c01_03a3_4daf_b420_ea7ea952b3c9 +#define UTF8_FOR_CPP_207e906c01_03a3_4daf_b420_ea7ea952b3c9 + +#include "cpp17.h" + +namespace utf8 +{ + inline std::u8string utf16tou8(const std::u16string& s) + { + std::u8string result; + utf16to8(s.begin(), s.end(), std::back_inserter(result)); + return result; + } + + inline std::u8string utf16tou8(std::u16string_view s) + { + std::u8string result; + utf16to8(s.begin(), s.end(), std::back_inserter(result)); + return result; + } + + inline std::u16string utf8to16(const std::u8string& s) + { + std::u16string result; + utf8to16(s.begin(), s.end(), std::back_inserter(result)); + return result; + } + + inline std::u16string utf8to16(const std::u8string_view& s) + { + std::u16string result; + utf8to16(s.begin(), s.end(), std::back_inserter(result)); + return result; + } + + inline std::u8string utf32tou8(const std::u32string& s) + { + std::u8string result; + utf32to8(s.begin(), s.end(), std::back_inserter(result)); + return result; + } + + inline std::u8string utf32tou8(const std::u32string_view& s) + { + std::u8string result; + utf32to8(s.begin(), s.end(), std::back_inserter(result)); + return result; + } + + inline std::u32string utf8to32(const std::u8string& s) + { + std::u32string result; + utf8to32(s.begin(), s.end(), std::back_inserter(result)); + return result; + } + + inline std::u32string utf8to32(const std::u8string_view& s) + { + std::u32string result; + utf8to32(s.begin(), s.end(), std::back_inserter(result)); + return result; + } + + inline std::size_t find_invalid(const std::u8string& s) + { + std::u8string::const_iterator invalid = find_invalid(s.begin(), s.end()); + return (invalid == s.end()) ? std::string_view::npos : static_cast(invalid - s.begin()); + } + + inline bool is_valid(const std::u8string& s) + { + return is_valid(s.begin(), s.end()); + } + + inline std::u8string replace_invalid(const std::u8string& s, char32_t replacement) + { + std::u8string result; + replace_invalid(s.begin(), s.end(), std::back_inserter(result), replacement); + return result; + } + + inline std::u8string replace_invalid(const std::u8string& s) + { + std::u8string result; + replace_invalid(s.begin(), s.end(), std::back_inserter(result)); + return result; + } + + inline bool starts_with_bom(const std::u8string& s) + { + return starts_with_bom(s.begin(), s.end()); + } + +} // namespace utf8 + +#endif // header guard + diff --git a/src/3rdparty/utfcpp/utf8/unchecked.h b/src/3rdparty/utfcpp/utf8/unchecked.h index 2f3eb4d..173d030 100755 --- a/src/3rdparty/utfcpp/utf8/unchecked.h +++ b/src/3rdparty/utfcpp/utf8/unchecked.h @@ -32,197 +32,255 @@ DEALINGS IN THE SOFTWARE. namespace utf8 { - namespace unchecked + namespace unchecked { template - octet_iterator append(uint32_t cp, octet_iterator result) + octet_iterator append(utfchar32_t cp, octet_iterator result) { - if (cp < 0x80) // one octet - *(result++) = static_cast(cp); - else if (cp < 0x800) { // two octets - *(result++) = static_cast((cp >> 6) | 0xc0); - *(result++) = static_cast((cp & 0x3f) | 0x80); - } - else if (cp < 0x10000) { // three octets - *(result++) = static_cast((cp >> 12) | 0xe0); - *(result++) = static_cast(((cp >> 6) & 0x3f) | 0x80); - *(result++) = static_cast((cp & 0x3f) | 0x80); - } - else { // four octets - *(result++) = static_cast((cp >> 18) | 0xf0); - *(result++) = static_cast(((cp >> 12) & 0x3f)| 0x80); - *(result++) = static_cast(((cp >> 6) & 0x3f) | 0x80); - *(result++) = static_cast((cp & 0x3f) | 0x80); + return internal::append(cp, result); + } + + template + word_iterator append16(utfchar32_t cp, word_iterator result) + { + return internal::append16(cp, result); + } + + template + output_iterator replace_invalid(octet_iterator start, octet_iterator end, output_iterator out, utfchar32_t replacement) + { + while (start != end) { + octet_iterator sequence_start = start; + internal::utf_error err_code = utf8::internal::validate_next(start, end); + switch (err_code) { + case internal::UTF8_OK : + for (octet_iterator it = sequence_start; it != start; ++it) + *out++ = *it; + break; + case internal::NOT_ENOUGH_ROOM: + out = utf8::unchecked::append(replacement, out); + start = end; + break; + case internal::INVALID_LEAD: + out = utf8::unchecked::append(replacement, out); + ++start; + break; + case internal::INCOMPLETE_SEQUENCE: + case internal::OVERLONG_SEQUENCE: + case internal::INVALID_CODE_POINT: + out = utf8::unchecked::append(replacement, out); + ++start; + // just one replacement mark for the sequence + while (start != end && utf8::internal::is_trail(*start)) + ++start; + break; + } } + return out; + } + + template + inline output_iterator replace_invalid(octet_iterator start, octet_iterator end, output_iterator out) + { + static const utfchar32_t replacement_marker = static_cast(utf8::internal::mask16(0xfffd)); + return utf8::unchecked::replace_invalid(start, end, out, replacement_marker); + } + + inline std::string replace_invalid(const std::string& s, utfchar32_t replacement) + { + std::string result; + replace_invalid(s.begin(), s.end(), std::back_inserter(result), replacement); + return result; + } + + inline std::string replace_invalid(const std::string& s) + { + std::string result; + replace_invalid(s.begin(), s.end(), std::back_inserter(result)); return result; } template - uint32_t next(octet_iterator& it) + utfchar32_t next(octet_iterator& it) { - uint32_t cp = internal::mask8(*it); - typename std::iterator_traits::difference_type length = utf8::internal::sequence_length(it); - switch (length) { + utfchar32_t cp = utf8::internal::mask8(*it); + switch (utf8::internal::sequence_length(it)) { case 1: break; case 2: - it++; + ++it; cp = ((cp << 6) & 0x7ff) + ((*it) & 0x3f); break; case 3: - ++it; - cp = ((cp << 12) & 0xffff) + ((internal::mask8(*it) << 6) & 0xfff); ++it; - cp += (*it) & 0x3f; + cp = ((cp << 12) & 0xffff) + ((utf8::internal::mask8(*it) << 6) & 0xfff); + ++it; + cp = static_cast(cp + ((*it) & 0x3f)); break; case 4: ++it; - cp = ((cp << 18) & 0x1fffff) + ((internal::mask8(*it) << 12) & 0x3ffff); + cp = ((cp << 18) & 0x1fffff) + ((utf8::internal::mask8(*it) << 12) & 0x3ffff); ++it; - cp += (internal::mask8(*it) << 6) & 0xfff; + cp = static_cast(cp + ((utf8::internal::mask8(*it) << 6) & 0xfff)); ++it; - cp += (*it) & 0x3f; + cp = static_cast(cp + ((*it) & 0x3f)); break; } ++it; - return cp; + return cp; } template - uint32_t peek_next(octet_iterator it) + utfchar32_t peek_next(octet_iterator it) { - return next(it); + return utf8::unchecked::next(it); } - template - uint32_t prior(octet_iterator& it) + template + utfchar32_t next16(word_iterator& it) { - while (internal::is_trail(*(--it))) ; - octet_iterator temp = it; - return next(temp); + utfchar32_t cp = utf8::internal::mask16(*it++); + if (utf8::internal::is_lead_surrogate(cp)) + return (cp << 10) + *it++ + utf8::internal::SURROGATE_OFFSET; + return cp; } - // Deprecated in versions that include prior, but only for the sake of consistency (see utf8::previous) template - inline uint32_t previous(octet_iterator& it) + utfchar32_t prior(octet_iterator& it) { - return prior(it); + while (utf8::internal::is_trail(*(--it))) ; + octet_iterator temp = it; + return utf8::unchecked::next(temp); } template - void advance (octet_iterator& it, distance_type n) + void advance(octet_iterator& it, distance_type n) { - for (distance_type i = 0; i < n; ++i) - next(it); + const distance_type zero(0); + if (n < zero) { + // backward + for (distance_type i = n; i < zero; ++i) + utf8::unchecked::prior(it); + } else { + // forward + for (distance_type i = zero; i < n; ++i) + utf8::unchecked::next(it); + } } template typename std::iterator_traits::difference_type - distance (octet_iterator first, octet_iterator last) + distance(octet_iterator first, octet_iterator last) { typename std::iterator_traits::difference_type dist; - for (dist = 0; first < last; ++dist) - next(first); + for (dist = 0; first < last; ++dist) + utf8::unchecked::next(first); return dist; } template - octet_iterator utf16to8 (u16bit_iterator start, u16bit_iterator end, octet_iterator result) - { + octet_iterator utf16to8(u16bit_iterator start, u16bit_iterator end, octet_iterator result) + { while (start != end) { - uint32_t cp = internal::mask16(*start++); - // Take care of surrogate pairs first - if (internal::is_lead_surrogate(cp)) { - uint32_t trail_surrogate = internal::mask16(*start++); + utfchar32_t cp = utf8::internal::mask16(*start++); + // Take care of surrogate pairs first + if (utf8::internal::is_lead_surrogate(cp)) { + if (start == end) + return result; + utfchar32_t trail_surrogate = utf8::internal::mask16(*start++); cp = (cp << 10) + trail_surrogate + internal::SURROGATE_OFFSET; } - result = append(cp, result); + result = utf8::unchecked::append(cp, result); } - return result; + return result; } template - u16bit_iterator utf8to16 (octet_iterator start, octet_iterator end, u16bit_iterator result) + u16bit_iterator utf8to16(octet_iterator start, octet_iterator end, u16bit_iterator result) { while (start < end) { - uint32_t cp = next(start); + utfchar32_t cp = utf8::unchecked::next(start); if (cp > 0xffff) { //make a surrogate pair - *result++ = static_cast((cp >> 10) + internal::LEAD_OFFSET); - *result++ = static_cast((cp & 0x3ff) + internal::TRAIL_SURROGATE_MIN); + *result++ = static_cast((cp >> 10) + internal::LEAD_OFFSET); + *result++ = static_cast((cp & 0x3ff) + internal::TRAIL_SURROGATE_MIN); } else - *result++ = static_cast(cp); + *result++ = static_cast(cp); } return result; } template - octet_iterator utf32to8 (u32bit_iterator start, u32bit_iterator end, octet_iterator result) + octet_iterator utf32to8(u32bit_iterator start, u32bit_iterator end, octet_iterator result) { while (start != end) - result = append(*(start++), result); + result = utf8::unchecked::append(*(start++), result); return result; } template - u32bit_iterator utf8to32 (octet_iterator start, octet_iterator end, u32bit_iterator result) + u32bit_iterator utf8to32(octet_iterator start, octet_iterator end, u32bit_iterator result) { while (start < end) - (*result++) = next(start); + (*result++) = utf8::unchecked::next(start); return result; } // The iterator class template - class iterator : public std::iterator { + class iterator { octet_iterator it; public: - iterator () {}; + typedef utfchar32_t value_type; + typedef utfchar32_t* pointer; + typedef utfchar32_t& reference; + typedef std::ptrdiff_t difference_type; + typedef std::bidirectional_iterator_tag iterator_category; + iterator () {} explicit iterator (const octet_iterator& octet_it): it(octet_it) {} // the default "big three" are OK octet_iterator base () const { return it; } - uint32_t operator * () const + utfchar32_t operator * () const { octet_iterator temp = it; - return next(temp); + return utf8::unchecked::next(temp); } - bool operator == (const iterator& rhs) const - { + bool operator == (const iterator& rhs) const + { return (it == rhs.it); } bool operator != (const iterator& rhs) const { return !(operator == (rhs)); } - iterator& operator ++ () + iterator& operator ++ () { - std::advance(it, internal::sequence_length(it)); + ::std::advance(it, utf8::internal::sequence_length(it)); return *this; } iterator operator ++ (int) { iterator temp = *this; - std::advance(it, internal::sequence_length(it)); + ::std::advance(it, utf8::internal::sequence_length(it)); return temp; - } + } iterator& operator -- () { - prior(it); + utf8::unchecked::prior(it); return *this; } iterator operator -- (int) { iterator temp = *this; - prior(it); + utf8::unchecked::prior(it); return temp; } }; // class iterator } // namespace utf8::unchecked -} // namespace utf8 - +} // namespace utf8 #endif // header guard diff --git a/src/bin/phonetisaurus-align.cc b/src/bin/phonetisaurus-align.cc index 8812671..49c6a51 100644 --- a/src/bin/phonetisaurus-align.cc +++ b/src/bin/phonetisaurus-align.cc @@ -97,7 +97,7 @@ void write_alignments (M2MFstAligner* aligner, string ofile_name, for (unsigned int i = 0; i < aligner->fsas.size (); i++) { //Map to Tropical semiring VectorFst* tfst = new VectorFst (); - Map (aligner->fsas.at (i), tfst, LogToStdMapper ()); + ArcMap (aligner->fsas.at (i), tfst, LogToStdMapper ()); pruner.prune_fst (tfst); RmEpsilon (tfst); //Skip empty results. This should only happen @@ -160,7 +160,7 @@ void compileNBestFarArchive (M2MFstAligner* aligner, string key_suffix = ""; string key = ""; char keybuf[16]; - int32 generate_keys = 7; //Suitable for up to several million lattices + int32_t generate_keys = 7; //Suitable for up to several million lattices bool set_syms = false; //Have we set the isyms successfully yet?? //Build us a FarWriter to compile the archive FarWriter *far_writer = \ @@ -180,11 +180,11 @@ void compileNBestFarArchive (M2MFstAligner* aligner, VectorFst* ffst = new VectorFst (); //Map to the Tropical semiring - Map (fsts->at(i), tfst, LogToStdMapper ()); + ArcMap (fsts->at(i), tfst, LogToStdMapper ()); pruner.prune_fst (tfst); //Map back to the Log semiring - Map (*tfst, lfst, StdToLogMapper ()); + ArcMap (*tfst, lfst, StdToLogMapper ()); //Perform posterior normalization of the N-best lattice by pushing weights // in the log semiring and then removing the final weight. @@ -204,7 +204,7 @@ void compileNBestFarArchive (M2MFstAligner* aligner, if (pfst->NumStates () == 0) continue; //Finally map back to the Tropical semiring for the last time - Map (*pfst, ffst, LogToStdMapper ()); + ArcMap (*pfst, ffst, LogToStdMapper ()); if (set_syms == false) { ffst->SetInputSymbols (aligner->isyms); @@ -267,28 +267,28 @@ int main( int argc, char* argv[] ){ PhonetisaurusSetFlags (usage.c_str(), &argc, &argv, false); M2MFstAligner aligner; - if (FLAGS_load_model == true) { - aligner = *(new M2MFstAligner (FLAGS_model_file, FLAGS_penalize, - FLAGS_penalize_em, FLAGS_restrict)); - switch (load_input_file (&aligner, FLAGS_input, FLAGS_delim, - FLAGS_s1_char_delim, FLAGS_s2_char_delim, - FLAGS_load_model)) { + if (FST_FLAGS_load_model == true) { + aligner = *(new M2MFstAligner (FST_FLAGS_model_file, FST_FLAGS_penalize, + FST_FLAGS_penalize_em, FST_FLAGS_restrict)); + switch (load_input_file (&aligner, FST_FLAGS_input, FST_FLAGS_delim, + FST_FLAGS_s1_char_delim, FST_FLAGS_s2_char_delim, + FST_FLAGS_load_model)) { case 0: cerr << "Please provide a valid input file." << endl; case -1: return -1; } } else { - aligner = *(new M2MFstAligner (FLAGS_seq1_del, FLAGS_seq2_del, - FLAGS_seq1_max, FLAGS_seq2_max, - FLAGS_seq1_sep, FLAGS_seq2_sep, - FLAGS_s1s2_sep, FLAGS_eps, FLAGS_skip, - FLAGS_penalize, FLAGS_penalize_em, - FLAGS_restrict, FLAGS_grow + aligner = *(new M2MFstAligner (FST_FLAGS_seq1_del, FST_FLAGS_seq2_del, + FST_FLAGS_seq1_max, FST_FLAGS_seq2_max, + FST_FLAGS_seq1_sep, FST_FLAGS_seq2_sep, + FST_FLAGS_s1s2_sep, FST_FLAGS_eps, FST_FLAGS_skip, + FST_FLAGS_penalize, FST_FLAGS_penalize_em, + FST_FLAGS_restrict, FST_FLAGS_grow )); - switch (load_input_file (&aligner, FLAGS_input, FLAGS_delim, - FLAGS_s1_char_delim, FLAGS_s2_char_delim, - FLAGS_load_model)) { + switch (load_input_file (&aligner, FST_FLAGS_input, FST_FLAGS_delim, + FST_FLAGS_s1_char_delim, FST_FLAGS_s2_char_delim, + FST_FLAGS_load_model)) { case 0: cerr << "Please provide a valid input file." << endl; case -1: @@ -298,7 +298,7 @@ int main( int argc, char* argv[] ){ cerr << "Starting EM..." << endl; aligner.maximization (false); cerr << "Finished first iter..." << endl; - for (int i = 1; i <= FLAGS_iter; i++) { + for (int i = 1; i <= FST_FLAGS_iter; i++) { cerr << "Iteration: " << i << " Change: "; aligner.expectation (); cerr << aligner.maximization (false) << endl; @@ -309,21 +309,21 @@ int main( int argc, char* argv[] ){ aligner.maximization (true); } - StdArc::Weight pthresh = FLAGS_pthresh == -99.0 + StdArc::Weight pthresh = FST_FLAGS_pthresh == -99.0 ? LogWeight::Zero().Value() - : FLAGS_pthresh; - if (FLAGS_write_model.compare ("") != 0) { + : FST_FLAGS_pthresh; + if (FST_FLAGS_write_model.compare ("") != 0) { cerr << "Writing alignment model in OpenFst format to file: " - << FLAGS_write_model << endl; - aligner.write_model (FLAGS_write_model); + << FST_FLAGS_write_model << endl; + aligner.write_model (FST_FLAGS_write_model); } - if (FLAGS_lattice == true) - compileNBestFarArchive (&aligner, &aligner.fsas, FLAGS_ofile, pthresh, - FLAGS_nbest, FLAGS_fb, FLAGS_penalize); + if (FST_FLAGS_lattice == true) + compileNBestFarArchive (&aligner, &aligner.fsas, FST_FLAGS_ofile, pthresh, + FST_FLAGS_nbest, FST_FLAGS_fb, FST_FLAGS_penalize); else - write_alignments (&aligner, FLAGS_ofile, pthresh, FLAGS_nbest, - FLAGS_fb, FLAGS_penalize); + write_alignments (&aligner, FST_FLAGS_ofile, pthresh, FST_FLAGS_nbest, + FST_FLAGS_fb, FST_FLAGS_penalize); return 0; } diff --git a/src/bin/phonetisaurus-arpa2wfst.cc b/src/bin/phonetisaurus-arpa2wfst.cc index a74bb84..1c4507f 100644 --- a/src/bin/phonetisaurus-arpa2wfst.cc +++ b/src/bin/phonetisaurus-arpa2wfst.cc @@ -52,23 +52,23 @@ int main (int argc, char* argv []) { set_new_handler (FailedNewHandler); PhonetisaurusSetFlags (usage.c_str(), &argc, &argv, false); - if (FLAGS_lm.compare ("") == 0) { + if (FST_FLAGS_lm.compare ("") == 0) { cerr << "You must supply an ARPA format lm " "to --lm for conversion!" << endl; return 0; } cerr << "Initializing..." << endl; - ARPA2WFST* converter = new ARPA2WFST (FLAGS_lm, FLAGS_eps, FLAGS_sb, - FLAGS_se, FLAGS_split, FLAGS_skip, - FLAGS_tie); + ARPA2WFST* converter = new ARPA2WFST (FST_FLAGS_lm, FST_FLAGS_eps, FST_FLAGS_sb, + FST_FLAGS_se, FST_FLAGS_split, FST_FLAGS_skip, + FST_FLAGS_tie); cerr << "Converting..." << endl; converter->arpa_to_wfst (); - converter->arpafst.Write (FLAGS_ofile); + converter->arpafst.Write (FST_FLAGS_ofile); - if (FLAGS_ssyms.compare ("") != 0) { - converter->ssyms->WriteText (FLAGS_ssyms); + if (FST_FLAGS_ssyms.compare ("") != 0) { + converter->ssyms->WriteText (FST_FLAGS_ssyms); } delete converter; diff --git a/src/bin/phonetisaurus-g2pfst.cc b/src/bin/phonetisaurus-g2pfst.cc index 5ff0572..7c9a40b 100644 --- a/src/bin/phonetisaurus-g2pfst.cc +++ b/src/bin/phonetisaurus-g2pfst.cc @@ -38,21 +38,21 @@ using namespace fst; typedef unordered_map > RMAP; -void PrintPathData (const vector& results, string FLAGS_word, +void PrintPathData (const vector& results, string FST_FLAGS_word, const SymbolTable* osyms, bool print_scores = true, bool nlog_probs = true) { for (int i = 0; i < results.size (); i++) { - cout << FLAGS_word << "\t"; + cout << FST_FLAGS_word << "\t"; if (print_scores == true) { if (nlog_probs == true) cout << results [i].PathWeight << "\t"; - else + else cout << std::setprecision (3) << exp (-results [i].PathWeight) << "\t"; } - for (int j = 0; j < results [i].Uniques.size (); j++) { - cout << osyms->Find (results [i].Uniques [j]); - if (j < results [i].Uniques.size () - 1) + for (int j = 0; j < results[i].Uniques.size(); j++) { + cout << osyms->Find(results[i].Uniques[j]); + if (j < results[i].Uniques.size() - 1) cout << " "; } cout << endl; @@ -60,20 +60,20 @@ void PrintPathData (const vector& results, string FLAGS_word, } void EvaluateWordlist (PhonetisaurusScript& decoder, vector corpus, - int FLAGS_beam, int FLAGS_nbest, bool FLAGS_reverse, - string FLAGS_skip, double FLAGS_thresh, string FLAGS_gsep, - bool FLAGS_write_fsts, bool FLAGS_print_scores, - bool FLAGS_accumulate, double FLAGS_pmass, - bool FLAGS_nlog_probs) { + int FST_FLAGS_beam, int FST_FLAGS_nbest, bool FST_FLAGS_reverse, + string FST_FLAGS_skip, double FST_FLAGS_thresh, string FST_FLAGS_gsep, + bool FST_FLAGS_write_fsts, bool FST_FLAGS_print_scores, + bool FST_FLAGS_accumulate, double FST_FLAGS_pmass, + bool FST_FLAGS_nlog_probs) { for (int i = 0; i < corpus.size (); i++) { - vector results = decoder.Phoneticize (corpus [i], FLAGS_nbest, - FLAGS_beam, FLAGS_thresh, - FLAGS_write_fsts, - FLAGS_accumulate, FLAGS_pmass); + vector results = decoder.Phoneticize (corpus [i], FST_FLAGS_nbest, + FST_FLAGS_beam, FST_FLAGS_thresh, + FST_FLAGS_write_fsts, + FST_FLAGS_accumulate, FST_FLAGS_pmass); PrintPathData (results, corpus [i], decoder.osyms_, - FLAGS_print_scores, - FLAGS_nlog_probs); + FST_FLAGS_print_scores, + FST_FLAGS_nlog_probs); } } @@ -99,65 +99,65 @@ int main (int argc, char* argv []) { set_new_handler (FailedNewHandler); PhonetisaurusSetFlags (usage.c_str(), &argc, &argv, false); - if (FLAGS_model.compare ("") == 0) { + if (FST_FLAGS_model.compare ("") == 0) { cerr << "You must supply an FST model to --model" << endl; exit (1); } else { - std::ifstream model_ifp (FLAGS_model); + std::ifstream model_ifp (FST_FLAGS_model); if (!model_ifp.good ()) { cout << "Failed to open --model file '" - << FLAGS_model << "'" << endl; + << FST_FLAGS_model << "'" << endl; exit (1); } } - if (FLAGS_pmass < 0.0 || FLAGS_pmass > 1) { + if (FST_FLAGS_pmass < 0.0 || FST_FLAGS_pmass > 1) { cout << "--pmass must be a float value between 0.0 and 1.0." << endl; exit (1); } - if (FLAGS_pmass == 0.0) - FLAGS_pmass = 99.0; + if (FST_FLAGS_pmass == 0.0) + FST_FLAGS_pmass = 99.0; else - FLAGS_pmass = -log (FLAGS_pmass); + FST_FLAGS_pmass = -log (FST_FLAGS_pmass); bool use_wordlist = false; - if (FLAGS_wordlist.compare ("") != 0) { - std::ifstream wordlist_ifp (FLAGS_wordlist); + if (FST_FLAGS_wordlist.compare ("") != 0) { + std::ifstream wordlist_ifp (FST_FLAGS_wordlist); if (!wordlist_ifp.good ()) { cout << "Failed to open --wordlist file '" - << FLAGS_wordlist << "'" << endl; + << FST_FLAGS_wordlist << "'" << endl; exit (1); } else { use_wordlist = true; } } - if (FLAGS_wordlist.compare ("") == 0 && FLAGS_word.compare ("") == 0) { + if (FST_FLAGS_wordlist.compare ("") == 0 && FST_FLAGS_word.compare ("") == 0) { cout << "Either --wordlist or --word must be set!" << endl; exit (1); } if (use_wordlist == true) { vector corpus; - LoadWordList (FLAGS_wordlist, &corpus); + LoadWordList (FST_FLAGS_wordlist, &corpus); - PhonetisaurusScript decoder (FLAGS_model, FLAGS_gsep); + PhonetisaurusScript decoder (FST_FLAGS_model, FST_FLAGS_gsep); EvaluateWordlist ( - decoder, corpus, FLAGS_beam, FLAGS_nbest, FLAGS_reverse, - FLAGS_skip, FLAGS_thresh, FLAGS_gsep, FLAGS_write_fsts, - FLAGS_print_scores, FLAGS_accumulate, FLAGS_pmass, - FLAGS_nlog_probs + decoder, corpus, FST_FLAGS_beam, FST_FLAGS_nbest, FST_FLAGS_reverse, + FST_FLAGS_skip, FST_FLAGS_thresh, FST_FLAGS_gsep, FST_FLAGS_write_fsts, + FST_FLAGS_print_scores, FST_FLAGS_accumulate, FST_FLAGS_pmass, + FST_FLAGS_nlog_probs ); } else { - PhonetisaurusScript decoder (FLAGS_model, FLAGS_gsep); + PhonetisaurusScript decoder (FST_FLAGS_model, FST_FLAGS_gsep); vector results = decoder.Phoneticize ( - FLAGS_word, FLAGS_nbest, FLAGS_beam, FLAGS_thresh, - FLAGS_write_fsts, FLAGS_accumulate, FLAGS_pmass + FST_FLAGS_word, FST_FLAGS_nbest, FST_FLAGS_beam, FST_FLAGS_thresh, + FST_FLAGS_write_fsts, FST_FLAGS_accumulate, FST_FLAGS_pmass ); - PrintPathData (results, FLAGS_word, + PrintPathData (results, FST_FLAGS_word, decoder.osyms_, - FLAGS_print_scores, - FLAGS_nlog_probs); + FST_FLAGS_print_scores, + FST_FLAGS_nlog_probs); } return 0; diff --git a/src/bin/phonetisaurus-g2prnn.cc b/src/bin/phonetisaurus-g2prnn.cc index d50589d..c4a1173 100644 --- a/src/bin/phonetisaurus-g2prnn.cc +++ b/src/bin/phonetisaurus-g2prnn.cc @@ -17,33 +17,33 @@ typedef unordered_map RMAP; void ThreadedEvaluateWordlist (vector& corpus, RMAP& rmap, LegacyRnnLMHash& h, Decodable& s, - int FLAGS_threads, int FLAGS_beam, - int FLAGS_kmax, int FLAGS_nbest, - bool FLAGS_reverse, string FLAGS_gpdelim, - string FLAGS_gdelim, string FLAGS_skip, - double FLAGS_thresh, string FLAGS_gsep) { + int FST_FLAGS_threads, int FST_FLAGS_beam, + int FST_FLAGS_kmax, int FST_FLAGS_nbest, + bool FST_FLAGS_reverse, string FST_FLAGS_gpdelim, + string FST_FLAGS_gdelim, string FST_FLAGS_skip, + double FST_FLAGS_thresh, string FST_FLAGS_gsep) { int csize = corpus.size (); #ifdef _OPENMP #pragma omp parallel for #endif - for (int x = 0; x < FLAGS_threads; x++) { + for (int x = 0; x < FST_FLAGS_threads; x++) { RnnLMDecoder decoder (s); - int start = x * (csize / FLAGS_threads); - int end = (x == FLAGS_threads - 1) ? csize \ - : start + (csize / FLAGS_threads); + int start = x * (csize / FST_FLAGS_threads); + int end = (x == FST_FLAGS_threads - 1) ? csize \ + : start + (csize / FST_FLAGS_threads); for (int i = start; i < end; i++) { vector graphemes = tokenize_utf8_string (&corpus [i], - &FLAGS_gsep); - if (FLAGS_reverse == true) + &FST_FLAGS_gsep); + if (FST_FLAGS_reverse == true) reverse (graphemes.begin (), graphemes.end ()); graphemes.push_back (""); SimpleResult result = \ - decoder.Decode (graphemes, FLAGS_beam, FLAGS_kmax, - FLAGS_nbest, FLAGS_thresh, FLAGS_gpdelim, - FLAGS_gdelim, FLAGS_skip); + decoder.Decode (graphemes, FST_FLAGS_beam, FST_FLAGS_kmax, + FST_FLAGS_nbest, FST_FLAGS_thresh, FST_FLAGS_gpdelim, + FST_FLAGS_gdelim, FST_FLAGS_skip); rmap [i] = result; } } @@ -58,25 +58,25 @@ void ThreadedEvaluateWordlist (vector& corpus, RMAP& rmap, } void EvaluateWordlist (vector& corpus, - LegacyRnnLMHash& h, Decodable& s, int FLAGS_beam, - int FLAGS_kmax, int FLAGS_nbest, bool FLAGS_reverse, - string FLAGS_gpdelim, string FLAGS_gdelim, - string FLAGS_skip, double FLAGS_thresh, - string FLAGS_gsep) { + LegacyRnnLMHash& h, Decodable& s, int FST_FLAGS_beam, + int FST_FLAGS_kmax, int FST_FLAGS_nbest, bool FST_FLAGS_reverse, + string FST_FLAGS_gpdelim, string FST_FLAGS_gdelim, + string FST_FLAGS_skip, double FST_FLAGS_thresh, + string FST_FLAGS_gsep) { RnnLMDecoder decoder (s); for (int i = 0; i < corpus.size (); i++) { vector graphemes = tokenize_utf8_string (&corpus [i], - &FLAGS_gsep); - if (FLAGS_reverse == true) + &FST_FLAGS_gsep); + if (FST_FLAGS_reverse == true) reverse (graphemes.begin (), graphemes.end ()); graphemes.push_back (""); SimpleResult result = \ - decoder.Decode (graphemes, FLAGS_beam, FLAGS_kmax, - FLAGS_nbest, FLAGS_thresh, FLAGS_gpdelim, - FLAGS_gdelim, FLAGS_skip); + decoder.Decode (graphemes, FST_FLAGS_beam, FST_FLAGS_kmax, + FST_FLAGS_nbest, FST_FLAGS_thresh, FST_FLAGS_gpdelim, + FST_FLAGS_gdelim, FST_FLAGS_skip); for (int k = 0; k < result.pronunciations.size (); k++) cout << result.word << "\t" << result.scores [k] << "\t" @@ -85,22 +85,22 @@ void EvaluateWordlist (vector& corpus, } void EvaluateWord (string word, LegacyRnnLMHash& h, Decodable& s, - int FLAGS_beam, int FLAGS_kmax, int FLAGS_nbest, - bool FLAGS_reverse, string FLAGS_gpdelim, - string FLAGS_gdelim, string FLAGS_skip, - double FLAGS_thresh, string FLAGS_gsep) { + int FST_FLAGS_beam, int FST_FLAGS_kmax, int FST_FLAGS_nbest, + bool FST_FLAGS_reverse, string FST_FLAGS_gpdelim, + string FST_FLAGS_gdelim, string FST_FLAGS_skip, + double FST_FLAGS_thresh, string FST_FLAGS_gsep) { vector graphemes = tokenize_utf8_string (&word, - &FLAGS_gsep); - if (FLAGS_reverse == true) + &FST_FLAGS_gsep); + if (FST_FLAGS_reverse == true) reverse (graphemes.begin (), graphemes.end ()); graphemes.push_back (""); RnnLMDecoder decoder (s); SimpleResult result = \ - decoder.Decode (graphemes, FLAGS_beam, FLAGS_kmax, - FLAGS_nbest, FLAGS_thresh, FLAGS_gpdelim, - FLAGS_gdelim, FLAGS_skip); + decoder.Decode (graphemes, FST_FLAGS_beam, FST_FLAGS_kmax, + FST_FLAGS_nbest, FST_FLAGS_thresh, FST_FLAGS_gpdelim, + FST_FLAGS_gdelim, FST_FLAGS_skip); for (int k = 0; k < result.pronunciations.size (); k++) cout << result.word << "\t" << result.scores [k] << "\t" @@ -128,64 +128,64 @@ int main (int argc, char* argv []) { set_new_handler (FailedNewHandler); PhonetisaurusSetFlags (usage.c_str (), &argc, &argv, false); - if (FLAGS_rnnlm.compare ("") == 0) { + if (FST_FLAGS_rnnlm.compare ("") == 0) { cout << "--rnnlm model is required!" << endl; exit (1); } else { - std::ifstream rnnlm_ifp (FLAGS_rnnlm); + std::ifstream rnnlm_ifp (FST_FLAGS_rnnlm); if (!rnnlm_ifp.good ()) { cout << "Faile to open --rnnlm file '" - << FLAGS_rnnlm << "'" << endl; + << FST_FLAGS_rnnlm << "'" << endl; exit (1); } } bool use_wordlist = false; - if (FLAGS_wordlist.compare ("") != 0) { - std::ifstream wordlist_ifp (FLAGS_wordlist); + if (FST_FLAGS_wordlist.compare ("") != 0) { + std::ifstream wordlist_ifp (FST_FLAGS_wordlist); if (!wordlist_ifp.good ()) { cout << "Failed to open --wordlist file '" - << FLAGS_wordlist << "'" << endl; + << FST_FLAGS_wordlist << "'" << endl; exit (1); } else { use_wordlist = true; } } - if (FLAGS_wordlist.compare ("") == 0 && FLAGS_word.compare ("") == 0) { + if (FST_FLAGS_wordlist.compare ("") == 0 && FST_FLAGS_word.compare ("") == 0) { cout << "Either --wordlist or --word must be set!" << endl; } #ifdef _OPENMP - omp_set_num_threads (FLAGS_threads); + omp_set_num_threads (FST_FLAGS_threads); #endif vector corpus; - LoadWordList (FLAGS_wordlist, &corpus); + LoadWordList (FST_FLAGS_wordlist, &corpus); RMAP rmap; - LegacyRnnLMReader reader (FLAGS_rnnlm); - LegacyRnnLMHash h = reader.CopyVocabHash (FLAGS_gdelim, FLAGS_gpdelim); + LegacyRnnLMReader reader (FST_FLAGS_rnnlm); + LegacyRnnLMHash h = reader.CopyVocabHash (FST_FLAGS_gdelim, FST_FLAGS_gpdelim); Decodable s = reader.CopyLegacyRnnLM (h); if (use_wordlist == true) { - if (FLAGS_threads > 1) { - ThreadedEvaluateWordlist (corpus, rmap, h, s, FLAGS_threads, - FLAGS_beam, FLAGS_kmax, FLAGS_nbest, - FLAGS_reverse, FLAGS_gpdelim, - FLAGS_gdelim, FLAGS_skip, - FLAGS_thresh, FLAGS_gsep); + if (FST_FLAGS_threads > 1) { + ThreadedEvaluateWordlist (corpus, rmap, h, s, FST_FLAGS_threads, + FST_FLAGS_beam, FST_FLAGS_kmax, FST_FLAGS_nbest, + FST_FLAGS_reverse, FST_FLAGS_gpdelim, + FST_FLAGS_gdelim, FST_FLAGS_skip, + FST_FLAGS_thresh, FST_FLAGS_gsep); } else { - EvaluateWordlist (corpus, h, s, FLAGS_beam, - FLAGS_kmax, FLAGS_nbest, FLAGS_reverse, - FLAGS_gpdelim, FLAGS_gdelim, FLAGS_skip, - FLAGS_thresh, FLAGS_gsep); + EvaluateWordlist (corpus, h, s, FST_FLAGS_beam, + FST_FLAGS_kmax, FST_FLAGS_nbest, FST_FLAGS_reverse, + FST_FLAGS_gpdelim, FST_FLAGS_gdelim, FST_FLAGS_skip, + FST_FLAGS_thresh, FST_FLAGS_gsep); } } else { - EvaluateWord (FLAGS_word, h, s, FLAGS_beam, FLAGS_kmax, - FLAGS_nbest, FLAGS_reverse, FLAGS_gpdelim, - FLAGS_gdelim, FLAGS_skip, FLAGS_thresh, FLAGS_gsep); + EvaluateWord (FST_FLAGS_word, h, s, FST_FLAGS_beam, FST_FLAGS_kmax, + FST_FLAGS_nbest, FST_FLAGS_reverse, FST_FLAGS_gpdelim, + FST_FLAGS_gdelim, FST_FLAGS_skip, FST_FLAGS_thresh, FST_FLAGS_gsep); } return 0; diff --git a/src/include/LegacyRnnLMDecodable.h b/src/include/LegacyRnnLMDecodable.h index fc0a154..29f0ce8 100644 --- a/src/include/LegacyRnnLMDecodable.h +++ b/src/include/LegacyRnnLMDecodable.h @@ -44,8 +44,8 @@ static inline float FAST_EXP (float p) { template class LegacyRnnLMDecodable { public: - LegacyRnnLMDecodable (H& hash, int i, int h, int o, int d, int m) - : h (hash), isize (i), hsize (h), osize (o), order (d), max_order (m) { } + LegacyRnnLMDecodable (H& _hash, int i, int h, int o, int d, int m) + : h (_hash), isize (i), hsize (h), osize (o), order (d), max_order (m) { } double ComputeNet (const T& p, T* t) { vector olayer; @@ -73,28 +73,28 @@ class LegacyRnnLMDecodable { // Begin class direct connection activations if (synd.size () > 0) { - // Feature hash begin - vector hash; - hash.resize (max_order, 0); + // Feature _hash begin + vector _hash; + _hash.resize (max_order, 0); for (int i = 0; i < order; i++) { if (i > 0) if (t->history [i - 1] == -1) break; - hash [i] = h.primes_[0] * h.primes_[1]; + _hash [i] = h.primes_[0] * h.primes_[1]; for (int j = 1; j <= i; j++) - hash [i] += + _hash [i] += h.primes_[(i * h.primes_[j] + j) % h.primes_.size ()] - * static_cast(t->history [j - 1] + 1); + * static_cast(t->history [j - 1] + 1); - hash [i] = hash [i] % (synd.size () / 2); + _hash [i] = _hash [i] % (synd.size () / 2); } - // Feature hash end + // Feature _hash end for (int i = h.vocab_.size (); i < osize; i++) { for (int j = 0; j < order; j++) { - if (hash [j]) { - olayer [i] += synd [hash [j]]; - hash [j]++; + if (_hash [j]) { + olayer [i] += synd [_hash [j]]; + _hash [j]++; } else { break; } @@ -127,33 +127,33 @@ class LegacyRnnLMDecodable { // Begin word direct connection activations if (synd.size () > 0) { - // Begin feature hashing - uint64 hash [max_order]; + // Begin feature _hashing + uint64_t _hash [max_order]; for (int i = 0; i < order; i++) - hash [i] = 0; + _hash [i] = 0; for (int i = 0; i < order; i++) { if (i > 0) if (t->history [i - 1] == -1) break; - hash [i] = h.primes_[0] * h.primes_[1] - * static_cast (h.vocab_[t->word].class_index + 1); + _hash [i] = h.primes_[0] * h.primes_[1] + * static_cast (h.vocab_[t->word].class_index + 1); for (int j = 1; j <= i; j++) - hash [i] += h.primes_[(i * h.primes_[j] + j) % h.primes_.size ()] - * static_cast (t->history [j - 1] + 1); + _hash [i] += h.primes_[(i * h.primes_[j] + j) % h.primes_.size ()] + * static_cast (t->history [j - 1] + 1); - hash [i] = (hash [i] % (synd.size () / 2)) + (synd.size () / 2); + _hash [i] = (_hash [i] % (synd.size () / 2)) + (synd.size () / 2); } - // End feature hashing + // End feature _hashing for (int i = begin; i <= end; i++) { for (int j = 0; j < order; j++) { - if (hash [j]) { - olayer [i] += synd [hash [j]]; - hash [j]++; - hash [j] = hash [j] % synd.size (); + if (_hash [j]) { + olayer [i] += synd [_hash [j]]; + _hash [j]++; + _hash [j] = _hash [j] % synd.size (); } else { break; } @@ -179,7 +179,7 @@ class LegacyRnnLMDecodable { * olayer [h.vocab_.size () + h.vocab_[t->word].class_index]; } - // We need the synapses and the vocabulary hash + // We need the synapses and the vocabulary _hash H& h; int isize; int hsize; diff --git a/src/include/util.h b/src/include/util.h index 54f397c..3854d6a 100644 --- a/src/include/util.h +++ b/src/include/util.h @@ -39,6 +39,7 @@ #include #endif using namespace fst; +using namespace std; typedef struct LabelDatum {int max, tot, lhs, rhs; bool lhsE, rhsE;} LabelDatum; typedef unordered_map LabelData; diff --git a/src/lib/LatticePruner.cc b/src/lib/LatticePruner.cc index 8ec2263..8f7a2b3 100644 --- a/src/lib/LatticePruner.cc +++ b/src/lib/LatticePruner.cc @@ -108,7 +108,7 @@ void LatticePruner::_forward_backward( VectorFst* fst ){ VectorFst* lfst = new VectorFst(); vector alpha, beta; - Map(*fst, lfst, StdToLogMapper()); + ArcMap(*fst, lfst, StdToLogMapper()); //Normalize so that subsequent operations don't go crazy Push(*lfst, pfst, kPushWeights); @@ -137,7 +137,7 @@ void LatticePruner::_forward_backward( VectorFst* fst ){ } } - Map(*pfst, fst, LogToStdMapper()); + ArcMap(*pfst, fst, LogToStdMapper()); delete lfst; delete pfst; diff --git a/src/lib/util.cc b/src/lib/util.cc index 01e2a1f..72a51ed 100644 --- a/src/lib/util.cc +++ b/src/lib/util.cc @@ -60,7 +60,7 @@ vector tokenize_utf8_string (string* utf8_string, string* delimiter) { http://stackoverflow.com/questions/2852895/c-iterate-or-split-\ utf-8-string-into-array-of-symbols#2856241 */ - char* str = (char*) utf8_string->c_str (); // utf-8 string + char* str = (char*) utf8_string->c_str (); char* str_i = str; // string iterator char* str_j = str; char* end = str + strlen (str) + 1; // end iterator @@ -70,7 +70,7 @@ vector tokenize_utf8_string (string* utf8_string, string* delimiter) { do { str_j = str_i; - utf8::uint32_t code = utf8::next (str_i, end); // get 32 bit code + utf8::utfchar32_t code = utf8::next(str_i, end); // get 32 bit code if (code == 0) continue; int start = strlen (str) - strlen (str_j); @@ -86,7 +86,6 @@ vector tokenize_utf8_string (string* utf8_string, string* delimiter) { string_vec [string_vec.size () - 1] += utf8_string->substr (start, len); } } while (str_i < end); - return string_vec; } @@ -190,12 +189,12 @@ void PhonetisaurusSetFlags (const char* usage, int* argc, char*** argv, FlagRegister::GetRegister(); if (string_register->SetFlag(arg, val)) continue; - FlagRegister *int32_register = - FlagRegister::GetRegister(); + FlagRegister *int32_register = + FlagRegister::GetRegister(); if (int32_register->SetFlag(arg, val)) continue; - FlagRegister *int64_register = - FlagRegister::GetRegister(); + FlagRegister *int64_register = + FlagRegister::GetRegister(); if (int64_register->SetFlag(arg, val)) continue; FlagRegister *double_register = @@ -206,7 +205,7 @@ void PhonetisaurusSetFlags (const char* usage, int* argc, char*** argv, LOG(FATAL) << "SetFlags: Bad option: " << (*argv)[index]; } - if (FLAGS_help) { + if (FST_FLAGS_help) { //Just show program flags - NOT general OpenFst flags // There are too many and they are just confusing. std::set< pair > usage_set; @@ -217,9 +216,9 @@ void PhonetisaurusSetFlags (const char* usage, int* argc, char*** argv, bool_register->GetUsage(&usage_set); FlagRegister *string_register = FlagRegister::GetRegister(); string_register->GetUsage(&usage_set); - FlagRegister *int32_register = FlagRegister::GetRegister(); + FlagRegister *int32_register = FlagRegister::GetRegister(); int32_register->GetUsage(&usage_set); - FlagRegister *int64_register = FlagRegister::GetRegister(); + FlagRegister *int64_register = FlagRegister::GetRegister(); int64_register->GetUsage(&usage_set); FlagRegister *double_register = FlagRegister::GetRegister(); double_register->GetUsage(&usage_set); diff --git a/src/scripts/phonetisaurus-train b/src/scripts/phonetisaurus-train index c8fd15d..d238eda 100755 --- a/src/scripts/phonetisaurus-train +++ b/src/scripts/phonetisaurus-train @@ -170,8 +170,17 @@ class G2PModelTrainer () : ]) ) return self._mitlm + if lm == "kenlm" : + if self.which ("lmplz") == None : + raise EnvironmentError( + " ".join([ + "lmplz binary 'lmplz' not ", + "found in path." + ]) + ) + return self._kenlm else : - raise NotImplementedError("Only mitlm is currently supported.") + raise NotImplementedError("Only mitlm and kenlm is currently supported.") def _mitlm (self) : @@ -195,6 +204,27 @@ class G2PModelTrainer () : return command + def _kenlm (self) : + """kenlm lmplz joint ngram training command. + + Build the kenlm joint ngram training command using the + lmplz utility and provided arguments. + + Returns: + list: The command in subprocess list format. + """ + + command = [ + "lmplz", + "-o", str (self.ngram_order), + "--text", self.corpus_path, + "--arpa", self.arpa_path + ] + + self.logger.debug (u" ".join (command)) + + return command + def makeAlignerCommand (self) : """Build the aligner command from the provided arguments.