Coverage for src/botadi/mokadi/mokadi_wikipedia.py: 89%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# -*- coding: utf-8 -*-
2"""
3@file
4@brief Look into the mail box
5"""
6import re
7import wikipedia
8from .mokadi_exceptions import WikipediaException
11def definition_wikipedia(query, summary=True, lang="fr"):
12 """
13 Search for the definition of something on wikipedia.
15 @param query what to search for
16 @param summary summary or long definition
17 @param lang language
18 @return definition
19 """
20 wikipedia.set_lang("fr")
21 if summary:
22 res = wikipedia.summary(query, sentences=1, auto_suggest=False)
23 return res.strip("# ")
24 else:
25 res = wikipedia.search(query, results=1)
26 if len(res) == 0:
27 raise WikipediaException(
28 "Unable to find a page for '{0}'".format(query))
29 title = res[0]
30 page = wikipedia.page(title)
31 if page is None or not hasattr(page, "content"):
32 raise WikipediaException(
33 "Unable to find a page for '{0}' (2)".format(page))
34 page = page.content
35 if lang == "fr":
36 if "== Notes et références ==" in page:
37 page = page.split("== Notes et références ==")[0]
38 if "== Voir aussi ==" in page:
39 page = page.split("== Notes et références ==")[0]
40 return page
43def suggestions_wikipedia(query, lang="fr"):
44 """
45 Suggestions for something on wikipedia.
47 @param query what to search for
48 @param lang language
49 @return definition
50 """
51 wikipedia.set_lang("fr")
52 return wikipedia.search(query, results=10)
55def synonyms_wiktionary(name, lang="fr"):
56 """
57 Returns the definition from Wiktionary.
59 @param name name to look for
60 @param lang language
61 @return definition
63 `Wiktionay API <https://en.wiktionary.org/w/api.php>`_.
64 """
65 import wptools
66 page = wptools.page(name, wiki='{0}.wiktionary.org'.format(
67 lang), lang=lang, silent=True)
68 page.get_parse()
69 text = page.data['wikitext']
70 syn = "==== {{S|synonymes}} ===="
71 if syn not in text:
72 return None
73 text = text.split(syn)[1].split("====")[0]
74 reg = re.compile("[[]{2}(.*?)[]]{2}")
75 res = reg.findall(text)
76 return res