ter.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. # Copyright The Lightning team.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # referenced from
  15. # Library Name: torchtext
  16. # Authors: torchtext authors
  17. # Date: 2021-11-30
  18. # Link:
  19. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  20. # Copyright 2020 Memsource
  21. #
  22. # Licensed under the Apache License, Version 2.0 (the "License");
  23. # you may not use this file except in compliance with the License.
  24. # You may obtain a copy of the License at
  25. #
  26. # http://www.apache.org/licenses/LICENSE-2.0
  27. #
  28. # Unless required by applicable law or agreed to in writing, software
  29. # distributed under the License is distributed on an "AS IS" BASIS,
  30. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  31. # See the License for the specific language governing permissions and
  32. # limitations under the License.
  33. import re
  34. from collections.abc import Iterator, Sequence
  35. from functools import lru_cache
  36. from typing import List, Optional, Union
  37. from torch import Tensor, tensor
  38. from torchmetrics.functional.text.helper import (
  39. _flip_trace,
  40. _LevenshteinEditDistance,
  41. _trace_to_alignment,
  42. _validate_inputs,
  43. )
  44. # Tercom-inspired limits
  45. _MAX_SHIFT_SIZE = 10
  46. _MAX_SHIFT_DIST = 50
  47. # Sacrebleu-inspired limits
  48. _MAX_SHIFT_CANDIDATES = 1000
  49. class _TercomTokenizer:
  50. """Re-implementation of Tercom Tokenizer in Python 3.
  51. See src/ter/core/Normalizer.java in https://github.com/jhclark/tercom Note that Python doesn't support named Unicode
  52. blocks so the mapping for relevant blocks was taken from here: https://unicode-table.com/en/blocks/
  53. This implementation follows the implementation from
  54. https://github.com/mjpost/sacrebleu/blob/master/sacrebleu/tokenizers/tokenizer_ter.py.
  55. """
  56. _ASIAN_PUNCTUATION = r"([\u3001\u3002\u3008-\u3011\u3014-\u301f\uff61-\uff65\u30fb])"
  57. _FULL_WIDTH_PUNCTUATION = r"([\uff0e\uff0c\uff1f\uff1a\uff1b\uff01\uff02\uff08\uff09])"
  58. def __init__(
  59. self,
  60. normalize: bool = False,
  61. no_punctuation: bool = False,
  62. lowercase: bool = True,
  63. asian_support: bool = False,
  64. ) -> None:
  65. """Initialize the tokenizer.
  66. Args:
  67. normalize: An indication whether a general tokenization to be applied.
  68. no_punctuation: An indication whteher a punctuation to be removed from the sentences.
  69. lowercase: An indication whether to enable case-insensitivity.
  70. asian_support: An indication whether asian characters to be processed.
  71. """
  72. self.normalize = normalize
  73. self.no_punctuation = no_punctuation
  74. self.lowercase = lowercase
  75. self.asian_support = asian_support
  76. @lru_cache(maxsize=2**16) # noqa: B019
  77. def __call__(self, sentence: str) -> str:
  78. """Apply a different tokenization techniques according.
  79. Args:
  80. sentence: An input sentence to pre-process and tokenize.
  81. Return:
  82. A tokenized and pre-processed sentence.
  83. """
  84. if not sentence:
  85. return ""
  86. if self.lowercase:
  87. sentence = sentence.lower()
  88. if self.normalize:
  89. sentence = self._normalize_general_and_western(sentence)
  90. if self.asian_support:
  91. sentence = self._normalize_asian(sentence)
  92. if self.no_punctuation:
  93. sentence = self._remove_punct(sentence)
  94. if self.asian_support:
  95. sentence = self._remove_asian_punct(sentence)
  96. # Strip extra whitespaces
  97. return " ".join(sentence.split())
  98. @staticmethod
  99. def _normalize_general_and_western(sentence: str) -> str:
  100. """Apply a language-independent (general) tokenization."""
  101. sentence = f" {sentence} "
  102. rules = [
  103. (r"\n-", ""),
  104. # join lines
  105. (r"\n", " "),
  106. # handle XML escaped symbols
  107. (r""", '"'),
  108. (r"&", "&"),
  109. (r"&lt;", "<"),
  110. (r"&gt;", ">"),
  111. # tokenize punctuation
  112. (r"([{-~[-` -&(-+:-@/])", r" \1 "),
  113. # handle possessive
  114. (r"'s ", r" 's "),
  115. (r"'s$", r" 's"),
  116. # tokenize period and comma unless preceded by a digit
  117. (r"([^0-9])([\.,])", r"\1 \2 "),
  118. # tokenize period and comma unless followed by a digit
  119. (r"([\.,])([^0-9])", r" \1 \2"),
  120. # tokenize dash when preceded by a digit
  121. (r"([0-9])(-)", r"\1 \2 "),
  122. ]
  123. for pattern, replacement in rules:
  124. sentence = re.sub(pattern, replacement, sentence)
  125. return sentence
  126. @classmethod
  127. def _normalize_asian(cls: type["_TercomTokenizer"], sentence: str) -> str:
  128. """Split Chinese chars and Japanese kanji down to character level."""
  129. # 4E00—9FFF CJK Unified Ideographs
  130. # 3400—4DBF CJK Unified Ideographs Extension A
  131. sentence = re.sub(r"([\u4e00-\u9fff\u3400-\u4dbf])", r" \1 ", sentence)
  132. # 31C0—31EF CJK Strokes
  133. # 2E80—2EFF CJK Radicals Supplement
  134. sentence = re.sub(r"([\u31c0-\u31ef\u2e80-\u2eff])", r" \1 ", sentence)
  135. # 3300—33FF CJK Compatibility
  136. # F900—FAFF CJK Compatibility Ideographs
  137. # FE30—FE4F CJK Compatibility Forms
  138. sentence = re.sub(r"([\u3300-\u33ff\uf900-\ufaff\ufe30-\ufe4f])", r" \1 ", sentence)
  139. # 3200—32FF Enclosed CJK Letters and Months
  140. sentence = re.sub(r"([\u3200-\u3f22])", r" \1 ", sentence)
  141. # Split Hiragana, Katakana, and KatakanaPhoneticExtensions
  142. # only when adjacent to something else
  143. # 3040—309F Hiragana
  144. # 30A0—30FF Katakana
  145. # 31F0—31FF Katakana Phonetic Extensions
  146. sentence = re.sub(r"(^|^[\u3040-\u309f])([\u3040-\u309f]+)(?=$|^[\u3040-\u309f])", r"\1 \2 ", sentence)
  147. sentence = re.sub(r"(^|^[\u30a0-\u30ff])([\u30a0-\u30ff]+)(?=$|^[\u30a0-\u30ff])", r"\1 \2 ", sentence)
  148. sentence = re.sub(r"(^|^[\u31f0-\u31ff])([\u31f0-\u31ff]+)(?=$|^[\u31f0-\u31ff])", r"\1 \2 ", sentence)
  149. sentence = re.sub(cls._ASIAN_PUNCTUATION, r" \1 ", sentence)
  150. return re.sub(cls._FULL_WIDTH_PUNCTUATION, r" \1 ", sentence)
  151. @staticmethod
  152. def _remove_punct(sentence: str) -> str:
  153. """Remove punctuation from an input sentence string."""
  154. return re.sub(r"[\.,\?:;!\"\(\)]", "", sentence)
  155. @classmethod
  156. def _remove_asian_punct(cls: type["_TercomTokenizer"], sentence: str) -> str:
  157. """Remove asian punctuation from an input sentence string."""
  158. sentence = re.sub(cls._ASIAN_PUNCTUATION, r"", sentence)
  159. return re.sub(cls._FULL_WIDTH_PUNCTUATION, r"", sentence)
  160. def _preprocess_sentence(sentence: str, tokenizer: _TercomTokenizer) -> str:
  161. """Given a sentence, apply tokenization.
  162. Args:
  163. sentence: The input sentence string.
  164. tokenizer: An instance of ``_TercomTokenizer`` handling a sentence tokenization.
  165. Return:
  166. The pre-processed output sentence string.
  167. """
  168. return tokenizer(sentence.rstrip())
  169. def _find_shifted_pairs(pred_words: list[str], target_words: list[str]) -> Iterator[tuple[int, int, int]]:
  170. """Find matching word sub-sequences in two lists of words. Ignores sub- sequences starting at the same position.
  171. Args:
  172. pred_words: A list of a tokenized hypothesis sentence.
  173. target_words: A list of a tokenized reference sentence.
  174. Return:
  175. Yields tuples of ``target_start, pred_start, length`` such that:
  176. ``target_words[target_start : target_start + length] == pred_words[pred_start : pred_start + length]``
  177. pred_start:
  178. A list of hypothesis start indices.
  179. target_start:
  180. A list of reference start indices.
  181. length:
  182. A length of a word span to be considered.
  183. """
  184. for pred_start in range(len(pred_words)):
  185. for target_start in range(len(target_words)):
  186. # this is slightly different from what tercom does but this should
  187. # really only kick in in degenerate cases
  188. if abs(target_start - pred_start) > _MAX_SHIFT_DIST:
  189. continue
  190. for length in range(1, _MAX_SHIFT_SIZE):
  191. # Check if hypothesis and reference are equal so far
  192. if pred_words[pred_start + length - 1] != target_words[target_start + length - 1]:
  193. break
  194. yield pred_start, target_start, length
  195. # Stop processing once a sequence is consumed.
  196. _hyp = len(pred_words) == pred_start + length
  197. _ref = len(target_words) == target_start + length
  198. if _hyp or _ref:
  199. break
  200. def _handle_corner_cases_during_shifting(
  201. alignments: dict[int, int],
  202. pred_errors: list[int],
  203. target_errors: list[int],
  204. pred_start: int,
  205. target_start: int,
  206. length: int,
  207. ) -> bool:
  208. """Return ``True`` if any of corner cases has been met. Otherwise, ``False`` is returned.
  209. Args:
  210. alignments: A dictionary mapping aligned positions between a reference and a hypothesis.
  211. pred_errors: A list of error positions in a hypothesis.
  212. target_errors: A list of error positions in a reference.
  213. pred_start: A hypothesis start index.
  214. target_start: A reference start index.
  215. length: A length of a word span to be considered.
  216. Return:
  217. An indication whether any of conrner cases has been met.
  218. """
  219. # don't do the shift unless both the hypothesis was wrong and the
  220. # reference doesn't match hypothesis at the target position
  221. if sum(pred_errors[pred_start : pred_start + length]) == 0:
  222. return True
  223. if sum(target_errors[target_start : target_start + length]) == 0:
  224. return True
  225. # don't try to shift within the subsequence
  226. return pred_start <= alignments[target_start] < pred_start + length
  227. def _perform_shift(words: list[str], start: int, length: int, target: int) -> list[str]:
  228. """Perform a shift in ``words`` from ``start`` to ``target``.
  229. Args:
  230. words: A words to shift.
  231. start: An index where to start shifting from.
  232. length: A number of how many words to be considered.
  233. target: An index where to end shifting.
  234. Return:
  235. A list of shifted words.
  236. """
  237. def _shift_word_before_previous_position(words: list[str], start: int, target: int, length: int) -> list[str]:
  238. return words[:target] + words[start : start + length] + words[target:start] + words[start + length :]
  239. def _shift_word_after_previous_position(words: list[str], start: int, target: int, length: int) -> list[str]:
  240. return words[:start] + words[start + length : target] + words[start : start + length] + words[target:]
  241. def _shift_word_within_shifted_string(words: list[str], start: int, target: int, length: int) -> list[str]:
  242. shifted_words = words[:start]
  243. shifted_words += words[start + length : length + target]
  244. shifted_words += words[start : start + length]
  245. shifted_words += words[length + target :]
  246. return shifted_words
  247. if target < start:
  248. return _shift_word_before_previous_position(words, start, target, length)
  249. if target > start + length:
  250. return _shift_word_after_previous_position(words, start, target, length)
  251. return _shift_word_within_shifted_string(words, start, target, length)
  252. def _shift_words(
  253. pred_words: list[str],
  254. target_words: list[str],
  255. cached_edit_distance: _LevenshteinEditDistance,
  256. checked_candidates: int,
  257. ) -> tuple[int, list[str], int]:
  258. """Attempt to shift words to match a hypothesis with a reference.
  259. It returns the lowest number of required edits between a hypothesis and a provided reference, a list of shifted
  260. words and number of checked candidates. Note that the filtering of possible shifts and shift selection are heavily
  261. based on somewhat arbitrary heuristics. The code here follows as closely as possible the logic in Tercom, not
  262. always justifying the particular design choices.
  263. The paragraph copied from https://github.com/mjpost/sacrebleu/blob/master/sacrebleu/metrics/lib_ter.py.
  264. Args:
  265. pred_words: A list of tokenized hypothesis sentence.
  266. target_words: A list of lists of tokenized reference sentences.
  267. cached_edit_distance: A pre-computed edit distance between a hypothesis and a reference.
  268. checked_candidates: A number of checked hypothesis candidates to match a provided reference.
  269. Return:
  270. best_score:
  271. The best (lowest) number of required edits to match hypothesis and reference sentences.
  272. shifted_words:
  273. A list of shifted words in hypothesis sentences.
  274. checked_candidates:
  275. A number of checked hypothesis candidates to match a provided reference.
  276. """
  277. edit_distance, inverted_trace = cached_edit_distance(pred_words)
  278. trace = _flip_trace(inverted_trace)
  279. alignments, target_errors, pred_errors = _trace_to_alignment(trace)
  280. best: Optional[tuple[int, int, int, int, list[str]]] = None
  281. for pred_start, target_start, length in _find_shifted_pairs(pred_words, target_words):
  282. if _handle_corner_cases_during_shifting(
  283. alignments, pred_errors, target_errors, pred_start, target_start, length
  284. ):
  285. continue
  286. prev_idx = -1
  287. for offset in range(-1, length):
  288. if target_start + offset == -1:
  289. idx = 0
  290. elif target_start + offset in alignments:
  291. idx = alignments[target_start + offset] + 1
  292. # offset is out of bounds => aims past reference
  293. else:
  294. break
  295. # Skip idx if already tried
  296. if idx == prev_idx:
  297. continue
  298. prev_idx = idx
  299. shifted_words = _perform_shift(pred_words, pred_start, length, idx)
  300. # Elements of the tuple are designed to replicate Tercom ranking of shifts:
  301. candidate = (
  302. edit_distance - cached_edit_distance(shifted_words)[0], # highest score first
  303. length, # then, longest match first
  304. -pred_start, # then, earliest match first
  305. -idx, # then, earliest target position first
  306. shifted_words,
  307. )
  308. checked_candidates += 1
  309. if not best or candidate > best:
  310. best = candidate
  311. if checked_candidates >= _MAX_SHIFT_CANDIDATES:
  312. break
  313. if not best:
  314. return 0, pred_words, checked_candidates
  315. best_score, _, _, _, shifted_words = best
  316. return best_score, shifted_words, checked_candidates
  317. def _translation_edit_rate(pred_words: list[str], target_words: list[str]) -> Tensor:
  318. """Compute translation edit rate between hypothesis and reference sentences.
  319. Args:
  320. pred_words: A list of a tokenized hypothesis sentence.
  321. target_words: A list of lists of tokenized reference sentences.
  322. Return:
  323. A number of required edits to match hypothesis and reference sentences.
  324. """
  325. if len(target_words) == 0:
  326. return tensor(0.0)
  327. cached_edit_distance = _LevenshteinEditDistance(target_words)
  328. num_shifts = 0
  329. checked_candidates = 0
  330. input_words = pred_words
  331. while True:
  332. # do shifts until they stop reducing the edit distance
  333. delta, new_input_words, checked_candidates = _shift_words(
  334. input_words, target_words, cached_edit_distance, checked_candidates
  335. )
  336. if checked_candidates >= _MAX_SHIFT_CANDIDATES or delta <= 0:
  337. break
  338. num_shifts += 1
  339. input_words = new_input_words
  340. edit_distance, _ = cached_edit_distance(input_words)
  341. total_edits = num_shifts + edit_distance
  342. return tensor(total_edits)
  343. def _compute_sentence_statistics(pred_words: list[str], target_words: list[list[str]]) -> tuple[Tensor, Tensor]:
  344. """Compute sentence TER statistics between hypothesis and provided references.
  345. Args:
  346. pred_words: A list of tokenized hypothesis sentence.
  347. target_words: A list of lists of tokenized reference sentences.
  348. Return:
  349. best_num_edits:
  350. The best (lowest) number of required edits to match hypothesis and reference sentences.
  351. avg_tgt_len:
  352. Average length of tokenized reference sentences.
  353. """
  354. tgt_lengths = tensor(0.0)
  355. best_num_edits = tensor(2e16)
  356. for tgt_words in target_words:
  357. num_edits = _translation_edit_rate(tgt_words, pred_words)
  358. tgt_lengths += len(tgt_words)
  359. if num_edits < best_num_edits:
  360. best_num_edits = num_edits
  361. avg_tgt_len = tgt_lengths / len(target_words)
  362. return best_num_edits, avg_tgt_len
  363. def _compute_ter_score_from_statistics(num_edits: Tensor, tgt_length: Tensor) -> Tensor:
  364. """Compute TER score based on pre-computed a number of edits and an average reference length.
  365. Args:
  366. num_edits: A number of required edits to match hypothesis and reference sentences.
  367. tgt_length: An average length of reference sentences.
  368. Return:
  369. A corpus-level TER score or 1 if reference_length == 0.
  370. """
  371. if tgt_length > 0 and num_edits > 0:
  372. return num_edits / tgt_length
  373. if tgt_length == 0 and num_edits > 0:
  374. return tensor(1.0)
  375. return tensor(0.0)
  376. def _ter_update(
  377. preds: Union[str, Sequence[str]],
  378. target: Sequence[Union[str, Sequence[str]]],
  379. tokenizer: _TercomTokenizer,
  380. total_num_edits: Tensor,
  381. total_tgt_length: Tensor,
  382. sentence_ter: Optional[List[Tensor]] = None,
  383. ) -> tuple[Tensor, Tensor, Optional[List[Tensor]]]:
  384. """Update TER statistics.
  385. Args:
  386. preds: An iterable of hypothesis corpus.
  387. target: An iterable of iterables of reference corpus.
  388. tokenizer: An instance of ``_TercomTokenizer`` handling a sentence tokenization.
  389. total_num_edits: A total number of required edits to match hypothesis and reference sentences.
  390. total_tgt_length: A total average length of reference sentences.
  391. sentence_ter: A list of sentence-level TER values
  392. Return:
  393. total_num_edits:
  394. A total number of required edits to match hypothesis and reference sentences.
  395. total_tgt_length:
  396. A total average length of reference sentences.
  397. sentence_ter:
  398. (Optionally) A list of sentence-level TER.
  399. Raises:
  400. ValueError:
  401. If length of ``preds`` and ``target`` differs.
  402. """
  403. target, preds = _validate_inputs(target, preds)
  404. for pred, tgt in zip(preds, target):
  405. tgt_words_: list[list[str]] = [_preprocess_sentence(_tgt, tokenizer).split() for _tgt in tgt]
  406. pred_words_: list[str] = _preprocess_sentence(pred, tokenizer).split()
  407. num_edits, tgt_length = _compute_sentence_statistics(pred_words_, tgt_words_)
  408. total_num_edits += num_edits
  409. total_tgt_length += tgt_length
  410. if sentence_ter is not None:
  411. sentence_ter.append(_compute_ter_score_from_statistics(num_edits, tgt_length).unsqueeze(0))
  412. return total_num_edits, total_tgt_length, sentence_ter
  413. def _ter_compute(total_num_edits: Tensor, total_tgt_length: Tensor) -> Tensor:
  414. """Compute TER based on pre-computed a total number of edits and a total average reference length.
  415. Args:
  416. total_num_edits: A total number of required edits to match hypothesis and reference sentences.
  417. total_tgt_length: A total average length of reference sentences.
  418. Return:
  419. A corpus-level TER score.
  420. """
  421. return _compute_ter_score_from_statistics(total_num_edits, total_tgt_length)
  422. def translation_edit_rate(
  423. preds: Union[str, Sequence[str]],
  424. target: Sequence[Union[str, Sequence[str]]],
  425. normalize: bool = False,
  426. no_punctuation: bool = False,
  427. lowercase: bool = True,
  428. asian_support: bool = False,
  429. return_sentence_level_score: bool = False,
  430. ) -> Union[Tensor, tuple[Tensor, List[Tensor]]]:
  431. """Calculate Translation edit rate (`TER`_) of machine translated text with one or more references.
  432. This implementation follows the implementations from
  433. https://github.com/mjpost/sacrebleu/blob/master/sacrebleu/metrics/ter.py. The `sacrebleu` implementation is a
  434. near-exact reimplementation of the Tercom algorithm, produces identical results on all "sane" outputs.
  435. Args:
  436. preds: An iterable of hypothesis corpus.
  437. target: An iterable of iterables of reference corpus.
  438. normalize: An indication whether a general tokenization to be applied.
  439. no_punctuation: An indication whteher a punctuation to be removed from the sentences.
  440. lowercase: An indication whether to enable case-insensitivity.
  441. asian_support: An indication whether asian characters to be processed.
  442. return_sentence_level_score: An indication whether a sentence-level TER to be returned.
  443. Return:
  444. A corpus-level translation edit rate (TER).
  445. (Optionally) A list of sentence-level translation_edit_rate (TER) if `return_sentence_level_score=True`.
  446. Example:
  447. >>> preds = ['the cat is on the mat']
  448. >>> target = [['there is a cat on the mat', 'a cat is on the mat']]
  449. >>> translation_edit_rate(preds, target)
  450. tensor(0.1538)
  451. References:
  452. [1] A Study of Translation Edit Rate with Targeted Human Annotation
  453. by Mathew Snover, Bonnie Dorr, Richard Schwartz, Linnea Micciulla and John Makhoul `TER`_
  454. """
  455. if not isinstance(normalize, bool):
  456. raise ValueError(f"Expected argument `normalize` to be of type boolean but got {normalize}.")
  457. if not isinstance(no_punctuation, bool):
  458. raise ValueError(f"Expected argument `no_punctuation` to be of type boolean but got {no_punctuation}.")
  459. if not isinstance(lowercase, bool):
  460. raise ValueError(f"Expected argument `lowercase` to be of type boolean but got {lowercase}.")
  461. if not isinstance(asian_support, bool):
  462. raise ValueError(f"Expected argument `asian_support` to be of type boolean but got {asian_support}.")
  463. tokenizer: _TercomTokenizer = _TercomTokenizer(normalize, no_punctuation, lowercase, asian_support)
  464. total_num_edits = tensor(0.0)
  465. total_tgt_length = tensor(0.0)
  466. sentence_ter: Optional[List[Tensor]] = [] if return_sentence_level_score else None
  467. total_num_edits, total_tgt_length, sentence_ter = _ter_update(
  468. preds,
  469. target,
  470. tokenizer,
  471. total_num_edits,
  472. total_tgt_length,
  473. sentence_ter,
  474. )
  475. ter_score = _ter_compute(total_num_edits, total_tgt_length)
  476. if sentence_ter:
  477. return ter_score, sentence_ter
  478. return ter_score