/
sapient.ml
364 lines (333 loc) · 11.7 KB
/
sapient.ml
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
type t = string list list
type groups = (string * t) list
type sep =
| Comma
| And
| Semi
let rec ligature = function
| ("report"| "review" | "suggestions" as x) :: "and" :: q ->
x :: "&&&" :: ligature q
| "request" :: "and" :: q ->
"feature request" :: "&&&" :: ligature q
| "with" :: "help" :: "and" :: "review" :: "from":: q ->
"help" :: "by" :: q
| "with" :: "help" :: "and" :: "advice" :: q ->
"help" :: q
| "much" :: "input" :: "and" :: "thought" :: q ->
"thought" :: q
| "testing" :: "and" :: "regression" :: "fix" :: q ->
"regression" :: "fix" :: q
| "advice" :: "and" :: "review" :: q ->
"review" :: q
| x :: q -> x :: ligature q
| [] -> []
let by_connector = function
| "," -> Group_by.Sep Comma
| "and" -> Group_by.Sep And
| ";" -> Sep Semi
| x -> Elt x
let strip_postfix q = match List.rev q with
| ("Coq" | "testing") :: "for" :: q -> List.rev q
| _ :: "XCode" :: "against" :: q -> List.rev q
| _ :: "in" :: q -> List.rev q
| [ "skills"; "MSDN"; "impressive"; "displaying"] -> []
| _ -> q
let normalize_name ~warn = function
(* groups *)
| ["the"; "Tarides" ; "multicore"; "team"] -> ["Tarides multicore team"]
| ["the"; "OCaml"; "core"; "development"; "team"] -> ["OCaml core development team"]
| ["others"] | ["many"; "other"; "valued"; "reviewers"] | [("Many"|"many"); "fine"; "eyes"] ->
["Many fine eyes"]
| ["same"] | ["the"; "same"] | ["reviewing"; "each"; "other"; "without"; "self-loops";] ->
["Author group review"]
(* typo *)
| (["Nicolas"; "Ojeda"; "Bar"]) -> ["Nicolás"; "Ojeda"; "Bär"]
| ["Jacques-"; "Henri"; "Jourdan"] -> ["Jacques-Henri"; "Jourdan"]
| ["Florian"; "Angetti"] -> ["Florian"; "Angeletti"]
| ["Francois"; "Berenger"] -> ["François"; "Berenger"]
| [("Nathanaël"|"Naëla"); "Courant"] -> ["Nathanaëlle"; "Courant"]
| ["Frederic"; "Bour"] -> ["Frédéric"; "Bour"]
| ["Sebastien"; "Hinderer"] -> ["Sébastien"; "Hinderer"]
(* pseudo*)
| ["octachron@"] -> ["Florian"; "Angeletti"]
| ["Daniel"; "C."; "Bünzli"] -> ["Daniel"; "Bünzli"]
| ["David"; ("Alsopp" | "Allsop") ] -> ["David"; "Allsopp"]
| ["Oliver"; "Andrieu"] -> ["Olivier"; "Andrieu"]
| ["Gabiel"; "Scherer"] -> ["Gabriel"; "Scherer"]
| ["San"; "Vu"; "Ngoc"] -> ["San";"Vũ";"Ngọc"]
| ["Stephen"; "DOlan"] -> ["Stephen"; "Dolan"]
| ["Demi"; "Obenour"] -> ["Demi"; "Marie"; "Obenour"]
| ["Fuyong"; "Quah"] -> ["Fu"; "Yong"; "Quah"]
| ["Mekhrubon"; "Tuarev"] -> ["Mekhrubon"; "Turaev"]
(* Long names*)
| (["Perry"; "E."; "Metzger"] as q)
| (["Hezekiah"; "M."; "Carty"] as q)
| (["Paul-Elliot"; "Anglès"; "d'Auriac"] as q)
| (["Antonio"; "Nuno"; "Monteiro"] as q)
| ( [ _; ("von"|"De"|"Van"); _] as q )
| (["Richard"; "L"; "Ford"] as q)
| (["Fabrice"; "Le"; "Fessant"] as q)
| (["Nicolás"; "Ojeda"; "Bär"] as q)
| (["Demi"; "Marie"; "Obenour"] as q)
| (["Raphael"; "Sousa"; "Santos"] as q)
| (["Fu"; "Yong"; "Quah"] as q)
| (["San"; "Vũ"; "Ngọc"] as q)
| (["Isaac"; {|"Izzy"|}; "Avram"] as q)
| (["Gabriel"; "de"; "Perthuis"] as q)
| (["John"; "Christopher"; "McAlpine"] as q)
| (["Peter"; "Michael"; "Green"] as q)
| (["Khoo"; "Yit"; "Phang"] as q)
| ("Github"|"github") :: "user" :: q
| (["Xavier"; "Van"; "de"; "Woestyne" ] as q) -> q
(* mixed format *)
| ["Leo"; "White"; "(#2269)"] -> ["Leo"; "White"]
| ["&"; "Mark"; "Shinwell" ] -> ["Mark"; "Shinwell" ]
| x ->
if warn then Format.eprintf "Complex name or error:%s@." (String.concat " " x);
x
let elt ?(warn=false) x= Group_by.Elt (normalize_name ~warn x)
let split_section x = match strip_postfix (List.filter ((<>) "") x) with
| "additional" :: "testing" :: "by" :: q ->
[Group_by.Sep "tests"; elt q]
(* alternative name for authors *)
| ("code"|"patch") :: "by" :: q
| "final" :: "fix" :: "by" :: q ->
[Group_by.Sep "authors"; elt q]
(* factorizations *)
| "review" :: "&&&" :: "final" :: "fix" :: "by" :: name ->
[Sep "review"; Elt name; Sep "authors"; Elt name]
| "report" :: "&&&" :: "fix" :: "by" :: name ->
[Sep "report"; Elt name; Sep "authors"; Elt name]
| x :: "&&&" :: y :: "by" :: name ->
[Sep x; Elt name; Sep y; Elt name]
| x :: "&&&" :: y :: z :: "by" :: name ->
[Sep x; Elt name; Sep (String.concat " " [y;z]); elt name]
| x :: "&&&" :: y :: z :: w :: "by" :: name ->
[Sep x; Elt name; Sep (String.concat " " [y;z;w]); elt name]
(* author section "header"*)
| ("help"|"review" | "thought" | "feedback" | "contributions" | "advice" | "design" | "discussion" as x)
:: ("from"|"with") :: q
| "patch" :: ("review" as x) :: "by" :: q
| "with" :: ("help" | "inspiration" as x) :: "from" :: q
| ("inspiration" as x) :: "from" :: q
| ("fix" as x) :: "suggested" :: "by" :: q
| _ :: ("design" as x) :: "by" :: q
| ("light"|"code") :: ("review" as x) :: "by" :: q
| _ :: ("fix" as x) :: "by" :: q
| _ :: ("report" as x) :: "by" :: q
| "thanks" :: "to" :: "a" :: ("report" as x) :: "of" :: q
| ("thanks" as x) :: "to" :: q
| "following" :: ("discussion" as x) :: "with" :: q
| "with" :: ("thanks" | "feedback" | "contributions" as x) :: ("to"|"from") :: q
| ("report" as x) :: "par" :: q
| "additional" :: x :: "by" :: q
| ("fix" as x) :: "in" :: _ :: "by" :: q
| "from" :: ("an"|"a") :: x :: "by" :: q
| "initial" :: ("debugging" as x) :: "by" :: q
| "design":: ("advice" as x) :: "by" :: q
| ("compatibility" as x) :: "hacking":: "by" :: q
| ("report" as x) :: "on" :: "the" :: q
| "additional" :: x :: "with" :: q
| "superior" :: "implementation" :: x :: "by" :: q
| ("debugging" as x) :: "&" :: "test" :: ("cases" | "case") :: "by" :: q
| x :: "by" :: q
| ("review" | "report" as x) :: q
->
if List.is_empty q then
[Group_by.Sep x]
else
[Group_by.Sep x; elt q]
| "designed" :: "with" :: q -> [Group_by.Sep "design"; elt q]
| ("initial"|"first") :: ("PR"|"patch") :: "by" :: q ->
[Group_by.Sep "initial PR"; elt q]
| "based" :: "on" :: "an" :: "initial" :: "work" :: "by" :: q ->
[Group_by.Sep "initial work"; elt q]
| "feedbacks" :: "from" :: q ->
[Group_by.Sep "feedback"; elt q]
| "reports" :: "from" :: q ->
[Group_by.Sep "report"; elt q]
| ("feature" | "original") :: "request" :: ("from"|"by") :: q ->
[Group_by.Sep "feature request"; elt q]
| "bug" :: "reported" :: q
| "regression" :: "spotted" :: q ->
[Group_by.Sep "report"; elt q]
| "stealth" :: "commit" :: "by" :: q ->
[ Sep "stealth commit"; elt q]
| x :: y :: ("review" | "reviewed") :: "by" :: q ->
[Elt [x;y]; Sep "review"; elt q]
| x :: y :: z :: ("review" | "reviewed") :: "by" :: q ->
[Elt [x;y;z]; Sep "review"; elt q]
| _ :: "bug" :: "report" :: "by" :: q ->
[Group_by.Sep "bug report"; elt q]
| "split" :: "off" :: "from" :: _ :: "by" :: q ->
[Group_by.Sep "PR editing"; elt q]
(* with grouping *)
| [x;y;"with";w;z] ->
[Elt [x;y]; Elt [w;z]]
(* typo fixes *)
| ["Xavier"; "Leroy"; "Guillaume"; "Munch-Maccagnoni";] ->
[Elt ["Xavier"; "Leroy"]; Elt ["Guillaume"; "Munch-Maccagnoni";]]
| ["caml-list"; "discussion"] ->
[Sep "review"; elt ["caml-list"]]
| ["reported"; "on"; "caml-list"] | ["reported"; "on"; "caml-list"; "on"; _] ->
[Sep "report"; elt ["caml-list"]]
| [] -> []
| q ->
let warn = List.length q > 2 in
[Elt (normalize_name ~warn q)]
let known_non_authors = function
| "no cross ref to class after dump+load"
| "changes the generated sequences"
| "error message in french"
| "partially"
| "Windows"
| "PREFIX"
| "reported in private"
| "bytecode"
| "genprintval.ml"
| "camlp4 revised syntax"
| "ocamlopt, x86"
| "mingw"
| "crash"
| "less formatting in html code"
| "i.e. non-contractive"
| "duplicate of #6686"
| "CC0 1.0"
| "GADTs"
| "PACKLD is more complete"
| "with -g"
| "interface with Unix DBM key-value stores"
| "armhf" | "armel"
| "MAKE"
| "lazy"
| "Random.self_init()"
| "from #5318"
| "set|clear"
| "probably fixed"
| "or"
| "" | "_" -> true
| s ->
if List.exists (fun prefix -> String.starts_with ~prefix s)
[ "module ";
"val ";
"fun ";
"i.e. ";
"e.g. ";
"flag ";
"More information in";
"Allow easy retrieval";
"ocamlc ";
"but does not";
"-runtime-";
"-with-debug";
"(val ";
]
then
(
Format.eprintf "Prefix non author detection: %s@." s;
true
)
else
false
module Dict = Map.Make(String)
let merge l =
l |> Dict.of_list |> Dict.bindings
let validate_author s =
if known_non_authors s then Error [] else
match Angstrom.(parse_string ~consume:Prefix) Refs.parse_many s with
| Ok x ->
Format.eprintf "Old reference syntax detected: %s=%a@."
s Fmt.(Dump.list int) x;
Error x
| Error _ -> Ok ()
let parse authors =
let split_punct s =
let len = String.length s in
let last = s.[len-1] in
if (len > 1 && (last = ',' || last = ';')) ||
(len > 2 && last = '.') then
Seq.cons (String.sub s 0 (len-1)) (Seq.return ",")
else
Seq.return s
in
match validate_author authors with
| Error _ as x -> x
| Ok () ->
let words =
authors
|> String.map (function '\n' -> ' ' | c -> c )
|> String.split_on_char ' '
|> List.to_seq
|> Seq.filter (function "" -> false | _ -> true)
|> Seq.concat_map split_punct
|> List.of_seq
|> ligature
|> Group_by.list ~debug:Fmt.string ~and_then:Fun.id ~parent:Comma by_connector
|> List.map snd
|> List.concat_map split_section
|> Group_by.list ~debug:Fmt.(list string) ~and_then:Fun.id ~parent:"authors" Fun.id
|> merge
in Ok words
let rec seq_parentheses stack pos s () =
if pos < 0 then Seq.Nil
else match s.[pos] with
| ')' ->
seq_parentheses (pos :: stack) (pos-1) s ()
| '(' ->
begin match stack with
| last :: stack ->
Seq.cons (pos,last) (seq_parentheses stack (pos-1) s) ()
| [] ->
Seq.cons (pos, String.length s) (seq_parentheses stack (pos-1) s) ()
end
| _ ->
seq_parentheses stack (pos-1) s ()
let seq_parentheses s () = seq_parentheses [] (String.length s - 1) s ()
let cut_at_char s pos =
let gap = 1 in
String.sub s 0 pos,
String.sub s (pos + gap) (String.length s - pos - gap )
let split_middle s start stop =
let rest, last =
if stop >= String.length s then
s, ""
else cut_at_char s stop in
let before, mid =
if start >= String.length rest then
rest, ""
else
cut_at_char rest start in
before, mid, last
let validate_parenthese s (first,last) =
let before, mid, last = split_middle s first last in
match parse mid with
| Error _ as x -> x
| Ok authors -> Ok (before,authors,last)
type split = { main:string; refs: int list; sapients: groups}
let no_authors refs main expl =
Format.eprintf "Entry without authors %t: %s @." expl main;
{ refs; main; sapients= []}
let rec punct_trim_right ~blank s pos =
if pos < 0 then ""
else match s.[pos] with
| '.' -> punct_trim_right ~blank:false s (pos-1)
| ' ' | '\t' | '\n' -> punct_trim_right ~blank:true s (pos-1)
| _ ->
if blank then
String.sub s 0 (1+pos)
else
String.sub s 0 (2+pos)
let trim_right s = punct_trim_right ~blank:true s (String.length s - 1)
let (+?) s l = if String.trim s = "" then l else s :: l
let rec find_sapients refs s seq =
match seq () with
| Seq.Nil -> no_authors refs s (Format.dprintf "No parenthesis group found")
| Seq.Cons(x, seq) ->
match validate_parenthese s x with
| Ok (before,sapients,after) ->
{ refs; main = before ^ trim_right after; sapients }
| Error rmore ->
find_sapients (rmore@refs) s seq
let split lines =
let text = String.concat "\n" lines in
find_sapients [] text (seq_parentheses text)