mooc-rr/module2/exo4/analyse-journal.org
2022-11-20 20:51:53 +01:00

3.2 KiB

Analyse du journal

Récupération des données du journal

Ici nous allons importer les étiquettes et les exporter dans un fichier data.csv.

grep -oP "(?<=:)([a-zA-Z]*)(?=:)" ~/org/journal.org > data.csv
head -n 5 data.csv
informatique
wikipedia
biologie
virus
allergie

Traitement des données

import csv
from collections import Counter

temporaryList = []

with open('data.csv', 'r', encoding='utf8') as csvfile:
    reader = csv.reader(csvfile)
    for row in reader:
        temporaryList.append(row[0])

tagCount = Counter(temporaryList)

tagList = []
countList = []

for tag in tagCount:
    tagList.append(tag)
    countList.append(tagCount[tag])

Ici on convertit les données en dataframe pandas afin de pouvoir faire l'affichage plus facilement.

import pandas as pd

preDataframe = dict(tagCount)


print(preDataframe)

tagCountDataframe = pd.DataFrame.from_dict({'tags':list(preDataframe), 'values':list(preDataframe.values())})
tagCountDataframe['values'] = pd.to_numeric(tagCountDataframe['values'])

print(tagCountDataframe)
{'informatique': 1, 'wikipedia': 1, 'biologie': 2, 'virus': 1, 'allergie': 1, 'LOGBOOK': 2, 'END': 2}
           tags  values
0  informatique       1
1     wikipedia       1
2      biologie       2
3         virus       1
4      allergie       1
5       LOGBOOK       2
6           END       2

Affichage des données

Diverses infos

print(f"Les tags les plus cités : {tagCount.most_common(3)}")
Les tags les plus cités : [('biologie', 2), ('LOGBOOK', 2), ('END', 2)]

Graphiques

import matplotlib.pyplot as plt
plt.figure(figsize=(10,5))
plt.tight_layout()

# Affichage 
ax = tagCountDataframe.plot(x="tags", y="values", kind='bar')

plt.savefig(matplot_lib_filename)
matplot_lib_filename

/tmp/babel-DfWAKd/figureDe0QxU.png