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+
Add quality score (1-10) to UVW 2026 dataset.
|
| 4 |
+
|
| 5 |
+
Quality scoring based on Wikipedia article quality research:
|
| 6 |
+
- Article length (comprehensiveness)
|
| 7 |
+
- Number of sentences (content depth)
|
| 8 |
+
- Sentence density (readability)
|
| 9 |
+
|
| 10 |
+
References:
|
| 11 |
+
- https://meta.wikimedia.org/wiki/Research:Prioritization_of_Wikipedia_Articles/Language-Agnostic_Quality
|
| 12 |
+
- https://dl.acm.org/doi/10.1145/3625286
|
| 13 |
+
"""
|
| 14 |
+
|
| 15 |
+
import json
|
| 16 |
+
import math
|
| 17 |
+
from pathlib import Path
|
| 18 |
+
from tqdm import tqdm
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
INPUT_PATH = Path(__file__).parent.parent / "data" / "processed" / "uvw_2026.jsonl"
|
| 22 |
+
OUTPUT_PATH = Path(__file__).parent.parent / "data" / "processed" / "uvw_2026_quality.jsonl"
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def calculate_quality_score(num_chars: int, num_sentences: int) -> int:
|
| 26 |
+
"""
|
| 27 |
+
Calculate quality score from 1-10 based on article metrics.
|
| 28 |
+
|
| 29 |
+
Scoring criteria:
|
| 30 |
+
- Length score (40%): Based on character count thresholds
|
| 31 |
+
- Sentence score (30%): Based on number of sentences
|
| 32 |
+
- Density score (30%): Based on average sentence length (optimal ~80-150 chars)
|
| 33 |
+
"""
|
| 34 |
+
# 1. Length score (1-10) - based on Wikipedia quality research
|
| 35 |
+
# Longer articles tend to be more comprehensive
|
| 36 |
+
if num_chars < 200:
|
| 37 |
+
length_score = 1
|
| 38 |
+
elif num_chars < 500:
|
| 39 |
+
length_score = 2
|
| 40 |
+
elif num_chars < 1000:
|
| 41 |
+
length_score = 3
|
| 42 |
+
elif num_chars < 2000:
|
| 43 |
+
length_score = 4
|
| 44 |
+
elif num_chars < 5000:
|
| 45 |
+
length_score = 5
|
| 46 |
+
elif num_chars < 10000:
|
| 47 |
+
length_score = 6
|
| 48 |
+
elif num_chars < 20000:
|
| 49 |
+
length_score = 7
|
| 50 |
+
elif num_chars < 50000:
|
| 51 |
+
length_score = 8
|
| 52 |
+
elif num_chars < 100000:
|
| 53 |
+
length_score = 9
|
| 54 |
+
else:
|
| 55 |
+
length_score = 10
|
| 56 |
+
|
| 57 |
+
# 2. Sentence score (1-10) - content depth
|
| 58 |
+
if num_sentences < 3:
|
| 59 |
+
sentence_score = 1
|
| 60 |
+
elif num_sentences < 5:
|
| 61 |
+
sentence_score = 2
|
| 62 |
+
elif num_sentences < 10:
|
| 63 |
+
sentence_score = 3
|
| 64 |
+
elif num_sentences < 20:
|
| 65 |
+
sentence_score = 4
|
| 66 |
+
elif num_sentences < 50:
|
| 67 |
+
sentence_score = 5
|
| 68 |
+
elif num_sentences < 100:
|
| 69 |
+
sentence_score = 6
|
| 70 |
+
elif num_sentences < 200:
|
| 71 |
+
sentence_score = 7
|
| 72 |
+
elif num_sentences < 500:
|
| 73 |
+
sentence_score = 8
|
| 74 |
+
elif num_sentences < 1000:
|
| 75 |
+
sentence_score = 9
|
| 76 |
+
else:
|
| 77 |
+
sentence_score = 10
|
| 78 |
+
|
| 79 |
+
# 3. Density score (1-10) - readability
|
| 80 |
+
# Optimal Vietnamese sentence length: ~80-150 chars
|
| 81 |
+
if num_sentences > 0:
|
| 82 |
+
avg_sentence_len = num_chars / num_sentences
|
| 83 |
+
if avg_sentence_len < 20: # Too short - likely fragments
|
| 84 |
+
density_score = 3
|
| 85 |
+
elif avg_sentence_len < 40:
|
| 86 |
+
density_score = 5
|
| 87 |
+
elif avg_sentence_len < 80:
|
| 88 |
+
density_score = 8
|
| 89 |
+
elif avg_sentence_len < 150: # Optimal range
|
| 90 |
+
density_score = 10
|
| 91 |
+
elif avg_sentence_len < 250:
|
| 92 |
+
density_score = 7
|
| 93 |
+
elif avg_sentence_len < 400:
|
| 94 |
+
density_score = 5
|
| 95 |
+
else: # Too long - hard to read
|
| 96 |
+
density_score = 3
|
| 97 |
+
else:
|
| 98 |
+
density_score = 1
|
| 99 |
+
|
| 100 |
+
# Weighted average: length (40%), sentences (30%), density (30%)
|
| 101 |
+
final_score = (length_score * 0.4) + (sentence_score * 0.3) + (density_score * 0.3)
|
| 102 |
+
|
| 103 |
+
# Round to nearest integer, ensure 1-10 range
|
| 104 |
+
return max(1, min(10, round(final_score)))
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
def main():
|
| 108 |
+
"""Add quality scores to dataset."""
|
| 109 |
+
print("Adding quality scores to UVW 2026 dataset...")
|
| 110 |
+
print(f"Input: {INPUT_PATH}")
|
| 111 |
+
print(f"Output: {OUTPUT_PATH}")
|
| 112 |
+
|
| 113 |
+
# Count lines first
|
| 114 |
+
with open(INPUT_PATH, "r", encoding="utf-8") as f:
|
| 115 |
+
total = sum(1 for _ in f)
|
| 116 |
+
|
| 117 |
+
# Process and add quality scores
|
| 118 |
+
quality_distribution = {i: 0 for i in range(1, 11)}
|
| 119 |
+
|
| 120 |
+
with open(INPUT_PATH, "r", encoding="utf-8") as fin, \
|
| 121 |
+
open(OUTPUT_PATH, "w", encoding="utf-8") as fout:
|
| 122 |
+
|
| 123 |
+
for line in tqdm(fin, total=total, desc="Processing"):
|
| 124 |
+
article = json.loads(line)
|
| 125 |
+
|
| 126 |
+
# Calculate quality score
|
| 127 |
+
quality = calculate_quality_score(
|
| 128 |
+
article["num_chars"],
|
| 129 |
+
article["num_sentences"]
|
| 130 |
+
)
|
| 131 |
+
article["quality"] = quality
|
| 132 |
+
quality_distribution[quality] += 1
|
| 133 |
+
|
| 134 |
+
fout.write(json.dumps(article, ensure_ascii=False) + "\n")
|
| 135 |
+
|
| 136 |
+
# Print distribution
|
| 137 |
+
print("\nQuality score distribution:")
|
| 138 |
+
print("-" * 40)
|
| 139 |
+
for score in range(1, 11):
|
| 140 |
+
count = quality_distribution[score]
|
| 141 |
+
pct = count / total * 100
|
| 142 |
+
bar = "█" * int(pct / 2)
|
| 143 |
+
print(f" {score:2d}: {count:8,} ({pct:5.1f}%) {bar}")
|
| 144 |
+
|
| 145 |
+
print(f"\nTotal articles: {total:,}")
|
| 146 |
+
print(f"Output saved to: {OUTPUT_PATH}")
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
if __name__ == "__main__":
|
| 150 |
+
main()
|