Coverage for src/wiktextract/extractor/es/translation.py: 86%

48 statements  

« prev     ^ index     » next       coverage.py v7.6.4, created at 2024-10-25 10:11 +0000

1import itertools 

2 

3from mediawiki_langcodes import code_to_name 

4from wikitextprocessor.parser import LevelNode, NodeKind, TemplateNode 

5 

6from ...page import clean_node 

7from ...wxr_context import WiktextractContext 

8from .models import Translation, WordEntry 

9from .tags import translate_raw_tags 

10 

11 

12def extract_translation_section( 

13 wxr: WiktextractContext, word_entry: WordEntry, level_node: LevelNode 

14) -> None: 

15 for template_node in level_node.find_child(NodeKind.TEMPLATE): 

16 if template_node.template_name == "t": 16 ↛ 15line 16 didn't jump to line 15 because the condition on line 16 was always true

17 process_t_template(wxr, word_entry, template_node) 

18 

19 

20# https://es.wiktionary.org/wiki/Módulo:t 

21T_GENDERS = { 

22 "m": "masculine", 

23 "f": "feminine", 

24 "mf": ["masculine", "feminine"], 

25 "n": "neuter", 

26 "c": "common", 

27} 

28T_NUMBERS = { 

29 "s": "singular", 

30 "sg": "singular", 

31 "p": "plural", 

32 "pl": "plural", 

33 "d": "dual", 

34 "du": "dual", 

35} 

36 

37 

38def process_t_template( 

39 wxr: WiktextractContext, word_entry: WordEntry, template_node: TemplateNode 

40) -> None: 

41 # https://es.wiktionary.org/wiki/Plantilla:t 

42 lang_code = template_node.template_parameters.get(1, "") 

43 template_text = clean_node(wxr, word_entry, template_node) 

44 lang_name = template_text[: template_text.find(":")].strip("* ") 

45 if lang_name == "": # in case Lua error 45 ↛ 46line 45 didn't jump to line 46 because the condition on line 45 was never true

46 lang_name = code_to_name(lang_code, "es") 

47 

48 sense_index = "" 

49 for tr_index in itertools.count(1): 49 ↛ exitline 49 didn't return from function 'process_t_template' because the loop on line 49 didn't complete

50 if "t" + str(tr_index) not in template_node.template_parameters: 

51 break 

52 tr_data = Translation(lang_code=lang_code, lang=lang_name, word="") 

53 for param_prefix, field in ( 

54 ("t", "word"), 

55 ("a", "sense_index"), 

56 ("tl", "roman"), 

57 ("nota", "raw_tags"), 

58 ("g", "tags"), 

59 ("n", "tags"), 

60 ): 

61 param = param_prefix + str(tr_index) 

62 if param not in template_node.template_parameters: 

63 continue 

64 value = clean_node( 

65 wxr, None, template_node.template_parameters[param] 

66 ) 

67 if param_prefix == "g": 

68 value = T_GENDERS.get(value) 

69 elif param_prefix == "n": 69 ↛ 70line 69 didn't jump to line 70 because the condition on line 69 was never true

70 value = T_NUMBERS.get(value) 

71 elif param_prefix == "a": 

72 sense_index = value 

73 if value is None: 73 ↛ 74line 73 didn't jump to line 74 because the condition on line 73 was never true

74 continue 

75 

76 pre_value = getattr(tr_data, field) 

77 if isinstance(pre_value, list): 

78 if isinstance(value, list): 78 ↛ 79line 78 didn't jump to line 79 because the condition on line 78 was never true

79 pre_value.extend(value) 

80 else: 

81 pre_value.append(value) 

82 else: 

83 setattr(tr_data, field, value) 

84 

85 if tr_data.sense_index == "" and sense_index != "": 

86 # usually only first word has index param 

87 tr_data.sense_index = sense_index 

88 

89 if len(tr_data.word) > 0: 89 ↛ 49line 89 didn't jump to line 49 because the condition on line 89 was always true

90 translate_raw_tags(tr_data) 

91 word_entry.translations.append(tr_data)