| 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, Table, 1); |
| 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 (me.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 (me.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 (me.get(), ifile, 2, annotationFileName.string); |
| 66 | } |
| 67 | return me; |
| 68 | } |
| 69 | |
| 70 | autoCorpus Corpus_readFromCGN (conststring32 rootFolderPath) { |
| 71 | autoCorpus me = Thing_new (Corpus); |
| 72 | const conststring32 columnNames_array [] = { U"comp", U"region", U"data" }; |
| 73 | Table_initWithColumnNames (me.get(), 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 ", MelderFolder_messageName (& rootFolder), U" does not exist."); |
| 79 | |
| 80 | autoSTRVEC compFolderNames = folderNames_STRVEC (Melder_cat (MelderFolder_peekPath (& rootFolder), U"/comp-*")); |
| 81 | Melder_require (compFolderNames.size > 0, |
| 82 | U"The folder ", MelderFolder_messageName (& rootFolder), U" contains no folders whose names start with “comp-”."); |
| 83 | |
| 84 | const conststring32 regionNames_array [] = { U"vl", U"nl" }; |
| 85 | const constSTRVEC regionNames = ARRAY_TO_STRVEC (regionNames_array); |
| 86 | for (integer icomp = 1; icomp <= compFolderNames.size; icomp ++) { |
| 87 | structMelderFolder compFolder { }; |
| 88 | MelderFolder_getSubfolder (& rootFolder, compFolderNames [icomp].get(), & compFolder); |
| 89 | for (integer iregion = 1; iregion <= regionNames.size; iregion ++) { |
| 90 | structMelderFolder regionFolder { }; |
| 91 | MelderFolder_getSubfolder (& compFolder, regionNames [iregion], & regionFolder); |
| 92 | if (MelderFolder_exists (& regionFolder)) { // allow partial corpora |
| 93 | autoSTRVEC soundFileNames = fileNames_STRVEC (Melder_cat (MelderFolder_peekPath (& regionFolder), U"/*.wav")); |
| 94 | const conststring32 brokenFiles [] = { |
| 95 | U"fn000483.wav", // zeroes, |
| 96 | U"fv701103.wav" // missing ort |
| 97 | }; |
| 98 | for (integer ifile = 1; ifile <= soundFileNames.size; ifile ++) if (! NUMfindFirst (ARRAY_TO_STRVEC (brokenFiles), soundFileNames [ifile].get())) { |
| 99 | TRACE |
| 100 | trace (U"Reading file ", soundFileNames [ifile].get()); |
| 101 | autoTextGrid textGrid; |
| 102 | autoSound sound; |
| 103 | try { |
| 104 | textGrid = TextGrid_Sound_readFromCorpusGesprokenNederlands (Melder_cat (MelderFolder_peekPath (& regionFolder), U"/", soundFileNames [ifile].get()), nullptr); |
| 105 | Table_appendRow (me.get()); |
| 106 | Table_setStringValue (me.get(), my rows.size, 1, compFolderNames [icomp].get()); |
| 107 | Table_setStringValue (me.get(), my rows.size, 2, regionNames [iregion]); |
| 108 | Table_setStringValue (me.get(), my rows.size, 3, soundFileNames [ifile].get()); |
| 109 | my textGrids. addItem_move (textGrid.move()); |
| 110 | } catch (MelderError) { |
| 111 | Melder_clearError (); |
| 112 | trace (U"Error handling file ", soundFileNames [ifile].get()); |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | return me; |
| 119 | } |
| 120 | |
| 121 | /* End of file Corpus.cpp */ |