Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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 && \
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ 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 -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)
Expand Down
2 changes: 1 addition & 1 deletion Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -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 -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)
Expand Down
80 changes: 27 additions & 53 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -38,49 +39,45 @@ $ 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 ..
```

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
Expand Down Expand Up @@ -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:
```
Expand Down Expand Up @@ -280,25 +261,18 @@ 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:

* directly, to process files on your computer without needing to install/compile anything (apart from docker)
* 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
```
62 changes: 31 additions & 31 deletions src/bin/phonetisaurus-align.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<StdArc>* tfst = new VectorFst<StdArc> ();
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
Expand Down Expand Up @@ -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<StdArc> *far_writer = \
Expand All @@ -180,11 +180,11 @@ void compileNBestFarArchive (M2MFstAligner* aligner,
VectorFst<StdArc>* ffst = new VectorFst<StdArc> ();

//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.
Expand All @@ -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);
Expand Down Expand Up @@ -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:
Expand All @@ -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;
Expand All @@ -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;
}
14 changes: 7 additions & 7 deletions src/bin/phonetisaurus-arpa2wfst.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading