Debian Codegraph @

eutils/trie.go

raw
1 // ===========================================================================
2 //
3 // PUBLIC DOMAIN NOTICE
4 // National Center for Biotechnology Information (NCBI)
5 //
6 // This software/database is a "United States Government Work" under the
7 // terms of the United States Copyright Act. It was written as part of
8 // the author's official duties as a United States Government employee and
9 // thus cannot be copyrighted. This software/database is freely available
10 // to the public for use. The National Library of Medicine and the U.S.
11 // Government do not place any restriction on its use or reproduction.
12 // We would, however, appreciate having the NCBI and the author cited in
13 // any work or product based on this material.
14 //
15 // Although all reasonable efforts have been taken to ensure the accuracy
16 // and reliability of the software and data, the NLM and the U.S.
17 // Government do not and cannot warrant the performance or results that
18 // may be obtained by using this software or data. The NLM and the U.S.
19 // Government disclaim all warranties, express or implied, including
20 // warranties of performance, merchantability or fitness for any particular
21 // purpose.
22 //
23 // ===========================================================================
24 //
25 // File Name: trie.go
26 //
27 // Author: Jonathan Kans
28 //
29 // ==========================================================================
30
31 package eutils
32
33 import (
34 "path/filepath"
35 "strings"
36 "unicode"
37 )
38
39 // LinkLen controls the number of directory levels for link terms
40 const LinkLen = 4
41
42 // TrieLen directory depth parameters are based on the observed size distribution of PubMed indices
43 var TrieLen = map[string]int{
44 "17": 4,
45 "18": 4,
46 "19": 4,
47 "20": 4,
48 "a1": 3,
49 "ab": 3,
50 "ac": 4,
51 "ad": 3,
52 "af": 4,
53 "ag": 3,
54 "al": 3,
55 "an": 4,
56 "ap": 4,
57 "ar": 3,
58 "as": 4,
59 "b0": 3,
60 "ba": 4,
61 "be": 4,
62 "bi": 3,
63 "br": 3,
64 "c0": 3,
65 "c1": 3,
66 "ca": 4,
67 "ce": 4,
68 "ch": 4,
69 "cl": 4,
70 "co": 4,
71 "cr": 3,
72 "cy": 3,
73 "d0": 4,
74 "d1": 4,
75 "d2": 3,
76 "da": 4,
77 "de": 4,
78 "di": 4,
79 "do": 3,
80 "dr": 3,
81 "e0": 3,
82 "ef": 4,
83 "el": 4,
84 "en": 4,
85 "ev": 3,
86 "ex": 4,
87 "fa": 3,
88 "fe": 4,
89 "fi": 3,
90 "fo": 4,
91 "fr": 4,
92 "fu": 4,
93 "g0": 3,
94 "ge": 4,
95 "gr": 4,
96 "he": 4,
97 "hi": 4,
98 "ho": 4,
99 "hu": 4,
100 "hy": 4,
101 "jo": 4,
102 "im": 3,
103 "in": 4,
104 "la": 3,
105 "le": 3,
106 "li": 3,
107 "lo": 3,
108 "ma": 3,
109 "me": 4,
110 "mi": 3,
111 "mo": 4,
112 "mu": 3,
113 "mz": 3,
114 "n0": 3,
115 "ne": 3,
116 "no": 4,
117 "ob": 3,
118 "on": 3,
119 "oz": 3,
120 "pa": 4,
121 "pe": 4,
122 "ph": 3,
123 "pl": 4,
124 "po": 4,
125 "pr": 4,
126 "pu": 4,
127 "ra": 3,
128 "re": 4,
129 "ri": 3,
130 "ro": 4,
131 "rz": 3,
132 "sa": 4,
133 "sc": 4,
134 "se": 3,
135 "si": 4,
136 "so": 4,
137 "sp": 4,
138 "st": 4,
139 "su": 4,
140 "sy": 4,
141 "te": 3,
142 "th": 3,
143 "ti": 3,
144 "to": 4,
145 "tr": 4,
146 "tw": 4,
147 "un": 3,
148 "va": 3,
149 "ve": 3,
150 "vi": 3,
151 "we": 3,
152 "wh": 3,
153 }
154
155 // MergLen directory depth parameters are based on the observed size distribution of PubMed indices
156 var MergLen = map[string]int{
157 "ana": 4,
158 "app": 4,
159 "ass": 4,
160 "can": 4,
161 "cas": 4,
162 "cha": 4,
163 "cli": 4,
164 "com": 4,
165 "con": 4,
166 "d00": 4,
167 "d01": 4,
168 "d02": 4,
169 "d12": 4,
170 "dam": 4,
171 "dat": 4,
172 "dec": 4,
173 "ded": 4,
174 "del": 4,
175 "dem": 4,
176 "dep": 4,
177 "des": 4,
178 "det": 4,
179 "dev": 4,
180 "dif": 4,
181 "dis": 4,
182 "eff": 4,
183 "enf": 4,
184 "exp": 4,
185 "for": 4,
186 "gen": 4,
187 "gro": 4,
188 "hea": 4,
189 "hig": 4,
190 "hum": 4,
191 "inc": 4,
192 "ind": 4,
193 "int": 4,
194 "inv": 4,
195 "met": 4,
196 "mod": 4,
197 "pat": 4,
198 "per": 4,
199 "pre": 4,
200 "pro": 4,
201 "pub": 4,
202 "rel": 4,
203 "rep": 4,
204 "res": 4,
205 "sig": 4,
206 "sta": 4,
207 "str": 4,
208 "stu": 4,
209 "tre": 4,
210 }
211
212 func getPrefixAndPadID(id string) (string, string) {
213
214 // "2539356"
215
216 if len(id) > 64 {
217 return "", id
218 }
219
220 str := id
221
222 if IsAllDigits(str) {
223
224 // pad numeric identifier to 8 characters with leading zeros
225 ln := len(str)
226 if ln < 8 {
227 zeros := "00000000"
228 str = zeros[ln:] + str
229 }
230 }
231
232 // "02539356"
233
234 if IsAllDigitsOrPeriod(str) {
235
236 // limit trie to first 6 characters
237 if len(str) > 6 {
238 str = str[:6]
239 }
240 }
241
242 // "025393"
243
244 max := 4
245 k := 0
246 for _, ch := range str {
247 if unicode.IsLetter(ch) {
248 k++
249 continue
250 }
251 if ch == '_' {
252 k++
253 max = 6
254 }
255 break
256 }
257
258 // prefix is up to three letters if followed by digits,
259 // or up to four letters if followed by an underscore
260 pfx := str[:k]
261 if len(pfx) < max {
262 str = str[k:]
263 } else {
264 pfx = ""
265 }
266
267 // "", "025393"
268
269 return pfx, str
270 }
271
272 // PadNumericID returns 8-character leading zero-padded numeric identifier
273 func PadNumericID(id string) string {
274
275 // "2539356"
276
277 if len(id) > 64 {
278 return id
279 }
280
281 str := id
282
283 if IsAllDigits(str) {
284
285 // pad numeric identifier to 8 characters with leading zeros
286 ln := len(str)
287 if ln < 8 {
288 zeros := "00000000"
289 str = zeros[ln:] + str
290 }
291 }
292
293 // "02539356"
294
295 return str
296 }
297
298 // ArchiveTrie allows a short prefix of letters with an optional underscore,
299 // and splits the remainder into character pairs
300 func ArchiveTrie(id string) (string, string) {
301
302 // "2539356"
303
304 if len(id) > 64 {
305 return "", ""
306 }
307
308 pfx, str := getPrefixAndPadID(id)
309
310 var arry [132]rune
311
312 i := 0
313
314 if pfx != "" {
315 for _, ch := range pfx {
316 arry[i] = ch
317 i++
318 }
319 arry[i] = '/'
320 i++
321 }
322
323 between := 0
324 doSlash := false
325
326 // remainder is divided in character pairs, e.g., NP_/06/00/51 for NP_060051.2
327 for _, ch := range str {
328 // break at period separating accession from version
329 if ch == '.' {
330 break
331 }
332 if doSlash {
333 arry[i] = '/'
334 i++
335 doSlash = false
336 }
337 if ch == ' ' {
338 ch = '_'
339 }
340 if !unicode.IsLetter(ch) && !unicode.IsDigit(ch) {
341 ch = '_'
342 }
343 arry[i] = ch
344 i++
345 between++
346 if between > 1 {
347 doSlash = true
348 between = 0
349 }
350 }
351
352 res := string(arry[:i])
353
354 if !strings.HasSuffix(res, "/") {
355 arry[i] = '/'
356 i++
357 res = string(arry[:i])
358 }
359
360 // "02/53/93/", "2539356"
361
362 return strings.ToUpper(res), id
363 }
364
365 // IndexTrie generates the path and file name for first-level incremental index files
366 func IndexTrie(id string) (string, string) {
367
368 // "2539356"
369
370 if len(id) > 64 {
371 return "", ""
372 }
373
374 pfx, str := getPrefixAndPadID(id)
375
376 // "02539356"
377
378 var arry [132]rune
379
380 i := 0
381
382 if pfx != "" {
383 for _, ch := range pfx {
384 arry[i] = ch
385 i++
386 }
387 arry[i] = '/'
388 i++
389 }
390
391 between := 0
392 doSlash := false
393
394 // remainder is divided in character pairs, e.g., NP_/06/00/51 for NP_060051.2
395 for _, ch := range str {
396 // break at period separating accession from version
397 if ch == '.' {
398 break
399 }
400 if doSlash {
401 arry[i] = '/'
402 i++
403 doSlash = false
404 }
405 if ch == ' ' {
406 ch = '_'
407 }
408 if !unicode.IsLetter(ch) && !unicode.IsDigit(ch) {
409 ch = '_'
410 }
411 arry[i] = ch
412 i++
413 between++
414 if between > 1 {
415 doSlash = true
416 between = 0
417 }
418 }
419
420 // trim back one subdirectory level from Archive for Index
421 i -= 3
422
423 res := string(arry[:i])
424
425 if !strings.HasSuffix(res, "/") {
426 arry[i] = '/'
427 i++
428 res = string(arry[:i])
429 }
430
431 // return the 6-digit .e2x file name for precomputed incremental
432 // index cached for all .xml records in 10 adjacent leaf folders
433 if IsAllDigits(id) {
434
435 // pad numeric identifier to 8 characters with leading zeros
436 ln := len(id)
437 if ln < 8 {
438 zeros := "00000000"
439 id = zeros[ln:] + id
440 }
441 }
442
443 idx := id
444
445 if IsAllDigitsOrPeriod(id) {
446
447 // limit index trie to first 6 characters
448 if len(idx) > 6 {
449 idx = idx[:6]
450 }
451 }
452
453 // "02/53/", "025393"
454
455 return strings.ToUpper(res), idx
456 }
457
458 // InvertTrie generates the path and file name for second-level inverted index files
459 func InvertTrie(id string) (string, string) {
460
461 if len(id) > 64 {
462 return "", ""
463 }
464
465 // "2539356"
466
467 dir, inv := IndexTrie(id)
468
469 // "02/53/", "025393"
470
471 // limit invert directory to first 3 characters
472 if len(dir) > 3 {
473 dir = dir[:3]
474 }
475 // limit invert trie to first 4 characters
476 if len(inv) > 4 {
477 inv = inv[:4]
478 }
479
480 // "02/", "0253"
481
482 return dir, inv
483 }
484
485 // PostingDir returns directory trie (without slashes) for location of indices for a given term
486 func PostingDir(term string) string {
487
488 if len(term) < 3 {
489 return term
490 }
491
492 key := term[:2]
493
494 num, ok := TrieLen[key]
495 if ok && len(term) >= num {
496 return term[:num]
497 }
498
499 switch term[0] {
500 case 'u', 'v', 'w', 'x', 'y', 'z':
501 return term[:2]
502 }
503
504 return term[:3]
505 }
506
507 // IdentifierKey cleans up a term then returns the posting directory
508 func IdentifierKey(term string) string {
509
510 // remove punctuation from term
511 key := strings.Map(func(c rune) rune {
512 if !unicode.IsLetter(c) && !unicode.IsDigit(c) && c != ' ' && c != '-' && c != '_' {
513 return -1
514 }
515 return c
516 }, term)
517
518 key = strings.Replace(key, " ", "_", -1)
519 key = strings.Replace(key, "-", "_", -1)
520
521 // use first 2, 3, or 4 characters of identifier for directory
522 key = PostingDir(key)
523
524 return key
525 }
526
527 // PostingsTrie splits a string into characters, separated by path delimiting slashes
528 func PostingsTrie(term string) (string, string) {
529
530 // "cancer"
531
532 if len(term) > 256 {
533 return "", term
534 }
535
536 // use first few characters of identifier for directory
537 key := IdentifierKey(term)
538
539 str := key
540
541 var arry [516]rune
542
543 if IsNotASCII(str) {
544 // expand Greek letters, anglicize characters in other alphabets
545 str = TransformAccents(str, true, true)
546 }
547 if HasAdjacentSpaces(str) {
548 str = CompressRunsOfSpaces(str)
549 }
550 str = strings.TrimSpace(str)
551
552 i := 0
553 doSlash := false
554
555 for _, ch := range str {
556 if doSlash {
557 arry[i] = '/'
558 i++
559 }
560 if ch == ' ' {
561 ch = '_'
562 }
563 if !unicode.IsLetter(ch) && !unicode.IsDigit(ch) {
564 ch = '_'
565 }
566 arry[i] = ch
567 i++
568 doSlash = true
569 }
570
571 // "c/a/n/c", "canc"
572
573 return strings.ToLower(string(arry[:i])), key
574 }
575
576 // PostingPath constructs a Postings directory subpath for a given term prefix
577 func PostingPath(prom, field, term string, isLink bool) (string, string) {
578
579 // "/Volumes/archive/pubmed/Postings", "TIAB", "cancer"
580
581 if isLink {
582 dir, _ := LinksTrie(term, false)
583 if dir == "" {
584 return "", ""
585 }
586 dpath := filepath.Join(prom, field, dir)
587
588 return dpath, term
589 }
590
591 // use first few characters of identifier for directory
592 key := IdentifierKey(term)
593
594 dir, _ := PostingsTrie(term)
595 if dir == "" {
596 return "", ""
597 }
598
599 dpath := filepath.Join(prom, field, dir)
600
601 // "/Volumes/archive/pubmed/Postings/TIAB/c/a/n/c", "canc"
602
603 return dpath, key
604 }
605
606 // LinksTrie generates the path and file name for link inverted index files
607 func LinksTrie(id string, pad bool) (string, string) {
608
609 // "2539356"
610
611 if len(id) > 64 {
612 return "", id
613 }
614
615 str := id
616
617 if pad {
618
619 // true from ProcessLinks, false from PostingPath (indexed link terms are already zero-padded)
620
621 str = PadNumericID(id)
622
623 // "02539356"
624 }
625
626 // links always use 4 directory levels of padded identifiers, grouped numerically,
627 // so ProcessLinks may be able to get multiple nearby links with fewer reads
628 if len(str) > LinkLen {
629 str = str[:LinkLen]
630 }
631
632 // "0253"
633
634 var arry [132]rune
635
636 i := 0
637 doSlash := false
638
639 for _, ch := range str {
640 if doSlash {
641 arry[i] = '/'
642 i++
643 }
644 if ch == ' ' {
645 ch = '_'
646 }
647 if !unicode.IsLetter(ch) && !unicode.IsDigit(ch) {
648 ch = '_'
649 }
650 arry[i] = ch
651 i++
652 doSlash = true
653 }
654
655 res := string(arry[:i])
656
657 if !strings.HasSuffix(res, "/") {
658 arry[i] = '/'
659 i++
660 res = string(arry[:i])
661 }
662
663 // "0/2/5/3/", "0253" (pad true)
664
665 return strings.ToUpper(res), str
666 }