Coverage for src/wiktextract/extractor/es/inflection.py: 89%

47 statements  

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

1from wikitextprocessor.parser import NodeKind, TemplateNode 

2 

3from ...page import clean_node 

4from ...wxr_context import WiktextractContext 

5from .models import Form, WordEntry 

6from .tags import translate_raw_tags 

7 

8 

9def extract_inflection( 

10 wxr: WiktextractContext, 

11 page_data: list[WordEntry], 

12 template_node: TemplateNode, 

13) -> None: 

14 if template_node.template_name.startswith("inflect."): 14 ↛ exitline 14 didn't return from function 'extract_inflection' because the condition on line 14 was always true

15 process_inflect_template(wxr, page_data, template_node) 

16 

17 

18def process_inflect_template( 

19 wxr: WiktextractContext, 

20 page_data: list[WordEntry], 

21 template_node: TemplateNode, 

22) -> None: 

23 # https://es.wiktionary.org/wiki/Plantilla:inflect.es.sust.reg 

24 expanded_node = wxr.wtp.parse( 

25 wxr.wtp.node_to_wikitext(template_node), expand_all=True 

26 ) 

27 table_nodes = list(expanded_node.find_child(NodeKind.TABLE)) 

28 if len(table_nodes) == 0: 28 ↛ 29line 28 didn't jump to line 29 because the condition on line 28 was never true

29 return 

30 table_node = table_nodes[0] 

31 col_headers = [] 

32 row_header = "" 

33 forms_with_row_span = [] 

34 for row in table_node.find_child(NodeKind.TABLE_ROW): 

35 col_index = 0 

36 is_col_header_row = not row.contain_node(NodeKind.TABLE_CELL) 

37 for cell in row.find_child( 

38 NodeKind.TABLE_HEADER_CELL | NodeKind.TABLE_CELL 

39 ): 

40 cell_text = clean_node(wxr, None, cell) 

41 if cell_text == "": 

42 continue 

43 elif cell.kind == NodeKind.TABLE_HEADER_CELL: 

44 if is_col_header_row: 

45 col_headers.append(cell_text) 

46 else: 

47 row_header = cell_text 

48 for form in forms_with_row_span[:]: 

49 form.raw_tags.append(row_header) 

50 translate_raw_tags(form) 

51 if form.row_span == 1: 51 ↛ 54line 51 didn't jump to line 54 because the condition on line 51 was always true

52 forms_with_row_span.remove(form) 

53 else: 

54 form.row_span -= 1 

55 elif cell.kind == NodeKind.TABLE_CELL: 55 ↛ 37line 55 didn't jump to line 37 because the condition on line 55 was always true

56 form = Form(form=cell_text) 

57 row_span = int(cell.attrs.get("rowspan", "1")) 

58 if row_span > 1: 

59 forms_with_row_span.append(form) 

60 if len(row_header) > 0: 

61 form.raw_tags.append(row_header) 

62 if col_index < len(col_headers): 62 ↛ 64line 62 didn't jump to line 64 because the condition on line 62 was always true

63 form.raw_tags.append(col_headers[col_index]) 

64 if len(form.form) > 0: 64 ↛ 67line 64 didn't jump to line 67 because the condition on line 64 was always true

65 translate_raw_tags(form) 

66 page_data[-1].forms.append(form) 

67 col_index += 1