30 using namespace kaldi;
35 "Computes alignment between two sentences with the same key in the\n" 36 "two given input text-rspecifiers. The current implementation uses\n" 37 "Levenshtein distance as the distance metric.\n" 39 "The input text file looks like follows:\n" 43 "The output alignment file looks like follows:\n" 44 " key1 a a ; b <eps> ; c c \n" 46 "where the aligned pairs are separated by \";\"\n" 48 "Usage: align-text [options] <text1-rspecifier> <text2-rspecifier> \\\n" 49 " <alignment-wspecifier>\n" 50 " e.g.: align-text ark:text1.txt ark:text2.txt ark,t:alignment.txt\n" 51 "See also: compute-wer,\n" 52 "Example scoring script: egs/wsj/s5/steps/score_kaldi.sh\n";
56 std::string special_symbol =
"<eps>";
57 std::string separator =
";";
58 po.Register(
"special-symbol", &special_symbol,
"Special symbol to be " 59 "aligned with the inserted or deleted words. Your sentences " 60 "should not contain this symbol.");
61 po.Register(
"separator", &separator,
"Separator for each aligned pair in " 62 "the output alignment file. Note: it should not be necessary " 63 "to change this even if your sentences contain ';', because " 64 "to parse the output of this program you can just split on " 65 "space and then assert that every third token is ';'.");
69 if (po.NumArgs() != 3) {
74 std::string text1_rspecifier = po.GetArg(1),
75 text2_rspecifier = po.GetArg(2),
76 align_wspecifier = po.GetArg(3);
84 for (; !text1_reader.Done(); text1_reader.Next()) {
85 std::string key = text1_reader.Key();
87 if (!text2_reader.HasKey(key)) {
88 KALDI_WARN <<
"Key " << key <<
" is in " << text1_rspecifier
89 <<
", but not in " << text2_rspecifier;
93 const std::vector<std::string> &text1 = text1_reader.Value();
94 const std::vector<std::string> &text2 = text2_reader.Value(key);
96 if (std::find_if(text1.begin(), text1.end(),
IsNotToken) != text1.end()) {
97 KALDI_ERR <<
"In text1, the utterance " << key
98 <<
" contains unprintable characters. That means there is" 99 <<
" a problem with the text (such as incorrect encoding).";
101 if (std::find_if(text2.begin(), text2.end(),
IsNotToken) != text2.end()) {
102 KALDI_ERR <<
"In text2, the utterance " << key
103 <<
" contains unprintable characters. That means there is" 104 <<
" a problem with the text (such as incorrect encoding).";
108 if (std::find(text1.begin(), text1.end(), special_symbol) != text1.end()){
109 KALDI_ERR <<
"In text1, the utterance " << key
110 <<
" contains the special symbol '" << special_symbol
111 <<
"'. This is not allowed.";
113 if (std::find(text2.begin(), text2.end(), special_symbol) != text2.end()){
114 KALDI_ERR <<
"In text2, the utterance " << key
115 <<
" contains the special symbol '" << special_symbol
116 <<
"'. This is not allowed.";
119 std::vector<std::pair<std::string, std::string> > aligned;
122 std::vector<std::string> token_vec;
123 std::vector<std::pair<std::string, std::string> >::const_iterator iter;
124 for (iter = aligned.begin(); iter != aligned.end(); ++iter) {
125 token_vec.push_back(iter->first);
126 token_vec.push_back(iter->second);
127 if (aligned.end() - iter != 1)
128 token_vec.push_back(separator);
130 align_writer.Write(key, token_vec);
135 KALDI_LOG <<
"Done " << n_done <<
" sentences, failed for " << n_fail;
136 return (n_done != 0 ? 0 : 1);
137 }
catch(
const std::exception &e) {
138 std::cerr << e.what();
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
int32 LevenshteinAlignment(const std::vector< T > &a, const std::vector< T > &b, T eps_symbol, std::vector< std::pair< T, T > > *output)
A templated class for writing objects to an archive or script file; see The Table concept...
Allows random access to a collection of objects in an archive or script file; see The Table concept...
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
bool IsNotToken(const std::string &token)