Skip to content
Snippets Groups Projects
Commit b38252cd authored by Mathieu Loiseau's avatar Mathieu Loiseau
Browse files

wikstraktor server

parent f18ac0e9
No related branches found
No related tags found
No related merge requests found
...@@ -9,3 +9,4 @@ user_list.py ...@@ -9,3 +9,4 @@ user_list.py
KNM.csv KNM.csv
.~lock* .~lock*
test.json test.json
wikstraktorenv
...@@ -19,5 +19,38 @@ This project does depend on python packages. ...@@ -19,5 +19,38 @@ This project does depend on python packages.
* [```pip install wikitextparser```](https://pypi.org/project/wikitextparser/) * [```pip install wikitextparser```](https://pypi.org/project/wikitextparser/)
* [```pip install importlib```](https://pypi.org/project/importlib/) * [```pip install importlib```](https://pypi.org/project/importlib/)
### Wikstraktor Server
If you want wikstraktor as a server, you need to install [flask](https://flask.palletsprojects.com/en/2.0.x/installation/), 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
```
## Use
### Wikstraktor
```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
```
### 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 :
```http
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 ## Licence
TODO but will be open source TODO but will be open source
...@@ -373,14 +373,14 @@ class Wikstraktor: ...@@ -373,14 +373,14 @@ class Wikstraktor:
res = title in self.constants['pro'] res = title in self.constants['pro']
print(title, res) print(title, res)
return res return res
def isEty(self, title): def isEty(self, title):
if type(self.constants['ety']) == str: if type(self.constants['ety']) == str:
res = title == self.constants['ety'] res = title == self.constants['ety']
else: else:
res = title in self.constants['ety'] res = title in self.constants['ety']
return res return res
def process_POS(self, parsedwikitext): def process_POS(self, parsedwikitext):
pass#in subclass pass#in subclass
......
#!/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 wikstraktor import Wikstraktor
import wikstraktor_server_config as config
app = Flask(__name__)
@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
else:
resp = f"{word} is unknown"
status = 404
return Response(resp, status=status)
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
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