-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_combo.inc
218 lines (200 loc) · 5.29 KB
/
parse_combo.inc
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
// two ways of parsing the instrumentation string (hier3) for a score,
// e.g. 'For Flute, 2 Oboes, and Harp (Yokoyama)'
//
// The first just returns 'Flute, 2 Oboes, and Harp';
// this is used to populate the arrangement_target table.
// Possibly deprecated.
//
// The second creates a data structure describing
// one or more "instrument combos": lists of (count, instrument)
////////////////// 1st way //////////////////
// return the arrangement target
// e.g. if the score has
// hier1 = 'Arrangements and Transcriptions'
// hier3 = 'For Flute, 2 Oboes, and Harp (Yokoyama)'
//
// Note: this is semi deprecated;
// see parse_arrangement_string()
function parse_arrangement_target($hier) {
$x = $hier[2];
if (substr2($x, 0, 4) == 'For ') {
$start = 4;
} else if (substr2($x, 0, 5) == '*For ') {
$start = 5;
} else {
return null;
}
$n = strpos($x, '(');
if ($n) {
$end = $n-1;
} else {
$end = strlen($x);
}
return substr2($x, $start, $end);
}
////////////////// 2nd way //////////////////
// parse a string like
// 'For 2 Clarinets, 2 Bassoons and 2 Horns (Patterson)'
// return a list of inst combos or null
// Each inst combo is a list of [count, code]
//
// for 'For Violin or Cello and Piano', return 2 combos:
// (violin, piano) and (cello, piano)
//
// TODO: make it work for
// For 4 Horns (or 3 Horns and Bassoon) (Miller)
// *For Violin, Cello or 2 Violins and Piano (Hoffmann)
// I guess this means (vio+cel+piano) or (2vio + piano)
// *For Piano Trio or 2 Violins and Piano (Hofmann)
// For 2 Flutes (2nd also Piccolo), 2 Oboes, 2 Clarinets, 2 Bassoons and 2 Horns (Clements)
//
// deal with missing stuff:
// Theater Orchestra
// Theatre Orchestra
// Wind Band
// String Orchestra
// Bandurria
// String Quartet
// Treble Recorder
// No.1 For Small Orchestra (Mouton)
// *For Instrument and Piano (Larocque)
//
function parse_arrangement_string($x) {
$x = strtolower($x);
// some special cases
$x = str_replace('pianos 8 hands', 'piano 8 hands', $x);
$x = str_replace('piano 4-hands', 'piano 4 hands', $x);
$x = str_replace('piano solo', 'piano', $x);
$x = str_replace('piano trio', 'violin, cello and piano', $x);
$x = str_replace('/', ' or ', $x);
// most but not all start with 'for '
$i = strpos($x, 'for ');
if ($i !== false) {
$x = substr($x, $i+4);
}
$i = strpos($x, '(');
if ($i !== false) {
$x = substr($x, 0, $i);
}
$x = str_replace(' and ', ' , ', $x);
$parts = explode(',', $x);
$pp = [];
$got_or = false;
foreach ($parts as $part) {
if (!$part) continue;
$p = parse_arr_part($part);
if (!$p) return null;
$pp[] = $p;
if (is_array($p[0])) {
$got_or = true;
}
}
if ($got_or) {
return [make_ic($pp, 0), make_ic($pp, 1)];
} else {
return [make_ic($pp)];
}
}
// $pp is a list of [count, code]
// or (for "or") [[count, code], [count, code]]
// Make a list, using one or the other of the "or" parts
//
function make_ic($pp, $i=0) {
$ic = [];
foreach ($pp as $p) {
if (is_array($p[0])) {
$ic[] = $p[$i];
} else {
$ic[] = $p;
}
}
return $ic;
}
// $x is like
// Piano 4 hands
// 2 Clarinets
// Chromatic Harmonica or Flute
//
// return [count, code] or [[count, code], [count, code]]
//
function parse_arr_part($x) {
$y = explode(' or ', $x);
if (count($y) > 1) {
$a = parse_arr_part($y[0]);
$b = parse_arr_part($y[1]);
if ($a && $b) return [$a, $b];
return null;
}
$x = trim($x);
$y = explode(' ', $x);
if (count($y)>1 && is_numeric($y[0])) {
$count = (int)$y[0];
$i = strpos($x, ' ');
$x = substr($x, $i+1);
} else {
$count = 1;
}
$code = inst_name_to_code($x);
if (!$code && $count>1 && substr($x, -1, 1)=='s') {
$x = substr($x, 0, -1);
$code = inst_name_to_code($x);
}
if ($code) {
return [$count, $code];
} else {
//echo "not found: $x\n";
return null;
}
}
// map instrument name to code
//
function inst_name_to_code($name) {
static $arr = [];
if (!$arr) {
$insts = DB_instrument::enum();
foreach ($insts as $inst) {
$arr[strtolower($inst->name)] = $inst->code;
}
}
if (array_key_exists($name, $arr)) {
return $arr[$name];
}
return null;
}
if (0) {
echo inst_name_to_code('voices');
}
if (0) {
$str = '*For Flute, 2 Oboes, Clarinet, Bassoon and Horn or Harp (Yokoyama)';
print_r(parse_arrangement_string($str));
}
if (0) {
// uncomment "not found" above
$fs = DB_score_file_set::enum("hier3<>''", 'limit 1000');
foreach ($fs as $f) {
$s = $f->hier3;
$x = parse_arrangement_string($s);
if ($x) {
echo "success: $s\n";
} else {
echo "fail: $s\n";
}
}
}
if (0) {
require_once("imslp_db.inc");
$fs = DB_audio_file_set::enum();
foreach ($fs as $f) {
$s = $f->hier3;
if (!str_starts_with($s, 'For ')) continue;
$x = parse_arrangement_string($s);
if ($x) {
echo "$s\n";
print_r($x);
} else {
echo "cant parse $s\n";
}
}
}
?>