feat(tags): rewrite tag_frequency to read SQLite and export per-category CSVs
Browse filesReplaces the Parquet-based counter so CSVs come straight from the live
database without an intermediate parquet export step.
- scripts/tag_frequency.py +121 -17
scripts/tag_frequency.py
CHANGED
|
@@ -1,28 +1,132 @@
|
|
| 1 |
-
import
|
|
|
|
|
|
|
| 2 |
from collections import Counter
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
from tqdm import tqdm
|
| 6 |
|
| 7 |
-
from _paths import
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
def main() -> None:
|
| 11 |
-
TAGS_DIR.mkdir(parents=True, exist_ok=True)
|
| 12 |
-
table = pq.read_table(str(PARQUET_PATH), columns=["tag_string_general"])
|
| 13 |
-
tag_columns = ["tag_string_general"]
|
| 14 |
counter: Counter[str] = Counter()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
|
| 28 |
if __name__ == "__main__":
|
|
|
|
| 1 |
+
import argparse
|
| 2 |
+
import csv
|
| 3 |
+
import sqlite3
|
| 4 |
from collections import Counter
|
| 5 |
+
from pathlib import Path
|
| 6 |
|
| 7 |
+
from rich.progress import BarColumn, Progress, TaskProgressColumn, TextColumn, TimeElapsedColumn
|
|
|
|
| 8 |
|
| 9 |
+
from _paths import DANBOORU_DB_PATH, TAGS_DIR
|
| 10 |
|
| 11 |
+
DEFAULT_TAG_FIELDS = (
|
| 12 |
+
"tag_string_general",
|
| 13 |
+
"tag_string_artist",
|
| 14 |
+
"tag_string_character",
|
| 15 |
+
"tag_string_copyright",
|
| 16 |
+
)
|
| 17 |
+
ALLOWED_TAG_FIELDS = set(DEFAULT_TAG_FIELDS) | {"tag_string", "tag_string_meta"}
|
| 18 |
+
OUTPUT_FILENAMES = {
|
| 19 |
+
"tag_string_general": "tag_frequency_general.csv",
|
| 20 |
+
"tag_string_artist": "tag_frequency_artist.csv",
|
| 21 |
+
"tag_string_character": "tag_frequency_character.csv",
|
| 22 |
+
"tag_string_copyright": "tag_frequency_copyright.csv",
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def parse_args() -> argparse.Namespace:
|
| 27 |
+
parser = argparse.ArgumentParser(
|
| 28 |
+
description="Count tag frequency from a Danbooru SQLite database and save it as CSV."
|
| 29 |
+
)
|
| 30 |
+
parser.add_argument("--database", type=str, default=str(DANBOORU_DB_PATH))
|
| 31 |
+
parser.add_argument("--tag-field", type=str, default=None, choices=sorted(ALLOWED_TAG_FIELDS))
|
| 32 |
+
parser.add_argument("--output", type=str, default=None)
|
| 33 |
+
parser.add_argument("--output-dir", type=str, default=str(TAGS_DIR))
|
| 34 |
+
parser.add_argument("--start-date", type=str, default=None)
|
| 35 |
+
parser.add_argument("--batch-size", type=int, default=10_000)
|
| 36 |
+
args = parser.parse_args()
|
| 37 |
+
if args.batch_size <= 0:
|
| 38 |
+
parser.error("--batch-size must be greater than 0")
|
| 39 |
+
if args.output is not None and args.tag_field is None:
|
| 40 |
+
parser.error("--output requires --tag-field")
|
| 41 |
+
return args
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def build_queries(tag_field: str, start_date: str | None) -> tuple[str, str, tuple[str, ...]]:
|
| 45 |
+
where_clause = ""
|
| 46 |
+
params: tuple[str, ...] = ()
|
| 47 |
+
if start_date is not None:
|
| 48 |
+
where_clause = " WHERE created_at > ?"
|
| 49 |
+
params = (start_date,)
|
| 50 |
+
|
| 51 |
+
total_query = f"SELECT COUNT(*) FROM posts{where_clause}"
|
| 52 |
+
data_query = f"SELECT {tag_field} FROM posts{where_clause}"
|
| 53 |
+
return total_query, data_query, params
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
def count_tags(
|
| 57 |
+
connection: sqlite3.Connection,
|
| 58 |
+
tag_field: str,
|
| 59 |
+
start_date: str | None,
|
| 60 |
+
batch_size: int,
|
| 61 |
+
) -> Counter[str]:
|
| 62 |
+
total_query, data_query, params = build_queries(tag_field, start_date)
|
| 63 |
+
total_count = connection.execute(total_query, params).fetchone()
|
| 64 |
+
total_rows = int(total_count[0]) if total_count is not None else 0
|
| 65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
counter: Counter[str] = Counter()
|
| 67 |
+
cursor = connection.execute(data_query, params)
|
| 68 |
+
|
| 69 |
+
with Progress(
|
| 70 |
+
TextColumn("[progress.description]{task.description}"),
|
| 71 |
+
BarColumn(),
|
| 72 |
+
TaskProgressColumn(),
|
| 73 |
+
TimeElapsedColumn(),
|
| 74 |
+
) as progress:
|
| 75 |
+
task = progress.add_task("Counting tags", total=total_rows)
|
| 76 |
+
while True:
|
| 77 |
+
rows = cursor.fetchmany(batch_size)
|
| 78 |
+
if not rows:
|
| 79 |
+
break
|
| 80 |
+
|
| 81 |
+
for (tag_string,) in rows:
|
| 82 |
+
if not tag_string:
|
| 83 |
+
continue
|
| 84 |
+
counter.update(str(tag_string).split())
|
| 85 |
|
| 86 |
+
progress.update(task, advance=len(rows))
|
| 87 |
+
|
| 88 |
+
return counter
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
def write_csv(output_file: Path, counter: Counter[str]) -> None:
|
| 92 |
+
output_path = Path(output_file)
|
| 93 |
+
output_path.parent.mkdir(parents=True, exist_ok=True)
|
| 94 |
+
with output_path.open("w", encoding="utf-8", newline="") as csv_file:
|
| 95 |
+
writer = csv.writer(csv_file)
|
| 96 |
+
writer.writerow(["tag", "count"])
|
| 97 |
+
for tag, count in counter.most_common():
|
| 98 |
+
writer.writerow([tag, count])
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
def build_output_path(output_dir: Path, tag_field: str) -> Path:
|
| 102 |
+
return output_dir / OUTPUT_FILENAMES.get(tag_field, f"{tag_field}.csv")
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
def iter_tag_fields(tag_field: str | None) -> list[str]:
|
| 106 |
+
if tag_field is not None:
|
| 107 |
+
return [tag_field]
|
| 108 |
+
return list(DEFAULT_TAG_FIELDS)
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
def main() -> None:
|
| 112 |
+
args = parse_args()
|
| 113 |
+
output_dir = Path(args.output_dir)
|
| 114 |
+
tag_fields = iter_tag_fields(args.tag_field)
|
| 115 |
|
| 116 |
+
connection = sqlite3.connect(args.database)
|
| 117 |
+
try:
|
| 118 |
+
for tag_field in tag_fields:
|
| 119 |
+
counter = count_tags(
|
| 120 |
+
connection=connection,
|
| 121 |
+
tag_field=tag_field,
|
| 122 |
+
start_date=args.start_date,
|
| 123 |
+
batch_size=args.batch_size,
|
| 124 |
+
)
|
| 125 |
+
output_path = Path(args.output) if args.output is not None else build_output_path(output_dir, tag_field)
|
| 126 |
+
write_csv(output_file=output_path, counter=counter)
|
| 127 |
+
print(f"Saved {len(counter)} tags to {output_path}")
|
| 128 |
+
finally:
|
| 129 |
+
connection.close()
|
| 130 |
|
| 131 |
|
| 132 |
if __name__ == "__main__":
|