File size: 15,663 Bytes
1026b11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
542bb72
 
 
1026b11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
542bb72
 
 
1026b11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/env python3
"""Generate a deterministic, license-clean Thai multitask dataset."""
import hashlib
import json
import random
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]
RNG = random.Random(38027)
LICENSE = "Apache-2.0"

NAMES = ["กานต์", "มะลิ", "นที", "พลอย", "ธันวา", "ขวัญ", "เมฆ", "ตะวัน", "พิม", "อรุณ"]
PLACES = ["เชียงใหม่", "ขอนแก่น", "นครราชสีมา", "กรุงเทพฯ", "ชลบุรี", "สงขลา", "ระยอง", "สุโขทัย"]
OBJECTS = ["สมุด", "ดินสอ", "ส้ม", "ขวดน้ำ", "หนังสือ", "ต้นกล้า", "กล่อง", "ตั๋ว"]
TOPICS = [
    ("การนอนหลับ", "การนอนให้เพียงพอช่วยให้สมองจัดเก็บความจำและฟื้นฟูร่างกาย"),
    ("การออกกำลังกาย", "การเคลื่อนไหวอย่างสม่ำเสมอช่วยเสริมความแข็งแรงและลดความเครียด"),
    ("การออมเงิน", "การแบ่งรายได้บางส่วนไว้ออมช่วยสร้างเงินสำรองสำหรับอนาคต"),
    ("การแยกขยะ", "การแยกวัสดุก่อนทิ้งช่วยให้รีไซเคิลได้ง่ายและลดขยะฝังกลบ"),
    ("การดื่มน้ำ", "การดื่มน้ำให้เหมาะสมช่วยรักษาสมดุลและการทำงานของร่างกาย"),
    ("การอ่านหนังสือ", "การอ่านสม่ำเสมอช่วยเพิ่มความรู้ คำศัพท์ และทักษะการคิด"),
]
POSITIVE = ["บริการรวดเร็วและสุภาพ", "สินค้าเรียบร้อยและใช้งานได้ดี", "อาหารอร่อยเกินคาด", "เจ้าหน้าที่ช่วยเหลือดีมาก"]
NEUTRAL = ["ร้านเปิดเวลาเก้าโมง", "พัสดุมาถึงวันอังคาร", "สินค้ามีสีตามที่ระบุ", "ห้องพักอยู่ชั้นสาม"]
NEGATIVE = ["สินค้าชำรุดเมื่อเปิดกล่อง", "ต้องรอนานกว่าที่แจ้ง", "อาหารเย็นและรสชาติจืด", "ติดต่อเจ้าหน้าที่ไม่ได้"]


def row(task, user, assistant, score=0.94):
    digest = hashlib.sha1(f"{task}\0{user}\0{assistant}".encode()).hexdigest()[:12]
    return {
        "id": f"th_{task}_{digest}",
        "task": task,
        "messages": [{"role": "user", "content": user}, {"role": "assistant", "content": assistant}],
        "source": "synthetic_rules_v1",
        "license": LICENSE,
        "quality_score": score,
    }


