示例#1
0
autoConfusion Confusion_condense (Confusion me, const char32 *search, const char32 *replace,
	long maximumNumberOfReplaces, int use_regexp) {
	try {
		long nmatches, nstringmatches;

		if (my rowLabels == 0 || my columnLabels == 0) {
			Melder_throw (U"No row or column labels.");
		}
		autostring32vector rowLabels (strs_replace (my rowLabels, 1, my numberOfRows, search, replace,
			maximumNumberOfReplaces, &nmatches, &nstringmatches, use_regexp), 1, my numberOfRows);

		autostring32vector columnLabels (strs_replace (my columnLabels, 1, my numberOfColumns,  search, replace,
			 maximumNumberOfReplaces, &nmatches, &nstringmatches, use_regexp), 1, my numberOfColumns);

		autoStrings srow = Thing_new (Strings);
		srow -> numberOfStrings = my numberOfRows;
		srow -> strings = rowLabels.transfer();

		autoStrings scol = Thing_new (Strings);
		scol -> numberOfStrings = my numberOfColumns;
		scol -> strings = columnLabels.transfer();

		/* Find dimension of new Confusion */
		autoDistributions dcol = Strings_to_Distributions (scol.get());
		long nresp = dcol -> numberOfRows;

		autoDistributions drow = Strings_to_Distributions (srow.get());
		long nstim = drow -> numberOfRows;

		autoConfusion thee = Confusion_create (nstim, nresp);

		NUMstrings_copyElements (drow -> rowLabels, thy rowLabels, 1, nstim);
		NUMstrings_copyElements (dcol -> rowLabels, thy columnLabels, 1, nresp);

		autoNUMvector<long> rowIndex (1, my numberOfRows);
		create_index (srow -> strings, 1, my numberOfRows, drow -> rowLabels, 1, nstim, rowIndex.peek());
		autoNUMvector<long> columnIndex (1, my numberOfColumns);
		create_index (scol -> strings, 1, my numberOfColumns, dcol -> rowLabels, 1, nresp, columnIndex.peek());

		for (long i = 1; i <= my numberOfRows; i++) {
			for (long j = 1; j <= my numberOfColumns; j++) {
				thy data [rowIndex [i]][columnIndex[j]] += my data[i][j];
			}
		}
		return thee;
	} catch (MelderError) {
		Melder_throw (me, U": not condensed.");
	}
}
void TextTier_changeLabels (I, long from, long to, const char32 *search, const char32 *replace, int use_regexp, long *nmatches, long *nstringmatches) {
	iam (TextTier);
	try {
		if (from == 0) {
			from = 1;
		}
		if (to == 0) {
			to = my points -> size;
		}
		if (from > to || from < 1 || to > my points -> size) {
			Melder_throw (U"Incorrect specification of where to act.");
		}
		if (use_regexp && str32len (search) == 0) Melder_throw (U"The regex search string cannot be empty.\n"
			        U"You may search for an empty string with the expression \"^$\"");

		long nmarks = to - from + 1;
		autoNUMvector<char32 *> marks (1, nmarks);

		for (long i = from; i <= to; i++) {
			TextPoint point = (TextPoint) my points -> item[i];
			marks[i - from + 1] = point -> mark;   // Shallow copy.
		}
		autostring32vector newmarks (strs_replace (marks.peek(), 1, nmarks, search, replace, 0, nmatches, nstringmatches, use_regexp), 1, nmarks);

		for (long i = from; i <= to; i++) {
			TextPoint point = (TextPoint) my points -> item[i];
			Melder_free (point -> mark);
			point -> mark = newmarks[i - from + 1];   // Transfer of ownership.
			newmarks[i - from + 1] = 0;
		}
	} catch (MelderError) {
		Melder_throw (me, U": no labels changed.");
	}
}
void IntervalTier_changeLabels (I, long from, long to, const char32 *search, const char32 *replace, int use_regexp, long *nmatches, long *nstringmatches) {
	iam (IntervalTier);
	try {
		if (from == 0) {
			from = 1;
		}
		if (to == 0) {
			to = my intervals -> size;
		}
		if (from > to || from < 1 || to > my intervals -> size) {
			Melder_throw (U"Incorrect specification of where to act.");
		}
		if (use_regexp && str32len (search) == 0) Melder_throw (U"The regex search string cannot be empty.\n"
			        U"You may search for an empty string with the expression \"^$\"");

		long nlabels = to - from + 1;
		autoNUMvector<char32 *> labels (1, nlabels);

		for (long i = from; i <= to; i++) {
			TextInterval interval = (TextInterval) my intervals -> item[i];
			labels[i - from + 1] = interval -> text;   // Shallow copy.
		}
		autostring32vector newlabels (strs_replace (labels.peek(), 1, nlabels, search, replace, 0, nmatches, nstringmatches, use_regexp), 1, nlabels);

		for (long i = from; i <= to; i++) {
			TextInterval interval = (TextInterval) my intervals -> item[i];
			Melder_free (interval -> text);
			interval -> text = newlabels[i - from + 1];   // Transfer of ownership.
			newlabels[i - from + 1] = 0;
		}
	} catch (MelderError) {
		Melder_throw (me, U": labels not changed.");
	}
}
autoStrings Strings_change (Strings me, const char32 *search, const char32 *replace, int maximumNumberOfReplaces, long *nmatches, long *nstringmatches, int use_regexp) {
	try {
		autoStrings thee = Thing_new (Strings);
		char32 **strings = strs_replace (my strings, 1, my numberOfStrings, search, replace, maximumNumberOfReplaces,
		                                  nmatches, nstringmatches, use_regexp);
		thy numberOfStrings = my numberOfStrings;
		thy strings = strings;
		return thee;
	} catch (MelderError) {
		Melder_throw (me, U": not changed.");
	}
}