Coverage for src/wiktextract/extractor/ja/etymology.py: 94%

32 statements  

« prev     ^ index     » next       coverage.py v7.16.1, created at 2026-09-21 09:06 +0000

1from wikitextprocessor.parser import LEVEL_KIND_FLAGS, LevelNode, NodeKind 

2 

3from ...page import clean_node 

4from ...wxr_context import WiktextractContext 

5from .models import WordEntry 

6 

7 

8def extract_etymology_section( 

9 wxr: WiktextractContext, 

10 page_data: list[WordEntry], 

11 base_data: WordEntry, 

12 level_node: LevelNode, 

13) -> None: 

14 etymology_texts = [] 

15 etymology_links: list[tuple[str, str]] = [] 

16 cats = {} 

17 for list_node in level_node.find_child(NodeKind.LIST): 

18 # don't use `find_child_recursively` to avoid lists in subsection 

19 for list_item in list_node.find_child(NodeKind.LIST_ITEM): 

20 e_links: list[tuple[str, str]] = [] 

21 text = clean_node( 

22 wxr, 

23 cats, 

24 list( 

25 list_item.invert_find_child( 

26 NodeKind.LIST, include_empty_str=True 

27 ) 

28 ), 

29 link_collector=e_links, 

30 ) 

31 if len(text) > 0: 31 ↛ 19line 31 didn't jump to line 19 because the condition on line 31 was always true

32 etymology_texts.append(text) 

33 etymology_links.extend(e_links) 

34 if len(etymology_texts) == 0: 

35 e_links: list[tuple[str, str]] = [] 

36 text = clean_node( 

37 wxr, 

38 cats, 

39 list( 

40 level_node.invert_find_child( 

41 LEVEL_KIND_FLAGS, include_empty_str=True 

42 ) 

43 ), 

44 link_collector=e_links, 

45 ) 

46 if len(text) > 0: 46 ↛ 49line 46 didn't jump to line 49 because the condition on line 46 was always true

47 etymology_texts.append(text) 

48 etymology_links.extend(e_links) 

49 for link in level_node.find_child(NodeKind.LINK): 

50 clean_node(wxr, cats, link) 

51 base_data.etymology_texts = etymology_texts 

52 base_data.etymology_links = etymology_links 

53 base_data.categories.extend(cats.get("categories", [])) 

54 if level_node.kind != NodeKind.LEVEL3: # under POS section 

55 for data in page_data: 

56 if ( 56 ↛ 55line 56 didn't jump to line 55 because the condition on line 56 was always true

57 data.lang_code == base_data.lang_code 

58 and len(data.etymology_texts) == 0 

59 ): 

60 data.etymology_texts = etymology_texts 

61 data.etymology_links = etymology_links.copy() 

62 data.categories.extend(cats.get("categories", []))