def generate():
    rows = []

    # Arithmetic reasoning: exact, automatically verifiable answers.
    for _ in range(2600):
        name, obj = RNG.choice(NAMES), RNG.choice(OBJECTS)
        groups, each = RNG.randint(2, 40), RNG.randint(2, 50)
        total = groups * each
        mode = RNG.randrange(3)
        if mode == 0:
            q = f"{name}มี{obj} {total} ชิ้น แบ่งเท่า ๆ กัน {groups} กลุ่ม แต่ละกลุ่มมีกี่ชิ้น"
            a = f"{total} หารด้วย {groups} เท่ากับ {each} ดังนั้นแต่ละกลุ่มมี {each} ชิ้น"
        elif mode == 1:
            q = f"มี{obj} {groups} กลุ่ม กลุ่มละ {each} ชิ้น รวมทั้งหมดกี่ชิ้น"
            a = f"{groups} คูณ {each} เท่ากับ {total} ดังนั้นมีทั้งหมด {total} ชิ้น"
        else:
            used = RNG.randint(1, total - 1)
            q = f"{name}มี{obj} {total} ชิ้น ใช้ไป {used} ชิ้น เหลือกี่ชิ้น"
            a = f"{total} ลบ {used} เท่ากับ {total-used} ดังนั้นเหลือ {total-used} ชิ้น"
        rows.append(row("math_reasoning", q, a, 0.99))

    # Sentiment classification.
    for label, pool in [("บวก", POSITIVE), ("กลาง", NEUTRAL), ("ลบ", NEGATIVE)]:
        for i in range(400):
            text = RNG.choice(pool)
            context = RNG.choice(["รีวิวร้านค้า", "ความคิดเห็นลูกค้า", "ข้อความหลังรับบริการ", "ประสบการณ์ผู้ใช้"])
            place, day = RNG.choice(PLACES), RNG.randint(1, 31)
            channel = RNG.choice(["หน้าร้าน", "แอปพลิเคชัน", "โทรศัพท์", "แบบสอบถาม"])
            q = f"จำแนกความรู้สึกเป็น บวก กลาง หรือลบ\nประเภทข้อความ: {context}\nช่องทาง: {channel}\nพื้นที่: {place}\nวันที่: {day}\nข้อความ: {text}"
            rows.append(row("sentiment_classification", q, label, 0.98))

    # Translation with compositional but natural short sentences.
    verbs = [("อ่าน", "reads"), ("ซื้อ", "buys"), ("จัดเตรียม", "prepares"), ("ส่ง", "sends"), ("ตรวจสอบ", "checks")]
    en_objects = {"สมุด": "a notebook", "หนังสือ": "a book", "ตั๋ว": "a ticket", "ขวดน้ำ": "a bottle of water"}
    times = [("ตอนเช้า", "in the morning"), ("ทุกวัน", "every day"), ("ก่อนเดินทาง", "before the trip"), ("หลังเลิกงาน", "after work")]
    for _ in range(1000):
        name, place = RNG.choice(NAMES), RNG.choice(PLACES)
        obj = RNG.choice(list(en_objects))
        th_verb, en_verb = RNG.choice(verbs)
        th_time, en_time = RNG.choice(times)
        th = f"{name}{th_verb}{obj}{th_time}ที่{place}"
        en = f"{name} {en_verb} {en_objects[obj]} {en_time} in {place}."
        if RNG.random() < 0.5:
            rows.append(row("translation_th_en", f"แปลเป็นภาษาอังกฤษ: {th}", en, 0.96))
        else:
            rows.append(row("translation_en_th", f"แปลเป็นภาษาไทย: {en}", th, 0.96))

    # Reading comprehension grounded entirely in supplied context.
    for _ in range(1100):
        name, place, obj = RNG.choice(NAMES), RNG.choice(PLACES), RNG.choice(OBJECTS)
        count, hour = RNG.randint(2, 90), RNG.randint(6, 20)
        context = f"{name}เดินทางไป{place}เวลา {hour:02d}:00 น. และนำ{obj}ไป {count} ชิ้น"
        kind = RNG.randrange(3)
        if kind == 0:
            question, answer = "ใครเป็นผู้เดินทาง", name
        elif kind == 1:
            question, answer = "เดินทางไปที่ใด", place
        else:
            question, answer = f"นำ{obj}ไปกี่ชิ้น", f"{count} ชิ้น"
        rows.append(row("question_answering", f"บริบท: {context}\nคำถาม: {question}", answer, 0.99))

    # Information extraction to strict JSON.
    for _ in range(900):
        day, month = RNG.randint(1, 28), RNG.choice(["มกราคม", "มีนาคม", "มิถุนายน", "สิงหาคม", "พฤศจิกายน"])
        hour, minute, place = RNG.randint(8, 18), RNG.choice([0, 15, 30, 45]), RNG.choice(PLACES)
        event = RNG.choice(["ประชุมทีม", "อบรม", "นัดส่งมอบงาน", "สัมมนา"])
        text = f"{event}วันที่ {day} {month} เวลา {hour:02d}:{minute:02d} น. ณ {place}"
        answer = json.dumps({"event": event, "date": f"{day} {month}", "time": f"{hour:02d}:{minute:02d}", "location": place}, ensure_ascii=False, separators=(",", ":"))
        rows.append(row("information_extraction", f"อ่านข้อความแล้วตอบ JSON ที่มี event, date, time, location\n{text}", answer, 0.99))

    # Summarization and explanation.
    for _ in range(900):
        topic, benefit = RNG.choice(TOPICS)
        actor = RNG.choice(["โรงเรียน", "ชุมชน", "ครอบครัว", "สำนักงาน"])
        participants, weeks, place = RNG.randint(10, 200), RNG.randint(2, 16), RNG.choice(PLACES)
        text = f"{actor}ใน{place}จัดกิจกรรมเรื่อง{topic}นาน {weeks} สัปดาห์ มีผู้เข้าร่วม {participants} คน ผู้เข้าร่วมฝึกปฏิบัติและแลกเปลี่ยนประสบการณ์ เพราะ{benefit}"
        rows.append(row("summarization", f"สรุปข้อความต่อไปนี้เป็นหนึ่งประโยค: {text}", f"{actor}ใน{place}จัดกิจกรรม{topic} {weeks} สัปดาห์ให้ผู้เข้าร่วม {participants} คนเรียนรู้และนำไปใช้จริง", 0.93))
    for _ in range(500):
        topic, benefit = RNG.choice(TOPICS)
        audience = RNG.choice(["นักเรียน", "ผู้เริ่มต้น", "ผู้สูงอายุ", "คนทำงาน"])
        age_ranges = {"นักเรียน": (8, 18), "ผู้เริ่มต้น": (18, 65), "ผู้สูงอายุ": (60, 85), "คนทำงาน": (20, 65)}
        age = RNG.randint(*age_ranges[audience])
        rows.append(row("explanation", f"อธิบายประโยชน์ของ{topic}แบบเข้าใจง่ายสำหรับ{audience}อายุ {age} ปี", benefit, 0.95))

    # Formal rewriting and practical conversation.
    informal_formal = [
        ("เดี๋ยวส่งงานให้พรุ่งนี้นะ", "จะจัดส่งงานให้ภายในวันพรุ่งนี้"),
        ("ช่วยดูไฟล์นี้ให้หน่อย", "กรุณาตรวจสอบไฟล์นี้"),
        ("วันนี้ไปประชุมไม่ได้", "วันนี้ไม่สามารถเข้าร่วมการประชุมได้"),
        ("ขอเลื่อนนัดไปอาทิตย์หน้า", "ขอเลื่อนกำหนดนัดหมายเป็นสัปดาห์หน้า"),
        ("ของยังมาไม่ถึงเลย", "ขณะนี้ยังไม่ได้รับสินค้า"),
    ]
    for i in range(700):
        informal, formal = informal_formal[i % len(informal_formal)]
        context = RNG.choice(["สำหรับอีเมลถึงลูกค้า", "สำหรับข้อความถึงหัวหน้างาน", "สำหรับหนังสือแจ้ง", "โดยใช้ภาษาสุภาพ"])
        sender, place = RNG.choice(NAMES), RNG.choice(PLACES)
        rows.append(row("formal_rewriting", f"ผู้ส่งชื่อ{sender}อยู่ที่{place} เขียนให้เป็นทางการ {context}: {informal}", formal, 0.96))
    tips = [
        ("จัดเวลาทำงาน", "กำหนดงานสำคัญของวัน แบ่งเวลาเป็นช่วง และเผื่อเวลาสำหรับงานเร่งด่วน"),
        ("เตรียมตัวเดินทาง", "ตรวจเส้นทาง เอกสารสำคัญ สภาพอากาศ และเผื่อเวลาออกเดินทาง"),
        ("เริ่มออมเงิน", "ตั้งเป้าหมาย แยกเงินออมทันทีเมื่อมีรายได้ และติดตามรายจ่ายทุกเดือน"),
        ("อ่านหนังสือให้จำ", "ทบทวนแบบเว้นระยะ ตั้งคำถามกับเนื้อหา และสรุปด้วยภาษาของตัวเอง"),
    ]
    for i in range(600):
        subject, answer = tips[i % len(tips)]
        style = RNG.choice(["แบบสั้น ๆ", "สำหรับผู้เริ่มต้น", "เป็นสามขั้นตอน", "ให้ทำตามได้ง่าย"])
        name, place, days = RNG.choice(NAMES), RNG.choice(PLACES), RNG.randint(3, 30)
        rows.append(row("conversation", f"{name}อยู่ที่{place}และมีเวลา {days} วัน ช่วยแนะนำวิธี{subject}{style}", answer, 0.94))

    # Intent classification.
    intents = [
        ("ต้องการเปลี่ยนบ้านเลขที่สำหรับรับพัสดุ", "แก้ไขข้อมูลการจัดส่ง"),
        ("อยากยกเลิกสินค้าที่เพิ่งสั่ง", "ยกเลิกคำสั่งซื้อ"),
        ("เงินถูกหักแต่รายการไม่สำเร็จ", "ตรวจสอบการชำระเงิน"),
        ("ขอทราบว่าพัสดุอยู่ที่ไหน", "ติดตามสถานะการจัดส่ง"),
        ("ต้องการคืนสินค้าที่ได้รับ", "ขอคืนสินค้า"),
    ]
    for i in range(500):
        utterance, intent = intents[i % len(intents)]
        ticket = 1000 + i
        rows.append(row("intent_classification", f"ระบุเจตนาของข้อความหมายเลข {ticket}: {utterance}", intent, 0.98))

    # De-duplicate exact rows, shuffle deterministically, then split.
    unique = {r["id"]: r for r in rows}
    rows = list(unique.values())
    RNG.shuffle(rows)
    return rows


def main():
    rows = generate()
    cut_train = int(len(rows) * 0.90)
    cut_valid = int(len(rows) * 0.95)
    splits = {
        "train": rows[:cut_train],
        "validation": rows[cut_train:cut_valid],
        "test": rows[cut_valid:],
    }
    (ROOT / "data").mkdir(exist_ok=True)
    for name, records in splits.items():
        path = ROOT / "data" / f"{name}.jsonl"
        with path.open("w", encoding="utf-8") as handle:
            for record in records:
                handle.write(json.dumps(record, ensure_ascii=False, separators=(",", ":")) + "\n")
        print(f"{name}: {len(records):,}")
    print(f"total: {len(rows):,}")


if __name__ == "__main__":
    main()