Jay4769 commited on
Commit
85e9fae
·
verified ·
1 Parent(s): b1c5cca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -28
app.py CHANGED
@@ -33,7 +33,7 @@ viru_model = AutoModelForSeq2SeqLM.from_pretrained(
33
  )
34
 
35
  # =========================
36
- # WIKTIONARY LOOKUP
37
  # =========================
38
 
39
  def get_russian_info(word):
@@ -41,52 +41,81 @@ def get_russian_info(word):
41
  try:
42
 
43
  url = (
44
- "https://ru.wiktionary.org/w/api.php"
 
45
  )
46
 
47
- params = {
48
- "action": "query",
49
- "prop": "extracts",
50
- "exintro": True,
51
- "explaintext": True,
52
- "titles": word,
53
- "format": "json"
54
- }
55
-
56
  response = requests.get(
57
  url,
58
- params=params,
59
  timeout=10
60
  )
61
 
62
  data = response.json()
63
 
64
- pages = data["query"]["pages"]
65
 
66
- page = next(iter(pages.values()))
 
 
 
67
 
68
- extract = page.get(
69
- "extract",
70
- "Không tìm thấy định nghĩa"
71
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
 
73
- lines = extract.split("\n")
 
 
 
74
 
75
- definition = (
76
- lines[0]
77
- if len(lines) > 0
78
- else "Không tìm thấy"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
  )
80
 
81
- example = (
82
- lines[1]
83
- if len(lines) > 1
84
  else "Không có ví dụ"
85
  )
86
 
87
- return definition, example
 
 
 
88
 
89
- except:
90
 
91
  return (
92
  "Không tìm thấy định nghĩa",
 
33
  )
34
 
35
  # =========================
36
+ # WIKTIONARY API
37
  # =========================
38
 
39
  def get_russian_info(word):
 
41
  try:
42
 
43
  url = (
44
+ "https://ru.wiktionary.org/api/rest_v1/page/definition/"
45
+ + word
46
  )
47
 
 
 
 
 
 
 
 
 
 
48
  response = requests.get(
49
  url,
 
50
  timeout=10
51
  )
52
 
53
  data = response.json()
54
 
55
+ if "ru" not in data:
56
 
57
+ return (
58
+ "Không tìm thấy định nghĩa",
59
+ "Không có ví dụ"
60
+ )
61
 
62
+ entries = data["ru"]
63
+
64
+ definitions = []
65
+ examples = []
66
+
67
+ for entry in entries:
68
+
69
+ senses = entry.get(
70
+ "definitions",
71
+ []
72
+ )
73
+
74
+ for sense in senses:
75
+
76
+ definition = sense.get(
77
+ "definition",
78
+ ""
79
+ )
80
 
81
+ if definition:
82
+ definitions.append(
83
+ definition
84
+ )
85
 
86
+ exs = sense.get(
87
+ "examples",
88
+ []
89
+ )
90
+
91
+ for ex in exs:
92
+
93
+ text = ex.get(
94
+ "text",
95
+ ""
96
+ )
97
+
98
+ if text:
99
+ examples.append(text)
100
+
101
+ definition_text = (
102
+ "\n\n".join(definitions[:5])
103
+ if definitions
104
+ else "Không tìm thấy định nghĩa"
105
  )
106
 
107
+ example_text = (
108
+ "\n\n".join(examples[:5])
109
+ if examples
110
  else "Không có ví dụ"
111
  )
112
 
113
+ return (
114
+ definition_text,
115
+ example_text
116
+ )
117
 
118
+ except Exception:
119
 
120
  return (
121
  "Không tìm thấy định nghĩa",