|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
1 |
|
""" |
|
3
|
|
|
This module contains an addon for basic Word Model, |
|
4
|
|
|
which makes it possible to work with word's sources |
|
5
|
|
|
""" |
|
6
|
|
|
|
|
7
|
1 |
|
from typing import Optional, List, Union |
|
8
|
1 |
|
from flask_sqlalchemy import BaseQuery |
|
9
|
|
|
|
|
10
|
1 |
|
from loglan_db.model_db.base_type import BaseType |
|
11
|
1 |
|
from loglan_db.model_db.base_word import BaseWord |
|
12
|
1 |
|
from loglan_db import db |
|
13
|
1 |
|
from loglan_db.model_db.base_word_source import BaseWordSource |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
1 |
|
class AddonWordSourcer: |
|
17
|
|
|
"""AddonWordSourcer Model""" |
|
18
|
|
|
|
|
19
|
1 |
|
type: BaseType |
|
20
|
1 |
|
name: db.Column |
|
21
|
1 |
|
origin: db.Column |
|
22
|
1 |
|
origin_x: db.Column |
|
23
|
1 |
|
type_id: db.Column |
|
24
|
1 |
|
query: BaseQuery |
|
25
|
|
|
|
|
26
|
1 |
|
def get_sources_prim(self): |
|
27
|
|
|
""" |
|
28
|
|
|
|
|
29
|
|
|
Returns: |
|
30
|
|
|
|
|
31
|
|
|
""" |
|
32
|
|
|
# existing_prim_types = ["C", "D", "I", "L", "N", "O", "S", ] |
|
33
|
|
|
|
|
34
|
1 |
|
if not self.type.group == "Prim": |
|
35
|
1 |
|
return None |
|
36
|
|
|
|
|
37
|
1 |
|
prim_type = self.type.type[:1] |
|
38
|
|
|
|
|
39
|
1 |
|
if prim_type == "C": |
|
40
|
1 |
|
return self._get_sources_c_prim() |
|
41
|
|
|
|
|
42
|
1 |
|
return f"{self.name}: {self.origin}{' < ' + self.origin_x if self.origin_x else ''}" |
|
43
|
|
|
|
|
44
|
1 |
|
def _get_sources_c_prim(self) -> Optional[List[BaseWordSource]]: |
|
45
|
|
|
""" |
|
46
|
|
|
Returns: |
|
47
|
|
|
""" |
|
48
|
1 |
|
if self.type.type != "C-Prim": |
|
49
|
1 |
|
return None |
|
50
|
|
|
|
|
51
|
1 |
|
sources = str(self.origin).split(" | ") |
|
52
|
|
|
|
|
53
|
1 |
|
return [BaseWordSource(source) for source in sources] |
|
54
|
|
|
|
|
55
|
1 |
|
def get_sources_cpx(self, as_str: bool = False) -> List[Union[None, str, BaseWord]]: |
|
56
|
|
|
"""Extract source words from self.origin field accordingly |
|
57
|
|
|
Args: |
|
58
|
|
|
as_str (bool): return BaseWord objects if False else as simple str |
|
59
|
|
|
(Default value = False) |
|
60
|
|
|
Example: |
|
61
|
|
|
'foldjacea' > ['forli', 'djano', 'cenja'] |
|
62
|
|
|
Returns: |
|
63
|
|
|
List of words from which the self.name was created |
|
64
|
|
|
|
|
65
|
|
|
""" |
|
66
|
|
|
|
|
67
|
|
|
# these prims have switched djifoas like 'flo' for 'folma' |
|
68
|
1 |
|
switch_prims = [ |
|
69
|
|
|
'canli', 'farfu', 'folma', 'forli', 'kutla', 'marka', |
|
70
|
|
|
'mordu', 'sanca', 'sordi', 'suksi', 'surna'] |
|
71
|
|
|
|
|
72
|
1 |
|
if not self.type.group == "Cpx": |
|
73
|
1 |
|
return [] |
|
74
|
|
|
|
|
75
|
1 |
|
sources = self._prepare_sources_cpx() |
|
76
|
1 |
|
return sources if as_str else self.words_from_source_cpx(sources) |
|
77
|
|
|
|
|
78
|
1 |
|
@classmethod |
|
79
|
1 |
|
def words_from_source_cpx(cls, sources: List[str]) -> List[Optional[BaseWord]]: |
|
80
|
|
|
""" |
|
81
|
|
|
|
|
82
|
|
|
Args: |
|
83
|
|
|
sources: |
|
84
|
|
|
|
|
85
|
|
|
Returns: |
|
86
|
|
|
|
|
87
|
|
|
""" |
|
88
|
1 |
|
exclude_type_ids = [t.id for t in BaseType.by(["LW", "Cpd"]).all()] |
|
89
|
1 |
|
return cls.query \ |
|
90
|
|
|
.filter(cls.name.in_(sources)) \ |
|
91
|
|
|
.filter(cls.type_id.notin_(exclude_type_ids)).all() |
|
92
|
|
|
|
|
93
|
1 |
|
def _prepare_sources_cpx(self) -> List[str]: |
|
94
|
|
|
""" |
|
95
|
|
|
Returns: |
|
96
|
|
|
""" |
|
97
|
1 |
|
sources = self.origin.replace("(", "").replace(")", "").replace("/", "") |
|
98
|
1 |
|
sources = sources.split("+") |
|
99
|
1 |
|
sources = [ |
|
100
|
|
|
s if not s.endswith(("r", "h")) else s[:-1] |
|
101
|
|
|
for s in sources if s not in ["y", "r", "n"]] |
|
102
|
1 |
|
return sources |
|
103
|
|
|
|
|
104
|
1 |
|
def get_sources_cpd(self, as_str: bool = False) -> List[Union[None, str, BaseWord]]: |
|
105
|
|
|
"""Extract source words from self.origin field accordingly |
|
106
|
|
|
|
|
107
|
|
|
Args: |
|
108
|
|
|
as_str: bool: return BaseWord objects if False else as simple str |
|
109
|
|
|
(Default value = False) |
|
110
|
|
|
|
|
111
|
|
|
Returns: |
|
112
|
|
|
List of words from which the self.name was created |
|
113
|
|
|
""" |
|
114
|
|
|
|
|
115
|
1 |
|
if not self.type.type == "Cpd": |
|
116
|
1 |
|
return [] |
|
117
|
|
|
|
|
118
|
1 |
|
sources = self._prepare_sources_cpd() |
|
119
|
1 |
|
return sources if as_str else self.words_from_source_cpd(sources) |
|
120
|
|
|
|
|
121
|
1 |
|
def _prepare_sources_cpd(self) -> List[str]: |
|
122
|
|
|
""" |
|
123
|
|
|
Returns: |
|
124
|
|
|
""" |
|
125
|
1 |
|
sources = self.origin.replace("(", "").replace(")", "").replace("/", "").replace("-", "") |
|
126
|
1 |
|
sources = [s.strip() for s in sources.split("+") if s] |
|
127
|
1 |
|
return sources |
|
128
|
|
|
|
|
129
|
1 |
|
@classmethod |
|
130
|
1 |
|
def words_from_source_cpd(cls, sources: List[str]) -> List[Optional[BaseWord]]: |
|
131
|
|
|
""" |
|
132
|
|
|
|
|
133
|
|
|
Args: |
|
134
|
|
|
sources: |
|
135
|
|
|
|
|
136
|
|
|
Returns: |
|
137
|
|
|
|
|
138
|
|
|
""" |
|
139
|
1 |
|
type_ids = [t.id for t in BaseType.by(["LW", "Cpd"]).all()] |
|
140
|
1 |
|
return cls.query.filter(cls.name.in_(sources)) \ |
|
141
|
|
|
.filter(cls.type_id.in_(type_ids)).all() |
|
142
|
|
|
|