File size: 3,360 Bytes
f555d70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import sqlite3
import pandas as pd
import itertools
import ast
import uuid
from pathlib import Path

# Initialisations
print("Initialisations ...")
DATA_DIR = Path("../data")   # dossier parent du script
PARQUET_FILE = DATA_DIR / "medium_articles.parquet"
SQLITE_FILE = DATA_DIR / "articles.db"
# Créer le dossier data s'il n'existe pas
DATA_DIR.mkdir(exist_ok=True)

# Chargement des données
print("Chargement des données ...")
df = pd.read_parquet(PARQUET_FILE)

# Initialisations de la base SQLite
print("Initialisations de la base SQLite ...")
conn = sqlite3.connect(SQLITE_FILE)
cur = conn.cursor()

# Suppression des anciennes tables
cur.execute("DROP TABLE IF EXISTS tag_article")
cur.execute("DROP TABLE IF EXISTS tags")
cur.execute("DROP TABLE IF EXISTS articles")

# Création des tables Articles, Tags, et de la table d'association articles <-> tags
cur.execute("""

CREATE TABLE articles (

    article_id TEXT PRIMARY KEY,         -- UUID

    article_title TEXT,

    article_text TEXT,

    article_url TEXT,

    article_authors TEXT,

    article_date TEXT                    -- YYYY-MM-DD

)""")

cur.execute("""

CREATE TABLE tags (

    tag_id INTEGER PRIMARY KEY AUTOINCREMENT,

    tag_name TEXT UNIQUE

)""")

cur.execute("""

CREATE TABLE tag_article (

    tag_article_id INTEGER PRIMARY KEY AUTOINCREMENT,

    article_id TEXT,

    tag_id INTEGER,

    FOREIGN KEY(article_id) REFERENCES articles(article_id),

    FOREIGN KEY(tag_id) REFERENCES tags(tag_id)

)""")

# Extraction des tags en une liste unique
print("Extraction des tags en une liste unique ...")
df['list_tags'] = df['tags'].apply(lambda x: ast.literal_eval(x) if isinstance(x, str) else [])
# Extraire tous les tags uniques
all_tags = list(set(itertools.chain.from_iterable(df['list_tags'])))

# Insertion des tags dans la table
print("Insertion des tags dans la table ...")
cur.executemany("INSERT INTO tags (tag_name) VALUES (?)", [(tag,) for tag in all_tags])

# Récupération des correspondances tag_name -> tag_id
print("Récupération des correspondances tag_name -> tag_id ...")
cur.execute("SELECT tag_id, tag_name FROM tags")
dict_tag_map = {tag_name: tag_id for tag_id, tag_name in cur.fetchall()}

# Insertion des articles et table d'association dans les tables
print("Insertion des articles et table d'association dans les tables ...")
for _, row in df.iterrows():
    # Détermination de l'id article
    article_id = str(uuid.uuid4())

    # Extraction de la date du timestamp
    date_value = None
    if pd.notna(row["timestamp"]):
        try:
            date_value = str(pd.to_datetime(row["timestamp"]).date())
        except Exception:
            date_value = None

    # Insertion dans la table Articles
    cur.execute("""

        INSERT INTO articles (article_id, article_title, article_text, article_url, article_authors, article_date)

        VALUES (?, ?, ?, ?, ?, ?)""",
               (article_id, row["title"], row["text"], row["url"], row["authors"], date_value))

    # Association aux tags
    for tag_name in row['list_tags']:
        tag_id = dict_tag_map[tag_name]
        cur.execute("INSERT INTO tag_article (article_id, tag_id) VALUES (?, ?)",
                    (article_id, tag_id))

conn.commit()
conn.close()
print("Traitement terminé.")