Created
November 4, 2021 20:58
-
-
Save jbasko/c910f8d6bffce2d64276e2277e2e5027 to your computer and use it in GitHub Desktop.
Termina visu locījumu izdabūšana laukā no tezaurs.lv
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Dabūt laukā visus terminu locījumus no tezaurs.lv | |
""" | |
from typing import List | |
import requests | |
import dataclasses | |
API = "https://api.tezaurs.lv/v1/inflections/" | |
@dataclasses.dataclass | |
class Word: | |
word: str | |
index: int = 0 | |
should_inflect: bool = True | |
def __post_init__(self): | |
if self.word.startswith("{"): | |
self.word = self.word[1:-1] | |
self.should_inflect = False | |
def get_term_inflections(term: str) -> List[str]: | |
""" | |
Termins var saturēt vairākus vārdus. Nelokāmos vārdus atzīmē ar figūriekavām: | |
"diena" | |
"skaista diena" | |
"{Rīgas} mērs" | |
"{mēreni} skaista diena" | |
""" | |
term_words = [Word(word=w, index=i) for i, w in enumerate(term.split(" "))] | |
should_inflect = [w.word for w in term_words if w.should_inflect] | |
raw_inflections = [] | |
if should_inflect: | |
raw_inflections = requests.get(f"{API}{' '.join(w.word for w in term_words if w.should_inflect)}").json() | |
else: | |
return [" ".join(w.word for w in term_words)] | |
num_inflections = len(raw_inflections[0]) | |
inflections = [] | |
for i in range(num_inflections): | |
inflections.append([]) | |
ri_index = 0 | |
for w in term_words: | |
if w.should_inflect: | |
inflections[i].append(raw_inflections[ri_index][i]["Vārds"]) | |
ri_index += 1 | |
else: | |
inflections[i].append(w.word) | |
return [" ".join(infl) for infl in inflections] | |
def example(): | |
print(get_term_inflections("{mēreni} skaistā diena")) | |
print(get_term_inflections("{Rīgas} mērs")) | |
if __name__ == "__main__": | |
example() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment