Skip to content
Snippets Groups Projects
Commit 325528de authored by Ludovic Moncla's avatar Ludovic Moncla
Browse files

Update Tutoriel-geoparsing.ipynb

parent 244a893c
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
![CNRS](https://anf-tdm-2022.sciencesconf.org/data/header/LOGO_CNRS_CMJN_150x150.png) ![CNRS](https://anf-tdm-2022.sciencesconf.org/data/header/LOGO_CNRS_CMJN_150x150.png)
# Tutoriel - ANF TDM 2022 Python Geoparsing # Tutoriel - ANF TDM 2022 Python Geoparsing
Supports pour l'atelier [Librairies Python et Services Web pour la reconnaissance d’entités nommées et la résolution de toponymes](https://anf-tdm-2022.sciencesconf.org/resource/page/id/11) de la formation CNRS [ANF TDM 2022](https://anf-tdm-2022.sciencesconf.org). Supports pour l'atelier [Librairies Python et Services Web pour la reconnaissance d’entités nommées et la résolution de toponymes](https://anf-tdm-2022.sciencesconf.org/resource/page/id/11) de la formation CNRS [ANF TDM 2022](https://anf-tdm-2022.sciencesconf.org).
## 1. En bref ## 1. En bref
Dans ce tutoriel, nous allons apprendre plusieurs choses : Dans ce tutoriel, nous allons apprendre plusieurs choses :
- Charger des jeux de données : - Charger des jeux de données :
- à partir de fichiers txt importés depuis le disque dur ; - à partir de fichiers txt importés depuis le disque dur ;
- à partir de la librairie Python [Perdido](https://github.com/ludovicmoncla/perdido) dans un [Pandas dataframe](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.html) (articles encyclopédiques et descriptions de randonnées). - à partir de la librairie Python [Perdido](https://github.com/ludovicmoncla/perdido) dans un [Pandas dataframe](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.html) (articles encyclopédiques et descriptions de randonnées).
- Manipuler et interroger un dataframe - Manipuler et interroger un dataframe
- Utiliser les librairies [Stanza](https://stanfordnlp.github.io/stanza/index.html), [spaCy](https://spacy.io) et [Perdido](https://github.com/ludovicmoncla/perdido) pour la reconnaissance d'entités nommées - Utiliser les librairies [Stanza](https://stanfordnlp.github.io/stanza/index.html), [spaCy](https://spacy.io) et [Perdido](https://github.com/ludovicmoncla/perdido) pour la reconnaissance d'entités nommées
- afficher les entités nommées annotées ; - afficher les entités nommées annotées ;
- comparer les résultats de `Stanza`, `spaCy` et `Perdido` ; - comparer les résultats de `Stanza`, `spaCy` et `Perdido` ;
- discuter les limites des 3 outils pour la tâche de NER. - discuter les limites des 3 outils pour la tâche de NER.
- Utiliser la librarie `Perdido` pour le geoparsing : - Utiliser la librarie `Perdido` pour le geoparsing :
- cartographier les lieux geocodés ; - cartographier les lieux geocodés ;
- illustrer la problématique de désambiguïsation des toponymes. - illustrer la problématique de désambiguïsation des toponymes.
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## 2. Introduction ## 2. Introduction
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## 3. Configurer l'environnement ## 3. Configurer l'environnement
### 3.1 Installer les librairies Python ### 3.1 Installer les librairies Python
* Si vous avez configuré votre environnement Conda en utilisant le fichier `requirements.txt`, vous pouvez sauter cette étape et aller à la section `3.2 Importer les librairies`. * Si vous avez configuré votre environnement Conda en utilisant le fichier `requirements.txt`, vous pouvez sauter cette étape et aller à la section `3.2 Importer les librairies`.
* Si vous avez configuré votre environnement Conda en utilisant le fichier `environment.yml` ou si vous utilisez un environnement Google Colab / Binder, vous devez installer `perdido` en utilisant `pip` : * Si vous avez configuré votre environnement Conda en utilisant le fichier `environment.yml` ou si vous utilisez un environnement Google Colab / Binder, vous devez installer `perdido` en utilisant `pip` :
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
!pip install --upgrade perdido !pip install perdido
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
* Si vous avez déjà configuré votre environnement conda, soit avec conda, soit avec pip (voir le fichier readme), vous pouvez ignorer la cellule suivante. * Si vous avez déjà configuré votre environnement conda, soit avec conda, soit avec pip (voir le fichier readme), vous pouvez ignorer la cellule suivante.
* Si vous exécutez ce notebook depuis Google Colab / Binder, vous devez exécuter la cellule suivante : * Si vous exécutez ce notebook depuis Google Colab / Binder, vous devez exécuter la cellule suivante :
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
!pip install stanza !pip install stanza
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
### 3.2 Importer les librairies ### 3.2 Importer les librairies
Tout d'abord, nous allons charger certaines bibliothèques spécifiques de `Perdido` que nous utiliserons dans ce notebook. Ensuite, nous importons quelques outils qui nous aideront à analyser et à visualiser le texte. Tout d'abord, nous allons charger certaines bibliothèques spécifiques de `Perdido` que nous utiliserons dans ce notebook. Ensuite, nous importons quelques outils qui nous aideront à analyser et à visualiser le texte.
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
import warnings import warnings
warnings.filterwarnings('ignore') warnings.filterwarnings('ignore')
from perdido.geoparser import Geoparser from perdido.geoparser import Geoparser
from perdido.geocoder import Geocoder from perdido.geocoder import Geocoder
from perdido.datasets import load_edda_artfl, load_edda_perdido, load_choucas_perdido from perdido.datasets import load_edda_artfl, load_edda_perdido, load_choucas_perdido
from spacy import displacy from spacy import displacy
``` ```
%% Output
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
/Users/lmoncla/git/gitlab.liris/lmoncla/tutoriel-anf-tdm-2022-python-geoparsing/Tutoriel-geoparsing.ipynb Cellule 8 in <cell line: 7>()
<a href='vscode-notebook-cell:/Users/lmoncla/git/gitlab.liris/lmoncla/tutoriel-anf-tdm-2022-python-geoparsing/Tutoriel-geoparsing.ipynb#X10sZmlsZQ%3D%3D?line=3'>4</a> from perdido.geoparser import Geoparser
<a href='vscode-notebook-cell:/Users/lmoncla/git/gitlab.liris/lmoncla/tutoriel-anf-tdm-2022-python-geoparsing/Tutoriel-geoparsing.ipynb#X10sZmlsZQ%3D%3D?line=4'>5</a> from perdido.geocoder import Geocoder
----> <a href='vscode-notebook-cell:/Users/lmoncla/git/gitlab.liris/lmoncla/tutoriel-anf-tdm-2022-python-geoparsing/Tutoriel-geoparsing.ipynb#X10sZmlsZQ%3D%3D?line=6'>7</a> from perdido.datasets import load_edda_artfl, load_edda_perdido, load_choucas_perdido
<a href='vscode-notebook-cell:/Users/lmoncla/git/gitlab.liris/lmoncla/tutoriel-anf-tdm-2022-python-geoparsing/Tutoriel-geoparsing.ipynb#X10sZmlsZQ%3D%3D?line=8'>9</a> from spacy import displacy
ImportError: cannot import name 'load_choucas_perdido' from 'perdido.datasets' (/opt/homebrew/Caskroom/miniforge/base/envs/sunoikisis-py39/lib/python3.9/site-packages/perdido/datasets.py)
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## 4. Chargement et exploration des données ## 4. Chargement et exploration des données
### 4.1 Chargement d'un document texte à partir d'un fichier ### 4.1 Chargement d'un document texte à partir d'un fichier
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
filepath = 'data/volume01-4083.txt' filepath = 'data/volume01-4083.txt'
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
with open(filepath) as f: with open(filepath) as f:
content = f.read() content = f.read()
print(content)
``` ```
%% Output %% Cell type:markdown id: tags:
* Afficher le contenu du fichier
* ARQUES, (Géog.) petite ville de France, en Normandie, au pays de Caux, sur la petite riviere d'Arques. Long. 18. 50. lat. 49. 54. %% Cell type:code id: tags:
``` python
print(content)
```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
### 4.2 Chargement d'un jeu de données à partir de la librairie Perdido ### 4.2 Chargement d'un jeu de données à partir de la librairie Perdido
Perdido embarque 2 jeux de données : Perdido embarque 2 jeux de données :
1. articles encyclopédiques (volume 7 de l'Encyclopédie de Diderot et d'Alembert), fournit par l'ARTFL dans le cadre du projet GEODE. 1. articles encyclopédiques (volume 7 de l'Encyclopédie de Diderot et d'Alembert (1751-1772)), fournit par l'[ARTFL](https://encyclopedie.uchicago.edu) dans le cadre du projet [GEODE](https://geode-project.github.io) ;
2. descriptions de randonnées (chaque description est associée à sa trace GPS. Elles proviennent du site visorando.fr et ont été collectées dans le cadre du projet ANR CHOUCAS. 2. descriptions de randonnées (chaque description est associée à sa trace GPS. Elles proviennent du site [www.visorando.fr](https://www.visorando.com) et ont été collectées dans le cadre du projet [ANR CHOUCAS](http://choucas.ign.fr).
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
d = load_choucas_perdido() d = load_choucas_perdido()
df = d['data'].to_dataframe() df = d['data'].to_dataframe()
df.head() df.head()
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
### 4.3 Manipulation d'un dataframe ### 4.3 Manipulation d'un dataframe
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## 5. Reconnaissance d'Entités Nommées (NER) ## 5. Reconnaissance d'Entités Nommées (NER)
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
### 5.1 Stanza NER ### 5.1 Stanza NER
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
* Importer la librairie `Stanza` et télécharger le modèle pré-entrainé pour le français : * Importer la librairie `Stanza` et télécharger le modèle pré-entrainé pour le français :
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
import stanza import stanza
stanza.download('fr') stanza.download('fr')
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
* Instancier et paramétrer la chaîne de traitement : * Instancier et paramétrer la chaîne de traitement :
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
stanza_parser = stanza.Pipeline(lang='fr', processors='tokenize,ner') stanza_parser = stanza.Pipeline(lang='fr', processors='tokenize,ner')
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
* Executer la reconnaissance d'entités nommées : * Executer la reconnaissance d'entités nommées :
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
doc = stanza_parser(content) doc = stanza_parser(content)
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
* Afficher la liste des entités nommées repérées : * Afficher la liste des entités nommées repérées :
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
for ent in doc.ents: for ent in doc.ents:
print(ent.text, ent.type) print(ent.text, ent.type)
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
### 5.2 SpaCy NER ### 5.2 SpaCy NER
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
* Installer le modèle français pré-entrainé de `spaCy` : * Installer le modèle français pré-entrainé de `spaCy` :
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
!python -m spacy download fr_core_news_sm !python -m spacy download fr_core_news_sm
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
* Importer la librarie `spaCy` : * Importer la librarie `spaCy` :
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
import spacy import spacy
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
* Charger le modèle français pré-entrainé de `spaCy` * Charger le modèle français pré-entrainé de `spaCy`
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
spacy_parser = spacy.load('fr_core_news_sm') spacy_parser = spacy.load('fr_core_news_sm')
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
* Executer la reconnaissance d'entités nommées : * Executer la reconnaissance d'entités nommées :
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
doc = spacy_parser(content) doc = spacy_parser(content)
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
* Afficher la liste des entités nommées repérées : * Afficher la liste des entités nommées repérées :
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
for ent in doc.ents: for ent in doc.ents:
print(ent.text, ent.label_) print(ent.text, ent.label_)
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
* Afficher de manière graphique les entités nommées avec `displaCy` : * Afficher de manière graphique les entités nommées avec `displaCy` :
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
displacy.render(doc, style="ent", jupyter=True) displacy.render(doc, style="ent", jupyter=True)
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
### 5.3 Perdido Geoparser ### 5.3 Perdido Geoparser
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
* Instancier et paramétrer la chaîne de traitement : * Instancier et paramétrer la chaîne de traitement :
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
geoparser = Geoparser(version="Encyclopedie") geoparser = Geoparser(version="Encyclopedie")
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
* Executer la reconnaissance d'entités nommées : * Executer la reconnaissance d'entités nommées :
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
doc = geoparser(content) doc = geoparser(content)
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
* Afficher la liste des entités nommées repérées : * Afficher la liste des entités nommées repérées :
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
for ent in doc.named_entities: for ent in doc.named_entities:
print(ent.text, ent.tag) print(ent.text, ent.tag)
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
* Afficher de manière graphique les entités nommées avec `displaCy` : * Afficher de manière graphique les entités nommées avec `displaCy` :
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
displacy.render(doc.to_spacy_doc(), style="ent", jupyter=True) displacy.render(doc.to_spacy_doc(), style="ent", jupyter=True)
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
* Afficher de manière graphique les entités nommées étendues avec `displaCy` : * Afficher de manière graphique les entités nommées étendues avec `displaCy` :
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
displacy.render(doc.to_spacy_doc(), style="span", jupyter=True) displacy.render(doc.to_spacy_doc(), style="span", jupyter=True)
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
### 5.4 Expérimentations et comparaison ### 5.4 Expérimentations et comparaison
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## 6. Geoparsing / Geocoding ## 6. Geoparsing / Geocoding
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# geocoding avec perdido # geocoding avec perdido
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# afficher une carte # afficher une carte
d['data'][1].get_folium_map() d['data'][1].get_folium_map()
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
### 6.2 Résolution de toponymes / désambiguïsation ### 6.2 Résolution de toponymes / désambiguïsation
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Exemple de requetes sans stratégies de désambiguisation Exemple de requetes sans stratégies de désambiguisation
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Délimiter un zone restreinte lors de la requête Délimiter un zone restreinte lors de la requête
Premier niveau : utilisation d'un code pays. Premier niveau : utilisation d'un code pays.
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Deuxième niveau : utilisation d'une bounding box délimitant la zone de recherche Deuxième niveau : utilisation d'une bounding box délimitant la zone de recherche
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Désambiguisation basé sur la proximité géographique Désambiguisation basé sur la proximité géographique
Clustering avec la méthode DBSCAN. Cette stratégie est adaptée pour une description d'itinéraire où les différents lieux cités doivent être localisés à proximité les uns des autres. Clustering avec la méthode DBSCAN. Cette stratégie est adaptée pour une description d'itinéraire où les différents lieux cités doivent être localisés à proximité les uns des autres.
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
#### Résultats avant désambiguisation #### Résultats avant désambiguisation
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
d['data'][1].get_folium_map() d['data'][1].get_folium_map()
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
d['data'][1].cluster_disambiguation() d['data'][1].cluster_disambiguation()
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
d['data'][1].get_folium_map() d['data'][1].get_folium_map()
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Utilisation du contexte (autres entités nommées repérées dans le texte, relations spatiales, etc...). Développées dans le cadre du projet [Perdido]() (add ref 2014 et 2016) mais pas encore intégré à la librairie Python Perdido. Cette librairie est toujours en cours de développement et d'amélioration. Vos remarques et retours seront les bienvenues. Utilisation du contexte (autres entités nommées repérées dans le texte, relations spatiales, etc...). Développées dans le cadre du projet [Perdido]() (add ref 2014 et 2016) mais pas encore intégré à la librairie Python Perdido. Cette librairie est toujours en cours de développement et d'amélioration. Vos remarques et retours seront les bienvenues.
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
``` ```
......
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