Skip to content
Snippets Groups Projects
Commit dd011b93 authored by Pierre Fleutot's avatar Pierre Fleutot
Browse files

Utilisation nouveau wikstraktor

parent df1dcc2f
No related branches found
No related tags found
No related merge requests found
Showing
with 1027 additions and 366 deletions
File added
...@@ -104,23 +104,23 @@ class WiktionaryManager ...@@ -104,23 +104,23 @@ class WiktionaryManager
$morphologicalLabels = []; $morphologicalLabels = [];
$entryData = []; $entryData = [];
$word = array_key_first($wiktionaryData[0]); $word = array_keys($wiktionaryData[0])[2];
$entryData['Headword'] = $word; $entryData['Headword'] = $word;
$items = []; $items = [];
foreach ($wiktionaryData as $wordForm) { foreach ($wiktionaryData as $pos) {
if (array_key_first($wordForm) != $word) { if (array_keys($pos)[2] != $word) {
throw new \Exception(sprintf("Items se rapportant à des mots différents dans les données du wiktionnaire pour le mot %s.", $word)); throw new \Exception(sprintf("Items se rapportant à des mots différents dans les données du wiktionnaire pour le mot %s.", $word));
} }
$wordFormData = $wordForm[$word]; $posData = $pos[$word];
$item = []; $item = [];
$item['PartOfSpeech'] = $wordFormData['pos']; $item['PartOfSpeech'] = $posData['pos'];
$morphologicalLabels[] = $wordFormData['pos']; $morphologicalLabels[] = $posData['pos'];
$sense = []; $sense = [];
$sense['Pronunciations'] = $this->getPronunciations($wordFormData['pronunciations']); $sense['Pronunciations'] = $this->getPronunciations($posData['pronunciations']);
$sense['Definitions'] = $this->getDefinitions($wordFormData['senses']); $sense['Definitions'] = $this->getDefinitions($posData['senses']);
$item['Sense'] = $sense; $item['Sense'] = $sense;
$items[] = $item; $items[] = $item;
...@@ -143,6 +143,9 @@ class WiktionaryManager ...@@ -143,6 +143,9 @@ class WiktionaryManager
if (isset($wikPronunciation['accent'])) { if (isset($wikPronunciation['accent'])) {
$pronunciation['accent'] = $wikPronunciation['accent']; $pronunciation['accent'] = $wikPronunciation['accent'];
} }
if (isset($wikPronunciation['sounds'])) {
$pronunciation['sounds'] = $wikPronunciation['sounds'];
}
$result[] = $pronunciation; $result[] = $pronunciation;
} }
...@@ -154,12 +157,12 @@ class WiktionaryManager ...@@ -154,12 +157,12 @@ class WiktionaryManager
$result = []; $result = [];
foreach ($wikSenses as $wikSense) { foreach ($wikSenses as $wikSense) {
$definition = [ $definition = [
'Def' => reset($wikSense)['defs'][0]['definition'], 'Def' => $wikSense['Definitions'][0]['definition'],
]; ];
// Extraction des exemples // Extraction des exemples
$examples = []; $examples = [];
foreach (reset($wikSense)['exs'] as $wikExample) { foreach ($wikSense['Examples'] ?? [] as $wikExample) {
$ex = $wikExample['example']; $ex = $wikExample['example'];
if ($ex) { if ($ex) {
$examples[] = $wikExample; $examples[] = $wikExample;
......
user-config.py
user-password.py
__pycache__
apicache-py3
logs
throttle.ctrl
user_list.py
KNM.csv
.~lock*
*.json
*.lwp
wikstraktor_version.py
wikstraktorenv
wikstraktor
===========
A python tool to query the [wiktionary](https://wiktionary.org) and extract [structured lexical data](https://gitlab.liris.cnrs.fr/lex-game/wikstraktor/-/wikis/Entry-structure).
## Dependencies
This project does depend on python packages.
* [``pywikibot``](https://github.com/wikimedia/pywikibot) allows to use the mediawiki API
* [documentation](https://doc.wikimedia.org/pywikibot/stable/api_ref/pywikibot.html)
* [manual](https://www.mediawiki.org/wiki/Manual:Pywikibot)
* [configuration for the wiktionary](https://github.com/wikimedia/pywikibot/blob/master/pywikibot/families/wiktionary_family.py)
* [``wikitextparser``](https://github.com/5j9/wikitextparser) can parse mediawiki pages and extract sections, templates and links
* [documentation](https://wikitextparser.readthedocs.io/en/latest/#api-reference)
* [``importlib``](https://docs.python.org/3/library/importlib.html) : to import parser modules
## Installation
(maybe to be replaced by an automation of some sort, using a virtual environment might be better, see [server version](#wikstraktor-server))
* [```pip install pywikibot```](https://pypi.org/project/pywikibot/)
* [```pip install wikitextparser```](https://pypi.org/project/wikitextparser/)
* [```pip install gitpython```](https://gitpython.readthedocs.io/en/stable/)
* [```pip install sqlite3```](https://docs.python.org/3/library/sqlite3.html) Might be provided with python
* [```pip install importlib```](https://pypi.org/project/importlib/)
_Optional (for python 2.*, not tested)_
* run ``./setup.py`` (used to store wikstraktor version in wiktionary extracts)
### Wikstraktor Server
If you want wikstraktor as a server, you need to install [flask](https://flask.palletsprojects.com/en/2.0.x/installation/) and [flask-cors](https://flask-cors.readthedocs.io/en/latest/) — to allow other domains to query —, and best practice is to do so in a [virtual environment](https://docs.python.org/3/library/venv.html#module-venv).
The following commands are extracted from the aforementionned documentation, it is probably more secure to click on the link and follow the modules documentation :
```bash
python3 -m venv wikstraktorenv #create wikstraktorenv environment
. wikstraktorenv/bin/activate #activate environment
pip install Flask #install Flask
pip install -U flask-cors #install Flask cors
```
## Use
### Wikstraktor
#### Python
```python
from wikstraktor import Wikstraktor
f = Wikstraktor.get_instance('fr', 'en') #create a wikstraktor,
# first parameter is the language of the wiki
# second parameter is the language of the word sought for
f.fetch("blue") #fetch an article
str(f) #convert content to json
```
#### Bash
```
usage: wikstraktor.py [-h] [-l LANGUAGE] [-w WIKI_LANGUAGE] [-m MOT]
[-f DESTINATION_FILE] [-A] [-C]
Interroger un wiktionnaire
ex :
‣./wikstraktor.py -m blue
‣./wikstraktor.py -m blue -f blue.json -A -C
‣./wikstraktor.py -l en -w fr -m blue -f blue.json -A -C
options:
-h, --help show this help message and exit
-l LANGUAGE, --language LANGUAGE
la langue du mot
-w WIKI_LANGUAGE, --wiki_language WIKI_LANGUAGE
la langue du wiki
-m MOT, --mot MOT le mot à chercher
-f DESTINATION_FILE, --destination_file DESTINATION_FILE
le fichier dans lequel stocker le résultat
-A, --force_ascii json avec que des caractères ascii
-C, --compact json sans indentation
```
### Wikstraktor Server
The server runs by default on port 5000, you can change that in the ```wikstraktor_server_config.py``` file.
```bash
./wikstraktor_server.py
```
Then there is a very simple API :
* ``GET server_url/search/<word>`` : Searches the word in the default wiktionary
* ``GET server_url/search/<wiktlang>/<wordlang>/<word>`` : Searches the word In wordlang in the wiktlang wiktionary
Both API calls return a json object.
## Licence
TODO but will be open source
string_values = {
"ety":"Etymology",
"pro":"Pronunciation",
"en":"English",
"fr":"French",
"t_ipa":"IPA", #template for transcription
"t_snd":"audio", #template for audio
"t_acc":"a", #template for accents
"t_deflabel":"lb",
"t_ex":["ux", "usex"],
"POS": { #https://en.wiktionary.org/wiki/Wiktionary:POS
"Adjective":"Adj",
"Adverb":"Adv",
"Ambiposition":"Ambip",
"Article":"Art",
"Circumposition":"Circump",
"Classifier":"Class",
"Conjunction":"Conj",
"Contraction":"Cont",
"Counter":"Count",
"Determiner":"Det",
"Ideophone":"Ideophone",
"Interjection":"Interj",
"Noun":"N",
"Numeral":"Num",
"Participle":"Part",
"Particle":"Particle",
"Postposition":"Postp",
"Preposition":"Prep",
"Pronoun":"Pro",
"Proper noun":"NP",
"Verb":"V" # TODO: compléter
}
}
#!/usr/bin/env python3
from wikstraktor import Wikstraktor, Pronunciation, Sense
from en_constants import string_values
debugEty = 0
class En_en_straktor(Wikstraktor):
def __init__(self):
super().__init__()
self.wiki_language = "en"
self.entry_language = "en"
self.constants = string_values
self.site = self.pwb.Site(f'wiktionary:en')
def process_pronunciation(self, proContent):
# TODO: ne marche que pour les listes à 2 niveaux, voir water pour 3 niveaux
l = proContent.get_lists()[0]
i = 0
pronunciations = []
while i < len(l.fullitems):
p = Pronunciation()
templates = self.wtp.parse(l.fullitems[i]).templates
a = None
for j, t in enumerate(templates):
if (t.normal_name() == self.constants['t_acc'] and templates[j+1].normal_name()!= self.constants['t_acc']):
a = t.arguments[0].value
elif t.normal_name() == self.constants['t_ipa']:
p.set_transcription(t.arguments[1].value)
p.set_accent(a)
elif t.normal_name() == self.constants['t_snd']:
p.add_sound(self.get_file_url(t.arguments[1].value), a)
if j==len(templates)-1 or templates[j+1].normal_name()== self.constants['t_acc'] :
if p.ipa != None or p.accent != None:
pronunciations.append(p)
p = Pronunciation()
i += 1
return pronunciations
def process_etymology(self, etyContent):
global debugEty
debugEty += 1
return "Etymology" + str(debugEty)
def process_POS(self,parsedwikitext):
pos = None
if parsedwikitext in self.constants['POS'].keys():
pos = self.constants['POS'][parsedwikitext]
return pos
def process_senses(self, entry, pos, sensesContent):
baseId = f"{entry}_{pos}_"
l = sensesContent.get_lists(('\\# ', '\\#:','\\## ', '\\##:' ))
i = 0
senses = []
nombreDef = 0
while i < len(l):
if l[i].pattern == '\\# ':
nombreDef += 1
newSense = Sense(f"{baseId}{nombreDef}")
newSense.add_def(self.wiki_language, self.wtp.parse(l[i].items[0]).plain_text().strip())
elif l[i].pattern == '\\#:':
for j in l[i].items:
k = 0
isEx = 0
while k < len(self.wtp.parse(j).templates) and isEx == 0 :
if (self.wtp.parse(j).templates[k].normal_name() in self.constants['t_ex']):
newSense.add_example(self.wtp.parse(j).templates[0].arguments[-1].value)
isEx = 1
k += 1
if isEx == 0:
newSense.add_example(self.wtp.parse(j).plain_text().strip())
if i == len(l)-1 or l[i+1].pattern == '\\# ' or l[i+1].pattern == '\\## ':
senses.append(newSense)
cnt = 0
nombreSousDef = 0
while i < len(l) and l[i].level == 3 :
cnt +=1
if l[i].pattern == '\\## ':
nombreSousDef += 1
newSense2 = Sense(f"{baseId}{nombreDef}_{nombreSousDef}")
newSense2.add_def(self.wiki_language, self.wtp.parse(l[i].items[0]).plain_text().strip())
elif l[i].pattern == '\\##:':
for j in l[i].items:
k = 0
isEx = 0
while k < len(self.wtp.parse(j).templates) and isEx == 0 :
if (self.wtp.parse(j).templates[k].normal_name() in self.constants['t_ex']):
newSense2.add_example(self.wtp.parse(j).templates[0].arguments[-1].value)
isEx = 1
k += 1
if isEx == 0:
newSense2.add_example(self.wtp.parse(j).plain_text().strip())
if i == len(l)-1 or l[i+1].pattern == '\\# ' or l[i+1].pattern == '\\## ':
senses.append(newSense2)
i += 1
if cnt > 0:
i -= 1
i += 1
return senses
if __name__ == "__main__":
ensk = En_en_straktor()
print(ensk.fetch("test"), "entries added")
...@@ -15,8 +15,8 @@ ...@@ -15,8 +15,8 @@
"url1":"https://upload.wikimedia.org/wikipedia/commons/1/19/LL-Q1860_%28eng%29-Back_ache-water.wav" "url1":"https://upload.wikimedia.org/wikipedia/commons/1/19/LL-Q1860_%28eng%29-Back_ache-water.wav"
} }
], ],
"Senses":[ "Senses":{
{ "v1":{
"Translations":[ "Translations":[
"translation1", "translation1",
"...", "...",
...@@ -26,16 +26,16 @@ ...@@ -26,16 +26,16 @@
"Stilles Mineralwasser.jpg", "Stilles Mineralwasser.jpg",
"..." "..."
], ],
"Definition":"blabla", "Definition":{"lang":"fr", "definition" : "blabla"},
"Examples":[ "Examples":[
"blabla", "blabla",
"blabli", "blabli",
"blablou" "blablou"
], ],
"subSense":[ "SubSenses":[
{ {
"subdef":"blabla", "Definition":{"lang":"en", "definition" : "whatnot"},
"subex":[ "Examples":[
"subexa", "subexa",
"subexb", "subexb",
"subexz" "subexz"
...@@ -43,7 +43,7 @@ ...@@ -43,7 +43,7 @@
} }
] ]
} }
] }
} }
] ]
} }
...@@ -61,4 +61,3 @@ ...@@ -61,4 +61,3 @@
\"Supplementary field for devs 5\" \"Supplementary field for devs 5\"
... ...
\"Supplementary field for devs 10\ */ \"Supplementary field for devs 10\ */
...@@ -8,6 +8,9 @@ string_values = { ...@@ -8,6 +8,9 @@ string_values = {
"t_acc":"a", #template for accents "t_acc":"a", #template for accents
"t_deflabel":"lb", "t_deflabel":"lb",
"t_ex":["ux", "usex"], "t_ex":["ux", "usex"],
"sense_pattern":[ ## structure(s) for sense patterns add_subdef is to be added to def patterns
{"def":"\\#", "ex":"\\#[:;]", "add_subdef":"\\#"}
],
"POS": { #https://en.wiktionary.org/wiki/Wiktionary:POS "POS": { #https://en.wiktionary.org/wiki/Wiktionary:POS
"Adjective":"Adj", "Adjective":"Adj",
"Adverb":"Adv", "Adverb":"Adv",
......
#!/usr/bin/env python3 #!/usr/bin/env python3
from wikstraktor import Wikstraktor, Pronunciation, Sense from wikstraktor import Wikstraktor, Pronunciation, Sense, SubSense
from parsers.en_constants import string_values from parsers.en_constants import string_values
...@@ -41,64 +41,13 @@ class En_en_straktor(Wikstraktor): ...@@ -41,64 +41,13 @@ class En_en_straktor(Wikstraktor):
global debugEty global debugEty
debugEty += 1 debugEty += 1
return "Etymology" + str(debugEty) return "Etymology" + str(debugEty)
def process_POS(self,parsedwikitext): def process_POS(self,parsedwikitext):
pos = None pos = None
if parsedwikitext in self.constants['POS'].keys(): if parsedwikitext in self.constants['POS'].keys():
pos = self.constants['POS'][parsedwikitext] pos = self.constants['POS'][parsedwikitext]
return pos return pos
def process_senses(self, entry, pos, sensesContent):
baseId = f"{entry}_{pos}_"
l = sensesContent.get_lists(('\\# ', '\\#:','\\## ', '\\##:' ))
i = 0
senses = []
nombreDef = 0
while i < len(l):
if l[i].pattern == '\\# ':
nombreDef += 1
newSense = Sense(f"{baseId}{nombreDef}")
newSense.add_def(self.wiki_language, self.wtp.parse(l[i].items[0]).plain_text().strip())
elif l[i].pattern == '\\#:':
for j in l[i].items:
k = 0
isEx = 0
while k < len(self.wtp.parse(j).templates) and isEx == 0 :
if (self.wtp.parse(j).templates[k].normal_name() in self.constants['t_ex']):
newSense.add_example(self.wtp.parse(j).templates[0].arguments[-1].value)
isEx = 1
k += 1
if isEx == 0:
newSense.add_example(self.wtp.parse(j).plain_text().strip())
if i == len(l)-1 or l[i+1].pattern == '\\# ' or l[i+1].pattern == '\\## ':
senses.append(newSense)
cnt = 0
nombreSousDef = 0
while i < len(l) and l[i].level == 3 :
cnt +=1
if l[i].pattern == '\\## ':
nombreSousDef += 1
newSense2 = Sense(f"{baseId}{nombreDef}_{nombreSousDef}")
newSense2.add_def(self.wiki_language, self.wtp.parse(l[i].items[0]).plain_text().strip())
elif l[i].pattern == '\\##:':
for j in l[i].items:
k = 0
isEx = 0
while k < len(self.wtp.parse(j).templates) and isEx == 0 :
if (self.wtp.parse(j).templates[k].normal_name() in self.constants['t_ex']):
newSense2.add_example(self.wtp.parse(j).templates[0].arguments[-1].value)
isEx = 1
k += 1
if isEx == 0:
newSense2.add_example(self.wtp.parse(j).plain_text().strip())
if i == len(l)-1 or l[i+1].pattern == '\\# ' or l[i+1].pattern == '\\## ':
senses.append(newSense2)
i += 1
if cnt > 0:
i -= 1
i += 1
return senses
if __name__ == "__main__": if __name__ == "__main__":
ensk = En_en_straktor() ensk = En_en_straktor()
print(ensk.fetch("test"), "entries added") print(ensk.fetch("test"), "entries added")
...@@ -9,7 +9,9 @@ string_values = { ...@@ -9,7 +9,9 @@ string_values = {
"t_ipa":"pron", #template for transcription "t_ipa":"pron", #template for transcription
"t_snd":"écouter", #template for audio "t_snd":"écouter", #template for audio
"t_acc":["US", "UK"], #template for accents "t_acc":["US", "UK"], #template for accents
"sense_pattern":[ ## structure(s) for sense patterns add_subdef is to be added to def patterns
{"def":"\\#", "ex":"\\#\\*", "add_subdef":"\\#"}
],
"POS":{ "POS":{
"adjectif":["adjectif","adjectif qualificatif","adj"], "adjectif":["adjectif","adjectif qualificatif","adj"],
"adjectif démonstratif":["adjectif démonstratif","adj-dém","adjectif dém"], "adjectif démonstratif":["adjectif démonstratif","adj-dém","adjectif dém"],
...@@ -77,5 +79,5 @@ string_values = { ...@@ -77,5 +79,5 @@ string_values = {
"variante par contrainte typographique":["variante typographique","variante typo","variante par contrainte typographique","var-typo"], "variante par contrainte typographique":["variante typographique","variante typo","variante par contrainte typographique","var-typo"],
"verbe pronominal":["verbe pronominal","verb-pr","verbe pr"], "verbe pronominal":["verbe pronominal","verb-pr","verbe pr"],
"verbe":["verbe","verb"] "verbe":["verbe","verb"]
} }
} }
...@@ -43,7 +43,7 @@ class Fr_en_straktor(Wikstraktor): ...@@ -43,7 +43,7 @@ class Fr_en_straktor(Wikstraktor):
global debugEty global debugEty
debugEty += 1 debugEty += 1
return "Etymology" + str(debugEty) return "Etymology" + str(debugEty)
def process_POS(self,parsedwikitext): def process_POS(self,parsedwikitext):
pos = None pos = None
ik = 0 ik = 0
...@@ -53,62 +53,8 @@ class Fr_en_straktor(Wikstraktor): ...@@ -53,62 +53,8 @@ class Fr_en_straktor(Wikstraktor):
keys = list(self.constants['POS'].keys()) keys = list(self.constants['POS'].keys())
pos = keys[ik] pos = keys[ik]
ik += 1 ik += 1
# print(pos)
return pos return pos
def process_senses(self, entry, pos, sensesContent):
baseId = f"{entry}_{pos}_"
l = sensesContent.get_lists(('\\# ', '\\#:','\\## ', '\\##:' ))
i = 0
senses = []
nombreDef = 0
while i < len(l):
if l[i].pattern == '\\# ':
nombreDef += 1
newSense = Sense(f"{baseId}{nombreDef}")
newSense.add_def(self.wiki_language, self.wtp.parse(l[i].items[0]).plain_text().strip())
elif l[i].pattern == '\\#:':
for j in l[i].items:
k = 0
isEx = 0
while k < len(self.wtp.parse(j).templates) and isEx == 0 :
if (self.wtp.parse(j).templates[k].normal_name() in self.constants['t_ex']):
newSense.add_example(self.wtp.parse(j).templates[0].arguments[-1].value)
isEx = 1
k += 1
if isEx == 0:
newSense.add_example(self.wtp.parse(j).plain_text().strip())
if i == len(l)-1 or l[i+1].pattern == '\\# ' or l[i+1].pattern == '\\## ':
senses.append(newSense)
cnt = 0
nombreSousDef = 0
while i < len(l) and l[i].level == 3 :
cnt +=1
if l[i].pattern == '\\## ':
nombreSousDef += 1
newSense2 = Sense(f"{baseId}{nombreDef}_{nombreSousDef}")
newSense2.add_def(self.wiki_language, self.wtp.parse(l[i].items[0]).plain_text().strip())
elif l[i].pattern == '\\##:':
for j in l[i].items:
k = 0
isEx = 0
while k < len(self.wtp.parse(j).templates) and isEx == 0 :
if (self.wtp.parse(j).templates[k].normal_name() in self.constants['t_ex']):
newSense2.add_example(self.wtp.parse(j).templates[0].arguments[-1].value)
isEx = 1
k += 1
if isEx == 0:
newSense2.add_example(self.wtp.parse(j).plain_text().strip())
if i == len(l)-1 or l[i+1].pattern == '\\# ' or l[i+1].pattern == '\\## ':
senses.append(newSense2)
i += 1
if cnt > 0:
i -= 1
i += 1
return senses
if __name__ == "__main__": if __name__ == "__main__":
ensk = Fr_en_straktor() ensk = Fr_en_straktor()
print(ensk.fetch("test"), "entries added") print(ensk.fetch("test"), "entries added")
#!/usr/bin/env python3
import git
sha = git.Repo(search_parent_directories=True).head.object.hexsha
v = open("wikstraktor_version.py", "w")
v.write(f"version = '{sha}'")
v.close()
from wikstraktor import Wikstraktor
if __name__ == "__main__":
#e = Wikstraktor.get_instance('en', "en")
f = Wikstraktor.get_instance('en', 'en')
# print(e.get_file_url("File:LL-Q1860 (eng)-Nattes à chat----parent.wav"))
# print(e.get_file_url("File:LL-Q1860 (eng)-Nattes à chat-parent.wav"))
#e.fetch("water")
f.fetch("water")
# print(e.fetch("test"), "entries added")
#print(e)
file_path = 'test.json'
fichier = open(file_path, "w")
#fichier.write(str(f))
fichier.write(str(f))
fichier.close()
# site = pywikibot.Site(f'wiktionary:en')
# p = pywikibot.FilePage(site, "File:LL-Q1860 (eng)-Nattes à chat----parent.wav")
# print(p)
# if not p.exists():
# site = pywikibot.Site('commons')
# p = pywikibot.FilePage(site, "File:LL-Q1860 (eng)-Nattes à chat-parent.wav")
# print(p.get_file_url())
#print(e)
#Entry("test", wtp.parse(page.text)))
# PRENDS PAS LE FICHIER AUDIO POUR "LIVE" EN_EN
#!/usr/bin/env python3
import sqlite3
class Wikstraklog:
table_name = "Wikstraklog"
def __init__(self, wikstraktor_version, word_language, wiki_language, file="wikstraktor.sqlite"):
self.__connector__ = sqlite3.connect(file)
self.__cursor__ = self.__connector__.cursor()
if self.__execute__(f"SELECT name FROM sqlite_master WHERE type='table' AND name='{Wikstraklog.table_name}';").fetchone() is None :
self.__execute__(f"""CREATE TABLE "{Wikstraklog.table_name}" (
"Date" DATETIME DEFAULT CURRENT_TIMESTAMP,
"Wikstraktor_version" TEXT NOT NULL,
"Word_language" TEXT NOT NULL,
"Wiki_language" TEXT NOT NULL,
"Word_form" TEXT NOT NULL,
"Wiki_permanent_id" INTEGER NOT NULL,
"Caller_method" TEXT,
"Content" TEXT
);""")
self.wx_v = wikstraktor_version
self.w_l = word_language
self.wk_l = wiki_language
def set_context(self, word, permanentId):
self.cur_w = word
self.cur_pid = permanentId
def __execute__(self, query, params = None):
if params == None:
res = self.__cursor__.execute(query)
else:
res = self.__cursor__.execute(query, params)
self.__connector__.commit()
return res
def add_log(self, caller, content, word=None, permanentId=None):
if word == None:
word = self.cur_w
if permanentId == None:
permanentId = self.cur_pid
res = self.__execute__(f"INSERT INTO `{Wikstraklog.table_name}` (`Wikstraktor_version`, `Word_language`, `Wiki_language`, `Word_form`, `Wiki_permanent_id`, `Caller_method`, `Content`) VALUES (?, ?, ?, ?, ?, ?, ?)", (self.wx_v, self.w_l, self.wk_l, word, permanentId, caller, str(content)))
return res
if __name__ == "__main__":
from wikstraktor_version import version as the_version
log = Wikstraklog(the_version, "en", "fr")
log.set_context("blue", 123456789)
log.add_log("exampleMethod", "no relevant content")
This diff is collapsed.
File added
#!/usr/bin/env python3
from flask import Flask #server
from flask import request #to handle the different http requests
from flask import Response #to reply (we could use jsonify as well but we handled it)
from flask_cors import CORS #to allow cross-origin requests
from wikstraktor import Wikstraktor
import wikstraktor_server_config as config
app = Flask(__name__)
CORS(app)
@app.route('/', methods=['GET'])
def index():
c = request.remote_addr
response = f"<p>Server is running, your ip is {c}</p>"
return Response(response, 200)
@app.route('/search/<word>', methods=['GET'])
def default_search(word):
return search(config.wiktlang, config.wordlang, word)
@app.route('/search/<wiktlang>/<wordlang>/<word>', methods=['GET'])
def search(wiktlang, wordlang, word):
w = Wikstraktor.get_instance(wiktlang, wordlang)
if w.fetch(word) > 0:
resp = w.__str__()
status = 200
mimetype='application/json'
else:
resp = f"""<!doctype html>
<html>
<head>
<title>Error</title>
</head>
<body>
<h1>{word}</h1>
<p>{word} is unknown in “{wordlang}” in {wiktlang}.wiktionary.org.</p>
</body>
</html>"""
status = 404
mimetype='text/html'
return Response(resp, status=status, mimetype=mimetype)
if __name__ == "__main__":
app.run(host=config.host, port=config.port, debug=config.debugging)
wiktlang = "en"
wordlang = "en"
host = "0.0.0.0"
port = 5000
debugging = True
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
{% if app.user.picture %} {% if app.user.picture %}
<p><img src="{{ path('user_picture') }}" style="max-width: 120px; max-height: 120px;"></p> <p><img src="{{ path('user_picture') }}" style="max-width: 120px; max-height: 120px;"></p>
{% else %} {% else %}
<p>{{ app.user|badge }}</p> <p>{{ app.user|badgeXl }}</p>
{% endif %} {% endif %}
</div> </div>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment