| 1 |
/* Corpus.cpp |
| 2 |
* |
| 3 |
* Copyright (C) 2011,2016,2018,2020,2021,2026 Paul Boersma |
| 4 |
* |
| 5 |
* This code is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 3 of the License, or (at |
| 8 |
* your option) any later version. |
| 9 |
* |
| 10 |
* This code is distributed in the hope that it will be useful, but |
| 11 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
* See the GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
*/ |
| 18 |
|
| 19 |
#include "Corpus.h" |
| 20 |
#include "TextGrid_Sound.h" |
| 21 |
|
| 22 |
#include "oo_DESTROY.h" |
| 23 |
#include "Corpus_def.h" |
| 24 |
#include "oo_COPY.h" |
| 25 |
#include "Corpus_def.h" |
| 26 |
#include "oo_EQUAL.h" |
| 27 |
#include "Corpus_def.h" |
| 28 |
#include "oo_CAN_WRITE_AS_ENCODING.h" |
| 29 |
#include "Corpus_def.h" |
| 30 |
#include "oo_WRITE_TEXT.h" |
| 31 |
#include "Corpus_def.h" |
| 32 |
#include "oo_READ_TEXT.h" |
| 33 |
#include "Corpus_def.h" |
| 34 |
#include "oo_WRITE_BINARY.h" |
| 35 |
#include "Corpus_def.h" |
| 36 |
#include "oo_READ_BINARY.h" |
| 37 |
#include "Corpus_def.h" |
| 38 |
#include "oo_DESCRIPTION.h" |
| 39 |
#include "Corpus_def.h" |
| 40 |
|
| 41 |
Thing_implement (Corpus, Daata, 0); |
| 42 |
|
| 43 |
autoCorpus Corpus_create (conststring32 folderWithSoundFiles, conststring32 soundFileExtension, |
| 44 |
conststring32 folderWithAnnotationFiles, conststring32 annotationFileExtension) |
| 45 |
{ |
| 46 |
autoCorpus me = Thing_new (Corpus); |
| 47 |
my folderWithSoundFiles = Melder_dup (folderWithSoundFiles); |
| 48 |
if (folderWithAnnotationFiles [0] == U'\0') |
| 49 |
folderWithAnnotationFiles = folderWithSoundFiles; |
| 50 |
my folderWithAnnotationFiles = Melder_dup (folderWithAnnotationFiles); |
| 51 |
autoSTRVEC fileList = fileNames_STRVEC (Melder_cat (folderWithSoundFiles, U"/*.", soundFileExtension)); |
| 52 |
Table_initWithColumnNames (my recordings.get(), fileList.size, |
| 53 |
autoSTRVEC ({ U"Sound", U"Annotation" }).get()); |
| 54 |
autoMelderString annotationFileName; |
| 55 |
for (integer ifile = 1; ifile <= fileList.size; ifile ++) { |
| 56 |
conststring32 soundFileName = fileList [ifile].get(); |
| 57 |
Table_setStringValue (my recordings.get(), ifile, 1, soundFileName); |
| 58 |
const char32 *dotLocation = str32rchr (soundFileName, U'.'); |
| 59 |
Melder_assert (!! dotLocation); |
| 60 |
MelderString_ncopy (& annotationFileName, soundFileName, dotLocation - soundFileName + 1); |
| 61 |
MelderString_append (& annotationFileName, annotationFileExtension); |
| 62 |
structMelderFile annotationFile { }; |
| 63 |
Melder_pathToFile (Melder_cat (folderWithAnnotationFiles, U"/", annotationFileName.string), & annotationFile); |
| 64 |
if (MelderFile_exists (& annotationFile)) |
| 65 |
Table_setStringValue (my recordings.get(), ifile, 2, annotationFileName.string); |
| 66 |
} |
| 67 |
return me; |
| 68 |
} |
| 69 |
|
| 70 |
autoCorpus Corpus_importFromCGN (conststring32 rootFolderPath) { |
| 71 |
autoCorpus me = Thing_new (Corpus); |
| 72 |
const conststring32 columnNames_array [] = { U"component", U"region", U"recording" }; |
| 73 |
my recordings = Table_createWithColumnNames (0, ARRAY_TO_STRVEC (columnNames_array)); |
| 74 |
|
| 75 |
structMelderFolder rootFolder { }; |
| 76 |
Melder_relativePathToFolder (rootFolderPath, & rootFolder); |
| 77 |
Melder_require (MelderFolder_exists (& rootFolder), |
| 78 |
U"CGN folder ", & rootFolder, U" does not exist."); |
| 79 |
|
| 80 |
structMelderFolder dataFolder { }; |
| 81 |
MelderFolder_getSubfolder (& rootFolder, U"data", & dataFolder); |
| 82 |
Melder_require (MelderFolder_exists (& dataFolder), |
| 83 |
U"CGN folder ", & dataFolder, U" does not exist."); |
| 84 |
|
| 85 |
structMelderFolder audioFolder { }; |
| 86 |
MelderFolder_getSubfolder (& dataFolder, U"audio", & audioFolder); |
| 87 |
Melder_require (MelderFolder_exists (& audioFolder), |
| 88 |
U"CGN folder ", & audioFolder, U" does not exist."); |
| 89 |
|
| 90 |
structMelderFolder wavFolder { }; |
| 91 |
MelderFolder_getSubfolder (& audioFolder, U"wav", & wavFolder); |
| 92 |
Melder_require (MelderFolder_exists (& wavFolder), |
| 93 |
U"CGN folder ", & wavFolder, U" does not exist."); |
| 94 |
|
| 95 |
my folderWithSoundFiles = Melder_dup (MelderFolder_peekPath (& wavFolder)); |
| 96 |
|
| 97 |
autoSTRVEC compFolderNames = folderNames_STRVEC (Melder_cat (MelderFolder_peekPath (& wavFolder), U"/comp-*")); |
| 98 |
Melder_require (compFolderNames.size > 0, |
| 99 |
U"The folder ", MelderFolder_messageName (& rootFolder), U" contains no folders whose names start with “comp-”."); |
| 100 |
|
| 101 |
const conststring32 regionNames_array [] = { U"vl", U"nl" }; |
| 102 |
const constSTRVEC regionNames = ARRAY_TO_STRVEC (regionNames_array); |
| 103 |
for (integer icomp = 1; icomp <= compFolderNames.size; icomp ++) { |
| 104 |
structMelderFolder compFolder { }; |
| 105 |
MelderFolder_getSubfolder (& wavFolder, compFolderNames [icomp].get(), & compFolder); |
| 106 |
for (integer iregion = 1; iregion <= regionNames.size; iregion ++) { |
| 107 |
structMelderFolder regionFolder { }; |
| 108 |
MelderFolder_getSubfolder (& compFolder, regionNames [iregion], & regionFolder); |
| 109 |
if (MelderFolder_exists (& regionFolder)) { // allow partial corpora |
| 110 |
autoSTRVEC soundFileNames = fileNames_STRVEC (Melder_cat (MelderFolder_peekPath (& regionFolder), U"/*.wav")); |
| 111 |
for (integer ifile = 1; ifile <= soundFileNames.size; ifile ++) { |
| 112 |
//TRACE |
| 113 |
trace (U"Reading file ", soundFileNames [ifile].get()); |
| 114 |
autoTextGrid textGrid; |
| 115 |
autoSound sound; |
| 116 |
try { |
| 117 |
structMelderFile soundFile { }; |
| 118 |
MelderFolder_getFile (& regionFolder, soundFileNames [ifile].get(), & soundFile); |
| 119 |
textGrid = TextGrid_Sound_readFromCorpusGesprokenNederlands (MelderFile_peekPath (& soundFile), nullptr); |
| 120 |
Table_appendRow (my recordings.get()); |
| 121 |
Table_setStringValue (my recordings.get(), my recordings -> rows.size, 1, compFolderNames [icomp].get()); |
| 122 |
Table_setStringValue (my recordings.get(), my recordings -> rows.size, 2, regionNames [iregion]); |
| 123 |
Table_setStringValue (my recordings.get(), my recordings -> rows.size, 3, soundFileNames [ifile].get()); |
| 124 |
my textGrids. addItem_move (textGrid.move()); |
| 125 |
} catch (MelderError) { |
| 126 |
Melder_clearError (); |
| 127 |
trace (U"Error handling file ", soundFileNames [ifile].get()); |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
structMelderFolder metadataFolder { }; |
| 135 |
MelderFolder_getSubfolder (& dataFolder, U"meta", & metadataFolder); |
| 136 |
Melder_require (MelderFolder_exists (& metadataFolder), |
| 137 |
U"CGN folder ", & metadataFolder, U" does not exist."); |
| 138 |
|
| 139 |
structMelderFolder metadataTextFolder { }; |
| 140 |
MelderFolder_getSubfolder (& metadataFolder, U"text", & metadataTextFolder); |
| 141 |
Melder_require (MelderFolder_exists (& metadataTextFolder), |
| 142 |
U"CGN folder ", & metadataTextFolder, U" does not exist."); |
| 143 |
|
| 144 |
structMelderFile speakersFile { }; |
| 145 |
MelderFolder_getFile (& metadataTextFolder, U"speakers.txt", & speakersFile); |
| 146 |
Melder_require (MelderFile_exists (& speakersFile), |
| 147 |
U"CGN file ", & speakersFile, U" does not exist."); |
| 148 |
|
| 149 |
my speakers = Table_readFromCharacterSeparatedTextFile (& speakersFile, U'\t', false); |
| 150 |
return me; |
| 151 |
} |
| 152 |
|
| 153 |
autoTextGrid Corpus_extractTextGrid_number (Corpus me, integer recordingNumber) { |
| 154 |
Melder_require (recordingNumber >= 1, |
| 155 |
U"The recording number (", recordingNumber, U") should be at least 1."); |
| 156 |
Melder_require (recordingNumber <= my recordings -> rows.size, |
| 157 |
U"The recording number (", recordingNumber, U") should be at most the number of recordings (", my recordings -> rows.size, U")."); |
| 158 |
autoTextGrid thee = Data_copy (my textGrids.at [recordingNumber]); |
| 159 |
Thing_setName (thee.get(), Table_getStringValue_a (my recordings.get(), recordingNumber, 3)); |
| 160 |
return thee; |
| 161 |
} |
| 162 |
|
| 163 |
autoSound Corpus_extractSound_number (Corpus me, integer recordingNumber) { |
| 164 |
Melder_require (recordingNumber >= 1, |
| 165 |
U"The recording number (", recordingNumber, U") should be at least 1."); |
| 166 |
Melder_require (recordingNumber <= my recordings -> rows.size, |
| 167 |
U"The recording number (", recordingNumber, U") should be at most the number of recordings (", my recordings -> rows.size, U")."); |
| 168 |
structMelderFolder wavFolder { }; |
| 169 |
Melder_relativePathToFolder (my folderWithSoundFiles.get(), & wavFolder); |
| 170 |
structMelderFolder compFolder { }; |
| 171 |
MelderFolder_getSubfolder (& wavFolder, Table_getStringValue_a (my recordings.get(), recordingNumber, 1), & compFolder); |
| 172 |
structMelderFolder countryFolder { }; |
| 173 |
MelderFolder_getSubfolder (& compFolder, Table_getStringValue_a (my recordings.get(), recordingNumber, 2), & countryFolder); |
| 174 |
structMelderFile soundFile { }; |
| 175 |
MelderFolder_getFile (& countryFolder, Table_getStringValue_a (my recordings.get(), recordingNumber, 3), & soundFile); |
| 176 |
autoSound thee = Sound_readFromSoundFile (& soundFile); |
| 177 |
Thing_setName (thee.get(), Table_getStringValue_a (my recordings.get(), recordingNumber, 3)); |
| 178 |
return thee; |
| 179 |
} |
| 180 |
|
| 181 |
autoCorpus Corpus_extractComponents (Corpus me, |
| 182 |
bool comp_a, bool comp_b, bool comp_c, bool comp_d, bool comp_e, |
| 183 |
bool comp_f, bool comp_g, bool comp_h, bool comp_i, bool comp_j, |
| 184 |
bool comp_k, bool comp_l, bool comp_m, bool comp_n, bool comp_o |
| 185 |
) { |
| 186 |
bool components [15] { |
| 187 |
comp_a, comp_b, comp_c, comp_d, comp_e, |
| 188 |
comp_f, comp_g, comp_h, comp_i, comp_j, |
| 189 |
comp_k, comp_l, comp_m, comp_n, comp_o |
| 190 |
}; |
| 191 |
autoCorpus thee = Data_copy (me); |
| 192 |
for (integer row = thy recordings -> rows.size; row >= 1; row --) { |
| 193 |
conststring32 component = Table_getStringValue_a (thy recordings.get(), row, 1); |
| 194 |
Melder_require (Melder_stringMatchesCriterion (component, kMelder_string::STARTS_WITH, U"comp-", true), |
| 195 |
U"Component ", component, U" does't start with “comp-”."); |
| 196 |
const char32 componentLetter = component [5]; |
| 197 |
Melder_require (componentLetter >= U'a' && componentLetter <= U'o', |
| 198 |
U"The sixth character of component ", component, U" should have been between “a” and “o”."); |
| 199 |
if (! components [componentLetter - U'a']) { |
| 200 |
Table_removeRow (thy recordings.get(), row); |
| 201 |
thy textGrids. removeItem (row); |
| 202 |
} |
| 203 |
} |
| 204 |
return thee; |
| 205 |
} |
| 206 |
|
| 207 |
autoCorpus Corpus_extractEducationRegions (Corpus me, |
| 208 |
bool sHolland, bool nHolland, bool wUtrecht, bool zeeland, bool eUtrecht, |
| 209 |
bool guelders, bool veluwe, bool wFriesland, bool polders, bool achterhoek, |
| 210 |
bool overijssel, bool drenthe, bool groningen, bool frisia, bool nBrabant, |
| 211 |
bool eLimburg, bool netherlands, bool sBrabant, bool eFlanders, bool wFlanders, |
| 212 |
bool wLimburg, bool nBelgium, bool sBelgium, bool reg_other, bool reg_unknown |
| 213 |
) { |
| 214 |
bool regions [25] { |
| 215 |
sHolland, nHolland, wUtrecht, zeeland, eUtrecht, |
| 216 |
guelders, veluwe, wFriesland, polders, achterhoek, |
| 217 |
overijssel, drenthe, groningen, frisia, nBrabant, |
| 218 |
eLimburg, netherlands, sBrabant, eFlanders, wFlanders, |
| 219 |
wLimburg, nBelgium, sBelgium, reg_other, reg_unknown |
| 220 |
}; |
| 221 |
autoCorpus thee = Data_copy (me); |
| 222 |
for (integer row = thy recordings -> rows.size; row >= 1; row --) { |
| 223 |
TextGrid textgrid = thy textGrids.at [row]; |
| 224 |
bool foundMatch = false; |
| 225 |
for (integer itier = 1; itier <= textgrid -> tiers->size; itier ++) { |
| 226 |
IntervalTier tier = (IntervalTier) textgrid -> tiers->at [itier]; // TODO: check cast |
| 227 |
conststring32 tierName = tier -> name.get(); |
| 228 |
if (Melder_stringMatchesCriterion (tierName, kMelder_string::STARTS_WITH, U"ort/", true)) { |
| 229 |
const conststring32 speakerName = & tier -> name [4]; |
| 230 |
integer speakerRow = Table_searchColumn (thy speakers.get(), 5, speakerName); |
| 231 |
if (speakerRow == 0) { |
| 232 |
if (! str32equ (speakerName, U"BACKGROUND") && |
| 233 |
! str32equ (speakerName, U"COMMENT") && |
| 234 |
! str32equ (speakerName, U"UNKNOWN") && |
| 235 |
! str32equ (speakerName, U"N00383")) |
| 236 |
Melder_crash (U"<<", speakerName, U">> <<", tierName, U">>"); |
| 237 |
} else { |
| 238 |
conststring32 speakerRegionCode = Table_getStringValue_a (thy speakers.get(), speakerRow, 17); |
| 239 |
integer icode = 0; |
| 240 |
for (; icode < 25; icode ++) |
| 241 |
if (str32equ (speakerRegionCode, Corpus_CGN_regionCodes [icode])) |
| 242 |
break; |
| 243 |
if (icode >= 25) { |
| 244 |
if (str32equ (speakerRegionCode, U"regB")) |
| 245 |
icode = 24; |
| 246 |
} |
| 247 |
if (icode >= 25) |
| 248 |
Melder_crash (icode, U" <<", speakerRegionCode, U">>"); |
| 249 |
if (regions [icode]) { |
| 250 |
foundMatch = true; |
| 251 |
break; |
| 252 |
} |
| 253 |
} |
| 254 |
} |
| 255 |
} |
| 256 |
if (! foundMatch) { |
| 257 |
/* |
| 258 |
Remove this TextGrid and Sound. |
| 259 |
*/ |
| 260 |
thy textGrids. removeItem (row); |
| 261 |
Table_removeRow (thy recordings.get(), row); |
| 262 |
} |
| 263 |
} |
| 264 |
return thee; |
| 265 |
} |
| 266 |
|
| 267 |
/* End of file Corpus.cpp */ |