-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Daniel Mann <dmann@apptek.com> Co-authored-by: Eugen Beck <ebeck@apptek.com>
- Loading branch information
1 parent
a942e39
commit f4ed97e
Showing
21 changed files
with
381 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
"""Setup.py inspired by https://thomastrapp.com/posts/building-a-pypi-package-for-a-modern-cpp-project/""" | ||
import os | ||
from setuptools import setup, dist | ||
|
||
PACKAGE_NAME = "librasr" # keyword to import | ||
LIBRASR_SUBDIR = os.path.join("src", "Tools", "LibRASR") | ||
LIBRASR_SO = "librasr.so" | ||
|
||
assert os.path.exists(os.path.join(LIBRASR_SUBDIR, LIBRASR_SO)), \ | ||
"Could not find librasr shared library. Did you compile it correctly?" | ||
class BinaryDistribution(dist.Distribution): | ||
"""Helper class: We assume that librasr.so has already been compiled.""" | ||
def has_ext_modules(self) -> bool: | ||
return True | ||
|
||
# get README content for long description | ||
this_directory = os.path.abspath(os.path.dirname(__file__)) | ||
with open(os.path.join(this_directory, 'README')) as f: | ||
long_description = f.read() | ||
|
||
setup( | ||
name='RASR', | ||
|
||
# include shared library and link to lower case package name | ||
packages=[PACKAGE_NAME], | ||
package_dir={PACKAGE_NAME: LIBRASR_SUBDIR}, | ||
package_data={PACKAGE_NAME: [LIBRASR_SO]}, | ||
include_package_data=True, | ||
|
||
description="RASR as a python module.", | ||
long_description=long_description, | ||
long_description_content_type="text/markdown", | ||
|
||
# use custom distribution class to export librasr.so | ||
distclass=BinaryDistribution, | ||
|
||
version='0.0.1', | ||
url='https://github.com/rwth-i6/rasr', | ||
author_email='rwthasr@i6.informatik.rwth-aachen.de', | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#include "AllophoneStateFsaBuilder.hh" | ||
|
||
#include <memory> | ||
#include <vector> | ||
|
||
#undef ensure // macro duplication in pybind11/numpy.h | ||
#include <pybind11/pybind11.h> | ||
#include <pybind11/numpy.h> | ||
|
||
#include <Core/Application.hh> | ||
#include <Core/Component.hh> | ||
#include <Core/Archive.hh> | ||
#include <Bliss/CorpusDescription.hh> | ||
#include <Nn/AllophoneStateFsaExporter.hh> | ||
|
||
namespace py = pybind11; | ||
|
||
struct BuildSegmentToOrthMapVisitor : public Bliss::CorpusVisitor { | ||
BuildSegmentToOrthMapVisitor() | ||
: Bliss::CorpusVisitor(), map_(new Core::StringHashMap<std::string>()) {} | ||
|
||
virtual void visitSpeechSegment(Bliss::SpeechSegment* s) { | ||
(*map_)[s->fullName()] = s->orth(); | ||
} | ||
|
||
std::shared_ptr<Core::StringHashMap<std::string>> map_; | ||
}; | ||
|
||
static std::shared_ptr<Core::StringHashMap<std::string>> build_segment_to_orth_map(Core::Configuration const& config) { | ||
Bliss::CorpusDescription corpus(config); | ||
BuildSegmentToOrthMapVisitor visitor; | ||
corpus.accept(&visitor); | ||
return visitor.map_; | ||
} | ||
|
||
AllophoneStateFsaBuilder::AllophoneStateFsaBuilder(const Core::Configuration& c) | ||
: Core::Component(c), | ||
allophoneStateFsaExporter_(nullptr), | ||
segmentToOrthMap_(nullptr) { | ||
// init exporter and orth map | ||
allophoneStateFsaExporter_ = std::make_shared<Nn::AllophoneStateFsaExporter>(select("alignment-fsa-exporter")); | ||
segmentToOrthMap_ = build_segment_to_orth_map(select("corpus")); | ||
} | ||
|
||
py::tuple AllophoneStateFsaBuilder::buildBySegmentName(const std::string& segmentName) { | ||
auto iter = segmentToOrthMap_->find(segmentName); | ||
if (iter == segmentToOrthMap_->end()) { | ||
throw std::invalid_argument("Could not find segment with name " + segmentName); | ||
} | ||
return buildByOrthography(iter->second); | ||
} | ||
|
||
py::tuple AllophoneStateFsaBuilder::buildByOrthography(const std::string& orthography) { | ||
Nn::AllophoneStateFsaExporter::ExportedAutomaton automaton = allophoneStateFsaExporter_->exportFsaForOrthography(orthography); | ||
return py::make_tuple( | ||
automaton.num_states, | ||
automaton.num_edges, | ||
py::array_t<u32>(automaton.edges.size(), automaton.edges.data()), | ||
py::array_t<f32>(automaton.weights.size(), automaton.weights.data()) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#ifndef _PYTHON_ALLOPHONESTATEFSABUILDER_HH | ||
#define _PYTHON_ALLOPHONESTATEFSABUILDER_HH | ||
|
||
#include <pybind11/pybind11.h> | ||
|
||
#include <Core/Application.hh> | ||
#include <Core/Archive.hh> | ||
#include <Core/Configuration.hh> | ||
#include <Nn/AllophoneStateFsaExporter.hh> | ||
|
||
namespace py = pybind11; | ||
|
||
class AllophoneStateFsaBuilder : public Core::Component { | ||
public: | ||
AllophoneStateFsaBuilder(const Core::Configuration& c); | ||
~AllophoneStateFsaBuilder() = default; | ||
|
||
py::tuple buildBySegmentName(const std::string& segmentName); | ||
|
||
py::tuple buildByOrthography(const std::string& orthography); | ||
|
||
private: | ||
std::shared_ptr<Nn::AllophoneStateFsaExporter> allophoneStateFsaExporter_; | ||
std::shared_ptr<Core::StringHashMap<std::string>> segmentToOrthMap_; | ||
|
||
}; | ||
|
||
#endif // _PYTHON_ALLOPHONESTATEFSABUILDER_HH |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#include "Configuration.hh" | ||
|
||
#include <Core/Configuration.hh> | ||
|
||
PyConfiguration::PyConfiguration() | ||
: Core::Configuration() { | ||
setSelection("lib-rasr"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#ifndef _PYTHON_CONFIGURATION_HH | ||
#define _PYTHON_CONFIGURATION_HH | ||
|
||
#include <Core/Configuration.hh> | ||
|
||
class PyConfiguration : public Core::Configuration { | ||
public: | ||
PyConfiguration(); | ||
}; | ||
|
||
#endif // _PYTHON_CONFIGURATION_HH |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.