charactergenerator 1.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- charactergenerator-1.0.0/PKG-INFO +192 -0
- charactergenerator-1.0.0/README.md +181 -0
- charactergenerator-1.0.0/pyproject.toml +24 -0
- charactergenerator-1.0.0/pyproject.toml.orig +25 -0
- charactergenerator-1.0.0/src/charactergenerator/__init__.py +5 -0
- charactergenerator-1.0.0/src/charactergenerator/__main__.py +3 -0
- charactergenerator-1.0.0/src/charactergenerator/cli.py +108 -0
- charactergenerator-1.0.0/src/charactergenerator/generator.py +108 -0
- charactergenerator-1.0.0/src/charactergenerator/tables.py +184 -0
- charactergenerator-1.0.0/src/charactergenerator/tree.py +149 -0
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: charactergenerator
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Generates Characters from user tables
|
|
5
|
+
Author: vdbma
|
|
6
|
+
Author-email: vdbma <vandenbossche.marc@hotmail.com>
|
|
7
|
+
Requires-Dist: pyperclip>=1.11.0
|
|
8
|
+
Requires-Dist: rich>=15.0.0
|
|
9
|
+
Requires-Python: >=3.10
|
|
10
|
+
Description-Content-Type: text/markdown
|
|
11
|
+
|
|
12
|
+
# Character Generator
|
|
13
|
+
[](https://pre-commit.com/)
|
|
14
|
+
|
|
15
|
+
This package can be used to randomly generate characters from a set of tables.
|
|
16
|
+
|
|
17
|
+
## Basic use
|
|
18
|
+
|
|
19
|
+
Given a folder of csv tables names `TABLES_DIR/`
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
$ char-gen TABLES_DIR
|
|
23
|
+
```
|
|
24
|
+
will produce a random combination of one entry in each table.
|
|
25
|
+
|
|
26
|
+
## Options:
|
|
27
|
+
This can be obtained with
|
|
28
|
+
```
|
|
29
|
+
$ char-gen --help
|
|
30
|
+
usage: char-gen [-h] [-c CONDITIONING] [--cn] [-n N] [--sep SEP] [--tree] [--mermaid] [tables_dir]
|
|
31
|
+
|
|
32
|
+
Generate random characters from CSV tables.
|
|
33
|
+
|
|
34
|
+
positional arguments:
|
|
35
|
+
tables_dir Location of the tables. Default is "./"
|
|
36
|
+
|
|
37
|
+
options:
|
|
38
|
+
-h, --help show this help message and exit
|
|
39
|
+
-c CONDITIONING, --conditioning CONDITIONING
|
|
40
|
+
The name of the conditioning table found in the TABLES_DIR folder.
|
|
41
|
+
--cn Treat the last column as conditions. Must be used if CONDITIONING_TABLE uses numbers as entries.
|
|
42
|
+
-n N Number of characters to generate.
|
|
43
|
+
--sep SEP Sets the new character to separated conditionned entries. Default is '+'.
|
|
44
|
+
--tree Display the conditioning tree. Can't be used withtout conditioning.
|
|
45
|
+
--mermaid Export the conditioning tree to Mermaid. Can't be used withtout conditioning. Mermaid code is automatically added to clipboard.
|
|
46
|
+
|
|
47
|
+
The generated character list is automatically add to clipboard.
|
|
48
|
+
```
|
|
49
|
+
## Table format
|
|
50
|
+
|
|
51
|
+
The expected format for the tables are
|
|
52
|
+
|
|
53
|
+
### Entries only
|
|
54
|
+
|
|
55
|
+
| Column 1|
|
|
56
|
+
|---------|
|
|
57
|
+
| Entry 1 |
|
|
58
|
+
| Entry 2 |
|
|
59
|
+
| Entry 3 |
|
|
60
|
+
| ... |
|
|
61
|
+
|
|
62
|
+
this assumes the weight of all entries to be the same
|
|
63
|
+
|
|
64
|
+
### Entries + weights
|
|
65
|
+
| Column 1| Column 2 |
|
|
66
|
+
|---------|----------|
|
|
67
|
+
| Entry 1 | Weigth 1 |
|
|
68
|
+
| Entry 2 | Weight 2 |
|
|
69
|
+
| Entry 3 | Weight 3 |
|
|
70
|
+
| ... | ... |
|
|
71
|
+
|
|
72
|
+
#### Weight format
|
|
73
|
+
Weight can be either floats between 0.0 and 1.0 or integer.
|
|
74
|
+
|
|
75
|
+
If the weights are floats, their sum **must be 1**, otherwise an Error will be raised.
|
|
76
|
+
|
|
77
|
+
If the weigths are integers, the corresponding probability will be `weight/(sum of all weights)`.
|
|
78
|
+
|
|
79
|
+
**Behaviour:**
|
|
80
|
+
* if a weight with value > 1 is detected, it will assume that all weigths are integers and be cast to `int` if they it makes sense (eg 1.0 -> 1, but not 1.5)
|
|
81
|
+
|
|
82
|
+
## Advanced use
|
|
83
|
+
|
|
84
|
+
Some entries of a table may be conditioned by what was picked in another one. This introduces a new type of table: the conditioned table.
|
|
85
|
+
|
|
86
|
+
```
|
|
87
|
+
$ char-gen TABLES_DIR -c tables/conditioning_table.csv
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### Conditioned table formats
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
#### Without weights
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
| Column 1| Column 2 |
|
|
97
|
+
|---------|-------------|
|
|
98
|
+
| Entry 1 | Condition 1 |
|
|
99
|
+
| Entry 2 | Condition 2 |
|
|
100
|
+
| Entry 3 | Condition 3 |
|
|
101
|
+
| ... | ... |
|
|
102
|
+
|
|
103
|
+
#### With weights
|
|
104
|
+
| Column 1| Column 2 | Column 3 |
|
|
105
|
+
|---------|----------|-------------|
|
|
106
|
+
| Entry 1 | Weigth 1 | Condition 1 |
|
|
107
|
+
| Entry 2 | Weight 2 | Condition 2 |
|
|
108
|
+
| Entry 3 | Weight 3 | Condition 3 |
|
|
109
|
+
| ... | ... | ... |
|
|
110
|
+
|
|
111
|
+
#### Weight and condition format
|
|
112
|
+
Same as above: Weights can be either floats between 0.0 and 1.0 or integer.
|
|
113
|
+
|
|
114
|
+
If the weights are floats, their sum **must be 1 for each and every condition**, otherwise an Error will be raised.
|
|
115
|
+
|
|
116
|
+
If the weigths are integers, the corresponding probability will be `weight/(sum of all weights with corresponding condition)`.
|
|
117
|
+
|
|
118
|
+
The conditions are treated as strings. If you use numbers as conditions you have to use the `--cn` option so it parses the last column as conditions and not weights.
|
|
119
|
+
|
|
120
|
+
Condition can be `None` or `none` or empty. In this case all conditions can lead to it.
|
|
121
|
+
|
|
122
|
+
Their can be multiple conditions that lead to the same Entry, those should be separated with `+` in the condition column (not space sensitive), this is a logical OR. The condition seperation character can be changed with ```--sep OR_CHARACTER```.
|
|
123
|
+
|
|
124
|
+
#### No condition
|
|
125
|
+
|
|
126
|
+
Same as above, all conditions are considered to be `None`.
|
|
127
|
+
|
|
128
|
+
### Example
|
|
129
|
+
|
|
130
|
+
Conditioning table:
|
|
131
|
+
| Column 1|
|
|
132
|
+
|---------|
|
|
133
|
+
| Entry A |
|
|
134
|
+
| Entry B |
|
|
135
|
+
| Entry C |
|
|
136
|
+
|
|
137
|
+
Conditioned table:
|
|
138
|
+
|
|
139
|
+
| Column 1| Column 2 |
|
|
140
|
+
|---------|----------|
|
|
141
|
+
| Entry 1 | Entry A |
|
|
142
|
+
| Entry 2 | Entry A |
|
|
143
|
+
| Entry 3 | Entry B |
|
|
144
|
+
| Entry 4 | Entry C |
|
|
145
|
+
| Entry 5 | None |
|
|
146
|
+
| Entry 6 | Entry A+Entry C |
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
This corresponds to the following probability tree
|
|
151
|
+
|
|
152
|
+
```mermaid
|
|
153
|
+
flowchart LR
|
|
154
|
+
Root["Conditioning"]
|
|
155
|
+
|
|
156
|
+
Root -->|1/3| A["Entry A"]
|
|
157
|
+
Root -->|1/3| B["Entry B"]
|
|
158
|
+
Root -->|1/3| C["Entry C"]
|
|
159
|
+
|
|
160
|
+
A -->|1/4| e1["Entry 1"]
|
|
161
|
+
A -->|1/4| e2["Entry 2"]
|
|
162
|
+
|
|
163
|
+
B -->|1/2| e3["Entry 3"]
|
|
164
|
+
B -->|1/2| e5
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
C -->|1/3| e4["Entry 4"]
|
|
168
|
+
C -->|1/3| e5
|
|
169
|
+
C -->|1/3| e6
|
|
170
|
+
|
|
171
|
+
A -->|1/4| e5["Entry 5"]
|
|
172
|
+
A -->|1/4| e6["Entry 6"]
|
|
173
|
+
|
|
174
|
+
style Root fill:#f3f4f6,stroke:#6b7280,color:#111827
|
|
175
|
+
style A fill:#dbeafe,stroke:#2563eb,color:#1e3a8a
|
|
176
|
+
style B fill:#dbeafe,stroke:#2563eb,color:#1e3a8a
|
|
177
|
+
style C fill:#dbeafe,stroke:#2563eb,color:#1e3a8a
|
|
178
|
+
style e1 fill:#dcfce7,stroke:#16a34a,color:#14532d
|
|
179
|
+
style e2 fill:#dcfce7,stroke:#16a34a,color:#14532d
|
|
180
|
+
style e3 fill:#dcfce7,stroke:#16a34a,color:#14532d
|
|
181
|
+
style e4 fill:#dcfce7,stroke:#16a34a,color:#14532d
|
|
182
|
+
style e5 fill:#dcfce7,stroke:#16a34a,color:#14532d
|
|
183
|
+
style e6 fill:#dcfce7,stroke:#16a34a,color:#14532d
|
|
184
|
+
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
## AI usage disclaimer
|
|
189
|
+
|
|
190
|
+
This project served me as a test of what can be actually _vibe coded_ with current free LLM capabilies. I used the free version of chat GPT (as of nov 2026) to produce the basis of the scripts from the above documentation. I then improved them (with my own python knowledge) when they inevitably failed to cover all the edges cases I wanted. As such it indirectly uses (via the model's training) repos that have been scraped without the consent of their authors. Make of that what you will.
|
|
191
|
+
|
|
192
|
+
Tests were written by hand.
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
# Character Generator
|
|
2
|
+
[](https://pre-commit.com/)
|
|
3
|
+
|
|
4
|
+
This package can be used to randomly generate characters from a set of tables.
|
|
5
|
+
|
|
6
|
+
## Basic use
|
|
7
|
+
|
|
8
|
+
Given a folder of csv tables names `TABLES_DIR/`
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
$ char-gen TABLES_DIR
|
|
12
|
+
```
|
|
13
|
+
will produce a random combination of one entry in each table.
|
|
14
|
+
|
|
15
|
+
## Options:
|
|
16
|
+
This can be obtained with
|
|
17
|
+
```
|
|
18
|
+
$ char-gen --help
|
|
19
|
+
usage: char-gen [-h] [-c CONDITIONING] [--cn] [-n N] [--sep SEP] [--tree] [--mermaid] [tables_dir]
|
|
20
|
+
|
|
21
|
+
Generate random characters from CSV tables.
|
|
22
|
+
|
|
23
|
+
positional arguments:
|
|
24
|
+
tables_dir Location of the tables. Default is "./"
|
|
25
|
+
|
|
26
|
+
options:
|
|
27
|
+
-h, --help show this help message and exit
|
|
28
|
+
-c CONDITIONING, --conditioning CONDITIONING
|
|
29
|
+
The name of the conditioning table found in the TABLES_DIR folder.
|
|
30
|
+
--cn Treat the last column as conditions. Must be used if CONDITIONING_TABLE uses numbers as entries.
|
|
31
|
+
-n N Number of characters to generate.
|
|
32
|
+
--sep SEP Sets the new character to separated conditionned entries. Default is '+'.
|
|
33
|
+
--tree Display the conditioning tree. Can't be used withtout conditioning.
|
|
34
|
+
--mermaid Export the conditioning tree to Mermaid. Can't be used withtout conditioning. Mermaid code is automatically added to clipboard.
|
|
35
|
+
|
|
36
|
+
The generated character list is automatically add to clipboard.
|
|
37
|
+
```
|
|
38
|
+
## Table format
|
|
39
|
+
|
|
40
|
+
The expected format for the tables are
|
|
41
|
+
|
|
42
|
+
### Entries only
|
|
43
|
+
|
|
44
|
+
| Column 1|
|
|
45
|
+
|---------|
|
|
46
|
+
| Entry 1 |
|
|
47
|
+
| Entry 2 |
|
|
48
|
+
| Entry 3 |
|
|
49
|
+
| ... |
|
|
50
|
+
|
|
51
|
+
this assumes the weight of all entries to be the same
|
|
52
|
+
|
|
53
|
+
### Entries + weights
|
|
54
|
+
| Column 1| Column 2 |
|
|
55
|
+
|---------|----------|
|
|
56
|
+
| Entry 1 | Weigth 1 |
|
|
57
|
+
| Entry 2 | Weight 2 |
|
|
58
|
+
| Entry 3 | Weight 3 |
|
|
59
|
+
| ... | ... |
|
|
60
|
+
|
|
61
|
+
#### Weight format
|
|
62
|
+
Weight can be either floats between 0.0 and 1.0 or integer.
|
|
63
|
+
|
|
64
|
+
If the weights are floats, their sum **must be 1**, otherwise an Error will be raised.
|
|
65
|
+
|
|
66
|
+
If the weigths are integers, the corresponding probability will be `weight/(sum of all weights)`.
|
|
67
|
+
|
|
68
|
+
**Behaviour:**
|
|
69
|
+
* if a weight with value > 1 is detected, it will assume that all weigths are integers and be cast to `int` if they it makes sense (eg 1.0 -> 1, but not 1.5)
|
|
70
|
+
|
|
71
|
+
## Advanced use
|
|
72
|
+
|
|
73
|
+
Some entries of a table may be conditioned by what was picked in another one. This introduces a new type of table: the conditioned table.
|
|
74
|
+
|
|
75
|
+
```
|
|
76
|
+
$ char-gen TABLES_DIR -c tables/conditioning_table.csv
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### Conditioned table formats
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
#### Without weights
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
| Column 1| Column 2 |
|
|
86
|
+
|---------|-------------|
|
|
87
|
+
| Entry 1 | Condition 1 |
|
|
88
|
+
| Entry 2 | Condition 2 |
|
|
89
|
+
| Entry 3 | Condition 3 |
|
|
90
|
+
| ... | ... |
|
|
91
|
+
|
|
92
|
+
#### With weights
|
|
93
|
+
| Column 1| Column 2 | Column 3 |
|
|
94
|
+
|---------|----------|-------------|
|
|
95
|
+
| Entry 1 | Weigth 1 | Condition 1 |
|
|
96
|
+
| Entry 2 | Weight 2 | Condition 2 |
|
|
97
|
+
| Entry 3 | Weight 3 | Condition 3 |
|
|
98
|
+
| ... | ... | ... |
|
|
99
|
+
|
|
100
|
+
#### Weight and condition format
|
|
101
|
+
Same as above: Weights can be either floats between 0.0 and 1.0 or integer.
|
|
102
|
+
|
|
103
|
+
If the weights are floats, their sum **must be 1 for each and every condition**, otherwise an Error will be raised.
|
|
104
|
+
|
|
105
|
+
If the weigths are integers, the corresponding probability will be `weight/(sum of all weights with corresponding condition)`.
|
|
106
|
+
|
|
107
|
+
The conditions are treated as strings. If you use numbers as conditions you have to use the `--cn` option so it parses the last column as conditions and not weights.
|
|
108
|
+
|
|
109
|
+
Condition can be `None` or `none` or empty. In this case all conditions can lead to it.
|
|
110
|
+
|
|
111
|
+
Their can be multiple conditions that lead to the same Entry, those should be separated with `+` in the condition column (not space sensitive), this is a logical OR. The condition seperation character can be changed with ```--sep OR_CHARACTER```.
|
|
112
|
+
|
|
113
|
+
#### No condition
|
|
114
|
+
|
|
115
|
+
Same as above, all conditions are considered to be `None`.
|
|
116
|
+
|
|
117
|
+
### Example
|
|
118
|
+
|
|
119
|
+
Conditioning table:
|
|
120
|
+
| Column 1|
|
|
121
|
+
|---------|
|
|
122
|
+
| Entry A |
|
|
123
|
+
| Entry B |
|
|
124
|
+
| Entry C |
|
|
125
|
+
|
|
126
|
+
Conditioned table:
|
|
127
|
+
|
|
128
|
+
| Column 1| Column 2 |
|
|
129
|
+
|---------|----------|
|
|
130
|
+
| Entry 1 | Entry A |
|
|
131
|
+
| Entry 2 | Entry A |
|
|
132
|
+
| Entry 3 | Entry B |
|
|
133
|
+
| Entry 4 | Entry C |
|
|
134
|
+
| Entry 5 | None |
|
|
135
|
+
| Entry 6 | Entry A+Entry C |
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
This corresponds to the following probability tree
|
|
140
|
+
|
|
141
|
+
```mermaid
|
|
142
|
+
flowchart LR
|
|
143
|
+
Root["Conditioning"]
|
|
144
|
+
|
|
145
|
+
Root -->|1/3| A["Entry A"]
|
|
146
|
+
Root -->|1/3| B["Entry B"]
|
|
147
|
+
Root -->|1/3| C["Entry C"]
|
|
148
|
+
|
|
149
|
+
A -->|1/4| e1["Entry 1"]
|
|
150
|
+
A -->|1/4| e2["Entry 2"]
|
|
151
|
+
|
|
152
|
+
B -->|1/2| e3["Entry 3"]
|
|
153
|
+
B -->|1/2| e5
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
C -->|1/3| e4["Entry 4"]
|
|
157
|
+
C -->|1/3| e5
|
|
158
|
+
C -->|1/3| e6
|
|
159
|
+
|
|
160
|
+
A -->|1/4| e5["Entry 5"]
|
|
161
|
+
A -->|1/4| e6["Entry 6"]
|
|
162
|
+
|
|
163
|
+
style Root fill:#f3f4f6,stroke:#6b7280,color:#111827
|
|
164
|
+
style A fill:#dbeafe,stroke:#2563eb,color:#1e3a8a
|
|
165
|
+
style B fill:#dbeafe,stroke:#2563eb,color:#1e3a8a
|
|
166
|
+
style C fill:#dbeafe,stroke:#2563eb,color:#1e3a8a
|
|
167
|
+
style e1 fill:#dcfce7,stroke:#16a34a,color:#14532d
|
|
168
|
+
style e2 fill:#dcfce7,stroke:#16a34a,color:#14532d
|
|
169
|
+
style e3 fill:#dcfce7,stroke:#16a34a,color:#14532d
|
|
170
|
+
style e4 fill:#dcfce7,stroke:#16a34a,color:#14532d
|
|
171
|
+
style e5 fill:#dcfce7,stroke:#16a34a,color:#14532d
|
|
172
|
+
style e6 fill:#dcfce7,stroke:#16a34a,color:#14532d
|
|
173
|
+
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
## AI usage disclaimer
|
|
178
|
+
|
|
179
|
+
This project served me as a test of what can be actually _vibe coded_ with current free LLM capabilies. I used the free version of chat GPT (as of nov 2026) to produce the basis of the scripts from the above documentation. I then improved them (with my own python knowledge) when they inevitably failed to cover all the edges cases I wanted. As such it indirectly uses (via the model's training) repos that have been scraped without the consent of their authors. Make of that what you will.
|
|
180
|
+
|
|
181
|
+
Tests were written by hand.
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "charactergenerator"
|
|
3
|
+
version = "1.0.0"
|
|
4
|
+
description = "Generates Characters from user tables"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
requires-python = ">=3.10"
|
|
7
|
+
dependencies = [
|
|
8
|
+
"pyperclip>=1.11.0",
|
|
9
|
+
"rich>=15.0.0",
|
|
10
|
+
]
|
|
11
|
+
|
|
12
|
+
[[project.authors]]
|
|
13
|
+
name = "vdbma"
|
|
14
|
+
email = "vandenbossche.marc@hotmail.com"
|
|
15
|
+
|
|
16
|
+
[project.scripts]
|
|
17
|
+
char-gen = "charactergenerator.cli:main"
|
|
18
|
+
|
|
19
|
+
[build-system]
|
|
20
|
+
requires = ["uv_build>=0.13.10,<0.14.0"]
|
|
21
|
+
build-backend = "uv_build"
|
|
22
|
+
|
|
23
|
+
[dependency-groups]
|
|
24
|
+
dev = ["pytest>=9.2.0"]
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "charactergenerator"
|
|
3
|
+
version = "1.0.0"
|
|
4
|
+
description = "Generates Characters from user tables"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
authors = [
|
|
7
|
+
{ name = "vdbma", email = "vandenbossche.marc@hotmail.com" }
|
|
8
|
+
]
|
|
9
|
+
requires-python = ">=3.10"
|
|
10
|
+
dependencies = [
|
|
11
|
+
"pyperclip>=1.11.0",
|
|
12
|
+
"rich>=15.0.0",
|
|
13
|
+
]
|
|
14
|
+
|
|
15
|
+
[project.scripts]
|
|
16
|
+
char-gen = "charactergenerator.cli:main"
|
|
17
|
+
|
|
18
|
+
[build-system]
|
|
19
|
+
requires = ["uv_build>=0.13.10,<0.14.0"]
|
|
20
|
+
build-backend = "uv_build"
|
|
21
|
+
|
|
22
|
+
[dependency-groups]
|
|
23
|
+
dev = [
|
|
24
|
+
"pytest>=9.2.0",
|
|
25
|
+
]
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import argparse
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
import sys
|
|
4
|
+
import pyperclip
|
|
5
|
+
|
|
6
|
+
from .generator import CharacterGenerator
|
|
7
|
+
from .tree import display_tree, export_mermaid
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def parse_args(sys_args):
|
|
11
|
+
parser = argparse.ArgumentParser(
|
|
12
|
+
prog="char-gen",
|
|
13
|
+
description="Generate random characters from CSV tables.",
|
|
14
|
+
epilog="The generated character list is automatically add to clipboard.",
|
|
15
|
+
)
|
|
16
|
+
parser.add_argument(
|
|
17
|
+
"tables_dir",
|
|
18
|
+
type=Path,
|
|
19
|
+
default=Path("./"),
|
|
20
|
+
nargs="?",
|
|
21
|
+
help='Location of the tables. Default is "./" ',
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
parser.add_argument(
|
|
25
|
+
"-c",
|
|
26
|
+
"--conditioning",
|
|
27
|
+
type=str,
|
|
28
|
+
help=" The name of the conditioning table found in the TABLES_DIR folder.",
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
parser.add_argument(
|
|
32
|
+
"--cn",
|
|
33
|
+
action="store_true",
|
|
34
|
+
help="Treat the last column as conditions. Must be used if CONDITIONING_TABLE uses numbers as entries.",
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
parser.add_argument(
|
|
38
|
+
"-n", type=int, default=1, help="Number of characters to generate."
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
parser.add_argument(
|
|
42
|
+
"--sep",
|
|
43
|
+
default="+",
|
|
44
|
+
help="Sets the new character to separated conditionned entries. Default is '+'.",
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
parser.add_argument(
|
|
48
|
+
"--tree",
|
|
49
|
+
action="store_true",
|
|
50
|
+
help="Display the conditioning tree. Can't be used withtout conditioning.",
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
parser.add_argument(
|
|
54
|
+
"--mermaid",
|
|
55
|
+
action="store_true",
|
|
56
|
+
help="Export the conditioning tree to Mermaid. Can't be used withtout conditioning. Mermaid code is automatically added to clipboard.",
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
args = parser.parse_args(sys_args)
|
|
60
|
+
if args.n < 1:
|
|
61
|
+
parser.error("-n must be greater than zero.")
|
|
62
|
+
|
|
63
|
+
if (args.tree or args.mermaid) and not args.conditioning:
|
|
64
|
+
parser.error("--tree and --mermaid don't make sense without conditioning.")
|
|
65
|
+
return args
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def print_and_add_to_clipboard(s, **kwargs):
|
|
69
|
+
pyperclip.copy(s)
|
|
70
|
+
print(s, **kwargs)
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def main():
|
|
74
|
+
args = parse_args(sys.argv[1:])
|
|
75
|
+
|
|
76
|
+
try:
|
|
77
|
+
generator = CharacterGenerator(
|
|
78
|
+
args.tables_dir,
|
|
79
|
+
conditioning=args.conditioning,
|
|
80
|
+
numeric_conditions=args.cn,
|
|
81
|
+
separator=args.sep,
|
|
82
|
+
)
|
|
83
|
+
|
|
84
|
+
if args.tree:
|
|
85
|
+
display_tree(generator.conditioning, generator.tables)
|
|
86
|
+
|
|
87
|
+
if args.mermaid:
|
|
88
|
+
print_and_add_to_clipboard(
|
|
89
|
+
export_mermaid(generator.conditioning, generator.tables)
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
if args.mermaid or args.tree:
|
|
93
|
+
return
|
|
94
|
+
|
|
95
|
+
characters = generator.generate(args.n)
|
|
96
|
+
|
|
97
|
+
print_and_add_to_clipboard(
|
|
98
|
+
"\n".join(
|
|
99
|
+
[generator.format_character(character) for character in characters]
|
|
100
|
+
)
|
|
101
|
+
)
|
|
102
|
+
|
|
103
|
+
except (ValueError, OSError) as error:
|
|
104
|
+
args.error(str(error))
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
if __name__ == "__main__":
|
|
108
|
+
main()
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import random
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
from .tables import load_table
|
|
4
|
+
import copy
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class CharacterGenerator:
|
|
8
|
+
def __init__(
|
|
9
|
+
self, tables_dir, conditioning=None, numeric_conditions=False, separator="+"
|
|
10
|
+
):
|
|
11
|
+
self.tables_dir = Path(tables_dir)
|
|
12
|
+
self.separator = separator
|
|
13
|
+
self.conditioning_name = conditioning
|
|
14
|
+
|
|
15
|
+
if not self.tables_dir.is_dir():
|
|
16
|
+
raise ValueError(f"Directory does not exist: {tables_dir}")
|
|
17
|
+
|
|
18
|
+
self.tables = {}
|
|
19
|
+
self.conditioning = None
|
|
20
|
+
|
|
21
|
+
for path in sorted(self.tables_dir.glob("*.csv")):
|
|
22
|
+
if conditioning and path.name == Path(conditioning).name:
|
|
23
|
+
self.conditioning = load_table(path, conditioned=False)
|
|
24
|
+
continue
|
|
25
|
+
|
|
26
|
+
self.tables[path.stem] = load_table(
|
|
27
|
+
path,
|
|
28
|
+
conditioned=bool(conditioning),
|
|
29
|
+
numeric_conditions=numeric_conditions,
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
if conditioning and self.conditioning is None:
|
|
33
|
+
raise ValueError(f"Conditioning table not found: {conditioning}")
|
|
34
|
+
|
|
35
|
+
if conditioning is not None:
|
|
36
|
+
self.complete_tables()
|
|
37
|
+
|
|
38
|
+
def complete_tables(self):
|
|
39
|
+
"""
|
|
40
|
+
This completes the tables for items with no condition
|
|
41
|
+
"""
|
|
42
|
+
completed_table = {}
|
|
43
|
+
for conditioned_option in self.tables.keys():
|
|
44
|
+
completed_table[conditioned_option] = {}
|
|
45
|
+
# add conditions that were not use in this table
|
|
46
|
+
for cond in self.conditioning["entries"]:
|
|
47
|
+
if cond is not None:
|
|
48
|
+
if cond in self.tables[conditioned_option]:
|
|
49
|
+
completed_table[conditioned_option][cond] = copy.deepcopy(
|
|
50
|
+
self.tables[conditioned_option][cond]
|
|
51
|
+
)
|
|
52
|
+
else:
|
|
53
|
+
completed_table[conditioned_option][cond] = copy.deepcopy(
|
|
54
|
+
self.tables[conditioned_option][None]
|
|
55
|
+
)
|
|
56
|
+
self.tables = completed_table
|
|
57
|
+
|
|
58
|
+
@staticmethod
|
|
59
|
+
def choose(table, condition=None):
|
|
60
|
+
"""Choose an entry from a normal or conditioned table."""
|
|
61
|
+
|
|
62
|
+
# Normal table
|
|
63
|
+
if "entries" in table:
|
|
64
|
+
return random.choices(table["entries"], weights=table["weights"], k=1)[0]
|
|
65
|
+
|
|
66
|
+
# Conditioned table
|
|
67
|
+
available = []
|
|
68
|
+
|
|
69
|
+
if condition in table:
|
|
70
|
+
available.append(table[condition])
|
|
71
|
+
|
|
72
|
+
if None in table:
|
|
73
|
+
available.append(table[None])
|
|
74
|
+
|
|
75
|
+
if not available:
|
|
76
|
+
raise ValueError(f"No entries available for condition: {condition}")
|
|
77
|
+
|
|
78
|
+
entries = []
|
|
79
|
+
weights = []
|
|
80
|
+
|
|
81
|
+
for group in available:
|
|
82
|
+
entries.extend(group["entries"])
|
|
83
|
+
weights.extend(group["weights"])
|
|
84
|
+
|
|
85
|
+
return random.choices(entries, weights=weights, k=1)[0]
|
|
86
|
+
|
|
87
|
+
def generate_one(self):
|
|
88
|
+
result = {}
|
|
89
|
+
|
|
90
|
+
# Pick the initial condition
|
|
91
|
+
condition = None
|
|
92
|
+
if self.conditioning is not None:
|
|
93
|
+
condition = self.choose(self.conditioning)
|
|
94
|
+
|
|
95
|
+
# Store condition first
|
|
96
|
+
result["conditioning"] = condition
|
|
97
|
+
|
|
98
|
+
# Generate entries from all tables
|
|
99
|
+
for name, table in self.tables.items():
|
|
100
|
+
result[name] = self.choose(table, condition)
|
|
101
|
+
|
|
102
|
+
return result
|
|
103
|
+
|
|
104
|
+
def generate(self, number=1):
|
|
105
|
+
return [self.generate_one() for _ in range(number)]
|
|
106
|
+
|
|
107
|
+
def format_character(self, character):
|
|
108
|
+
return "\t".join(str(value) for value in character.values())
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import csv
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def read_csv(path):
|
|
5
|
+
"""Read a CSV file and return its rows."""
|
|
6
|
+
with open(path, "r", encoding="utf-8-sig", newline="") as file:
|
|
7
|
+
reader = csv.reader(file)
|
|
8
|
+
rows = [
|
|
9
|
+
[item.strip() for item in row]
|
|
10
|
+
for row in reader
|
|
11
|
+
if row and any(cell.strip() for cell in row)
|
|
12
|
+
]
|
|
13
|
+
|
|
14
|
+
if not rows:
|
|
15
|
+
raise ValueError(f"Empty CSV file: {path}")
|
|
16
|
+
|
|
17
|
+
return rows
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def parse_weights(values):
|
|
21
|
+
"""Convert weights into normalized float probabilities."""
|
|
22
|
+
|
|
23
|
+
weights = [float(value) for value in values]
|
|
24
|
+
|
|
25
|
+
if not weights:
|
|
26
|
+
raise ValueError("A table must contain at least one weight.")
|
|
27
|
+
|
|
28
|
+
if any(weight < 0 for weight in weights):
|
|
29
|
+
raise ValueError("Weights cannot be negative.")
|
|
30
|
+
|
|
31
|
+
if all(weight.is_integer() for weight in weights):
|
|
32
|
+
weights = [int(weight) for weight in weights]
|
|
33
|
+
|
|
34
|
+
total = sum(weights)
|
|
35
|
+
|
|
36
|
+
if total == 0:
|
|
37
|
+
raise ValueError("The sum of weights cannot be zero.")
|
|
38
|
+
|
|
39
|
+
return [weight / total for weight in weights]
|
|
40
|
+
|
|
41
|
+
if any(weight > 1 for weight in weights):
|
|
42
|
+
raise ValueError(
|
|
43
|
+
"Weights must either all be integers or floats between 0 and 1."
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
total = sum(weights)
|
|
47
|
+
|
|
48
|
+
if abs(total - 1.0) > 1e-9:
|
|
49
|
+
raise ValueError(f"Float weights must sum to 1. Got {total}.")
|
|
50
|
+
|
|
51
|
+
return weights
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def load_table(path, conditioned=False, numeric_conditions=False):
|
|
55
|
+
"""
|
|
56
|
+
Load a CSV table.
|
|
57
|
+
|
|
58
|
+
Normal table:
|
|
59
|
+
Entry
|
|
60
|
+
Entry, Weight
|
|
61
|
+
|
|
62
|
+
Conditioned table:
|
|
63
|
+
Entry (applies to all conditions)
|
|
64
|
+
Entry, Weight (applies to all conditions)
|
|
65
|
+
Entry, Condition
|
|
66
|
+
Entry, Weight, Condition
|
|
67
|
+
|
|
68
|
+
Entries with no condition are appended to every existing condition.
|
|
69
|
+
|
|
70
|
+
Here we can not know if some conditions entries are never used, adding them to none contidition entries must be done later
|
|
71
|
+
"""
|
|
72
|
+
|
|
73
|
+
rows = read_csv(path)
|
|
74
|
+
|
|
75
|
+
if len(rows) < 1:
|
|
76
|
+
raise ValueError(f"Table must contain entries: {path}")
|
|
77
|
+
|
|
78
|
+
columns = len(rows[0])
|
|
79
|
+
|
|
80
|
+
# Normal table
|
|
81
|
+
if not conditioned:
|
|
82
|
+
if columns not in (1, 2):
|
|
83
|
+
raise ValueError(
|
|
84
|
+
f"Invalid table format: {path} found {columns} columns but doesn't have conditon. Use -c CONDITION_TABLE."
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
entries = [row[0].strip() for row in rows]
|
|
88
|
+
|
|
89
|
+
if columns == 1:
|
|
90
|
+
weights = [1 / len(entries)] * len(entries)
|
|
91
|
+
else:
|
|
92
|
+
weights = parse_weights([row[1].strip() for row in rows])
|
|
93
|
+
|
|
94
|
+
return {"entries": entries, "weights": weights}
|
|
95
|
+
|
|
96
|
+
if columns not in (1, 2, 3):
|
|
97
|
+
raise ValueError(
|
|
98
|
+
f"Invalid conditioned table format: {path} found {columns} columns but has condition."
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
groups = {}
|
|
102
|
+
unconditional = []
|
|
103
|
+
|
|
104
|
+
# One column: every entry applies to every condition.
|
|
105
|
+
if columns == 1:
|
|
106
|
+
unconditional = [(row[0].strip(), None) for row in rows]
|
|
107
|
+
|
|
108
|
+
# Two columns: automatically detect weights or conditions.
|
|
109
|
+
elif columns == 2:
|
|
110
|
+
is_condition = numeric_conditions
|
|
111
|
+
|
|
112
|
+
if not is_condition:
|
|
113
|
+
try:
|
|
114
|
+
for row in rows:
|
|
115
|
+
float(row[1].strip())
|
|
116
|
+
except ValueError:
|
|
117
|
+
is_condition = True
|
|
118
|
+
|
|
119
|
+
if is_condition:
|
|
120
|
+
for row in rows:
|
|
121
|
+
entry = row[0].strip()
|
|
122
|
+
condition = row[1].strip()
|
|
123
|
+
|
|
124
|
+
conditions = (
|
|
125
|
+
[]
|
|
126
|
+
if not condition or condition.lower() == "none"
|
|
127
|
+
else [c.strip() for c in condition.split("+")]
|
|
128
|
+
)
|
|
129
|
+
|
|
130
|
+
if not conditions:
|
|
131
|
+
unconditional.append((entry, None))
|
|
132
|
+
else:
|
|
133
|
+
for cond in conditions:
|
|
134
|
+
groups.setdefault(cond, []).append((entry, None))
|
|
135
|
+
|
|
136
|
+
else:
|
|
137
|
+
unconditional = [(row[0].strip(), row[1].strip()) for row in rows]
|
|
138
|
+
|
|
139
|
+
# Three columns: Entry, Weight, Condition.
|
|
140
|
+
else:
|
|
141
|
+
for row in rows:
|
|
142
|
+
entry = row[0].strip()
|
|
143
|
+
weight = row[1].strip()
|
|
144
|
+
condition = row[2].strip()
|
|
145
|
+
|
|
146
|
+
conditions = (
|
|
147
|
+
[]
|
|
148
|
+
if not condition or condition.lower() == "none"
|
|
149
|
+
else [c.strip() for c in condition.split("+")]
|
|
150
|
+
)
|
|
151
|
+
|
|
152
|
+
if not conditions:
|
|
153
|
+
unconditional.append((entry, weight))
|
|
154
|
+
else:
|
|
155
|
+
for cond in conditions:
|
|
156
|
+
groups.setdefault(cond, []).append((entry, weight))
|
|
157
|
+
|
|
158
|
+
# If there are no explicit conditions, create a default category.
|
|
159
|
+
if not groups:
|
|
160
|
+
groups[None] = unconditional
|
|
161
|
+
|
|
162
|
+
else:
|
|
163
|
+
# Append unconditional entries to every condition.
|
|
164
|
+
for condition in groups:
|
|
165
|
+
groups[condition].extend(unconditional)
|
|
166
|
+
groups[None] = unconditional
|
|
167
|
+
|
|
168
|
+
# Normalize weights independently for every condition.
|
|
169
|
+
result = {}
|
|
170
|
+
|
|
171
|
+
for condition, entries in groups.items():
|
|
172
|
+
names = [entry for entry, _ in entries]
|
|
173
|
+
|
|
174
|
+
if not names:
|
|
175
|
+
raise ValueError(f"No entries for condition: {condition}")
|
|
176
|
+
|
|
177
|
+
if entries[0][1] is None:
|
|
178
|
+
weights = [1 / len(names)] * len(names)
|
|
179
|
+
else:
|
|
180
|
+
weights = parse_weights([weight for _, weight in entries])
|
|
181
|
+
|
|
182
|
+
result[condition] = {"entries": names, "weights": weights}
|
|
183
|
+
|
|
184
|
+
return result
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
from rich.console import Console
|
|
2
|
+
from rich.tree import Tree
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
def build_tree(conditioning, tables):
|
|
6
|
+
"""
|
|
7
|
+
Build a tree representing all possible character combinations.
|
|
8
|
+
|
|
9
|
+
Parameters
|
|
10
|
+
----------
|
|
11
|
+
conditioning : dict | None
|
|
12
|
+
The initial conditioning table.
|
|
13
|
+
tables : dict
|
|
14
|
+
Dictionary of table names and their contents.
|
|
15
|
+
Tables can be either normal or conditioned.
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
root = Tree("[bold]Character Generator[/bold]")
|
|
19
|
+
|
|
20
|
+
# No conditioning: display all tables independently
|
|
21
|
+
if conditioning is None:
|
|
22
|
+
for name, table in tables.items():
|
|
23
|
+
branch = root.add(f"[bold cyan]{name}[/bold cyan]")
|
|
24
|
+
|
|
25
|
+
for entry, weight in zip(table["entries"], table["weights"]):
|
|
26
|
+
branch.add(f"[green]{entry}[/green] [dim]({weight:.2%})[/dim]")
|
|
27
|
+
|
|
28
|
+
return root
|
|
29
|
+
|
|
30
|
+
# Conditioning table
|
|
31
|
+
for condition, condition_weight in zip(
|
|
32
|
+
conditioning["entries"], conditioning["weights"]
|
|
33
|
+
):
|
|
34
|
+
condition_branch = root.add(
|
|
35
|
+
f"[bold cyan]{condition}[/bold cyan] "
|
|
36
|
+
f"[dim]({condition_weight:.2%})[/dim]"
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
# Iterate over all other tables
|
|
40
|
+
for name, table in tables.items():
|
|
41
|
+
table_branch = condition_branch.add(f"[bold yellow]{name}[/bold yellow]")
|
|
42
|
+
|
|
43
|
+
# Normal table
|
|
44
|
+
if "entries" in table:
|
|
45
|
+
entries = table["entries"]
|
|
46
|
+
weights = table["weights"]
|
|
47
|
+
|
|
48
|
+
# Conditioned table
|
|
49
|
+
else:
|
|
50
|
+
available = []
|
|
51
|
+
|
|
52
|
+
if condition in table:
|
|
53
|
+
available.append(table[condition])
|
|
54
|
+
|
|
55
|
+
if None in table:
|
|
56
|
+
available.append(table[None])
|
|
57
|
+
|
|
58
|
+
if not available:
|
|
59
|
+
table_branch.add("[red]No available entries[/red]")
|
|
60
|
+
continue
|
|
61
|
+
|
|
62
|
+
entries = []
|
|
63
|
+
weights = []
|
|
64
|
+
|
|
65
|
+
for group in available:
|
|
66
|
+
entries.extend(group["entries"])
|
|
67
|
+
weights.extend(group["weights"])
|
|
68
|
+
|
|
69
|
+
total = sum(weights)
|
|
70
|
+
weights = [w / total for w in weights]
|
|
71
|
+
|
|
72
|
+
for entry, weight in zip(entries, weights):
|
|
73
|
+
table_branch.add(
|
|
74
|
+
f"[green]{entry}[/green] " f"[dim]({weight:.2%})[/dim]"
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
return root
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def display_tree(conditioning, tables):
|
|
81
|
+
console = Console()
|
|
82
|
+
console.print(build_tree(conditioning, tables))
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def export_mermaid(conditioning, tables):
|
|
86
|
+
"""Export the complete character generation tree to Mermaid."""
|
|
87
|
+
|
|
88
|
+
lines = ["flowchart LR", ' Root["Character Generator"]']
|
|
89
|
+
|
|
90
|
+
if conditioning is None:
|
|
91
|
+
for i, (name, table) in enumerate(tables.items()):
|
|
92
|
+
table_node = f"T{i}"
|
|
93
|
+
|
|
94
|
+
lines.append(f' Root --> {table_node}["{name}"]')
|
|
95
|
+
|
|
96
|
+
for j, (entry, weight) in enumerate(
|
|
97
|
+
zip(table["entries"], table["weights"])
|
|
98
|
+
):
|
|
99
|
+
node = f"E{i}_{j}"
|
|
100
|
+
lines.append(
|
|
101
|
+
f" {table_node} -->|{weight:.2%}| " f'{node}["{entry}"]'
|
|
102
|
+
)
|
|
103
|
+
|
|
104
|
+
return "\n".join(lines)
|
|
105
|
+
|
|
106
|
+
for i, (condition, condition_weight) in enumerate(
|
|
107
|
+
zip(conditioning["entries"], conditioning["weights"])
|
|
108
|
+
):
|
|
109
|
+
condition_node = f"C{i}"
|
|
110
|
+
|
|
111
|
+
lines.append(
|
|
112
|
+
f" Root -->|{condition_weight:.2%}| " f'{condition_node}["{condition}"]'
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
for j, (name, table) in enumerate(tables.items()):
|
|
116
|
+
table_node = f"T{i}_{j}"
|
|
117
|
+
|
|
118
|
+
lines.append(f" {condition_node} --> " f'{table_node}["{name}"]')
|
|
119
|
+
|
|
120
|
+
if "entries" in table:
|
|
121
|
+
entries = table["entries"]
|
|
122
|
+
weights = table["weights"]
|
|
123
|
+
else:
|
|
124
|
+
available = []
|
|
125
|
+
|
|
126
|
+
if condition in table:
|
|
127
|
+
available.append(table[condition])
|
|
128
|
+
|
|
129
|
+
if None in table:
|
|
130
|
+
available.append(table[None])
|
|
131
|
+
|
|
132
|
+
entries = []
|
|
133
|
+
weights = []
|
|
134
|
+
|
|
135
|
+
for group in available:
|
|
136
|
+
entries.extend(group["entries"])
|
|
137
|
+
weights.extend(group["weights"])
|
|
138
|
+
|
|
139
|
+
total = sum(weights)
|
|
140
|
+
weights = [w / total for w in weights] if total else []
|
|
141
|
+
|
|
142
|
+
for k, (entry, weight) in enumerate(zip(entries, weights)):
|
|
143
|
+
node = f"E{i}_{j}_{k}"
|
|
144
|
+
|
|
145
|
+
lines.append(
|
|
146
|
+
f" {table_node} -->|{weight:.2%}| " f'{node}["{entry}"]'
|
|
147
|
+
)
|
|
148
|
+
|
|
149
|
+
return "\n".join(lines)
|