| 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: xplore.go |
| 26 |
// |
| 27 |
// Author: Jonathan Kans |
| 28 |
// |
| 29 |
// ========================================================================== |
| 30 |
|
| 31 |
package eutils |
| 32 |
|
| 33 |
import ( |
| 34 |
"encoding/base64" |
| 35 |
"fmt" |
| 36 |
"github.com/fatih/color" |
| 37 |
"github.com/surgebase/porter2" |
| 38 |
"golang.org/x/text/cases" |
| 39 |
"golang.org/x/text/language" |
| 40 |
"html" |
| 41 |
"maps" |
| 42 |
"math" |
| 43 |
"net/url" |
| 44 |
"os" |
| 45 |
"regexp" |
| 46 |
"slices" |
| 47 |
"strconv" |
| 48 |
"strings" |
| 49 |
"sync" |
| 50 |
"unicode" |
| 51 |
) |
| 52 |
|
| 53 |
// SUPPORT CODE FOR xtract DATA EXTRACTION |
| 54 |
|
| 55 |
var ( |
| 56 |
rlock sync.Mutex |
| 57 |
replx map[string]*regexp.Regexp |
| 58 |
) |
| 59 |
|
| 60 |
// Clause consolidates a dozen function arguments into a single structure |
| 61 |
type Clause struct { |
| 62 |
Prev string |
| 63 |
Pfx string |
| 64 |
Sfx string |
| 65 |
Plg string |
| 66 |
Sep string |
| 67 |
Def string |
| 68 |
Msk string |
| 69 |
Reg string |
| 70 |
Exp string |
| 71 |
Wth string |
| 72 |
Gcd int |
| 73 |
Frm int |
| 74 |
Wrp bool |
| 75 |
} |
| 76 |
|
| 77 |
// Atlas consolidates several maps and one structure pointer |
| 78 |
type Atlas struct { |
| 79 |
Variables map[string]string |
| 80 |
Transform map[string]string |
| 81 |
Srchr *FSMSearcher |
| 82 |
Histogram map[string]int |
| 83 |
} |
| 84 |
|
| 85 |
// processClause handles comma-separated -element arguments |
| 86 |
func processClause( |
| 87 |
curr *XMLNode, |
| 88 |
stages []*Step, |
| 89 |
mask string, |
| 90 |
cls Clause, |
| 91 |
status OpType, |
| 92 |
index int, |
| 93 |
level int, |
| 94 |
atl Atlas, |
| 95 |
) (string, bool) { |
| 96 |
|
| 97 |
if curr == nil || stages == nil { |
| 98 |
return "", false |
| 99 |
} |
| 100 |
|
| 101 |
pfx := cls.Pfx |
| 102 |
sfx := cls.Sfx |
| 103 |
sep := cls.Sep |
| 104 |
reg := cls.Reg |
| 105 |
exp := cls.Exp |
| 106 |
wrp := cls.Wrp |
| 107 |
msk := cls.Msk |
| 108 |
|
| 109 |
variables := atl.Variables |
| 110 |
transform := atl.Transform |
| 111 |
srchr := atl.Srchr |
| 112 |
histogram := atl.Histogram |
| 113 |
|
| 114 |
if replx == nil { |
| 115 |
rlock.Lock() |
| 116 |
if replx == nil { |
| 117 |
replx = make(map[string]*regexp.Regexp) |
| 118 |
} |
| 119 |
rlock.Unlock() |
| 120 |
} |
| 121 |
|
| 122 |
// field for -indexer derived from -pfx argument set by -wrp |
| 123 |
indexerField := "" |
| 124 |
|
| 125 |
if status == INDEXER { |
| 126 |
if strings.HasPrefix(pfx, "<") && strings.HasSuffix(pfx, ">") && !strings.Contains(pfx, "/") { |
| 127 |
// take label from -pfx argument minus the angle brackets added by -wrp |
| 128 |
indexerField = strings.TrimPrefix(pfx, "<") |
| 129 |
indexerField = strings.TrimSuffix(indexerField, ">") |
| 130 |
indexerField = strings.TrimSpace(indexerField) |
| 131 |
} |
| 132 |
// clear -wrp derived arguments (even if they weren't set by -wrp) |
| 133 |
pfx = "" |
| 134 |
sfx = "" |
| 135 |
sep = "" |
| 136 |
} |
| 137 |
|
| 138 |
// processElement handles individual -element constructs |
| 139 |
processElement := func(acc func(string)) { |
| 140 |
|
| 141 |
if acc == nil { |
| 142 |
return |
| 143 |
} |
| 144 |
|
| 145 |
// element names combined with commas are treated as a prefix-separator-suffix group |
| 146 |
for _, stage := range stages { |
| 147 |
|
| 148 |
stat := stage.Type |
| 149 |
item := stage.Value |
| 150 |
prnt := stage.Parent |
| 151 |
match := stage.Match |
| 152 |
attrib := stage.Attrib |
| 153 |
typL := stage.TypL |
| 154 |
strL := stage.StrL |
| 155 |
intL := stage.IntL |
| 156 |
typR := stage.TypR |
| 157 |
strR := stage.StrR |
| 158 |
intR := stage.IntR |
| 159 |
norm := stage.Norm |
| 160 |
wildcard := stage.Wild |
| 161 |
unescape := stage.Unesc |
| 162 |
|
| 163 |
// exploreElements is a wrapper for ExploreElements, inheriting most common arguments as closures |
| 164 |
exploreElements := func(proc func(string, int)) { |
| 165 |
ExploreElements(curr, mask, prnt, match, attrib, wildcard, unescape, level, proc) |
| 166 |
} |
| 167 |
|
| 168 |
// sendSlice applies optional [min:max] range restriction and sends result to accumulator |
| 169 |
sendSlice := func(str string) { |
| 170 |
|
| 171 |
if str == "" { |
| 172 |
return |
| 173 |
} |
| 174 |
|
| 175 |
// handle usual situation with no range first |
| 176 |
if norm { |
| 177 |
if wrp && stat != REPLACE { |
| 178 |
str = html.EscapeString(str) |
| 179 |
} |
| 180 |
acc(str) |
| 181 |
return |
| 182 |
} |
| 183 |
|
| 184 |
// check for [after|before] variant |
| 185 |
if typL == STRINGRANGE || typR == STRINGRANGE { |
| 186 |
if strL != "" { |
| 187 |
// use case-insensitive test |
| 188 |
strL = strings.ToUpper(strL) |
| 189 |
idx := strings.Index(strings.ToUpper(str), strL) |
| 190 |
if idx < 0 { |
| 191 |
// specified substring must be present in original string |
| 192 |
return |
| 193 |
} |
| 194 |
ln := len(strL) |
| 195 |
// remove leading text |
| 196 |
str = str[idx+ln:] |
| 197 |
} |
| 198 |
if strR != "" { |
| 199 |
strR = strings.ToUpper(strR) |
| 200 |
idx := strings.Index(strings.ToUpper(str), strR) |
| 201 |
if idx < 0 { |
| 202 |
// specified substring must be present in remaining string |
| 203 |
return |
| 204 |
} |
| 205 |
// remove trailing text |
| 206 |
str = str[:idx] |
| 207 |
} |
| 208 |
if str != "" { |
| 209 |
if wrp && stat != REPLACE { |
| 210 |
str = html.EscapeString(str) |
| 211 |
} |
| 212 |
acc(str) |
| 213 |
} |
| 214 |
return |
| 215 |
} |
| 216 |
|
| 217 |
min := 0 |
| 218 |
max := 0 |
| 219 |
|
| 220 |
// slice arguments use variable value +- adjustment or integer constant |
| 221 |
if typL == VARIABLERANGE { |
| 222 |
if strL == "" { |
| 223 |
return |
| 224 |
} |
| 225 |
lft, ok := variables[strL] |
| 226 |
if !ok { |
| 227 |
return |
| 228 |
} |
| 229 |
val, err := strconv.Atoi(lft) |
| 230 |
if err != nil { |
| 231 |
return |
| 232 |
} |
| 233 |
// range argument values are inclusive and 1-based, decrement variable start +- offset to use in slice |
| 234 |
min = val + intL - 1 |
| 235 |
} else if typL == INTEGERRANGE { |
| 236 |
// range argument values are inclusive and 1-based, decrement literal start to use in slice |
| 237 |
min = intL - 1 |
| 238 |
} |
| 239 |
if typR == VARIABLERANGE { |
| 240 |
if strR == "" { |
| 241 |
return |
| 242 |
} |
| 243 |
rgt, ok := variables[strR] |
| 244 |
if !ok { |
| 245 |
return |
| 246 |
} |
| 247 |
val, err := strconv.Atoi(rgt) |
| 248 |
if err != nil { |
| 249 |
return |
| 250 |
} |
| 251 |
if val+intR < 0 { |
| 252 |
// negative value is 1-based inset from end of string (undocumented) |
| 253 |
max = len(str) + val + intR + 1 |
| 254 |
} else { |
| 255 |
max = val + intR |
| 256 |
} |
| 257 |
} else if typR == INTEGERRANGE { |
| 258 |
if intR < 0 { |
| 259 |
// negative max is inset from end of string (undocumented) |
| 260 |
max = len(str) + intR + 1 |
| 261 |
} else { |
| 262 |
max = intR |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
doRevComp := false |
| 267 |
doUpCase := false |
| 268 |
if status == NUCLEIC { |
| 269 |
// -nucleic uses direction of range to decide between forward strand or reverse complement |
| 270 |
if min+1 > max { |
| 271 |
min, max = max-1, min+1 |
| 272 |
doRevComp = true |
| 273 |
} |
| 274 |
doUpCase = true |
| 275 |
} |
| 276 |
|
| 277 |
if min < 0 || max < 0 { |
| 278 |
return |
| 279 |
|
| 280 |
} |
| 281 |
|
| 282 |
// apply range reality checks and then extract requested subregion |
| 283 |
getStringSlice := func(str string, min, max int, nonAscii bool) string { |
| 284 |
|
| 285 |
var rns []rune |
| 286 |
ln := len(str) |
| 287 |
|
| 288 |
if nonAscii { |
| 289 |
// convert to array of runes for random access by character position |
| 290 |
rns = []rune(str) |
| 291 |
ln = len(rns) |
| 292 |
} |
| 293 |
|
| 294 |
if max == 0 { |
| 295 |
if min < ln { |
| 296 |
if nonAscii { |
| 297 |
str = string(rns[min:]) |
| 298 |
} else { |
| 299 |
str = str[min:] |
| 300 |
} |
| 301 |
} else { |
| 302 |
str = "" |
| 303 |
} |
| 304 |
|
| 305 |
} else if min == 0 { |
| 306 |
if max <= ln { |
| 307 |
if nonAscii { |
| 308 |
str = string(rns[:max]) |
| 309 |
} else { |
| 310 |
str = str[:max] |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
} else if min <= max && min < ln { |
| 315 |
if max <= ln { |
| 316 |
if nonAscii { |
| 317 |
str = string(rns[min:max]) |
| 318 |
} else { |
| 319 |
str = str[min:max] |
| 320 |
} |
| 321 |
} else { |
| 322 |
if nonAscii { |
| 323 |
str = string(rns[min:]) |
| 324 |
} else { |
| 325 |
str = str[min:] |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
} else { |
| 330 |
str = "" |
| 331 |
} |
| 332 |
|
| 333 |
return str |
| 334 |
} |
| 335 |
|
| 336 |
// numeric range now calculated, apply slice to string |
| 337 |
if min == 0 && max == 0 { |
| 338 |
// no slice extraction requested |
| 339 |
} else if status == NUCLEIC || !IsNotASCII(str) { |
| 340 |
str = getStringSlice(str, min, max, false) |
| 341 |
} else { |
| 342 |
str = getStringSlice(str, min, max, true) |
| 343 |
} |
| 344 |
|
| 345 |
if doRevComp { |
| 346 |
str = ReverseComplement(str) |
| 347 |
} |
| 348 |
if doUpCase { |
| 349 |
str = strings.ToUpper(str) |
| 350 |
} |
| 351 |
if wrp && stat != REPLACE { |
| 352 |
str = html.EscapeString(str) |
| 353 |
} |
| 354 |
|
| 355 |
// send final result to accumulator |
| 356 |
acc(str) |
| 357 |
} |
| 358 |
|
| 359 |
switch stat { |
| 360 |
case ELEMENT: |
| 361 |
exploreElements(func(str string, lvl int) { |
| 362 |
if str != "" { |
| 363 |
sendSlice(str) |
| 364 |
} |
| 365 |
}) |
| 366 |
case VARIABLE, ACCUMULATOR, CONVERTER: |
| 367 |
// use value of stored variable |
| 368 |
val, ok := variables[match] |
| 369 |
if ok { |
| 370 |
sendSlice(val) |
| 371 |
} |
| 372 |
case NUM, COUNT: |
| 373 |
// common code for -num command (NUM) or "#" object prefix (COUNT), allows counting of container objects |
| 374 |
count := 0 |
| 375 |
|
| 376 |
exploreElements(func(str string, lvl int) { |
| 377 |
count++ |
| 378 |
}) |
| 379 |
|
| 380 |
// number of element objects |
| 381 |
val := strconv.Itoa(count) |
| 382 |
acc(val) |
| 383 |
case LENGTH: |
| 384 |
// code only for "%" object prefix (LENGTH), but not for -len command (LEN), |
| 385 |
// sacrifices getting same result even with doubly-specified -len "%object" |
| 386 |
// in order to restore more important use of -len "*" to get length of XML |
| 387 |
length := 0 |
| 388 |
|
| 389 |
exploreElements(func(str string, lvl int) { |
| 390 |
length += len(str) |
| 391 |
}) |
| 392 |
|
| 393 |
// length of element strings |
| 394 |
val := strconv.Itoa(length) |
| 395 |
acc(val) |
| 396 |
case DEPTH: |
| 397 |
if match == "*" { |
| 398 |
// ^* construct visits all objects in scope |
| 399 |
maxlvl := 0 |
| 400 |
ExploreNodes(curr, "**", "*", 0, 0, func(crr *XMLNode, idx, lvl int) { |
| 401 |
if lvl > maxlvl { |
| 402 |
maxlvl = lvl |
| 403 |
} |
| 404 |
}) |
| 405 |
// to get maximum depth value |
| 406 |
val := strconv.Itoa(maxlvl) |
| 407 |
acc(val) |
| 408 |
} else { |
| 409 |
// otherwise print depth of each element in scope |
| 410 |
exploreElements(func(str string, lvl int) { |
| 411 |
val := strconv.Itoa(lvl) |
| 412 |
acc(val) |
| 413 |
}) |
| 414 |
} |
| 415 |
case INDEX: |
| 416 |
// -element "+" prints index of current XML object |
| 417 |
val := strconv.Itoa(index) |
| 418 |
acc(val) |
| 419 |
case INCR: |
| 420 |
// component of -0-based, -1-based, or -ucsc-based, not used for -inc |
| 421 |
exploreElements(func(str string, lvl int) { |
| 422 |
if str != "" { |
| 423 |
num, err := strconv.Atoi(str) |
| 424 |
if err == nil { |
| 425 |
// increment value |
| 426 |
num++ |
| 427 |
val := strconv.Itoa(num) |
| 428 |
acc(val) |
| 429 |
} |
| 430 |
} |
| 431 |
}) |
| 432 |
case DECR: |
| 433 |
// component of -0-based, -1-based, or -ucsc-based, not used for -dec |
| 434 |
exploreElements(func(str string, lvl int) { |
| 435 |
if str != "" { |
| 436 |
num, err := strconv.Atoi(str) |
| 437 |
if err == nil { |
| 438 |
// decrement value |
| 439 |
num-- |
| 440 |
val := strconv.Itoa(num) |
| 441 |
acc(val) |
| 442 |
} |
| 443 |
} |
| 444 |
}) |
| 445 |
case QUESTION: |
| 446 |
acc(curr.Name) |
| 447 |
case TILDE: |
| 448 |
acc(html.UnescapeString(curr.Contents)) |
| 449 |
case DOT: |
| 450 |
// -element "." prints current XML subtree as ASN.1 |
| 451 |
var buffer strings.Builder |
| 452 |
|
| 453 |
printASNtree(curr, |
| 454 |
func(str string) { |
| 455 |
if str != "" { |
| 456 |
buffer.WriteString(str) |
| 457 |
} |
| 458 |
}) |
| 459 |
|
| 460 |
txt := buffer.String() |
| 461 |
if txt != "" { |
| 462 |
if strings.HasSuffix(txt, "\n") { |
| 463 |
txt = strings.TrimSuffix(txt, "\n") |
| 464 |
} |
| 465 |
acc(txt) |
| 466 |
} |
| 467 |
case PRCNT: |
| 468 |
// -element "%" prints current XML subtree as JSON |
| 469 |
var buffer strings.Builder |
| 470 |
|
| 471 |
printJSONtree(curr, |
| 472 |
func(str string) { |
| 473 |
if str != "" { |
| 474 |
buffer.WriteString(str) |
| 475 |
} |
| 476 |
}) |
| 477 |
|
| 478 |
txt := buffer.String() |
| 479 |
if txt != "" { |
| 480 |
if strings.HasSuffix(txt, "\n") { |
| 481 |
txt = strings.TrimSuffix(txt, "\n") |
| 482 |
} |
| 483 |
acc(txt) |
| 484 |
} |
| 485 |
case STAR: |
| 486 |
// -element "*" prints current XML subtree on a single line |
| 487 |
style := SINGULARITY |
| 488 |
printAttrs := true |
| 489 |
|
| 490 |
for _, ch := range item { |
| 491 |
if ch == '*' { |
| 492 |
style++ |
| 493 |
} else if ch == '@' { |
| 494 |
printAttrs = false |
| 495 |
} |
| 496 |
} |
| 497 |
if style > WRAPPED { |
| 498 |
style = WRAPPED |
| 499 |
} |
| 500 |
if style < COMPACT { |
| 501 |
style = COMPACT |
| 502 |
} |
| 503 |
|
| 504 |
var buffer strings.Builder |
| 505 |
|
| 506 |
printXMLtree(curr, style, msk, printAttrs, |
| 507 |
func(str string) { |
| 508 |
if str != "" { |
| 509 |
buffer.WriteString(str) |
| 510 |
} |
| 511 |
}) |
| 512 |
|
| 513 |
txt := buffer.String() |
| 514 |
if txt != "" { |
| 515 |
acc(txt) |
| 516 |
} |
| 517 |
case DOLLAR: |
| 518 |
for chld := curr.Children; chld != nil; chld = chld.Next { |
| 519 |
acc(chld.Name) |
| 520 |
} |
| 521 |
case ATSIGN: |
| 522 |
if curr.Attributes != "" && curr.Attribs == nil { |
| 523 |
curr.Attribs = ParseAttributes(curr.Attributes) |
| 524 |
} |
| 525 |
for i := 0; i < len(curr.Attribs)-1; i += 2 { |
| 526 |
acc(curr.Attribs[i]) |
| 527 |
} |
| 528 |
default: |
| 529 |
exploreElements(func(str string, lvl int) { |
| 530 |
if str != "" { |
| 531 |
sendSlice(str) |
| 532 |
} |
| 533 |
}) |
| 534 |
} |
| 535 |
} |
| 536 |
} |
| 537 |
|
| 538 |
ok := false |
| 539 |
|
| 540 |
// format results in buffer |
| 541 |
var buffer strings.Builder |
| 542 |
|
| 543 |
buffer.WriteString(cls.Prev) |
| 544 |
buffer.WriteString(cls.Plg) |
| 545 |
buffer.WriteString(pfx) |
| 546 |
between := "" |
| 547 |
|
| 548 |
switch status { |
| 549 |
case ELEMENT: |
| 550 |
processElement(func(str string) { |
| 551 |
if str != "" { |
| 552 |
ok = true |
| 553 |
buffer.WriteString(between) |
| 554 |
buffer.WriteString(str) |
| 555 |
between = sep |
| 556 |
} |
| 557 |
}) |
| 558 |
|
| 559 |
case FIRST: |
| 560 |
single := "" |
| 561 |
|
| 562 |
processElement(func(str string) { |
| 563 |
ok = true |
| 564 |
if single == "" { |
| 565 |
single = str |
| 566 |
} |
| 567 |
}) |
| 568 |
|
| 569 |
if single != "" { |
| 570 |
buffer.WriteString(between) |
| 571 |
buffer.WriteString(single) |
| 572 |
between = sep |
| 573 |
} |
| 574 |
|
| 575 |
case LAST: |
| 576 |
single := "" |
| 577 |
|
| 578 |
processElement(func(str string) { |
| 579 |
ok = true |
| 580 |
single = str |
| 581 |
}) |
| 582 |
|
| 583 |
if single != "" { |
| 584 |
buffer.WriteString(between) |
| 585 |
buffer.WriteString(single) |
| 586 |
between = sep |
| 587 |
} |
| 588 |
|
| 589 |
case EVEN: |
| 590 |
even := false |
| 591 |
|
| 592 |
processElement(func(str string) { |
| 593 |
if str != "" { |
| 594 |
if even { |
| 595 |
ok = true |
| 596 |
buffer.WriteString(between) |
| 597 |
buffer.WriteString(str) |
| 598 |
between = sep |
| 599 |
} |
| 600 |
even = !even |
| 601 |
} |
| 602 |
}) |
| 603 |
|
| 604 |
case ODD: |
| 605 |
odd := true |
| 606 |
|
| 607 |
processElement(func(str string) { |
| 608 |
if str != "" { |
| 609 |
if odd { |
| 610 |
ok = true |
| 611 |
buffer.WriteString(between) |
| 612 |
buffer.WriteString(str) |
| 613 |
between = sep |
| 614 |
} |
| 615 |
odd = !odd |
| 616 |
} |
| 617 |
}) |
| 618 |
|
| 619 |
case BACKWARD: |
| 620 |
var arry []string |
| 621 |
|
| 622 |
processElement(func(str string) { |
| 623 |
if str != "" { |
| 624 |
ok = true |
| 625 |
arry = append(arry, str) |
| 626 |
} |
| 627 |
}) |
| 628 |
|
| 629 |
if ok { |
| 630 |
for i := len(arry) - 1; i >= 0; i-- { |
| 631 |
buffer.WriteString(between) |
| 632 |
buffer.WriteString(arry[i]) |
| 633 |
between = sep |
| 634 |
} |
| 635 |
} |
| 636 |
|
| 637 |
case ENCODE: |
| 638 |
processElement(func(str string) { |
| 639 |
if str != "" { |
| 640 |
ok = true |
| 641 |
if !wrp { |
| 642 |
str = html.EscapeString(str) |
| 643 |
} |
| 644 |
buffer.WriteString(between) |
| 645 |
buffer.WriteString(str) |
| 646 |
between = sep |
| 647 |
} |
| 648 |
}) |
| 649 |
|
| 650 |
case DECODE: |
| 651 |
// equivalent to "transmute -decode64" |
| 652 |
processElement(func(str string) { |
| 653 |
if str != "" { |
| 654 |
txt, err := base64.StdEncoding.DecodeString(str) |
| 655 |
if err == nil { |
| 656 |
ok = true |
| 657 |
buffer.WriteString(between) |
| 658 |
buffer.WriteString(string(txt)) |
| 659 |
between = sep |
| 660 |
} |
| 661 |
} |
| 662 |
}) |
| 663 |
|
| 664 |
case UPPER: |
| 665 |
processElement(func(str string) { |
| 666 |
if str != "" { |
| 667 |
ok = true |
| 668 |
str = strings.ToUpper(str) |
| 669 |
buffer.WriteString(between) |
| 670 |
buffer.WriteString(str) |
| 671 |
between = sep |
| 672 |
} |
| 673 |
}) |
| 674 |
|
| 675 |
case LOWER: |
| 676 |
processElement(func(str string) { |
| 677 |
if str != "" { |
| 678 |
ok = true |
| 679 |
str = strings.ToLower(str) |
| 680 |
buffer.WriteString(between) |
| 681 |
buffer.WriteString(str) |
| 682 |
between = sep |
| 683 |
} |
| 684 |
}) |
| 685 |
|
| 686 |
case CHAIN: |
| 687 |
processElement(func(str string) { |
| 688 |
if str != "" { |
| 689 |
ok = true |
| 690 |
str = strings.Replace(str, " ", "_", -1) |
| 691 |
buffer.WriteString(between) |
| 692 |
buffer.WriteString(str) |
| 693 |
between = sep |
| 694 |
} |
| 695 |
}) |
| 696 |
|
| 697 |
case TITLE: |
| 698 |
processElement(func(str string) { |
| 699 |
if str != "" { |
| 700 |
ok = true |
| 701 |
str = strings.ToLower(str) |
| 702 |
// str = strings.Title(str) |
| 703 |
csr := cases.Title(language.English) |
| 704 |
str = csr.String(str) |
| 705 |
buffer.WriteString(between) |
| 706 |
buffer.WriteString(str) |
| 707 |
between = sep |
| 708 |
} |
| 709 |
}) |
| 710 |
|
| 711 |
case MIRROR: |
| 712 |
processElement(func(str string) { |
| 713 |
if str != "" { |
| 714 |
ok = true |
| 715 |
rev := ReverseString(str) |
| 716 |
buffer.WriteString(between) |
| 717 |
buffer.WriteString(rev) |
| 718 |
between = sep |
| 719 |
} |
| 720 |
}) |
| 721 |
|
| 722 |
case ALPHA: |
| 723 |
processElement(func(str string) { |
| 724 |
if str != "" { |
| 725 |
// split at non-alphabetic characters |
| 726 |
words := strings.FieldsFunc(str, func(c rune) bool { |
| 727 |
return (!unicode.IsLetter(c)) || c > 127 |
| 728 |
}) |
| 729 |
str = strings.Join(words, " ") |
| 730 |
str = strings.TrimSpace(str) |
| 731 |
str = CompressRunsOfSpaces(str) |
| 732 |
if str != "" { |
| 733 |
ok = true |
| 734 |
buffer.WriteString(between) |
| 735 |
buffer.WriteString(str) |
| 736 |
between = sep |
| 737 |
} |
| 738 |
} |
| 739 |
}) |
| 740 |
|
| 741 |
case ALNUM: |
| 742 |
processElement(func(str string) { |
| 743 |
if str != "" { |
| 744 |
// split at non-alphanumeric characters |
| 745 |
words := strings.FieldsFunc(str, func(c rune) bool { |
| 746 |
return (!unicode.IsLetter(c) && !unicode.IsDigit(c)) || c > 127 |
| 747 |
}) |
| 748 |
str = strings.Join(words, " ") |
| 749 |
str = strings.TrimSpace(str) |
| 750 |
str = CompressRunsOfSpaces(str) |
| 751 |
if str != "" { |
| 752 |
ok = true |
| 753 |
buffer.WriteString(between) |
| 754 |
buffer.WriteString(str) |
| 755 |
between = sep |
| 756 |
} |
| 757 |
} |
| 758 |
}) |
| 759 |
|
| 760 |
case BASIC: |
| 761 |
processElement(func(str string) { |
| 762 |
if str != "" { |
| 763 |
ok = true |
| 764 |
str = CleanupBasic(str, wrp) |
| 765 |
buffer.WriteString(between) |
| 766 |
buffer.WriteString(str) |
| 767 |
between = sep |
| 768 |
} |
| 769 |
}) |
| 770 |
|
| 771 |
case PLAIN: |
| 772 |
processElement(func(str string) { |
| 773 |
if str != "" { |
| 774 |
ok = true |
| 775 |
str = CleanupPlain(str, wrp) |
| 776 |
buffer.WriteString(between) |
| 777 |
buffer.WriteString(str) |
| 778 |
between = sep |
| 779 |
} |
| 780 |
}) |
| 781 |
|
| 782 |
case SIMPLE: |
| 783 |
processElement(func(str string) { |
| 784 |
if str != "" { |
| 785 |
ok = true |
| 786 |
str = CleanupSimple(str, wrp) |
| 787 |
buffer.WriteString(between) |
| 788 |
buffer.WriteString(str) |
| 789 |
between = sep |
| 790 |
} |
| 791 |
}) |
| 792 |
|
| 793 |
case AUTHOR: |
| 794 |
processElement(func(str string) { |
| 795 |
if str != "" { |
| 796 |
ok = true |
| 797 |
str = CleanupAuthor(str, wrp) |
| 798 |
buffer.WriteString(between) |
| 799 |
buffer.WriteString(str) |
| 800 |
between = sep |
| 801 |
} |
| 802 |
}) |
| 803 |
|
| 804 |
case PROSE: |
| 805 |
processElement(func(str string) { |
| 806 |
if str != "" { |
| 807 |
ok = true |
| 808 |
str = CleanupProse(str, wrp) |
| 809 |
buffer.WriteString(between) |
| 810 |
buffer.WriteString(str) |
| 811 |
between = sep |
| 812 |
} |
| 813 |
}) |
| 814 |
|
| 815 |
case JOURNAL: |
| 816 |
processElement(func(str string) { |
| 817 |
if str != "" { |
| 818 |
ok = true |
| 819 |
str = NormalizeJournal(str) |
| 820 |
buffer.WriteString(between) |
| 821 |
buffer.WriteString(str) |
| 822 |
between = sep |
| 823 |
} |
| 824 |
}) |
| 825 |
|
| 826 |
case YEAR: |
| 827 |
year := "" |
| 828 |
|
| 829 |
processElement(func(str string) { |
| 830 |
if str != "" && year == "" { |
| 831 |
if len(str) == 9 && str[4] == ' ' && IsAllDigits(str[0:3]) && IsAllDigits(str[5:8]) { |
| 832 |
// bail on MedlineDate composed of PMID with a space inserted in the middle |
| 833 |
return |
| 834 |
} |
| 835 |
words := strings.FieldsFunc(str, func(c rune) bool { |
| 836 |
return !unicode.IsDigit(c) |
| 837 |
}) |
| 838 |
for _, item := range words { |
| 839 |
if len(item) == 4 && IsAllDigits(item) { |
| 840 |
year = item |
| 841 |
ok = true |
| 842 |
// only print integer for first year, e.g., PubDate/MedlineDate "2008 Dec-2009 Jan" is 2008 |
| 843 |
break |
| 844 |
} |
| 845 |
} |
| 846 |
} |
| 847 |
}) |
| 848 |
|
| 849 |
if year != "" { |
| 850 |
buffer.WriteString(between) |
| 851 |
buffer.WriteString(year) |
| 852 |
between = sep |
| 853 |
} |
| 854 |
|
| 855 |
case MONTH: |
| 856 |
month := "" |
| 857 |
|
| 858 |
processElement(func(str string) { |
| 859 |
if str != "" && month == "" { |
| 860 |
words := strings.FieldsFunc(str, func(c rune) bool { |
| 861 |
return !unicode.IsLetter(c) |
| 862 |
}) |
| 863 |
for _, item := range words { |
| 864 |
item = strings.ToLower(item) |
| 865 |
val, found := monthTable[item] |
| 866 |
if found { |
| 867 |
month = strconv.Itoa(val) |
| 868 |
ok = true |
| 869 |
// only print integer for first month, e.g., PubDate/MedlineDate "2008 Dec-2009 Jan" is 12 |
| 870 |
break |
| 871 |
} |
| 872 |
} |
| 873 |
} |
| 874 |
}) |
| 875 |
|
| 876 |
if month != "" { |
| 877 |
buffer.WriteString(between) |
| 878 |
buffer.WriteString(month) |
| 879 |
between = sep |
| 880 |
} |
| 881 |
|
| 882 |
case DATE: |
| 883 |
// xtract -pattern PubmedArticle -unit "PubDate" -date "*" |
| 884 |
// xtract -pattern collection -unit date -date "*" |
| 885 |
year := "" |
| 886 |
month := "" |
| 887 |
day := "" |
| 888 |
|
| 889 |
extractBetweenTags := func(txt, tag string) string { |
| 890 |
|
| 891 |
if txt == "" || tag == "" { |
| 892 |
return "" |
| 893 |
} |
| 894 |
_, after, found := strings.Cut(txt, "<"+tag+">") |
| 895 |
if !found || after == "" { |
| 896 |
return "" |
| 897 |
} |
| 898 |
res, _, found := strings.Cut(after, "</"+tag+">") |
| 899 |
if !found || res == "" { |
| 900 |
return "" |
| 901 |
} |
| 902 |
return res |
| 903 |
} |
| 904 |
|
| 905 |
processElement(func(str string) { |
| 906 |
if str != "" { |
| 907 |
if strings.Contains(str, "MedlineDate") { |
| 908 |
|
| 909 |
words := strings.FieldsFunc(str, func(c rune) bool { |
| 910 |
return !unicode.IsDigit(c) |
| 911 |
}) |
| 912 |
for _, item := range words { |
| 913 |
if len(item) == 4 && IsAllDigits(item) { |
| 914 |
year = item |
| 915 |
// only print integer for first year |
| 916 |
break |
| 917 |
} |
| 918 |
} |
| 919 |
if year != "" { |
| 920 |
words := strings.FieldsFunc(str, func(c rune) bool { |
| 921 |
return !unicode.IsLetter(c) |
| 922 |
}) |
| 923 |
for _, item := range words { |
| 924 |
item = strings.ToLower(item) |
| 925 |
val, found := monthTable[item] |
| 926 |
if found { |
| 927 |
month = strconv.Itoa(val) |
| 928 |
// only print integer for first month |
| 929 |
break |
| 930 |
} |
| 931 |
} |
| 932 |
} |
| 933 |
|
| 934 |
} else if strings.Contains(str, "PubMedPubDate") || strings.Contains(str, "PubDate") { |
| 935 |
|
| 936 |
year = extractBetweenTags(str, "Year") |
| 937 |
month = extractBetweenTags(str, "Month") |
| 938 |
if month != "" { |
| 939 |
if !IsAllDigits(month) { |
| 940 |
month = strings.ToLower(month) |
| 941 |
val, found := monthTable[month] |
| 942 |
if found { |
| 943 |
month = strconv.Itoa(val) |
| 944 |
} |
| 945 |
} |
| 946 |
} |
| 947 |
day = extractBetweenTags(str, "Day") |
| 948 |
|
| 949 |
} else if strings.Contains(str, "date") { |
| 950 |
|
| 951 |
str = html.UnescapeString(str) |
| 952 |
// <date>20201214</date> |
| 953 |
raw := extractBetweenTags(str, "date") |
| 954 |
if len(raw) == 8 { |
| 955 |
year = raw[0:4] |
| 956 |
month = raw[4:6] |
| 957 |
day = raw[6:8] |
| 958 |
} else if len(raw) == 6 { |
| 959 |
year = raw[0:4] |
| 960 |
month = raw[4:6] |
| 961 |
} else if len(raw) == 4 { |
| 962 |
year = raw[0:4] |
| 963 |
} |
| 964 |
|
| 965 |
} else { |
| 966 |
|
| 967 |
year = extractBetweenTags(str, "Year") |
| 968 |
month = extractBetweenTags(str, "Month") |
| 969 |
if month != "" { |
| 970 |
if !IsAllDigits(month) { |
| 971 |
month = strings.ToLower(month) |
| 972 |
val, found := monthTable[month] |
| 973 |
if found { |
| 974 |
month = strconv.Itoa(val) |
| 975 |
} |
| 976 |
} |
| 977 |
} |
| 978 |
day = extractBetweenTags(str, "Day") |
| 979 |
|
| 980 |
/* |
| 981 |
str = extractBetweenTags(str, "PubDate") |
| 982 |
items := strings.Split(str, " ") |
| 983 |
for _, itm := range items { |
| 984 |
if year == "" { |
| 985 |
year = itm |
| 986 |
} else if month == "" { |
| 987 |
month = itm |
| 988 |
} else if day == "" { |
| 989 |
day = itm |
| 990 |
} |
| 991 |
} |
| 992 |
if month != "" { |
| 993 |
if !IsAllDigits(month) { |
| 994 |
month = strings.ToLower(month) |
| 995 |
val, found := monthTable[month] |
| 996 |
if found { |
| 997 |
month = strconv.Itoa(val) |
| 998 |
} |
| 999 |
} |
| 1000 |
} |
| 1001 |
*/ |
| 1002 |
} |
| 1003 |
} |
| 1004 |
}) |
| 1005 |
|
| 1006 |
slash := "/" |
| 1007 |
if reg == "/" && exp != "" { |
| 1008 |
slash = exp |
| 1009 |
} |
| 1010 |
|
| 1011 |
txt := "" |
| 1012 |
if year != "" { |
| 1013 |
buffer.WriteString(between) |
| 1014 |
txt = year |
| 1015 |
if month != "" { |
| 1016 |
if len(month) == 1 { |
| 1017 |
txt += slash + "0" + month |
| 1018 |
} else { |
| 1019 |
txt += slash + month |
| 1020 |
} |
| 1021 |
if day != "" { |
| 1022 |
if len(day) == 1 { |
| 1023 |
txt += slash + "0" + day |
| 1024 |
} else { |
| 1025 |
txt += slash + day |
| 1026 |
} |
| 1027 |
} |
| 1028 |
} |
| 1029 |
ok = true |
| 1030 |
} |
| 1031 |
|
| 1032 |
if txt != "" { |
| 1033 |
buffer.WriteString(between) |
| 1034 |
buffer.WriteString(txt) |
| 1035 |
between = sep |
| 1036 |
} |
| 1037 |
|
| 1038 |
case PAGE: |
| 1039 |
processElement(func(str string) { |
| 1040 |
if str != "" { |
| 1041 |
words := strings.FieldsFunc(str, func(c rune) bool { |
| 1042 |
return !unicode.IsLetter(c) && !unicode.IsDigit(c) |
| 1043 |
}) |
| 1044 |
if len(words) > 0 { |
| 1045 |
firstPage := words[0] |
| 1046 |
if firstPage != "" { |
| 1047 |
ok = true |
| 1048 |
buffer.WriteString(between) |
| 1049 |
buffer.WriteString(firstPage) |
| 1050 |
between = sep |
| 1051 |
} |
| 1052 |
} |
| 1053 |
} |
| 1054 |
}) |
| 1055 |
|
| 1056 |
case AUTH: |
| 1057 |
processElement(func(str string) { |
| 1058 |
if str != "" { |
| 1059 |
ok = true |
| 1060 |
// convert GenBank author to searchable form |
| 1061 |
str = GenBankToMedlineAuthors(str) |
| 1062 |
buffer.WriteString(between) |
| 1063 |
buffer.WriteString(str) |
| 1064 |
between = sep |
| 1065 |
} |
| 1066 |
}) |
| 1067 |
|
| 1068 |
case INITIALS: |
| 1069 |
processElement(func(str string) { |
| 1070 |
if str != "" { |
| 1071 |
ok = true |
| 1072 |
// convert given name to initials |
| 1073 |
if len(str) != 2 || !unicode.IsUpper(rune(str[0])) || !unicode.IsUpper(rune(str[1])) { |
| 1074 |
lft, rgt, found := strings.Cut(str, " ") |
| 1075 |
if !found { |
| 1076 |
lft, rgt, found = strings.Cut(str, "-") |
| 1077 |
} |
| 1078 |
if !found { |
| 1079 |
lft, rgt, found = strings.Cut(str, ".") |
| 1080 |
} |
| 1081 |
if found && lft != "" && rgt != "" { |
| 1082 |
str = lft[:1] + rgt[:1] |
| 1083 |
} else { |
| 1084 |
str = str[:1] |
| 1085 |
} |
| 1086 |
} |
| 1087 |
str = strings.ToUpper(str) |
| 1088 |
buffer.WriteString(between) |
| 1089 |
buffer.WriteString(str) |
| 1090 |
between = sep |
| 1091 |
} |
| 1092 |
}) |
| 1093 |
|
| 1094 |
case PROP: |
| 1095 |
processElement(func(str string) { |
| 1096 |
if str != "" { |
| 1097 |
prop, fnd := propertyTable[str] |
| 1098 |
if fnd { |
| 1099 |
ok = true |
| 1100 |
buffer.WriteString(between) |
| 1101 |
buffer.WriteString(prop) |
| 1102 |
between = sep |
| 1103 |
} else { |
| 1104 |
ok = true |
| 1105 |
buffer.WriteString(between) |
| 1106 |
buffer.WriteString("Other") |
| 1107 |
between = sep |
| 1108 |
} |
| 1109 |
} |
| 1110 |
}) |
| 1111 |
|
| 1112 |
case TRIM: |
| 1113 |
processElement(func(str string) { |
| 1114 |
if str != "" { |
| 1115 |
str = strings.TrimPrefix(str, " ") |
| 1116 |
str = strings.TrimSuffix(str, " ") |
| 1117 |
str = strings.TrimSpace(str) |
| 1118 |
if strings.HasPrefix(str, "0") { |
| 1119 |
// also trim leading zeros |
| 1120 |
str = strings.TrimPrefix(str, "0") |
| 1121 |
if str == "" { |
| 1122 |
// but leave one if was only zeros |
| 1123 |
str = "0" |
| 1124 |
} |
| 1125 |
} |
| 1126 |
if str != "" { |
| 1127 |
ok = true |
| 1128 |
buffer.WriteString(between) |
| 1129 |
buffer.WriteString(str) |
| 1130 |
between = sep |
| 1131 |
} |
| 1132 |
} |
| 1133 |
}) |
| 1134 |
|
| 1135 |
case WCT: |
| 1136 |
count := 0 |
| 1137 |
|
| 1138 |
processElement(func(str string) { |
| 1139 |
if str != "" { |
| 1140 |
|
| 1141 |
words := strings.FieldsFunc(str, func(c rune) bool { |
| 1142 |
return !unicode.IsLetter(c) && !unicode.IsDigit(c) |
| 1143 |
}) |
| 1144 |
for _, item := range words { |
| 1145 |
item = strings.ToLower(item) |
| 1146 |
if deStop { |
| 1147 |
// exclude stop words from count |
| 1148 |
if IsStopWord(item) { |
| 1149 |
continue |
| 1150 |
} |
| 1151 |
} |
| 1152 |
if doStem { |
| 1153 |
item = porter2.Stem(item) |
| 1154 |
item = strings.TrimSpace(item) |
| 1155 |
} |
| 1156 |
if item == "" { |
| 1157 |
continue |
| 1158 |
} |
| 1159 |
count++ |
| 1160 |
ok = true |
| 1161 |
} |
| 1162 |
} |
| 1163 |
}) |
| 1164 |
|
| 1165 |
if ok { |
| 1166 |
// total number of words |
| 1167 |
val := strconv.Itoa(count) |
| 1168 |
buffer.WriteString(between) |
| 1169 |
buffer.WriteString(val) |
| 1170 |
between = sep |
| 1171 |
} |
| 1172 |
|
| 1173 |
case DOI: |
| 1174 |
processElement(func(str string) { |
| 1175 |
if str != "" { |
| 1176 |
ok = true |
| 1177 |
str = strings.TrimPrefix(str, "doi:") |
| 1178 |
str = strings.TrimSpace(str) |
| 1179 |
str = strings.TrimPrefix(str, "/") |
| 1180 |
str = strings.TrimPrefix(str, "https://doi.org/") |
| 1181 |
str = strings.TrimPrefix(str, "http://dx.doi.org/") |
| 1182 |
str = url.QueryEscape(str) |
| 1183 |
str = "https://doi.org/" + str |
| 1184 |
buffer.WriteString(between) |
| 1185 |
buffer.WriteString(str) |
| 1186 |
between = sep |
| 1187 |
} |
| 1188 |
}) |
| 1189 |
|
| 1190 |
case TRANSLATE: |
| 1191 |
processElement(func(str string) { |
| 1192 |
if str != "" { |
| 1193 |
txt, found := transform[str] |
| 1194 |
if found { |
| 1195 |
// require successful mapping |
| 1196 |
ok = true |
| 1197 |
buffer.WriteString(between) |
| 1198 |
buffer.WriteString(txt) |
| 1199 |
between = sep |
| 1200 |
} |
| 1201 |
} |
| 1202 |
}) |
| 1203 |
|
| 1204 |
case REPLACE: |
| 1205 |
processElement(func(str string) { |
| 1206 |
if str != "" { |
| 1207 |
rlock.Lock() |
| 1208 |
re, found := replx[str] |
| 1209 |
if !found { |
| 1210 |
re, found = replx[str] |
| 1211 |
if !found { |
| 1212 |
nw, err := regexp.Compile(reg) |
| 1213 |
if err == nil { |
| 1214 |
replx[str] = nw |
| 1215 |
re = nw |
| 1216 |
} |
| 1217 |
} |
| 1218 |
} |
| 1219 |
rlock.Unlock() |
| 1220 |
if re != nil { |
| 1221 |
txt := re.ReplaceAllString(str, exp) |
| 1222 |
if txt != "" { |
| 1223 |
ok = true |
| 1224 |
// wrp-directed EscapeString was delayed for REPLACE |
| 1225 |
if wrp { |
| 1226 |
txt = html.EscapeString(txt) |
| 1227 |
} |
| 1228 |
buffer.WriteString(between) |
| 1229 |
buffer.WriteString(txt) |
| 1230 |
between = sep |
| 1231 |
} |
| 1232 |
} |
| 1233 |
} |
| 1234 |
}) |
| 1235 |
|
| 1236 |
case VALUE: |
| 1237 |
processElement(func(str string) { |
| 1238 |
if str != "" { |
| 1239 |
ok = true |
| 1240 |
buffer.WriteString(between) |
| 1241 |
buffer.WriteString(str) |
| 1242 |
between = sep |
| 1243 |
} |
| 1244 |
}) |
| 1245 |
|
| 1246 |
case NUM: |
| 1247 |
// defer to "#" prefix processing to allow counting of container objects |
| 1248 |
processElement(func(str string) { |
| 1249 |
if str != "" { |
| 1250 |
ok = true |
| 1251 |
buffer.WriteString(between) |
| 1252 |
buffer.WriteString(str) |
| 1253 |
between = sep |
| 1254 |
} |
| 1255 |
}) |
| 1256 |
|
| 1257 |
case LEN: |
| 1258 |
// reverted defer to "%" prefix processing in processElement LEN, LENGTH case, |
| 1259 |
// sacrifices getting same result with trivial doubly-specified -len "%object" |
| 1260 |
// in order to restore more important use of -len "*" to get length of XML |
| 1261 |
length := 0 |
| 1262 |
|
| 1263 |
processElement(func(str string) { |
| 1264 |
length += len(str) |
| 1265 |
ok = true |
| 1266 |
}) |
| 1267 |
|
| 1268 |
if ok { |
| 1269 |
// length of element strings |
| 1270 |
val := strconv.Itoa(length) |
| 1271 |
buffer.WriteString(between) |
| 1272 |
buffer.WriteString(val) |
| 1273 |
between = sep |
| 1274 |
} |
| 1275 |
|
| 1276 |
case SUM: |
| 1277 |
sum := 0 |
| 1278 |
|
| 1279 |
processElement(func(str string) { |
| 1280 |
value, err := strconv.Atoi(str) |
| 1281 |
if err == nil { |
| 1282 |
sum += value |
| 1283 |
ok = true |
| 1284 |
} |
| 1285 |
}) |
| 1286 |
|
| 1287 |
if ok { |
| 1288 |
// sum of element values |
| 1289 |
val := strconv.Itoa(sum) |
| 1290 |
buffer.WriteString(between) |
| 1291 |
buffer.WriteString(val) |
| 1292 |
between = sep |
| 1293 |
} |
| 1294 |
|
| 1295 |
case ACC: |
| 1296 |
sum := 0 |
| 1297 |
|
| 1298 |
processElement(func(str string) { |
| 1299 |
value, err := strconv.Atoi(str) |
| 1300 |
if err == nil { |
| 1301 |
sum += value |
| 1302 |
ok = true |
| 1303 |
// running sum of element values |
| 1304 |
val := strconv.Itoa(sum) |
| 1305 |
buffer.WriteString(between) |
| 1306 |
buffer.WriteString(val) |
| 1307 |
between = sep |
| 1308 |
} |
| 1309 |
}) |
| 1310 |
|
| 1311 |
case MIN: |
| 1312 |
min := 0 |
| 1313 |
|
| 1314 |
processElement(func(str string) { |
| 1315 |
value, err := strconv.Atoi(str) |
| 1316 |
if err == nil { |
| 1317 |
if !ok || value < min { |
| 1318 |
min = value |
| 1319 |
} |
| 1320 |
ok = true |
| 1321 |
} |
| 1322 |
}) |
| 1323 |
|
| 1324 |
if ok { |
| 1325 |
// minimum of element values |
| 1326 |
val := strconv.Itoa(min) |
| 1327 |
buffer.WriteString(between) |
| 1328 |
buffer.WriteString(val) |
| 1329 |
between = sep |
| 1330 |
} |
| 1331 |
|
| 1332 |
case MAX: |
| 1333 |
max := 0 |
| 1334 |
|
| 1335 |
processElement(func(str string) { |
| 1336 |
value, err := strconv.Atoi(str) |
| 1337 |
if err == nil { |
| 1338 |
if !ok || value > max { |
| 1339 |
max = value |
| 1340 |
} |
| 1341 |
ok = true |
| 1342 |
} |
| 1343 |
}) |
| 1344 |
|
| 1345 |
if ok { |
| 1346 |
// maximum of element values |
| 1347 |
val := strconv.Itoa(max) |
| 1348 |
buffer.WriteString(between) |
| 1349 |
buffer.WriteString(val) |
| 1350 |
between = sep |
| 1351 |
} |
| 1352 |
|
| 1353 |
case INC: |
| 1354 |
processElement(func(str string) { |
| 1355 |
if str != "" { |
| 1356 |
num, err := strconv.Atoi(str) |
| 1357 |
if err == nil { |
| 1358 |
// increment value |
| 1359 |
num++ |
| 1360 |
val := strconv.Itoa(num) |
| 1361 |
ok = true |
| 1362 |
buffer.WriteString(between) |
| 1363 |
buffer.WriteString(val) |
| 1364 |
between = sep |
| 1365 |
} |
| 1366 |
} |
| 1367 |
}) |
| 1368 |
|
| 1369 |
case DEC: |
| 1370 |
processElement(func(str string) { |
| 1371 |
if str != "" { |
| 1372 |
num, err := strconv.Atoi(str) |
| 1373 |
if err == nil { |
| 1374 |
// decrement value |
| 1375 |
num-- |
| 1376 |
val := strconv.Itoa(num) |
| 1377 |
ok = true |
| 1378 |
buffer.WriteString(between) |
| 1379 |
buffer.WriteString(val) |
| 1380 |
between = sep |
| 1381 |
} |
| 1382 |
} |
| 1383 |
}) |
| 1384 |
|
| 1385 |
case SUB: |
| 1386 |
first := 0 |
| 1387 |
second := 0 |
| 1388 |
count := 0 |
| 1389 |
|
| 1390 |
processElement(func(str string) { |
| 1391 |
value, err := strconv.Atoi(str) |
| 1392 |
if err == nil { |
| 1393 |
count++ |
| 1394 |
if count == 1 { |
| 1395 |
first = value |
| 1396 |
} else if count == 2 { |
| 1397 |
second = value |
| 1398 |
} |
| 1399 |
} |
| 1400 |
}) |
| 1401 |
|
| 1402 |
if count == 2 { |
| 1403 |
// must have exactly 2 elements |
| 1404 |
ok = true |
| 1405 |
// difference of element values |
| 1406 |
val := strconv.Itoa(first - second) |
| 1407 |
buffer.WriteString(between) |
| 1408 |
buffer.WriteString(val) |
| 1409 |
between = sep |
| 1410 |
} |
| 1411 |
|
| 1412 |
case AVG: |
| 1413 |
sum := 0 |
| 1414 |
count := 0 |
| 1415 |
|
| 1416 |
processElement(func(str string) { |
| 1417 |
value, err := strconv.Atoi(str) |
| 1418 |
if err == nil { |
| 1419 |
sum += value |
| 1420 |
count++ |
| 1421 |
ok = true |
| 1422 |
} |
| 1423 |
}) |
| 1424 |
|
| 1425 |
if ok { |
| 1426 |
// average of element values |
| 1427 |
avg := int(float64(sum) / float64(count)) |
| 1428 |
val := strconv.Itoa(avg) |
| 1429 |
buffer.WriteString(between) |
| 1430 |
buffer.WriteString(val) |
| 1431 |
between = sep |
| 1432 |
} |
| 1433 |
|
| 1434 |
case DEV: |
| 1435 |
count := 0 |
| 1436 |
mean := 0.0 |
| 1437 |
m2 := 0.0 |
| 1438 |
|
| 1439 |
processElement(func(str string) { |
| 1440 |
value, err := strconv.Atoi(str) |
| 1441 |
if err == nil { |
| 1442 |
// Welford algorithm for one-pass standard deviation |
| 1443 |
count++ |
| 1444 |
x := float64(value) |
| 1445 |
delta := x - mean |
| 1446 |
mean += delta / float64(count) |
| 1447 |
m2 += delta * (x - mean) |
| 1448 |
} |
| 1449 |
}) |
| 1450 |
|
| 1451 |
if count > 1 { |
| 1452 |
// must have at least 2 elements |
| 1453 |
ok = true |
| 1454 |
// standard deviation of element values |
| 1455 |
vrc := m2 / float64(count-1) |
| 1456 |
dev := int(math.Sqrt(vrc)) |
| 1457 |
val := strconv.Itoa(dev) |
| 1458 |
buffer.WriteString(between) |
| 1459 |
buffer.WriteString(val) |
| 1460 |
between = sep |
| 1461 |
} |
| 1462 |
|
| 1463 |
case MED: |
| 1464 |
var arry []int |
| 1465 |
count := 0 |
| 1466 |
|
| 1467 |
processElement(func(str string) { |
| 1468 |
value, err := strconv.Atoi(str) |
| 1469 |
if err == nil { |
| 1470 |
arry = append(arry, value) |
| 1471 |
count++ |
| 1472 |
ok = true |
| 1473 |
} |
| 1474 |
}) |
| 1475 |
|
| 1476 |
if ok { |
| 1477 |
// median of element values |
| 1478 |
slices.Sort(arry) |
| 1479 |
med := 0 |
| 1480 |
// convention for even-numbered lists adapted from https://github.com/SimonWaldherr/golibs/blob/v0.11.0/xmath/math.go#L181 |
| 1481 |
if count%2 == 1 { |
| 1482 |
med = arry[count/2] |
| 1483 |
} else { |
| 1484 |
med = (arry[count/2] + arry[count/2-1]) / 2 |
| 1485 |
} |
| 1486 |
val := strconv.Itoa(med) |
| 1487 |
buffer.WriteString(between) |
| 1488 |
buffer.WriteString(val) |
| 1489 |
between = sep |
| 1490 |
} |
| 1491 |
|
| 1492 |
case MUL: |
| 1493 |
first := 0 |
| 1494 |
second := 0 |
| 1495 |
count := 0 |
| 1496 |
|
| 1497 |
processElement(func(str string) { |
| 1498 |
value, err := strconv.Atoi(str) |
| 1499 |
if err == nil { |
| 1500 |
count++ |
| 1501 |
if count == 1 { |
| 1502 |
first = value |
| 1503 |
} else if count == 2 { |
| 1504 |
second = value |
| 1505 |
} |
| 1506 |
} |
| 1507 |
}) |
| 1508 |
|
| 1509 |
if count == 2 { |
| 1510 |
// must have exactly 2 elements |
| 1511 |
ok = true |
| 1512 |
// product of element values |
| 1513 |
val := strconv.Itoa(first * second) |
| 1514 |
buffer.WriteString(between) |
| 1515 |
buffer.WriteString(val) |
| 1516 |
between = sep |
| 1517 |
} |
| 1518 |
|
| 1519 |
case DIV: |
| 1520 |
first := 0 |
| 1521 |
second := 0 |
| 1522 |
count := 0 |
| 1523 |
|
| 1524 |
processElement(func(str string) { |
| 1525 |
value, err := strconv.Atoi(str) |
| 1526 |
if err == nil { |
| 1527 |
count++ |
| 1528 |
if count == 1 { |
| 1529 |
first = value |
| 1530 |
} else if count == 2 { |
| 1531 |
second = value |
| 1532 |
} |
| 1533 |
} |
| 1534 |
}) |
| 1535 |
|
| 1536 |
if count == 2 { |
| 1537 |
// must have exactly 2 elements |
| 1538 |
ok = true |
| 1539 |
// quotient of element values |
| 1540 |
val := strconv.Itoa(first / second) |
| 1541 |
buffer.WriteString(between) |
| 1542 |
buffer.WriteString(val) |
| 1543 |
between = sep |
| 1544 |
} |
| 1545 |
|
| 1546 |
case MOD: |
| 1547 |
first := 0 |
| 1548 |
second := 0 |
| 1549 |
count := 0 |
| 1550 |
|
| 1551 |
processElement(func(str string) { |
| 1552 |
value, err := strconv.Atoi(str) |
| 1553 |
if err == nil { |
| 1554 |
count++ |
| 1555 |
if count == 1 { |
| 1556 |
first = value |
| 1557 |
} else if count == 2 { |
| 1558 |
second = value |
| 1559 |
} |
| 1560 |
} |
| 1561 |
}) |
| 1562 |
|
| 1563 |
if count == 2 { |
| 1564 |
// must have exactly 2 elements |
| 1565 |
ok = true |
| 1566 |
// modulus of element values |
| 1567 |
val := strconv.Itoa(first % second) |
| 1568 |
buffer.WriteString(between) |
| 1569 |
buffer.WriteString(val) |
| 1570 |
between = sep |
| 1571 |
} |
| 1572 |
|
| 1573 |
case GEO: |
| 1574 |
prod := float64(1) |
| 1575 |
count := 0 |
| 1576 |
|
| 1577 |
processElement(func(str string) { |
| 1578 |
value, err := strconv.Atoi(str) |
| 1579 |
if err == nil { |
| 1580 |
prod *= float64(value) |
| 1581 |
count++ |
| 1582 |
ok = true |
| 1583 |
} |
| 1584 |
}) |
| 1585 |
|
| 1586 |
if ok { |
| 1587 |
// adapted from https://github.com/SimonWaldherr/golibs/blob/v0.11.0/xmath/math.go#L246 |
| 1588 |
geo := int(float64(math.Pow(float64(prod), 1/float64(count)))) |
| 1589 |
val := strconv.Itoa(geo) |
| 1590 |
buffer.WriteString(between) |
| 1591 |
buffer.WriteString(val) |
| 1592 |
between = sep |
| 1593 |
} |
| 1594 |
|
| 1595 |
case HRM: |
| 1596 |
sum := float64(0) |
| 1597 |
count := 0 |
| 1598 |
|
| 1599 |
processElement(func(str string) { |
| 1600 |
value, err := strconv.Atoi(str) |
| 1601 |
if err == nil { |
| 1602 |
sum += 1 / float64(value) |
| 1603 |
count++ |
| 1604 |
ok = true |
| 1605 |
} |
| 1606 |
}) |
| 1607 |
|
| 1608 |
if ok { |
| 1609 |
// adapted from https://github.com/SimonWaldherr/golibs/blob/v0.11.0/xmath/math.go#L229 |
| 1610 |
hrm := int(float64(count) * 1 / sum) |
| 1611 |
val := strconv.Itoa(hrm) |
| 1612 |
buffer.WriteString(between) |
| 1613 |
buffer.WriteString(val) |
| 1614 |
between = sep |
| 1615 |
} |
| 1616 |
|
| 1617 |
case RMS: |
| 1618 |
sum := float64(0) |
| 1619 |
count := 0 |
| 1620 |
|
| 1621 |
processElement(func(str string) { |
| 1622 |
value, err := strconv.Atoi(str) |
| 1623 |
if err == nil { |
| 1624 |
sum += math.Pow(float64(value), 2) |
| 1625 |
count++ |
| 1626 |
ok = true |
| 1627 |
} |
| 1628 |
}) |
| 1629 |
|
| 1630 |
if ok { |
| 1631 |
// adapted from https://github.com/SimonWaldherr/golibs/blob/v0.11.0/xmath/math.go#L216 |
| 1632 |
rms := int(math.Sqrt(sum / float64(count))) |
| 1633 |
val := strconv.Itoa(rms) |
| 1634 |
buffer.WriteString(between) |
| 1635 |
buffer.WriteString(val) |
| 1636 |
between = sep |
| 1637 |
} |
| 1638 |
|
| 1639 |
case SQT: |
| 1640 |
processElement(func(str string) { |
| 1641 |
value, err := strconv.Atoi(str) |
| 1642 |
if err == nil && value >= 0 { |
| 1643 |
sqt := int(math.Sqrt(float64(value))) |
| 1644 |
val := strconv.Itoa(sqt) |
| 1645 |
buffer.WriteString(between) |
| 1646 |
buffer.WriteString(val) |
| 1647 |
between = sep |
| 1648 |
ok = true |
| 1649 |
} |
| 1650 |
}) |
| 1651 |
|
| 1652 |
case LG2, LGE, LOG: |
| 1653 |
// return logarithm truncated to integer |
| 1654 |
processElement(func(str string) { |
| 1655 |
value, err := strconv.Atoi(str) |
| 1656 |
if err == nil && value > 0 { |
| 1657 |
lg := float64(0) |
| 1658 |
if status == LG2 { |
| 1659 |
lg = math.Log2(float64(value)) |
| 1660 |
} else if status == LGE { |
| 1661 |
lg = math.Log(float64(value)) |
| 1662 |
} else if status == LOG { |
| 1663 |
lg = math.Log10(float64(value)) |
| 1664 |
} |
| 1665 |
dec, _ := math.Modf(lg) |
| 1666 |
val := strconv.Itoa(int(dec)) |
| 1667 |
buffer.WriteString(between) |
| 1668 |
buffer.WriteString(val) |
| 1669 |
between = sep |
| 1670 |
ok = true |
| 1671 |
} |
| 1672 |
}) |
| 1673 |
|
| 1674 |
case BIN: |
| 1675 |
processElement(func(str string) { |
| 1676 |
num, err := strconv.Atoi(str) |
| 1677 |
if err == nil { |
| 1678 |
// convert to binary representation |
| 1679 |
val := strconv.FormatInt(int64(num), 2) |
| 1680 |
buffer.WriteString(between) |
| 1681 |
buffer.WriteString(val) |
| 1682 |
between = sep |
| 1683 |
ok = true |
| 1684 |
} |
| 1685 |
}) |
| 1686 |
|
| 1687 |
case OCT: |
| 1688 |
processElement(func(str string) { |
| 1689 |
num, err := strconv.Atoi(str) |
| 1690 |
if err == nil { |
| 1691 |
// convert to octal representation |
| 1692 |
val := strconv.FormatInt(int64(num), 8) |
| 1693 |
buffer.WriteString(between) |
| 1694 |
buffer.WriteString(val) |
| 1695 |
between = sep |
| 1696 |
ok = true |
| 1697 |
} |
| 1698 |
}) |
| 1699 |
|
| 1700 |
case HEX: |
| 1701 |
processElement(func(str string) { |
| 1702 |
num, err := strconv.Atoi(str) |
| 1703 |
if err == nil { |
| 1704 |
// convert to hexadecimal representation |
| 1705 |
val := strconv.FormatInt(int64(num), 16) |
| 1706 |
val = strings.ToUpper(val) |
| 1707 |
// val := fmt.Sprintf("%X", num) |
| 1708 |
buffer.WriteString(between) |
| 1709 |
buffer.WriteString(val) |
| 1710 |
between = sep |
| 1711 |
ok = true |
| 1712 |
} |
| 1713 |
}) |
| 1714 |
|
| 1715 |
case BIT: |
| 1716 |
processElement(func(str string) { |
| 1717 |
num, err := strconv.Atoi(str) |
| 1718 |
if err == nil { |
| 1719 |
// Kernighan algorithm for counting set bits |
| 1720 |
count := 0 |
| 1721 |
for num != 0 { |
| 1722 |
num &= num - 1 |
| 1723 |
count++ |
| 1724 |
} |
| 1725 |
val := strconv.Itoa(count) |
| 1726 |
buffer.WriteString(between) |
| 1727 |
buffer.WriteString(val) |
| 1728 |
between = sep |
| 1729 |
ok = true |
| 1730 |
} |
| 1731 |
}) |
| 1732 |
|
| 1733 |
case PAD: |
| 1734 |
processElement(func(str string) { |
| 1735 |
if str != "" { |
| 1736 |
str = PadNumericID(str) |
| 1737 |
buffer.WriteString(between) |
| 1738 |
buffer.WriteString(str) |
| 1739 |
between = sep |
| 1740 |
ok = true |
| 1741 |
} |
| 1742 |
}) |
| 1743 |
|
| 1744 |
case RAW: |
| 1745 |
// for development and debugging of common XML cleanup functions (undocumented) |
| 1746 |
processElement(func(str string) { |
| 1747 |
if str != "" { |
| 1748 |
buffer.WriteString(between) |
| 1749 |
buffer.WriteString(str) |
| 1750 |
between = sep |
| 1751 |
ok = true |
| 1752 |
} |
| 1753 |
}) |
| 1754 |
|
| 1755 |
case ZEROBASED: |
| 1756 |
processElement(func(str string) { |
| 1757 |
if str != "" { |
| 1758 |
ok = true |
| 1759 |
buffer.WriteString(between) |
| 1760 |
buffer.WriteString(str) |
| 1761 |
between = sep |
| 1762 |
} |
| 1763 |
}) |
| 1764 |
|
| 1765 |
case ONEBASED: |
| 1766 |
processElement(func(str string) { |
| 1767 |
if str != "" { |
| 1768 |
ok = true |
| 1769 |
buffer.WriteString(between) |
| 1770 |
buffer.WriteString(str) |
| 1771 |
between = sep |
| 1772 |
} |
| 1773 |
}) |
| 1774 |
|
| 1775 |
case UCSCBASED: |
| 1776 |
processElement(func(str string) { |
| 1777 |
if str != "" { |
| 1778 |
ok = true |
| 1779 |
buffer.WriteString(between) |
| 1780 |
buffer.WriteString(str) |
| 1781 |
between = sep |
| 1782 |
} |
| 1783 |
}) |
| 1784 |
|
| 1785 |
case NUCLEIC: |
| 1786 |
processElement(func(str string) { |
| 1787 |
if str != "" { |
| 1788 |
ok = true |
| 1789 |
buffer.WriteString(between) |
| 1790 |
buffer.WriteString(str) |
| 1791 |
between = sep |
| 1792 |
} |
| 1793 |
}) |
| 1794 |
|
| 1795 |
case REVCOMP: |
| 1796 |
processElement(func(str string) { |
| 1797 |
if str != "" { |
| 1798 |
ok = true |
| 1799 |
buffer.WriteString(between) |
| 1800 |
str = ReverseComplement(str) |
| 1801 |
buffer.WriteString(str) |
| 1802 |
between = sep |
| 1803 |
} |
| 1804 |
}) |
| 1805 |
|
| 1806 |
case FASTA: |
| 1807 |
processElement(func(str string) { |
| 1808 |
for str != "" { |
| 1809 |
mx := len(str) |
| 1810 |
if mx > 70 { |
| 1811 |
mx = 70 |
| 1812 |
} |
| 1813 |
item := str[:mx] |
| 1814 |
str = str[mx:] |
| 1815 |
ok = true |
| 1816 |
item = strings.ToUpper(item) |
| 1817 |
buffer.WriteString(between) |
| 1818 |
buffer.WriteString(item) |
| 1819 |
between = sep |
| 1820 |
} |
| 1821 |
}) |
| 1822 |
|
| 1823 |
case NCBI2NA: |
| 1824 |
processElement(func(str string) { |
| 1825 |
if str != "" { |
| 1826 |
ok = true |
| 1827 |
buffer.WriteString(between) |
| 1828 |
str = Ncbi2naToIupac(str) |
| 1829 |
buffer.WriteString(str) |
| 1830 |
between = sep |
| 1831 |
} |
| 1832 |
}) |
| 1833 |
|
| 1834 |
case NCBI4NA: |
| 1835 |
processElement(func(str string) { |
| 1836 |
if str != "" { |
| 1837 |
ok = true |
| 1838 |
buffer.WriteString(between) |
| 1839 |
str = Ncbi4naToIupac(str) |
| 1840 |
buffer.WriteString(str) |
| 1841 |
between = sep |
| 1842 |
} |
| 1843 |
}) |
| 1844 |
|
| 1845 |
case CDS2PROT: |
| 1846 |
processElement(func(str string) { |
| 1847 |
if str != "" { |
| 1848 |
ok = true |
| 1849 |
buffer.WriteString(between) |
| 1850 |
str = TranslateCdRegion(str, cls.Gcd, cls.Frm, true, true, true, true, true, "") |
| 1851 |
buffer.WriteString(str) |
| 1852 |
between = sep |
| 1853 |
} |
| 1854 |
}) |
| 1855 |
|
| 1856 |
case PEPTS: |
| 1857 |
processElement(func(str string) { |
| 1858 |
if str != "" { |
| 1859 |
|
| 1860 |
clauses := strings.FieldsFunc(str, func(c rune) bool { |
| 1861 |
return c == '*' || c == '-' || c == 'X' || c == 'x' || !unicode.IsLetter(c) || c > 127 |
| 1862 |
}) |
| 1863 |
for _, item := range clauses { |
| 1864 |
item = strings.ToLower(item) |
| 1865 |
item = strings.TrimSpace(item) |
| 1866 |
if item == "" { |
| 1867 |
continue |
| 1868 |
} |
| 1869 |
ok = true |
| 1870 |
buffer.WriteString(between) |
| 1871 |
buffer.WriteString(item) |
| 1872 |
between = sep |
| 1873 |
} |
| 1874 |
} |
| 1875 |
}) |
| 1876 |
|
| 1877 |
case MOLWTX, MOLWTM, MOLWTF: |
| 1878 |
removeMet := false |
| 1879 |
formylMet := false |
| 1880 |
if status == MOLWTX { |
| 1881 |
removeMet = true |
| 1882 |
} else if status == MOLWTF { |
| 1883 |
formylMet = true |
| 1884 |
} |
| 1885 |
processElement(func(str string) { |
| 1886 |
if str != "" { |
| 1887 |
ok = true |
| 1888 |
buffer.WriteString(between) |
| 1889 |
str = ProteinWeight(str, removeMet, formylMet) |
| 1890 |
buffer.WriteString(str) |
| 1891 |
between = sep |
| 1892 |
} |
| 1893 |
}) |
| 1894 |
|
| 1895 |
case ACCESSION: |
| 1896 |
processElement(func(str string) { |
| 1897 |
if str != "" { |
| 1898 |
ok = true |
| 1899 |
buffer.WriteString(between) |
| 1900 |
str = strings.Replace(str, ".", "_", -1) |
| 1901 |
buffer.WriteString(str) |
| 1902 |
between = sep |
| 1903 |
} |
| 1904 |
}) |
| 1905 |
|
| 1906 |
case NUMERIC: |
| 1907 |
processElement(func(str string) { |
| 1908 |
if str != "" { |
| 1909 |
if IsAllDigits(str) { |
| 1910 |
ok = true |
| 1911 |
buffer.WriteString(between) |
| 1912 |
buffer.WriteString(str) |
| 1913 |
between = sep |
| 1914 |
} |
| 1915 |
} |
| 1916 |
}) |
| 1917 |
|
| 1918 |
case HGVS: |
| 1919 |
processElement(func(str string) { |
| 1920 |
if str != "" { |
| 1921 |
ok = true |
| 1922 |
buffer.WriteString(between) |
| 1923 |
str = ParseHGVS(str) |
| 1924 |
buffer.WriteString(str) |
| 1925 |
between = sep |
| 1926 |
} |
| 1927 |
}) |
| 1928 |
|
| 1929 |
case INDEXER: |
| 1930 |
// build positional index with a choice of TITL, TIAB, ABST, TERM, TEXT, and STEM field names |
| 1931 |
label := "TEXT" |
| 1932 |
if indexerField != "" { |
| 1933 |
label = indexerField |
| 1934 |
} |
| 1935 |
|
| 1936 |
indices := make(map[string][]string) |
| 1937 |
|
| 1938 |
cumulative := 0 |
| 1939 |
|
| 1940 |
var ilock sync.Mutex |
| 1941 |
|
| 1942 |
addItem := func(term string, position int) { |
| 1943 |
|
| 1944 |
// protect with mutex |
| 1945 |
ilock.Lock() |
| 1946 |
|
| 1947 |
arry, found := indices[term] |
| 1948 |
if !found { |
| 1949 |
arry = make([]string, 0, 1) |
| 1950 |
} |
| 1951 |
arry = append(arry, strconv.Itoa(position)) |
| 1952 |
indices[term] = arry |
| 1953 |
|
| 1954 |
ilock.Unlock() |
| 1955 |
} |
| 1956 |
|
| 1957 |
processElement(func(str string) { |
| 1958 |
|
| 1959 |
if str == "" { |
| 1960 |
return |
| 1961 |
} |
| 1962 |
|
| 1963 |
if str == "[Not Available]." { |
| 1964 |
return |
| 1965 |
} |
| 1966 |
|
| 1967 |
// remove parentheses to keep bracketed subscripts |
| 1968 |
/* |
| 1969 |
var ( |
| 1970 |
buffer []rune |
| 1971 |
prev rune |
| 1972 |
inside bool |
| 1973 |
) |
| 1974 |
for _, ch := range str { |
| 1975 |
if ch == '(' && prev != ' ' { |
| 1976 |
inside = true |
| 1977 |
} else if ch == ')' && inside { |
| 1978 |
inside = false |
| 1979 |
} else { |
| 1980 |
buffer = append(buffer, ch) |
| 1981 |
} |
| 1982 |
prev = ch |
| 1983 |
} |
| 1984 |
str = string(buffer) |
| 1985 |
*/ |
| 1986 |
|
| 1987 |
if IsNotASCII(str) { |
| 1988 |
str = FixMisusedLetters(str, true, false, true) |
| 1989 |
str = TransformAccents(str, true, true) |
| 1990 |
if HasUnicodeMarkup(str) { |
| 1991 |
str = RepairUnicodeMarkup(str, SPACE) |
| 1992 |
} |
| 1993 |
} |
| 1994 |
|
| 1995 |
str = strings.ToLower(str) |
| 1996 |
|
| 1997 |
if HasBadSpace(str) { |
| 1998 |
str = CleanupBadSpaces(str) |
| 1999 |
} |
| 2000 |
if HasAngleOrAmpersandEncoding(str) { |
| 2001 |
str = CleanupEncodedMarkup(str) |
| 2002 |
str = RepairTableMarkup(str, SPACE) |
| 2003 |
str = RepairScriptMarkup(str, SPACE) |
| 2004 |
str = RepairMathMLMarkup(str, SPACE) |
| 2005 |
// RemoveEmbeddedMarkup must be called before UnescapeString, which was suppressed in ExploreElements |
| 2006 |
str = RemoveEmbeddedMarkup(str, SPACE) |
| 2007 |
} |
| 2008 |
|
| 2009 |
if HasAmpOrNotASCII(str) { |
| 2010 |
str = html.UnescapeString(str) |
| 2011 |
str = strings.ToLower(str) |
| 2012 |
} |
| 2013 |
|
| 2014 |
if HasAdjacentSpaces(str) { |
| 2015 |
str = CompressRunsOfSpaces(str) |
| 2016 |
} |
| 2017 |
|
| 2018 |
str = strings.Replace(str, "(", " ", -1) |
| 2019 |
str = strings.Replace(str, ")", " ", -1) |
| 2020 |
|
| 2021 |
str = strings.Replace(str, "_", " ", -1) |
| 2022 |
|
| 2023 |
if HasHyphenOrApostrophe(str) { |
| 2024 |
str = FixSpecialCases(str) |
| 2025 |
} |
| 2026 |
|
| 2027 |
str = strings.Replace(str, "-", " ", -1) |
| 2028 |
|
| 2029 |
// remove trailing punctuation from each word |
| 2030 |
var arry []string |
| 2031 |
|
| 2032 |
terms := strings.Fields(str) |
| 2033 |
for _, item := range terms { |
| 2034 |
max := len(item) |
| 2035 |
for max > 1 { |
| 2036 |
ch := item[max-1] |
| 2037 |
if ch != '.' && ch != ',' && ch != ':' && ch != ';' { |
| 2038 |
break |
| 2039 |
} |
| 2040 |
// trim trailing period, comma, colon, and semicolon |
| 2041 |
item = item[:max-1] |
| 2042 |
// continue checking for runs of punctuation at end |
| 2043 |
max-- |
| 2044 |
} |
| 2045 |
if item == "" { |
| 2046 |
continue |
| 2047 |
} |
| 2048 |
arry = append(arry, item) |
| 2049 |
} |
| 2050 |
|
| 2051 |
// rejoin into string |
| 2052 |
cleaned := strings.Join(arry, " ") |
| 2053 |
|
| 2054 |
// break clauses at punctuation other than space or underscore, and at non-ASCII characters |
| 2055 |
clauses := strings.FieldsFunc(cleaned, func(c rune) bool { |
| 2056 |
return (!unicode.IsLetter(c) && !unicode.IsDigit(c)) && c != ' ' && c != '_' || c > 127 |
| 2057 |
}) |
| 2058 |
|
| 2059 |
// space replaces plus sign to separate runs of unpunctuated words |
| 2060 |
phrases := strings.Join(clauses, " ") |
| 2061 |
|
| 2062 |
// break phrases into individual words |
| 2063 |
words := strings.Fields(phrases) |
| 2064 |
|
| 2065 |
for _, item := range words { |
| 2066 |
|
| 2067 |
cumulative++ |
| 2068 |
|
| 2069 |
// skip at site of punctuation break |
| 2070 |
if item == "+" { |
| 2071 |
continue |
| 2072 |
} |
| 2073 |
|
| 2074 |
// skip if just a period, but allow terms that are all digits or period |
| 2075 |
if item == "." { |
| 2076 |
continue |
| 2077 |
} |
| 2078 |
|
| 2079 |
// optional stop word removal |
| 2080 |
if deStop && IsStopWord(item) { |
| 2081 |
continue |
| 2082 |
} |
| 2083 |
|
| 2084 |
if label == "STEM" { |
| 2085 |
// optionally apply stemming algorithm |
| 2086 |
item = porter2.Stem(item) |
| 2087 |
item = strings.TrimSpace(item) |
| 2088 |
} |
| 2089 |
|
| 2090 |
// index single normalized term with positions |
| 2091 |
addItem(item, cumulative) |
| 2092 |
ok = true |
| 2093 |
} |
| 2094 |
|
| 2095 |
// pad to avoid false positive proximity match of words in adjacent paragraphs |
| 2096 |
rounded := ((cumulative + 99) / 100) * 100 |
| 2097 |
if rounded-cumulative < 20 { |
| 2098 |
rounded += 100 |
| 2099 |
} |
| 2100 |
cumulative = rounded |
| 2101 |
}) |
| 2102 |
|
| 2103 |
prepareIndices := func() { |
| 2104 |
|
| 2105 |
if len(indices) < 1 { |
| 2106 |
return |
| 2107 |
} |
| 2108 |
|
| 2109 |
arry := slices.Sorted(maps.Keys(indices)) |
| 2110 |
|
| 2111 |
last := "" |
| 2112 |
for _, item := range arry { |
| 2113 |
item = strings.TrimSpace(item) |
| 2114 |
if item == "" { |
| 2115 |
continue |
| 2116 |
} |
| 2117 |
if item == last { |
| 2118 |
// skip duplicate entry |
| 2119 |
continue |
| 2120 |
} |
| 2121 |
buffer.WriteString("<") |
| 2122 |
buffer.WriteString(label) |
| 2123 |
if len(indices[item]) > 0 { |
| 2124 |
// use attribute for position |
| 2125 |
buffer.WriteString(" pos=\"") |
| 2126 |
attr := strings.Join(indices[item], ",") |
| 2127 |
buffer.WriteString(attr) |
| 2128 |
buffer.WriteString("\"") |
| 2129 |
} |
| 2130 |
buffer.WriteString(">") |
| 2131 |
buffer.WriteString(item) |
| 2132 |
buffer.WriteString("</") |
| 2133 |
buffer.WriteString(label) |
| 2134 |
buffer.WriteString(">") |
| 2135 |
last = item |
| 2136 |
} |
| 2137 |
} |
| 2138 |
|
| 2139 |
if ok { |
| 2140 |
prepareIndices() |
| 2141 |
} |
| 2142 |
|
| 2143 |
case TERMS: |
| 2144 |
processElement(func(str string) { |
| 2145 |
if str != "" { |
| 2146 |
|
| 2147 |
terms := strings.Fields(str) |
| 2148 |
for _, item := range terms { |
| 2149 |
max := len(item) |
| 2150 |
for max > 1 { |
| 2151 |
ch := item[max-1] |
| 2152 |
if ch != '.' && ch != ',' && ch != ':' && ch != ';' { |
| 2153 |
break |
| 2154 |
} |
| 2155 |
// trim trailing period, comma, colon, and semicolon |
| 2156 |
item = item[:max-1] |
| 2157 |
// continue checking for runs of punctuation at end |
| 2158 |
max-- |
| 2159 |
} |
| 2160 |
if item == "" { |
| 2161 |
continue |
| 2162 |
} |
| 2163 |
ok = true |
| 2164 |
buffer.WriteString(between) |
| 2165 |
buffer.WriteString(item) |
| 2166 |
between = sep |
| 2167 |
} |
| 2168 |
} |
| 2169 |
}) |
| 2170 |
|
| 2171 |
case WORDS: |
| 2172 |
processElement(func(str string) { |
| 2173 |
if str != "" { |
| 2174 |
|
| 2175 |
words := strings.FieldsFunc(str, func(c rune) bool { |
| 2176 |
return !unicode.IsLetter(c) && !unicode.IsDigit(c) |
| 2177 |
}) |
| 2178 |
for _, item := range words { |
| 2179 |
item = strings.ToLower(item) |
| 2180 |
if deStop { |
| 2181 |
if IsStopWord(item) { |
| 2182 |
continue |
| 2183 |
} |
| 2184 |
} |
| 2185 |
if doStem { |
| 2186 |
item = porter2.Stem(item) |
| 2187 |
item = strings.TrimSpace(item) |
| 2188 |
} |
| 2189 |
if item == "" { |
| 2190 |
continue |
| 2191 |
} |
| 2192 |
ok = true |
| 2193 |
buffer.WriteString(between) |
| 2194 |
buffer.WriteString(item) |
| 2195 |
between = sep |
| 2196 |
} |
| 2197 |
} |
| 2198 |
}) |
| 2199 |
|
| 2200 |
case PAIRS, PAIRX: |
| 2201 |
processElement(func(str string) { |
| 2202 |
if str != "" { |
| 2203 |
|
| 2204 |
doSingle := (status == PAIRX) |
| 2205 |
|
| 2206 |
if doSingle { |
| 2207 |
str = PrepareForIndexing(str, true, false, true, true, true) |
| 2208 |
} |
| 2209 |
|
| 2210 |
// break clauses at punctuation other than space, and at non-ASCII characters |
| 2211 |
clauses := strings.FieldsFunc(str, func(c rune) bool { |
| 2212 |
return (!unicode.IsLetter(c) && !unicode.IsDigit(c)) && c != ' ' || c > 127 |
| 2213 |
}) |
| 2214 |
|
| 2215 |
// plus sign separates runs of unpunctuated words |
| 2216 |
phrases := strings.Join(clauses, " + ") |
| 2217 |
|
| 2218 |
// break phrases into individual words |
| 2219 |
words := strings.FieldsFunc(phrases, func(c rune) bool { |
| 2220 |
return !unicode.IsLetter(c) && !unicode.IsDigit(c) |
| 2221 |
}) |
| 2222 |
|
| 2223 |
// word pairs (or isolated singletons) separated by stop words |
| 2224 |
if len(words) > 1 { |
| 2225 |
past := "" |
| 2226 |
run := 0 |
| 2227 |
for _, item := range words { |
| 2228 |
if item == "+" { |
| 2229 |
if doSingle && run == 1 && past != "" { |
| 2230 |
ok = true |
| 2231 |
buffer.WriteString(between) |
| 2232 |
buffer.WriteString(past) |
| 2233 |
between = sep |
| 2234 |
} |
| 2235 |
past = "" |
| 2236 |
run = 0 |
| 2237 |
continue |
| 2238 |
} |
| 2239 |
item = strings.ToLower(item) |
| 2240 |
if deStop { |
| 2241 |
if IsStopWord(item) { |
| 2242 |
if doSingle && run == 1 && past != "" { |
| 2243 |
ok = true |
| 2244 |
buffer.WriteString(between) |
| 2245 |
buffer.WriteString(past) |
| 2246 |
between = sep |
| 2247 |
} |
| 2248 |
past = "" |
| 2249 |
run = 0 |
| 2250 |
continue |
| 2251 |
} |
| 2252 |
} |
| 2253 |
if doStem { |
| 2254 |
item = porter2.Stem(item) |
| 2255 |
item = strings.TrimSpace(item) |
| 2256 |
} |
| 2257 |
if item == "" { |
| 2258 |
past = "" |
| 2259 |
continue |
| 2260 |
} |
| 2261 |
if past != "" { |
| 2262 |
ok = true |
| 2263 |
buffer.WriteString(between) |
| 2264 |
buffer.WriteString(past + " " + item) |
| 2265 |
between = sep |
| 2266 |
} |
| 2267 |
past = item |
| 2268 |
run++ |
| 2269 |
} |
| 2270 |
if doSingle && run == 1 && past != "" { |
| 2271 |
ok = true |
| 2272 |
buffer.WriteString(between) |
| 2273 |
buffer.WriteString(past) |
| 2274 |
between = sep |
| 2275 |
} |
| 2276 |
} |
| 2277 |
} |
| 2278 |
}) |
| 2279 |
|
| 2280 |
case SPLIT: |
| 2281 |
processElement(func(str string) { |
| 2282 |
if str != "" && cls.Wth != "" { |
| 2283 |
|
| 2284 |
items := strings.Split(str, cls.Wth) |
| 2285 |
for _, item := range items { |
| 2286 |
item = strings.TrimSpace(item) |
| 2287 |
if item == "" { |
| 2288 |
continue |
| 2289 |
} |
| 2290 |
ok = true |
| 2291 |
buffer.WriteString(between) |
| 2292 |
buffer.WriteString(item) |
| 2293 |
between = sep |
| 2294 |
} |
| 2295 |
} |
| 2296 |
}) |
| 2297 |
|
| 2298 |
case ORDER: |
| 2299 |
processElement(func(str string) { |
| 2300 |
if str != "" { |
| 2301 |
ok = true |
| 2302 |
str = SortStringByWords(str) |
| 2303 |
buffer.WriteString(between) |
| 2304 |
buffer.WriteString(str) |
| 2305 |
between = sep |
| 2306 |
} |
| 2307 |
}) |
| 2308 |
|
| 2309 |
case REVERSE: |
| 2310 |
processElement(func(str string) { |
| 2311 |
if str != "" { |
| 2312 |
|
| 2313 |
words := strings.FieldsFunc(str, func(c rune) bool { |
| 2314 |
return !unicode.IsLetter(c) && !unicode.IsDigit(c) |
| 2315 |
}) |
| 2316 |
for lf, rt := 0, len(words)-1; lf < rt; lf, rt = lf+1, rt-1 { |
| 2317 |
words[lf], words[rt] = words[rt], words[lf] |
| 2318 |
} |
| 2319 |
for _, item := range words { |
| 2320 |
item = strings.ToLower(item) |
| 2321 |
if deStop { |
| 2322 |
if IsStopWord(item) { |
| 2323 |
continue |
| 2324 |
} |
| 2325 |
} |
| 2326 |
if doStem { |
| 2327 |
item = porter2.Stem(item) |
| 2328 |
item = strings.TrimSpace(item) |
| 2329 |
} |
| 2330 |
if item == "" { |
| 2331 |
continue |
| 2332 |
} |
| 2333 |
ok = true |
| 2334 |
buffer.WriteString(between) |
| 2335 |
buffer.WriteString(item) |
| 2336 |
between = sep |
| 2337 |
} |
| 2338 |
} |
| 2339 |
}) |
| 2340 |
|
| 2341 |
case LETTERS: |
| 2342 |
processElement(func(str string) { |
| 2343 |
if str != "" { |
| 2344 |
for _, ch := range str { |
| 2345 |
ok = true |
| 2346 |
buffer.WriteString(between) |
| 2347 |
buffer.WriteRune(ch) |
| 2348 |
between = sep |
| 2349 |
} |
| 2350 |
} |
| 2351 |
}) |
| 2352 |
|
| 2353 |
case PENTAMERS: |
| 2354 |
processElement(func(str string) { |
| 2355 |
if str != "" { |
| 2356 |
arry := SlidingSlices(str, 5) |
| 2357 |
for _, item := range arry { |
| 2358 |
item = strings.ToLower(item) |
| 2359 |
item = strings.TrimSpace(item) |
| 2360 |
if item == "" { |
| 2361 |
continue |
| 2362 |
} |
| 2363 |
ok = true |
| 2364 |
buffer.WriteString(between) |
| 2365 |
buffer.WriteString(item) |
| 2366 |
between = sep |
| 2367 |
} |
| 2368 |
} |
| 2369 |
}) |
| 2370 |
|
| 2371 |
case CLAUSES: |
| 2372 |
processElement(func(str string) { |
| 2373 |
if str != "" { |
| 2374 |
|
| 2375 |
clauses := strings.FieldsFunc(str, func(c rune) bool { |
| 2376 |
return c == '.' || c == ',' || c == ';' || c == ':' |
| 2377 |
}) |
| 2378 |
for _, item := range clauses { |
| 2379 |
item = strings.ToLower(item) |
| 2380 |
item = strings.TrimSpace(item) |
| 2381 |
if item == "" { |
| 2382 |
continue |
| 2383 |
} |
| 2384 |
ok = true |
| 2385 |
buffer.WriteString(between) |
| 2386 |
buffer.WriteString(item) |
| 2387 |
between = sep |
| 2388 |
} |
| 2389 |
} |
| 2390 |
}) |
| 2391 |
|
| 2392 |
case MESHCODE: |
| 2393 |
var code []string |
| 2394 |
var tree []string |
| 2395 |
|
| 2396 |
processElement(func(str string) { |
| 2397 |
if str != "" { |
| 2398 |
txt, found := transform[str] |
| 2399 |
str = strings.ToLower(str) |
| 2400 |
code = append(code, str) |
| 2401 |
ok = true |
| 2402 |
|
| 2403 |
if !found { |
| 2404 |
return |
| 2405 |
} |
| 2406 |
txt = strings.ToLower(txt) |
| 2407 |
txt = strings.Replace(txt, ".", "_", -1) |
| 2408 |
codes := strings.FieldsFunc(txt, func(c rune) bool { |
| 2409 |
return c == ',' |
| 2410 |
}) |
| 2411 |
for _, item := range codes { |
| 2412 |
ch := item[0] |
| 2413 |
if item == "" { |
| 2414 |
continue |
| 2415 |
} |
| 2416 |
switch ch { |
| 2417 |
case 'a', 'c', 'd', 'e', 'f', 'g', 'z': |
| 2418 |
tree = append(tree, item) |
| 2419 |
default: |
| 2420 |
} |
| 2421 |
} |
| 2422 |
} |
| 2423 |
}) |
| 2424 |
|
| 2425 |
if len(code) > 1 { |
| 2426 |
slices.Sort(code) |
| 2427 |
} |
| 2428 |
if len(tree) > 1 { |
| 2429 |
slices.Sort(tree) |
| 2430 |
} |
| 2431 |
|
| 2432 |
last := "" |
| 2433 |
for _, item := range code { |
| 2434 |
if item == last { |
| 2435 |
// skip duplicate entry |
| 2436 |
continue |
| 2437 |
} |
| 2438 |
buffer.WriteString("<CODE>") |
| 2439 |
buffer.WriteString(item) |
| 2440 |
buffer.WriteString("</CODE>") |
| 2441 |
last = item |
| 2442 |
} |
| 2443 |
|
| 2444 |
last = "" |
| 2445 |
for _, item := range tree { |
| 2446 |
if item == last { |
| 2447 |
// skip duplicate entry |
| 2448 |
continue |
| 2449 |
} |
| 2450 |
buffer.WriteString("<TREE>") |
| 2451 |
buffer.WriteString(item) |
| 2452 |
buffer.WriteString("</TREE>") |
| 2453 |
last = item |
| 2454 |
} |
| 2455 |
|
| 2456 |
case MATRIX: |
| 2457 |
var arry []string |
| 2458 |
|
| 2459 |
processElement(func(str string) { |
| 2460 |
if str != "" { |
| 2461 |
txt, found := transform[str] |
| 2462 |
if found { |
| 2463 |
str = txt |
| 2464 |
} |
| 2465 |
arry = append(arry, str) |
| 2466 |
ok = true |
| 2467 |
} |
| 2468 |
}) |
| 2469 |
|
| 2470 |
if len(arry) > 1 { |
| 2471 |
slices.Sort(arry) |
| 2472 |
|
| 2473 |
for i, frst := range arry { |
| 2474 |
for j, scnd := range arry { |
| 2475 |
if i == j { |
| 2476 |
continue |
| 2477 |
} |
| 2478 |
buffer.WriteString(between) |
| 2479 |
buffer.WriteString(frst) |
| 2480 |
buffer.WriteString("\t") |
| 2481 |
buffer.WriteString(scnd) |
| 2482 |
between = "\n" |
| 2483 |
} |
| 2484 |
} |
| 2485 |
} |
| 2486 |
|
| 2487 |
case CLASSIFY: |
| 2488 |
processElement(func(str string) { |
| 2489 |
if str != "" { |
| 2490 |
kywds := make(map[string]bool) |
| 2491 |
|
| 2492 |
// search for whole word or whole phrase substrings |
| 2493 |
srchr.Search(str[:], |
| 2494 |
func(str, pat string, pos int) bool { |
| 2495 |
mtch := strings.TrimSpace(pat) |
| 2496 |
rslt := transform[mtch] |
| 2497 |
if rslt != "" { |
| 2498 |
items := strings.Split(rslt, ",") |
| 2499 |
for _, itm := range items { |
| 2500 |
tag, val := SplitInTwoRight(itm, ":") |
| 2501 |
txt := val |
| 2502 |
if tag != "" { |
| 2503 |
txt = "<" + tag + ">" + val + "</" + tag + ">" |
| 2504 |
} |
| 2505 |
kywds[txt] = true |
| 2506 |
} |
| 2507 |
} |
| 2508 |
return true |
| 2509 |
}) |
| 2510 |
|
| 2511 |
keys := slices.Sorted(maps.Keys(kywds)) |
| 2512 |
|
| 2513 |
// record single copy of each match, in alphabetical order |
| 2514 |
for _, key := range keys { |
| 2515 |
ok = true |
| 2516 |
buffer.WriteString(between) |
| 2517 |
buffer.WriteString(key) |
| 2518 |
between = sep |
| 2519 |
} |
| 2520 |
} |
| 2521 |
}) |
| 2522 |
|
| 2523 |
case HISTOGRAM: |
| 2524 |
processElement(func(str string) { |
| 2525 |
if str != "" { |
| 2526 |
ok = true |
| 2527 |
|
| 2528 |
hlock.Lock() |
| 2529 |
|
| 2530 |
val := histogram[str] |
| 2531 |
val++ |
| 2532 |
histogram[str] = val |
| 2533 |
/* |
| 2534 |
if strings.Contains(str, "<sub>") || strings.Contains(str, "<sub>") { |
| 2535 |
val := histogram["sub"] |
| 2536 |
val++ |
| 2537 |
histogram["sub"] = val |
| 2538 |
} |
| 2539 |
if strings.Contains(str, "<sup>") || strings.Contains(str, "<sup>") { |
| 2540 |
val := histogram["sup"] |
| 2541 |
val++ |
| 2542 |
histogram["sup"] = val |
| 2543 |
} |
| 2544 |
for _, ch := range str { |
| 2545 |
if IsUnicodeSubsc(ch) { |
| 2546 |
val := histogram["usb"] |
| 2547 |
val++ |
| 2548 |
histogram["usb"] = val |
| 2549 |
break |
| 2550 |
} |
| 2551 |
} |
| 2552 |
for _, ch := range str { |
| 2553 |
if IsUnicodeSuper(ch) { |
| 2554 |
val := histogram["usp"] |
| 2555 |
val++ |
| 2556 |
histogram["usp"] = val |
| 2557 |
break |
| 2558 |
} |
| 2559 |
} |
| 2560 |
*/ |
| 2561 |
/* |
| 2562 |
for _, ch := range str { |
| 2563 |
num := strconv.Itoa(int(ch)) |
| 2564 |
val := histogram[num] |
| 2565 |
val++ |
| 2566 |
histogram[num] = val |
| 2567 |
} |
| 2568 |
*/ |
| 2569 |
|
| 2570 |
hlock.Unlock() |
| 2571 |
} |
| 2572 |
}) |
| 2573 |
|
| 2574 |
case FREQUENCY: |
| 2575 |
processElement(func(str string) { |
| 2576 |
if str != "" { |
| 2577 |
ok = true |
| 2578 |
|
| 2579 |
hlock.Lock() |
| 2580 |
|
| 2581 |
for _, ch := range str { |
| 2582 |
if ch < 128 { |
| 2583 |
continue |
| 2584 |
} |
| 2585 |
key := fmt.Sprintf("0x%04X", int(ch)) |
| 2586 |
val := histogram[key] |
| 2587 |
val++ |
| 2588 |
histogram[key] = val |
| 2589 |
} |
| 2590 |
|
| 2591 |
hlock.Unlock() |
| 2592 |
} |
| 2593 |
}) |
| 2594 |
|
| 2595 |
case ACCENTED: |
| 2596 |
processElement(func(str string) { |
| 2597 |
if str != "" { |
| 2598 |
found := false |
| 2599 |
for _, ch := range str { |
| 2600 |
if ch > 127 { |
| 2601 |
found = true |
| 2602 |
break |
| 2603 |
} |
| 2604 |
} |
| 2605 |
if found { |
| 2606 |
ok = true |
| 2607 |
buffer.WriteString(between) |
| 2608 |
buffer.WriteString(str) |
| 2609 |
between = sep |
| 2610 |
} |
| 2611 |
} |
| 2612 |
}) |
| 2613 |
/* |
| 2614 |
processElement(func(str string) { |
| 2615 |
if str != "" { |
| 2616 |
found := false |
| 2617 |
for _, ch := range str { |
| 2618 |
if ch > 127 { |
| 2619 |
found = true |
| 2620 |
break |
| 2621 |
} |
| 2622 |
} |
| 2623 |
if found { |
| 2624 |
ok = true |
| 2625 |
buffer.WriteString(between) |
| 2626 |
buffer.WriteString(str) |
| 2627 |
between = sep |
| 2628 |
} |
| 2629 |
} |
| 2630 |
}) |
| 2631 |
*/ |
| 2632 |
/* |
| 2633 |
processElement(func(str string) { |
| 2634 |
if str != "" { |
| 2635 |
terms := strings.Fields(str) |
| 2636 |
for _, item := range terms { |
| 2637 |
max := len(item) |
| 2638 |
for max > 1 { |
| 2639 |
ch := item[max-1] |
| 2640 |
if ch != '.' && ch != ',' && ch != ':' && ch != ';' { |
| 2641 |
break |
| 2642 |
} |
| 2643 |
// trim trailing period, comma, colon, and semicolon |
| 2644 |
item = item[:max-1] |
| 2645 |
// continue checking for runs of punctuation at end |
| 2646 |
max-- |
| 2647 |
} |
| 2648 |
if item == "" { |
| 2649 |
continue |
| 2650 |
} |
| 2651 |
found := false |
| 2652 |
for _, ch := range item { |
| 2653 |
if ch > 127 { |
| 2654 |
found = true |
| 2655 |
break |
| 2656 |
} |
| 2657 |
} |
| 2658 |
if found { |
| 2659 |
ok = true |
| 2660 |
after := TransformAccents(item, true, false) |
| 2661 |
for _, c := range item { |
| 2662 |
if c > 127 { |
| 2663 |
tg := fmt.Sprintf("%d\t%U\t%s\t%s\t%s\n", c, c, string(c), item, after) |
| 2664 |
buffer.WriteString(tg) |
| 2665 |
} |
| 2666 |
} |
| 2667 |
} |
| 2668 |
} |
| 2669 |
} |
| 2670 |
}) |
| 2671 |
*/ |
| 2672 |
|
| 2673 |
case TEST: |
| 2674 |
suffix := "" |
| 2675 |
if reg == "" && exp != "" { |
| 2676 |
suffix = " in " + exp |
| 2677 |
} |
| 2678 |
|
| 2679 |
processElement(func(str string) { |
| 2680 |
if str != "" { |
| 2681 |
if HasCombiningAccent(str[:]) { |
| 2682 |
ok = true |
| 2683 |
buffer.WriteString(between) |
| 2684 |
buffer.WriteString("Combining Accent" + suffix) |
| 2685 |
between = sep |
| 2686 |
} |
| 2687 |
if HasInvisibleUnicode(str[:]) { |
| 2688 |
ok = true |
| 2689 |
buffer.WriteString(between) |
| 2690 |
buffer.WriteString("Invisible Unicode" + suffix) |
| 2691 |
between = sep |
| 2692 |
} |
| 2693 |
} |
| 2694 |
}) |
| 2695 |
|
| 2696 |
case SCAN: |
| 2697 |
// for identification of records with current data issues of interest (undocumented) |
| 2698 |
processElement(func(str string) { |
| 2699 |
if str != "" { |
| 2700 |
for _, ch := range str { |
| 2701 |
if ch < 128 { |
| 2702 |
continue |
| 2703 |
} |
| 2704 |
if ch == 223 { |
| 2705 |
// sharp s 0x00DF |
| 2706 |
ok = true |
| 2707 |
buffer.WriteString(between) |
| 2708 |
buffer.WriteString("0x00DF") |
| 2709 |
between = sep |
| 2710 |
break |
| 2711 |
} |
| 2712 |
if ch == 946 { |
| 2713 |
// beta 0x03B2 |
| 2714 |
ok = true |
| 2715 |
buffer.WriteString(between) |
| 2716 |
buffer.WriteString("0x03B2") |
| 2717 |
between = sep |
| 2718 |
break |
| 2719 |
} |
| 2720 |
} |
| 2721 |
/* |
| 2722 |
for _, ch := range str { |
| 2723 |
if ch < 128 { |
| 2724 |
continue |
| 2725 |
} |
| 2726 |
if ch == 181 { |
| 2727 |
// micro 0x00B5 |
| 2728 |
ok = true |
| 2729 |
buffer.WriteString(between) |
| 2730 |
buffer.WriteString("0x00B5") |
| 2731 |
between = sep |
| 2732 |
break |
| 2733 |
} |
| 2734 |
if ch == 956 { |
| 2735 |
// mu 0x03BC |
| 2736 |
ok = true |
| 2737 |
buffer.WriteString(between) |
| 2738 |
buffer.WriteString("0x03BC") |
| 2739 |
between = sep |
| 2740 |
break |
| 2741 |
} |
| 2742 |
} |
| 2743 |
*/ |
| 2744 |
/* |
| 2745 |
terms := strings.Fields(str) |
| 2746 |
for _, item := range terms { |
| 2747 |
hasmicro := false |
| 2748 |
hasmu := false |
| 2749 |
for _, ch := range item { |
| 2750 |
if ch < 128 { |
| 2751 |
continue |
| 2752 |
} |
| 2753 |
if ch == 181 { |
| 2754 |
// micro 0x00B5 |
| 2755 |
hasmicro = true |
| 2756 |
} |
| 2757 |
if ch == 956 { |
| 2758 |
// mu 0x03BC |
| 2759 |
hasmu = true |
| 2760 |
} |
| 2761 |
} |
| 2762 |
if hasmicro || hasmu { |
| 2763 |
ok = true |
| 2764 |
buffer.WriteString(between) |
| 2765 |
if hasmicro { |
| 2766 |
buffer.WriteString("r") |
| 2767 |
} |
| 2768 |
if hasmu { |
| 2769 |
buffer.WriteString("u") |
| 2770 |
} |
| 2771 |
buffer.WriteString("\t") |
| 2772 |
buffer.WriteString(item) |
| 2773 |
between = sep |
| 2774 |
} |
| 2775 |
} |
| 2776 |
*/ |
| 2777 |
} |
| 2778 |
}) |
| 2779 |
|
| 2780 |
default: |
| 2781 |
} |
| 2782 |
|
| 2783 |
// use default value if nothing written |
| 2784 |
if !ok && cls.Def != "" { |
| 2785 |
ok = true |
| 2786 |
buffer.WriteString(cls.Def) |
| 2787 |
} |
| 2788 |
|
| 2789 |
buffer.WriteString(sfx) |
| 2790 |
|
| 2791 |
if !ok { |
| 2792 |
return "", false |
| 2793 |
} |
| 2794 |
|
| 2795 |
txt := buffer.String() |
| 2796 |
|
| 2797 |
return txt, true |
| 2798 |
} |
| 2799 |
|
| 2800 |
// processInstructions performs extraction commands on a subset of XML |
| 2801 |
func processInstructions( |
| 2802 |
commands []*Operation, |
| 2803 |
curr *XMLNode, |
| 2804 |
mask string, |
| 2805 |
tab string, |
| 2806 |
ret string, |
| 2807 |
index int, |
| 2808 |
level int, |
| 2809 |
atl Atlas, |
| 2810 |
accum func(string), |
| 2811 |
) (string, string) { |
| 2812 |
|
| 2813 |
if accum == nil { |
| 2814 |
return tab, ret |
| 2815 |
} |
| 2816 |
|
| 2817 |
variables := atl.Variables |
| 2818 |
|
| 2819 |
sep := "\t" |
| 2820 |
pfx := "" |
| 2821 |
sfx := "" |
| 2822 |
plg := "" |
| 2823 |
elg := "" |
| 2824 |
lst := "" |
| 2825 |
|
| 2826 |
def := "" |
| 2827 |
msk := "" |
| 2828 |
|
| 2829 |
reg := "" |
| 2830 |
exp := "" |
| 2831 |
|
| 2832 |
wth := "" |
| 2833 |
|
| 2834 |
gcd := 0 |
| 2835 |
frm := 0 |
| 2836 |
|
| 2837 |
col := "\t" |
| 2838 |
lin := "\n" |
| 2839 |
|
| 2840 |
varname := "" |
| 2841 |
isAccum := false |
| 2842 |
|
| 2843 |
wrp := false |
| 2844 |
|
| 2845 |
plain := true |
| 2846 |
var currColor *color.Color |
| 2847 |
|
| 2848 |
// handles color, e.g., -color "red,bold", reset to plain by -color "-" (undocumented) |
| 2849 |
printInColor := func(str string) { |
| 2850 |
if plain || currColor == nil { |
| 2851 |
accum(str) |
| 2852 |
} else { |
| 2853 |
tx := currColor.SprintFunc() |
| 2854 |
tmp := fmt.Sprintf("%s", tx(str)) |
| 2855 |
accum(tmp) |
| 2856 |
} |
| 2857 |
} |
| 2858 |
|
| 2859 |
// process commands |
| 2860 |
for _, op := range commands { |
| 2861 |
|
| 2862 |
str := op.Value |
| 2863 |
|
| 2864 |
switch op.Type { |
| 2865 |
case ELEMENT: |
| 2866 |
cls := Clause{Prev: tab, Pfx: pfx, Sfx: sfx, Plg: plg, Sep: sep, Def: def, Msk: msk, Reg: reg, Exp: exp, Wth: wth, Gcd: gcd, Frm: frm, Wrp: wrp} |
| 2867 |
txt, ok := processClause(curr, op.Stages, mask, cls, op.Type, index, level, atl) |
| 2868 |
if ok { |
| 2869 |
if varname != "" { |
| 2870 |
// CONVERTER value may have preceding tab, change to space and trim spaces |
| 2871 |
txt = strings.Replace(txt, "\t", " ", -1) |
| 2872 |
txt = strings.TrimSpace(txt) |
| 2873 |
variables[varname] = txt |
| 2874 |
varname = "" |
| 2875 |
} else { |
| 2876 |
plg = "" |
| 2877 |
lst = elg |
| 2878 |
tab = col |
| 2879 |
ret = lin |
| 2880 |
if plain { |
| 2881 |
accum(txt) |
| 2882 |
} else { |
| 2883 |
printInColor(txt) |
| 2884 |
} |
| 2885 |
} |
| 2886 |
} |
| 2887 |
case HISTOGRAM: |
| 2888 |
cls := Clause{Prev: "", Pfx: "", Sfx: "", Plg: "", Sep: "", Def: "", Msk: "", Reg: "", Exp: "", Wth: "", Gcd: gcd, Frm: frm, Wrp: wrp} |
| 2889 |
txt, ok := processClause(curr, op.Stages, mask, cls, op.Type, index, level, atl) |
| 2890 |
if ok { |
| 2891 |
accum(txt) |
| 2892 |
} |
| 2893 |
case TAB: |
| 2894 |
col = str |
| 2895 |
case RET: |
| 2896 |
lin = str |
| 2897 |
case PFX: |
| 2898 |
pfx = str |
| 2899 |
case SFX: |
| 2900 |
sfx = str |
| 2901 |
case SEP: |
| 2902 |
sep = str |
| 2903 |
case TAG: |
| 2904 |
wrp = true |
| 2905 |
fallthrough |
| 2906 |
case LBL: |
| 2907 |
lbl := str |
| 2908 |
accum(tab) |
| 2909 |
accum(plg) |
| 2910 |
accum(pfx) |
| 2911 |
if plain { |
| 2912 |
accum(lbl) |
| 2913 |
} else { |
| 2914 |
printInColor(lbl) |
| 2915 |
} |
| 2916 |
accum(sfx) |
| 2917 |
plg = "" |
| 2918 |
lst = elg |
| 2919 |
tab = col |
| 2920 |
ret = lin |
| 2921 |
case PFC: |
| 2922 |
// preface clears previous tab and sets prefix in one command |
| 2923 |
pfx = str |
| 2924 |
fallthrough |
| 2925 |
case CLR: |
| 2926 |
// clear previous tab after the fact |
| 2927 |
tab = "" |
| 2928 |
case DEQ: |
| 2929 |
// set queued tab after the fact |
| 2930 |
tab = str |
| 2931 |
case PLG: |
| 2932 |
plg = str |
| 2933 |
case ELG: |
| 2934 |
elg = str |
| 2935 |
case WRP: |
| 2936 |
// shortcut to wrap elements in XML tags |
| 2937 |
if str == "" || str == "-" { |
| 2938 |
sep = "\t" |
| 2939 |
pfx = "" |
| 2940 |
sfx = "" |
| 2941 |
plg = "" |
| 2942 |
elg = "" |
| 2943 |
wrp = false |
| 2944 |
break |
| 2945 |
} |
| 2946 |
if strings.Index(str, ",") >= 0 { |
| 2947 |
// -wrp with comma-separated arguments is deprecated, but supported for backward compatibility |
| 2948 |
lft, rgt := SplitInTwoRight(str, ",") |
| 2949 |
if lft != "" { |
| 2950 |
plg = "<" + lft + ">" |
| 2951 |
elg = "</" + lft + ">" |
| 2952 |
} |
| 2953 |
if rgt != "" && rgt != "-" { |
| 2954 |
pfx = "<" + rgt + ">" |
| 2955 |
sfx = "</" + rgt + ">" |
| 2956 |
sep = sfx + pfx |
| 2957 |
} |
| 2958 |
wrp = true |
| 2959 |
break |
| 2960 |
} |
| 2961 |
if strings.Index(str, "/") >= 0 { |
| 2962 |
// supports slash-separated components |
| 2963 |
pfx = "" |
| 2964 |
sfx = "" |
| 2965 |
sep = "" |
| 2966 |
items := strings.Split(str, "/") |
| 2967 |
for i := range len(items) { |
| 2968 |
tmp := items[i] |
| 2969 |
// replace spaces with underscore |
| 2970 |
tmp = strings.Replace(tmp, " ", "_", -1) |
| 2971 |
pfx += "<" + tmp + ">" |
| 2972 |
} |
| 2973 |
for i := len(items) - 1; i >= 0; i-- { |
| 2974 |
tmp := items[i] |
| 2975 |
tmp = strings.Replace(tmp, " ", "_", -1) |
| 2976 |
sfx += "</" + tmp + ">" |
| 2977 |
} |
| 2978 |
sep = sfx + pfx |
| 2979 |
wrp = true |
| 2980 |
break |
| 2981 |
} |
| 2982 |
// shortcut for strings.HasPrefix(str, "&") and strings.TrimPrefix(str, "&") |
| 2983 |
if len(str) > 1 && str[0] == '&' { |
| 2984 |
str = str[1:] |
| 2985 |
// expand variable to get actual tag |
| 2986 |
str = variables[str] |
| 2987 |
} |
| 2988 |
// single object name, no comma or slash |
| 2989 |
tmp := strings.Replace(str, " ", "_", -1) |
| 2990 |
pfx = "<" + tmp + ">" |
| 2991 |
sfx = "</" + tmp + ">" |
| 2992 |
sep = sfx + pfx |
| 2993 |
wrp = true |
| 2994 |
case BKT: |
| 2995 |
// shortcut to wrap elements in bracketed fields |
| 2996 |
if str == "" || str == "-" { |
| 2997 |
sep = "\t" |
| 2998 |
pfx = "" |
| 2999 |
sfx = "" |
| 3000 |
plg = "" |
| 3001 |
elg = "" |
| 3002 |
wrp = false |
| 3003 |
break |
| 3004 |
} |
| 3005 |
// shortcut for strings.HasPrefix(str, "&") and strings.TrimPrefix(str, "&") |
| 3006 |
if len(str) > 1 && str[0] == '&' { |
| 3007 |
str = str[1:] |
| 3008 |
// expand variable to get actual tag |
| 3009 |
str = variables[str] |
| 3010 |
} |
| 3011 |
// single object name, no comma or slash |
| 3012 |
tmp := strings.Replace(str, " ", "_", -1) |
| 3013 |
pfx = "[" + tmp + "=" |
| 3014 |
sfx = "]" |
| 3015 |
sep = "," |
| 3016 |
wrp = true |
| 3017 |
case ENC: |
| 3018 |
// shortcut to mark unexpanded instances with XML tags |
| 3019 |
plg = "" |
| 3020 |
elg = "" |
| 3021 |
// shortcut for strings.HasPrefix(str, "&") and strings.TrimPrefix(str, "&") |
| 3022 |
if len(str) > 1 && str[0] == '&' { |
| 3023 |
str = str[1:] |
| 3024 |
// expand variable to get actual tag |
| 3025 |
str = variables[str] |
| 3026 |
} |
| 3027 |
if str != "" && str != "-" { |
| 3028 |
items := strings.Split(str, "/") |
| 3029 |
for i := range len(items) { |
| 3030 |
tmp := items[i] |
| 3031 |
// replace spaces with underscore |
| 3032 |
tmp = strings.Replace(tmp, " ", "_", -1) |
| 3033 |
plg += "<" + tmp + ">" |
| 3034 |
} |
| 3035 |
for i := len(items) - 1; i >= 0; i-- { |
| 3036 |
tmp := items[i] |
| 3037 |
tmp = strings.Replace(tmp, " ", "_", -1) |
| 3038 |
elg += "</" + tmp + ">" |
| 3039 |
} |
| 3040 |
} |
| 3041 |
case RST: |
| 3042 |
pfx = "" |
| 3043 |
sfx = "" |
| 3044 |
plg = "" |
| 3045 |
elg = "" |
| 3046 |
sep = "\t" |
| 3047 |
def = "" |
| 3048 |
msk = "" |
| 3049 |
wrp = false |
| 3050 |
case DEF: |
| 3051 |
def = str |
| 3052 |
case MSK: |
| 3053 |
msk = str |
| 3054 |
case REG: |
| 3055 |
reg = str |
| 3056 |
case EXP: |
| 3057 |
exp = str |
| 3058 |
case WITH: |
| 3059 |
wth = str |
| 3060 |
case GCODE: |
| 3061 |
gcd = 0 |
| 3062 |
if len(str) > 1 && str[0] == '&' { |
| 3063 |
str = str[1:] |
| 3064 |
// expand variable to get actual tag |
| 3065 |
str = variables[str] |
| 3066 |
} |
| 3067 |
if IsAllDigits(str) { |
| 3068 |
val, err := strconv.Atoi(str) |
| 3069 |
if err == nil { |
| 3070 |
gcd = val |
| 3071 |
} |
| 3072 |
} |
| 3073 |
case FRAME0, FRAME1: |
| 3074 |
frm = 0 |
| 3075 |
if len(str) > 1 && str[0] == '&' { |
| 3076 |
str = str[1:] |
| 3077 |
// expand variable to get actual tag |
| 3078 |
str = variables[str] |
| 3079 |
} |
| 3080 |
if IsAllDigits(str) { |
| 3081 |
val, err := strconv.Atoi(str) |
| 3082 |
if err == nil { |
| 3083 |
if op.Type == FRAME0 && val >= 0 { |
| 3084 |
// already 0-based offset (0-2) |
| 3085 |
frm = val |
| 3086 |
} else if op.Type == FRAME1 && val > 0 { |
| 3087 |
// convert from frame (1-3) to offset (0-2) |
| 3088 |
frm = val - 1 |
| 3089 |
} |
| 3090 |
} |
| 3091 |
} |
| 3092 |
case COLOR: |
| 3093 |
currColor = color.New() |
| 3094 |
if str == "-" || str == "reset" || str == "clear" { |
| 3095 |
plain = true |
| 3096 |
break |
| 3097 |
} |
| 3098 |
plain = false |
| 3099 |
items := strings.Split(str, ",") |
| 3100 |
for _, itm := range items { |
| 3101 |
switch itm { |
| 3102 |
case "red": |
| 3103 |
currColor.Add(color.FgRed) |
| 3104 |
case "grn", "green": |
| 3105 |
currColor.Add(color.FgGreen) |
| 3106 |
case "blu", "blue": |
| 3107 |
currColor.Add(color.FgBlue) |
| 3108 |
case "blk", "black": |
| 3109 |
currColor.Add(color.FgBlack) |
| 3110 |
case "bld", "bold": |
| 3111 |
currColor.Add(color.Bold) |
| 3112 |
case "ital", "italic", "italics": |
| 3113 |
currColor.Add(color.Italic) |
| 3114 |
case "blink", "flash": |
| 3115 |
currColor.Add(color.BlinkSlow) |
| 3116 |
default: |
| 3117 |
DisplayError("Unrecognized color argument '%s'", itm) |
| 3118 |
os.Exit(1) |
| 3119 |
} |
| 3120 |
} |
| 3121 |
case VARIABLE: |
| 3122 |
isAccum = false |
| 3123 |
varname = str |
| 3124 |
case ACCUMULATOR: |
| 3125 |
isAccum = true |
| 3126 |
varname = str |
| 3127 |
case CONVERTER: |
| 3128 |
varname = str |
| 3129 |
case VALUE: |
| 3130 |
length := len(str) |
| 3131 |
if length > 1 && str[0] == '(' && str[length-1] == ')' { |
| 3132 |
// set variable from literal text inside parentheses, e.g., -COM "(, )" |
| 3133 |
variables[varname] = str[1 : length-1] |
| 3134 |
// -if "&VARIABLE" will succeed if set to blank with empty parentheses "()" |
| 3135 |
} else if str == "" { |
| 3136 |
// -if "&VARIABLE" will fail if initialized with empty string "" |
| 3137 |
delete(variables, varname) |
| 3138 |
} else { |
| 3139 |
cls := Clause{Prev: "", Pfx: pfx, Sfx: sfx, Plg: plg, Sep: sep, Def: def, Msk: msk, Reg: reg, Exp: exp, Wth: wth, Gcd: gcd, Frm: frm, Wrp: wrp} |
| 3140 |
txt, ok := processClause(curr, op.Stages, mask, cls, op.Type, index, level, atl) |
| 3141 |
if ok { |
| 3142 |
plg = "" |
| 3143 |
lst = elg |
| 3144 |
if isAccum { |
| 3145 |
if variables[varname] == "" { |
| 3146 |
variables[varname] = txt |
| 3147 |
} else { |
| 3148 |
variables[varname] += sep + txt |
| 3149 |
} |
| 3150 |
} else { |
| 3151 |
variables[varname] = txt |
| 3152 |
} |
| 3153 |
} |
| 3154 |
} |
| 3155 |
varname = "" |
| 3156 |
isAccum = false |
| 3157 |
default: |
| 3158 |
cls := Clause{Prev: tab, Pfx: pfx, Sfx: sfx, Plg: plg, Sep: sep, Def: def, Msk: msk, Reg: reg, Exp: exp, Wth: wth, Gcd: gcd, Frm: frm, Wrp: wrp} |
| 3159 |
txt, ok := processClause(curr, op.Stages, mask, cls, op.Type, index, level, atl) |
| 3160 |
if ok { |
| 3161 |
if varname != "" { |
| 3162 |
// CONVERTER value may have preceding tab, change to space and trim spaces |
| 3163 |
txt = strings.Replace(txt, "\t", " ", -1) |
| 3164 |
txt = strings.TrimSpace(txt) |
| 3165 |
variables[varname] = txt |
| 3166 |
varname = "" |
| 3167 |
} else { |
| 3168 |
plg = "" |
| 3169 |
lst = elg |
| 3170 |
tab = col |
| 3171 |
ret = lin |
| 3172 |
if plain { |
| 3173 |
accum(txt) |
| 3174 |
} else { |
| 3175 |
printInColor(txt) |
| 3176 |
} |
| 3177 |
} |
| 3178 |
} |
| 3179 |
} |
| 3180 |
} |
| 3181 |
|
| 3182 |
if plain { |
| 3183 |
accum(lst) |
| 3184 |
} else { |
| 3185 |
printInColor(lst) |
| 3186 |
} |
| 3187 |
|
| 3188 |
return tab, ret |
| 3189 |
} |
| 3190 |
|
| 3191 |
// CONDITIONAL EXECUTION USES -if AND -unless STATEMENT, WITH SUPPORT FOR DEPRECATED -match AND -avoid STATEMENTS |
| 3192 |
|
| 3193 |
// conditionsAreSatisfied tests a set of conditions to determine if extraction should proceed |
| 3194 |
func conditionsAreSatisfied(conditions []*Operation, curr *XMLNode, mask string, index, level int, variables map[string]string) bool { |
| 3195 |
|
| 3196 |
if curr == nil { |
| 3197 |
return false |
| 3198 |
} |
| 3199 |
|
| 3200 |
required := 0 |
| 3201 |
observed := 0 |
| 3202 |
forbidden := 0 |
| 3203 |
isMatch := false |
| 3204 |
isAvoid := false |
| 3205 |
|
| 3206 |
// matchFound tests individual conditions |
| 3207 |
matchFound := func(stages []*Step) bool { |
| 3208 |
|
| 3209 |
if stages == nil || len(stages) < 1 { |
| 3210 |
return false |
| 3211 |
} |
| 3212 |
|
| 3213 |
stage := stages[0] |
| 3214 |
|
| 3215 |
var constraint *Step |
| 3216 |
|
| 3217 |
if len(stages) > 1 { |
| 3218 |
constraint = stages[1] |
| 3219 |
} |
| 3220 |
|
| 3221 |
status := stage.Type |
| 3222 |
prnt := stage.Parent |
| 3223 |
match := stage.Match |
| 3224 |
attrib := stage.Attrib |
| 3225 |
typL := stage.TypL |
| 3226 |
strL := stage.StrL |
| 3227 |
intL := stage.IntL |
| 3228 |
typR := stage.TypR |
| 3229 |
strR := stage.StrR |
| 3230 |
intR := stage.IntR |
| 3231 |
norm := stage.Norm |
| 3232 |
wildcard := stage.Wild |
| 3233 |
unescape := true |
| 3234 |
|
| 3235 |
found := false |
| 3236 |
number := "" |
| 3237 |
|
| 3238 |
// exploreElements is a wrapper for ExploreElements, obtaining most arguments as closures |
| 3239 |
exploreElements := func(proc func(string, int)) { |
| 3240 |
ExploreElements(curr, mask, prnt, match, attrib, wildcard, unescape, level, proc) |
| 3241 |
} |
| 3242 |
|
| 3243 |
// test string or numeric constraints |
| 3244 |
testConstraint := func(str string) bool { |
| 3245 |
|
| 3246 |
if str == "" || constraint == nil { |
| 3247 |
return false |
| 3248 |
} |
| 3249 |
|
| 3250 |
val := constraint.Value |
| 3251 |
stat := constraint.Type |
| 3252 |
|
| 3253 |
switch stat { |
| 3254 |
case EQUALS, CONTAINS, MIMICS, INCLUDES, EXCLUDES, ISWITHIN, STARTSWITH, ENDSWITH, ISNOT, ISBEFORE, ISAFTER, CONSISTSOF, MATCHES, RESEMBLES: |
| 3255 |
// substring test on element values |
| 3256 |
str = strings.ToUpper(str) |
| 3257 |
val = strings.ToUpper(val) |
| 3258 |
|
| 3259 |
switch stat { |
| 3260 |
case EQUALS: |
| 3261 |
if str == val { |
| 3262 |
return true |
| 3263 |
} |
| 3264 |
case CONTAINS: |
| 3265 |
if strings.Contains(str, val) { |
| 3266 |
return true |
| 3267 |
} |
| 3268 |
case MIMICS: |
| 3269 |
puncToSpace := func(c rune) rune { |
| 3270 |
if !unicode.IsLetter(c) && !unicode.IsDigit(c) { |
| 3271 |
return ' ' |
| 3272 |
} |
| 3273 |
return c |
| 3274 |
} |
| 3275 |
|
| 3276 |
str = strings.Map(puncToSpace, str) |
| 3277 |
val = strings.Map(puncToSpace, val) |
| 3278 |
str = CompressRunsOfSpaces(str) |
| 3279 |
val = CompressRunsOfSpaces(val) |
| 3280 |
if strings.Contains(str, val) { |
| 3281 |
return true |
| 3282 |
} |
| 3283 |
case INCLUDES: |
| 3284 |
str = strings.TrimSpace(str) |
| 3285 |
val = strings.TrimSpace(val) |
| 3286 |
if strings.Contains(" "+str+" ", " "+val+" ") { |
| 3287 |
return true |
| 3288 |
} |
| 3289 |
case EXCLUDES: |
| 3290 |
if !strings.Contains(str, val) { |
| 3291 |
return true |
| 3292 |
} |
| 3293 |
case ISWITHIN: |
| 3294 |
if strings.Contains(val, str) { |
| 3295 |
return true |
| 3296 |
} |
| 3297 |
case STARTSWITH: |
| 3298 |
if strings.HasPrefix(str, val) { |
| 3299 |
return true |
| 3300 |
} |
| 3301 |
case ENDSWITH: |
| 3302 |
if strings.HasSuffix(str, val) { |
| 3303 |
return true |
| 3304 |
} |
| 3305 |
case ISNOT: |
| 3306 |
if str != val { |
| 3307 |
return true |
| 3308 |
} |
| 3309 |
case ISBEFORE: |
| 3310 |
if str < val { |
| 3311 |
return true |
| 3312 |
} |
| 3313 |
case ISAFTER: |
| 3314 |
if str > val { |
| 3315 |
return true |
| 3316 |
} |
| 3317 |
case CONSISTSOF: |
| 3318 |
for _, ch := range str { |
| 3319 |
if !strings.Contains(val, string(ch)) { |
| 3320 |
return false |
| 3321 |
} |
| 3322 |
} |
| 3323 |
return true |
| 3324 |
case MATCHES: |
| 3325 |
str = TransformAccents(str, false, false) |
| 3326 |
val = TransformAccents(val, false, false) |
| 3327 |
str = RemoveCommaOrSemicolon(str) |
| 3328 |
val = RemoveCommaOrSemicolon(val) |
| 3329 |
if strings.ToLower(str) == strings.ToLower(val) { |
| 3330 |
return true |
| 3331 |
} |
| 3332 |
case RESEMBLES: |
| 3333 |
str = SortStringByWords(str) |
| 3334 |
val = SortStringByWords(val) |
| 3335 |
if strings.ToLower(str) == strings.ToLower(val) { |
| 3336 |
return true |
| 3337 |
} |
| 3338 |
default: |
| 3339 |
} |
| 3340 |
case ISEQUALTO, DIFFERSFROM: |
| 3341 |
// conditional argument is element specifier |
| 3342 |
if constraint.Parent != "" || constraint.Match != "" || constraint.Attrib != "" { |
| 3343 |
ch := val[0] |
| 3344 |
// pound, percent, and caret prefixes supported (undocumented) |
| 3345 |
switch ch { |
| 3346 |
case '#': |
| 3347 |
count := 0 |
| 3348 |
ExploreElements(curr, mask, constraint.Parent, constraint.Match, constraint.Attrib, constraint.Wild, true, level, func(stn string, lvl int) { |
| 3349 |
count++ |
| 3350 |
}) |
| 3351 |
val = strconv.Itoa(count) |
| 3352 |
case '%': |
| 3353 |
length := 0 |
| 3354 |
ExploreElements(curr, mask, constraint.Parent, constraint.Match, constraint.Attrib, constraint.Wild, true, level, func(stn string, lvl int) { |
| 3355 |
if stn != "" { |
| 3356 |
length += len(stn) |
| 3357 |
} |
| 3358 |
}) |
| 3359 |
val = strconv.Itoa(length) |
| 3360 |
case '^': |
| 3361 |
depth := 0 |
| 3362 |
ExploreElements(curr, mask, constraint.Parent, constraint.Match, constraint.Attrib, constraint.Wild, true, level, func(stn string, lvl int) { |
| 3363 |
depth = lvl |
| 3364 |
}) |
| 3365 |
val = strconv.Itoa(depth) |
| 3366 |
default: |
| 3367 |
ExploreElements(curr, mask, constraint.Parent, constraint.Match, constraint.Attrib, constraint.Wild, true, level, func(stn string, lvl int) { |
| 3368 |
if stn != "" { |
| 3369 |
val = stn |
| 3370 |
} |
| 3371 |
}) |
| 3372 |
} |
| 3373 |
} |
| 3374 |
str = strings.ToUpper(str) |
| 3375 |
val = strings.ToUpper(val) |
| 3376 |
|
| 3377 |
switch stat { |
| 3378 |
case ISEQUALTO: |
| 3379 |
if str == val { |
| 3380 |
return true |
| 3381 |
} |
| 3382 |
case DIFFERSFROM: |
| 3383 |
if str != val { |
| 3384 |
return true |
| 3385 |
} |
| 3386 |
default: |
| 3387 |
} |
| 3388 |
case GT, GE, LT, LE, EQ, NE: |
| 3389 |
// second argument of numeric test can be element specifier |
| 3390 |
if constraint.Parent != "" || constraint.Match != "" || constraint.Attrib != "" { |
| 3391 |
ch := val[0] |
| 3392 |
// pound, percent, and caret prefixes supported as potentially useful for data QA (undocumented) |
| 3393 |
switch ch { |
| 3394 |
case '#': |
| 3395 |
count := 0 |
| 3396 |
ExploreElements(curr, mask, constraint.Parent, constraint.Match, constraint.Attrib, constraint.Wild, true, level, func(stn string, lvl int) { |
| 3397 |
count++ |
| 3398 |
}) |
| 3399 |
val = strconv.Itoa(count) |
| 3400 |
case '%': |
| 3401 |
length := 0 |
| 3402 |
ExploreElements(curr, mask, constraint.Parent, constraint.Match, constraint.Attrib, constraint.Wild, true, level, func(stn string, lvl int) { |
| 3403 |
if stn != "" { |
| 3404 |
length += len(stn) |
| 3405 |
} |
| 3406 |
}) |
| 3407 |
val = strconv.Itoa(length) |
| 3408 |
case '^': |
| 3409 |
depth := 0 |
| 3410 |
ExploreElements(curr, mask, constraint.Parent, constraint.Match, constraint.Attrib, constraint.Wild, true, level, func(stn string, lvl int) { |
| 3411 |
depth = lvl |
| 3412 |
}) |
| 3413 |
val = strconv.Itoa(depth) |
| 3414 |
case '&': |
| 3415 |
if len(val) > 1 { |
| 3416 |
val = val[1:] |
| 3417 |
// expand variable to get actual tag |
| 3418 |
val = variables[val] |
| 3419 |
} |
| 3420 |
default: |
| 3421 |
ExploreElements(curr, mask, constraint.Parent, constraint.Match, constraint.Attrib, constraint.Wild, true, level, func(stn string, lvl int) { |
| 3422 |
if stn != "" { |
| 3423 |
_, errz := strconv.Atoi(stn) |
| 3424 |
if errz == nil { |
| 3425 |
val = stn |
| 3426 |
} |
| 3427 |
} |
| 3428 |
}) |
| 3429 |
} |
| 3430 |
} |
| 3431 |
|
| 3432 |
// numeric tests on element values |
| 3433 |
x, errx := strconv.Atoi(str) |
| 3434 |
y, erry := strconv.Atoi(val) |
| 3435 |
|
| 3436 |
// both arguments must resolve to integers |
| 3437 |
if errx != nil || erry != nil { |
| 3438 |
return false |
| 3439 |
} |
| 3440 |
|
| 3441 |
switch stat { |
| 3442 |
case GT: |
| 3443 |
if x > y { |
| 3444 |
return true |
| 3445 |
} |
| 3446 |
case GE: |
| 3447 |
if x >= y { |
| 3448 |
return true |
| 3449 |
} |
| 3450 |
case LT: |
| 3451 |
if x < y { |
| 3452 |
return true |
| 3453 |
} |
| 3454 |
case LE: |
| 3455 |
if x <= y { |
| 3456 |
return true |
| 3457 |
} |
| 3458 |
case EQ: |
| 3459 |
if x == y { |
| 3460 |
return true |
| 3461 |
} |
| 3462 |
case NE: |
| 3463 |
if x != y { |
| 3464 |
return true |
| 3465 |
} |
| 3466 |
default: |
| 3467 |
} |
| 3468 |
default: |
| 3469 |
} |
| 3470 |
|
| 3471 |
return false |
| 3472 |
} |
| 3473 |
|
| 3474 |
// checkConstraint applies optional [min:max] range restriction and sends result to testConstraint |
| 3475 |
checkConstraint := func(str string) bool { |
| 3476 |
|
| 3477 |
// handle usual situation with no range first |
| 3478 |
if norm { |
| 3479 |
return testConstraint(str) |
| 3480 |
} |
| 3481 |
|
| 3482 |
// check for [after|before] variant |
| 3483 |
if typL == STRINGRANGE || typR == STRINGRANGE { |
| 3484 |
if strL != "" { |
| 3485 |
// use case-insensitive test |
| 3486 |
strL = strings.ToUpper(strL) |
| 3487 |
idx := strings.Index(strings.ToUpper(str), strL) |
| 3488 |
if idx < 0 { |
| 3489 |
// specified substring must be present in original string |
| 3490 |
return false |
| 3491 |
} |
| 3492 |
ln := len(strL) |
| 3493 |
// remove leading text |
| 3494 |
str = str[idx+ln:] |
| 3495 |
} |
| 3496 |
if strR != "" { |
| 3497 |
strR = strings.ToUpper(strR) |
| 3498 |
idx := strings.Index(strings.ToUpper(str), strR) |
| 3499 |
if idx < 0 { |
| 3500 |
// specified substring must be present in remaining string |
| 3501 |
return false |
| 3502 |
} |
| 3503 |
// remove trailing text |
| 3504 |
str = str[:idx] |
| 3505 |
} |
| 3506 |
if str != "" { |
| 3507 |
return testConstraint(str) |
| 3508 |
} |
| 3509 |
return false |
| 3510 |
} |
| 3511 |
|
| 3512 |
min := 0 |
| 3513 |
max := 0 |
| 3514 |
|
| 3515 |
// slice arguments use variable value +- adjustment or integer constant |
| 3516 |
if typL == VARIABLERANGE { |
| 3517 |
if strL == "" { |
| 3518 |
return false |
| 3519 |
} |
| 3520 |
lft, ok := variables[strL] |
| 3521 |
if !ok { |
| 3522 |
return false |
| 3523 |
} |
| 3524 |
val, err := strconv.Atoi(lft) |
| 3525 |
if err != nil { |
| 3526 |
return false |
| 3527 |
} |
| 3528 |
// range argument values are inclusive and 1-based, decrement variable start +- offset to use in slice |
| 3529 |
min = val + intL - 1 |
| 3530 |
} else if typL == INTEGERRANGE { |
| 3531 |
// range argument values are inclusive and 1-based, decrement literal start to use in slice |
| 3532 |
min = intL - 1 |
| 3533 |
} |
| 3534 |
if typR == VARIABLERANGE { |
| 3535 |
if strR == "" { |
| 3536 |
return false |
| 3537 |
} |
| 3538 |
rgt, ok := variables[strR] |
| 3539 |
if !ok { |
| 3540 |
return false |
| 3541 |
} |
| 3542 |
val, err := strconv.Atoi(rgt) |
| 3543 |
if err != nil { |
| 3544 |
return false |
| 3545 |
} |
| 3546 |
if val+intR < 0 { |
| 3547 |
// negative value is 1-based inset from end of string (undocumented) |
| 3548 |
max = len(str) + val + intR + 1 |
| 3549 |
} else { |
| 3550 |
max = val + intR |
| 3551 |
} |
| 3552 |
} else if typR == INTEGERRANGE { |
| 3553 |
if intR < 0 { |
| 3554 |
// negative max is inset from end of string (undocumented) |
| 3555 |
max = len(str) + intR + 1 |
| 3556 |
} else { |
| 3557 |
max = intR |
| 3558 |
} |
| 3559 |
} |
| 3560 |
|
| 3561 |
// numeric range now calculated, apply slice to string |
| 3562 |
if min == 0 && max == 0 { |
| 3563 |
return testConstraint(str) |
| 3564 |
} else if max == 0 { |
| 3565 |
if min > 0 && min < len(str) { |
| 3566 |
str = str[min:] |
| 3567 |
if str != "" { |
| 3568 |
return testConstraint(str) |
| 3569 |
} |
| 3570 |
} |
| 3571 |
} else if min == 0 { |
| 3572 |
if max > 0 && max <= len(str) { |
| 3573 |
str = str[:max] |
| 3574 |
if str != "" { |
| 3575 |
return testConstraint(str) |
| 3576 |
} |
| 3577 |
} |
| 3578 |
} else { |
| 3579 |
if min < max && min > 0 && max <= len(str) { |
| 3580 |
str = str[min:max] |
| 3581 |
if str != "" { |
| 3582 |
return testConstraint(str) |
| 3583 |
} |
| 3584 |
} |
| 3585 |
} |
| 3586 |
|
| 3587 |
return false |
| 3588 |
} |
| 3589 |
|
| 3590 |
switch status { |
| 3591 |
case ELEMENT: |
| 3592 |
exploreElements(func(str string, lvl int) { |
| 3593 |
// match to XML container object sends empty string, so do not check for str != "" here |
| 3594 |
// test every selected element individually if value is specified |
| 3595 |
if constraint == nil || checkConstraint(str) { |
| 3596 |
found = true |
| 3597 |
} |
| 3598 |
}) |
| 3599 |
case VARIABLE: |
| 3600 |
// use value of stored variable |
| 3601 |
str, ok := variables[match] |
| 3602 |
if ok { |
| 3603 |
// -if &VARIABLE -equals VALUE is the supported construct |
| 3604 |
if constraint == nil || checkConstraint(str) { |
| 3605 |
found = true |
| 3606 |
} |
| 3607 |
} |
| 3608 |
case COUNT: |
| 3609 |
count := 0 |
| 3610 |
|
| 3611 |
exploreElements(func(str string, lvl int) { |
| 3612 |
count++ |
| 3613 |
found = true |
| 3614 |
}) |
| 3615 |
|
| 3616 |
// number of element objects |
| 3617 |
number = strconv.Itoa(count) |
| 3618 |
case LENGTH: |
| 3619 |
length := 0 |
| 3620 |
|
| 3621 |
exploreElements(func(str string, lvl int) { |
| 3622 |
length += len(str) |
| 3623 |
found = true |
| 3624 |
}) |
| 3625 |
|
| 3626 |
// length of element strings |
| 3627 |
number = strconv.Itoa(length) |
| 3628 |
case DEPTH: |
| 3629 |
depth := 0 |
| 3630 |
|
| 3631 |
exploreElements(func(str string, lvl int) { |
| 3632 |
depth = lvl |
| 3633 |
found = true |
| 3634 |
}) |
| 3635 |
|
| 3636 |
// depth of last element in scope |
| 3637 |
number = strconv.Itoa(depth) |
| 3638 |
case INDEX: |
| 3639 |
// index of explored parent object |
| 3640 |
number = strconv.Itoa(index) |
| 3641 |
found = true |
| 3642 |
default: |
| 3643 |
} |
| 3644 |
|
| 3645 |
if number == "" { |
| 3646 |
return found |
| 3647 |
} |
| 3648 |
|
| 3649 |
if constraint == nil || checkConstraint(number) { |
| 3650 |
return true |
| 3651 |
} |
| 3652 |
|
| 3653 |
return false |
| 3654 |
} |
| 3655 |
|
| 3656 |
// test conditional arguments |
| 3657 |
for _, op := range conditions { |
| 3658 |
|
| 3659 |
switch op.Type { |
| 3660 |
// -if tests for presence of element (deprecated -match can test element:value) |
| 3661 |
case SELECT, IF, MATCH: |
| 3662 |
// checking for failure here allows for multiple -if [ -and / -or ] clauses |
| 3663 |
if isMatch && observed < required { |
| 3664 |
return false |
| 3665 |
} |
| 3666 |
if isAvoid && forbidden > 0 { |
| 3667 |
return false |
| 3668 |
} |
| 3669 |
required = 0 |
| 3670 |
observed = 0 |
| 3671 |
forbidden = 0 |
| 3672 |
isMatch = true |
| 3673 |
isAvoid = false |
| 3674 |
// continue on to next two cases |
| 3675 |
fallthrough |
| 3676 |
case AND: |
| 3677 |
required++ |
| 3678 |
// continue on to next case |
| 3679 |
fallthrough |
| 3680 |
case OR: |
| 3681 |
if matchFound(op.Stages) { |
| 3682 |
observed++ |
| 3683 |
// record presence of forbidden element if in -unless clause |
| 3684 |
forbidden++ |
| 3685 |
} |
| 3686 |
// -unless tests for absence of element, or presence but with failure of subsequent value test (deprecated -avoid can test element:value) |
| 3687 |
case UNLESS, AVOID: |
| 3688 |
if isMatch && observed < required { |
| 3689 |
return false |
| 3690 |
} |
| 3691 |
if isAvoid && forbidden > 0 { |
| 3692 |
return false |
| 3693 |
} |
| 3694 |
required = 0 |
| 3695 |
observed = 0 |
| 3696 |
forbidden = 0 |
| 3697 |
isMatch = false |
| 3698 |
isAvoid = true |
| 3699 |
if matchFound(op.Stages) { |
| 3700 |
forbidden++ |
| 3701 |
} |
| 3702 |
default: |
| 3703 |
} |
| 3704 |
} |
| 3705 |
|
| 3706 |
if isMatch && observed < required { |
| 3707 |
return false |
| 3708 |
} |
| 3709 |
if isAvoid && forbidden > 0 { |
| 3710 |
return false |
| 3711 |
} |
| 3712 |
|
| 3713 |
return true |
| 3714 |
} |
| 3715 |
|
| 3716 |
// RECURSIVELY PROCESS EXPLORATION COMMANDS AND XML DATA STRUCTURE |
| 3717 |
|
| 3718 |
// processCommands visits XML nodes, performs conditional tests, and executes data extraction instructions |
| 3719 |
func processCommands( |
| 3720 |
cmds *Block, |
| 3721 |
curr *XMLNode, |
| 3722 |
tab string, |
| 3723 |
ret string, |
| 3724 |
index int, |
| 3725 |
level int, |
| 3726 |
atl Atlas, |
| 3727 |
accum func(string), |
| 3728 |
) (string, string) { |
| 3729 |
|
| 3730 |
if accum == nil { |
| 3731 |
return tab, ret |
| 3732 |
} |
| 3733 |
|
| 3734 |
prnt := cmds.Parent |
| 3735 |
match := cmds.Match |
| 3736 |
|
| 3737 |
variables := atl.Variables |
| 3738 |
|
| 3739 |
// allow path component to be specified by a variable with ampersand prefix (undocumented) |
| 3740 |
if len(match) > 1 && match[0] == '&' { |
| 3741 |
str := match[1:] |
| 3742 |
val, ok := variables[str] |
| 3743 |
if ok { |
| 3744 |
match = val |
| 3745 |
} |
| 3746 |
} |
| 3747 |
|
| 3748 |
// closure passes local variables to callback, which can modify caller tab and ret values |
| 3749 |
processNode := func(node *XMLNode, idx, lvl int) { |
| 3750 |
|
| 3751 |
// apply -if or -unless tests |
| 3752 |
if conditionsAreSatisfied(cmds.Conditions, node, match, idx, lvl, variables) { |
| 3753 |
|
| 3754 |
// execute data extraction commands |
| 3755 |
if len(cmds.Commands) > 0 { |
| 3756 |
tab, ret = processInstructions(cmds.Commands, node, match, tab, ret, idx, lvl, atl, accum) |
| 3757 |
} |
| 3758 |
|
| 3759 |
// process sub commands on child node |
| 3760 |
for _, sub := range cmds.Subtasks { |
| 3761 |
tab, ret = processCommands(sub, node, tab, ret, 1, lvl, atl, accum) |
| 3762 |
} |
| 3763 |
|
| 3764 |
} else { |
| 3765 |
|
| 3766 |
// execute commands after -else statement |
| 3767 |
if len(cmds.Failure) > 0 { |
| 3768 |
tab, ret = processInstructions(cmds.Failure, node, match, tab, ret, idx, lvl, atl, accum) |
| 3769 |
} |
| 3770 |
} |
| 3771 |
} |
| 3772 |
|
| 3773 |
// explorePath recursive definition |
| 3774 |
var explorePath func(*XMLNode, []string, int, int, func(*XMLNode, int, int)) int |
| 3775 |
|
| 3776 |
// explorePath visits child nodes and matches against next entry in path |
| 3777 |
explorePath = func(curr *XMLNode, path []string, indx, levl int, proc func(*XMLNode, int, int)) int { |
| 3778 |
|
| 3779 |
if curr == nil || proc == nil { |
| 3780 |
return indx |
| 3781 |
} |
| 3782 |
|
| 3783 |
if len(path) < 1 { |
| 3784 |
proc(curr, indx, levl) |
| 3785 |
indx++ |
| 3786 |
return indx |
| 3787 |
} |
| 3788 |
|
| 3789 |
name := path[0] |
| 3790 |
rest := path[1:] |
| 3791 |
|
| 3792 |
// allow path component to be specified by a variable with ampersand prefix (undocumented) |
| 3793 |
if len(name) > 1 && name[0] == '&' { |
| 3794 |
str := name[1:] |
| 3795 |
val, ok := variables[str] |
| 3796 |
if ok { |
| 3797 |
name = val |
| 3798 |
} |
| 3799 |
} |
| 3800 |
|
| 3801 |
// explore next level of child nodes |
| 3802 |
for chld := curr.Children; chld != nil; chld = chld.Next { |
| 3803 |
if chld.Name == name { |
| 3804 |
// recurse only if child matches next component in path |
| 3805 |
indx = explorePath(chld, rest, indx, levl+1, proc) |
| 3806 |
} |
| 3807 |
} |
| 3808 |
|
| 3809 |
return indx |
| 3810 |
} |
| 3811 |
|
| 3812 |
if cmds.Foreword != "" { |
| 3813 |
accum(cmds.Foreword) |
| 3814 |
} |
| 3815 |
|
| 3816 |
// apply -position test |
| 3817 |
|
| 3818 |
if cmds.Position == "" || cmds.Position == "all" { |
| 3819 |
|
| 3820 |
ExploreNodes(curr, prnt, match, index, level, processNode) |
| 3821 |
|
| 3822 |
} else if cmds.Position == "path" { |
| 3823 |
|
| 3824 |
ExploreNodes(curr, prnt, match, index, level, |
| 3825 |
func(node *XMLNode, idx, lvl int) { |
| 3826 |
// exploreNodes callback has matched first path component, now explore remainder one level and component at a time |
| 3827 |
explorePath(node, cmds.Path, idx, lvl, processNode) |
| 3828 |
}) |
| 3829 |
|
| 3830 |
} else { |
| 3831 |
|
| 3832 |
var single *XMLNode |
| 3833 |
lev := 0 |
| 3834 |
ind := 0 |
| 3835 |
|
| 3836 |
if cmds.Position == "first" { |
| 3837 |
|
| 3838 |
ExploreNodes(curr, prnt, match, index, level, |
| 3839 |
func(node *XMLNode, idx, lvl int) { |
| 3840 |
if single == nil { |
| 3841 |
single = node |
| 3842 |
ind = idx |
| 3843 |
lev = lvl |
| 3844 |
} |
| 3845 |
}) |
| 3846 |
|
| 3847 |
} else if cmds.Position == "last" { |
| 3848 |
|
| 3849 |
ExploreNodes(curr, prnt, match, index, level, |
| 3850 |
func(node *XMLNode, idx, lvl int) { |
| 3851 |
single = node |
| 3852 |
ind = idx |
| 3853 |
lev = lvl |
| 3854 |
}) |
| 3855 |
|
| 3856 |
} else if cmds.Position == "outer" { |
| 3857 |
|
| 3858 |
// print only first and last nodes |
| 3859 |
var beg *Limiter |
| 3860 |
var end *Limiter |
| 3861 |
|
| 3862 |
ExploreNodes(curr, prnt, match, index, level, |
| 3863 |
func(node *XMLNode, idx, lvl int) { |
| 3864 |
if beg == nil { |
| 3865 |
beg = &Limiter{Obj: node, Idx: idx, Lvl: lvl} |
| 3866 |
} else { |
| 3867 |
end = &Limiter{Obj: node, Idx: idx, Lvl: lvl} |
| 3868 |
} |
| 3869 |
}) |
| 3870 |
|
| 3871 |
if beg != nil { |
| 3872 |
processNode(beg.Obj, beg.Idx, beg.Lvl) |
| 3873 |
} |
| 3874 |
if end != nil { |
| 3875 |
processNode(end.Obj, end.Idx, end.Lvl) |
| 3876 |
} |
| 3877 |
|
| 3878 |
} else if cmds.Position == "inner" { |
| 3879 |
|
| 3880 |
// print all but first and last nodes |
| 3881 |
var prev *Limiter |
| 3882 |
var next *Limiter |
| 3883 |
first := true |
| 3884 |
|
| 3885 |
ExploreNodes(curr, prnt, match, index, level, |
| 3886 |
func(node *XMLNode, idx, lvl int) { |
| 3887 |
if first { |
| 3888 |
first = false |
| 3889 |
return |
| 3890 |
} |
| 3891 |
|
| 3892 |
prev = next |
| 3893 |
next = &Limiter{Obj: node, Idx: idx, Lvl: lvl} |
| 3894 |
|
| 3895 |
if prev != nil { |
| 3896 |
processNode(prev.Obj, prev.Idx, prev.Lvl) |
| 3897 |
} |
| 3898 |
}) |
| 3899 |
|
| 3900 |
} else if cmds.Position == "even" { |
| 3901 |
|
| 3902 |
even := false |
| 3903 |
|
| 3904 |
ExploreNodes(curr, prnt, match, index, level, |
| 3905 |
func(node *XMLNode, idx, lvl int) { |
| 3906 |
if even { |
| 3907 |
processNode(node, idx, lvl) |
| 3908 |
} |
| 3909 |
even = !even |
| 3910 |
}) |
| 3911 |
|
| 3912 |
} else if cmds.Position == "odd" { |
| 3913 |
|
| 3914 |
odd := true |
| 3915 |
|
| 3916 |
ExploreNodes(curr, prnt, match, index, level, |
| 3917 |
func(node *XMLNode, idx, lvl int) { |
| 3918 |
if odd { |
| 3919 |
processNode(node, idx, lvl) |
| 3920 |
} |
| 3921 |
odd = !odd |
| 3922 |
}) |
| 3923 |
|
| 3924 |
} else { |
| 3925 |
|
| 3926 |
// use numeric position |
| 3927 |
number, err := strconv.Atoi(cmds.Position) |
| 3928 |
if err == nil { |
| 3929 |
|
| 3930 |
pos := 0 |
| 3931 |
|
| 3932 |
ExploreNodes(curr, prnt, match, index, level, |
| 3933 |
func(node *XMLNode, idx, lvl int) { |
| 3934 |
pos++ |
| 3935 |
if pos == number { |
| 3936 |
single = node |
| 3937 |
ind = idx |
| 3938 |
lev = lvl |
| 3939 |
} |
| 3940 |
}) |
| 3941 |
|
| 3942 |
} else { |
| 3943 |
|
| 3944 |
DisplayError("Unrecognized position '%s'", cmds.Position) |
| 3945 |
os.Exit(1) |
| 3946 |
} |
| 3947 |
} |
| 3948 |
|
| 3949 |
if single != nil { |
| 3950 |
processNode(single, ind, lev) |
| 3951 |
} |
| 3952 |
} |
| 3953 |
|
| 3954 |
if cmds.Afterword != "" { |
| 3955 |
accum(cmds.Afterword) |
| 3956 |
} |
| 3957 |
|
| 3958 |
return tab, ret |
| 3959 |
} |
| 3960 |
|
| 3961 |
// PROCESS ONE XML COMPONENT RECORD |
| 3962 |
|
| 3963 |
// ProcessExtract perform data extraction driven by command-line arguments |
| 3964 |
func ProcessExtract(text, parent string, index int, hd, tl string, transform map[string]string, srchr *FSMSearcher, histogram map[string]int, cmds *Block) string { |
| 3965 |
|
| 3966 |
if text == "" || cmds == nil { |
| 3967 |
return "" |
| 3968 |
} |
| 3969 |
|
| 3970 |
// exit from function will collect garbage of node structure for current XML object |
| 3971 |
pat := ParseRecord(text, parent) |
| 3972 |
|
| 3973 |
if pat == nil { |
| 3974 |
return "" |
| 3975 |
} |
| 3976 |
|
| 3977 |
// exit from function will also free map of recorded variables for current -pattern |
| 3978 |
variables := make(map[string]string) |
| 3979 |
|
| 3980 |
var buffer strings.Builder |
| 3981 |
|
| 3982 |
ok := false |
| 3983 |
|
| 3984 |
if hd != "" { |
| 3985 |
buffer.WriteString(hd) |
| 3986 |
} |
| 3987 |
|
| 3988 |
ret := "" |
| 3989 |
|
| 3990 |
if cmds.Position == "select" { |
| 3991 |
|
| 3992 |
if conditionsAreSatisfied(cmds.Conditions, pat, cmds.Match, index, 1, variables) { |
| 3993 |
ok = true |
| 3994 |
buffer.WriteString(text) |
| 3995 |
ret = "\n" |
| 3996 |
} |
| 3997 |
|
| 3998 |
} else { |
| 3999 |
|
| 4000 |
// start processing at top of command tree and top of XML subregion selected by -pattern |
| 4001 |
atl := Atlas{Variables: variables, Transform: transform, Srchr: srchr, Histogram: histogram} |
| 4002 |
_, ret = processCommands(cmds, pat, "", "", index, 1, atl, |
| 4003 |
func(str string) { |
| 4004 |
if str != "" { |
| 4005 |
ok = true |
| 4006 |
buffer.WriteString(str) |
| 4007 |
} |
| 4008 |
}) |
| 4009 |
} |
| 4010 |
|
| 4011 |
if tl != "" { |
| 4012 |
buffer.WriteString(tl) |
| 4013 |
} |
| 4014 |
|
| 4015 |
if ret != "" { |
| 4016 |
ok = true |
| 4017 |
buffer.WriteString(ret) |
| 4018 |
} |
| 4019 |
|
| 4020 |
txt := buffer.String() |
| 4021 |
|
| 4022 |
// remove leading newline (-insd -pfx artifact) |
| 4023 |
if txt != "" && txt[0] == '\n' { |
| 4024 |
txt = txt[1:] |
| 4025 |
} |
| 4026 |
|
| 4027 |
if !ok { |
| 4028 |
return "" |
| 4029 |
} |
| 4030 |
|
| 4031 |
// return consolidated result string |
| 4032 |
return txt |
| 4033 |
} |
| 4034 |
|
| 4035 |
// XMLtoData applies xtract logic to XML string |
| 4036 |
func XMLtoData(xml string, args []string) string { |
| 4037 |
|
| 4038 |
if xml == "" { |
| 4039 |
return "" |
| 4040 |
} |
| 4041 |
|
| 4042 |
if len(args) < 1 { |
| 4043 |
DisplayError("Insufficient command-line arguments supplied to xtract") |
| 4044 |
os.Exit(1) |
| 4045 |
} |
| 4046 |
|
| 4047 |
head := "" |
| 4048 |
tail := "" |
| 4049 |
|
| 4050 |
hd := "" |
| 4051 |
tl := "" |
| 4052 |
|
| 4053 |
inSwitch := false |
| 4054 |
|
| 4055 |
for { |
| 4056 |
|
| 4057 |
inSwitch = true |
| 4058 |
|
| 4059 |
switch args[0] { |
| 4060 |
case "-head": |
| 4061 |
if len(args) < 2 { |
| 4062 |
DisplayError("Pattern missing after -head command") |
| 4063 |
os.Exit(1) |
| 4064 |
} |
| 4065 |
head = ConvertSlash(args[1]) |
| 4066 |
// allow splitting of -head argument, keep appending until next command (undocumented) |
| 4067 |
ofs, nxt := 0, args[2:] |
| 4068 |
for { |
| 4069 |
if len(nxt) < 1 { |
| 4070 |
break |
| 4071 |
} |
| 4072 |
tmp := nxt[0] |
| 4073 |
if strings.HasPrefix(tmp, "-") { |
| 4074 |
break |
| 4075 |
} |
| 4076 |
ofs++ |
| 4077 |
txt := ConvertSlash(tmp) |
| 4078 |
if head != "" && !strings.HasSuffix(head, "\t") { |
| 4079 |
head += "\t" |
| 4080 |
} |
| 4081 |
head += txt |
| 4082 |
nxt = nxt[1:] |
| 4083 |
} |
| 4084 |
if ofs > 0 { |
| 4085 |
args = args[ofs:] |
| 4086 |
} |
| 4087 |
case "-tail": |
| 4088 |
if len(args) < 2 { |
| 4089 |
DisplayError("Pattern missing after -tail command") |
| 4090 |
os.Exit(1) |
| 4091 |
} |
| 4092 |
tail = ConvertSlash(args[1]) |
| 4093 |
case "-hd": |
| 4094 |
if len(args) < 2 { |
| 4095 |
DisplayError("Pattern missing after -hd command") |
| 4096 |
os.Exit(1) |
| 4097 |
} |
| 4098 |
hd = ConvertSlash(args[1]) |
| 4099 |
case "-tl": |
| 4100 |
if len(args) < 2 { |
| 4101 |
DisplayError("Pattern missing after -tl command") |
| 4102 |
os.Exit(1) |
| 4103 |
} |
| 4104 |
tl = ConvertSlash(args[1]) |
| 4105 |
case "-wrp": |
| 4106 |
// shortcut to wrap records in XML tags |
| 4107 |
if len(args) < 2 { |
| 4108 |
DisplayError("Pattern missing after -wrp command") |
| 4109 |
os.Exit(1) |
| 4110 |
} |
| 4111 |
tmp := ConvertSlash(args[1]) |
| 4112 |
lft, rgt := SplitInTwoLeft(tmp, ",") |
| 4113 |
if lft != "" { |
| 4114 |
head = "<" + lft + ">" |
| 4115 |
tail = "</" + lft + ">" |
| 4116 |
} |
| 4117 |
if rgt != "" { |
| 4118 |
hd = "<" + rgt + ">" |
| 4119 |
tl = "</" + rgt + ">" |
| 4120 |
} |
| 4121 |
case "-set": |
| 4122 |
if len(args) < 2 { |
| 4123 |
DisplayError("Pattern missing after -set command") |
| 4124 |
os.Exit(1) |
| 4125 |
} |
| 4126 |
tmp := ConvertSlash(args[1]) |
| 4127 |
if tmp != "" { |
| 4128 |
head = "<" + tmp + ">" |
| 4129 |
tail = "</" + tmp + ">" |
| 4130 |
} |
| 4131 |
case "-rec": |
| 4132 |
if len(args) < 2 { |
| 4133 |
DisplayError("Pattern missing after -rec command") |
| 4134 |
os.Exit(1) |
| 4135 |
} |
| 4136 |
tmp := ConvertSlash(args[1]) |
| 4137 |
if tmp != "" { |
| 4138 |
hd = "<" + tmp + ">" |
| 4139 |
tl = "</" + tmp + ">" |
| 4140 |
} |
| 4141 |
default: |
| 4142 |
// if not any of the controls, set flag to break out of for loop |
| 4143 |
inSwitch = false |
| 4144 |
} |
| 4145 |
|
| 4146 |
if !inSwitch { |
| 4147 |
break |
| 4148 |
} |
| 4149 |
|
| 4150 |
// skip past arguments |
| 4151 |
args = args[2:] |
| 4152 |
|
| 4153 |
if len(args) < 1 { |
| 4154 |
DisplayError("Insufficient command-line arguments supplied to xtract") |
| 4155 |
os.Exit(1) |
| 4156 |
} |
| 4157 |
} |
| 4158 |
|
| 4159 |
// allow -record as synonym of -pattern (undocumented) |
| 4160 |
if args[0] == "-record" || args[0] == "-Record" { |
| 4161 |
args[0] = "-pattern" |
| 4162 |
} |
| 4163 |
|
| 4164 |
// make sure top-level -pattern command is next |
| 4165 |
if args[0] != "-pattern" && args[0] != "-Pattern" { |
| 4166 |
DisplayError("No -pattern in command-line arguments") |
| 4167 |
os.Exit(1) |
| 4168 |
} |
| 4169 |
if len(args) < 2 { |
| 4170 |
DisplayError("Item missing after -pattern command") |
| 4171 |
os.Exit(1) |
| 4172 |
} |
| 4173 |
|
| 4174 |
topPat := args[1] |
| 4175 |
if topPat == "" { |
| 4176 |
DisplayError("Item missing after -pattern command") |
| 4177 |
os.Exit(1) |
| 4178 |
} |
| 4179 |
if strings.HasPrefix(topPat, "-") { |
| 4180 |
DisplayError("Misplaced %s command", topPat) |
| 4181 |
os.Exit(1) |
| 4182 |
} |
| 4183 |
|
| 4184 |
// look for -pattern Parent/* construct for heterogeneous data, e.g., -pattern PubmedArticleSet/* |
| 4185 |
topPattern, star := SplitInTwoLeft(topPat, "/") |
| 4186 |
if topPattern == "" { |
| 4187 |
return "" |
| 4188 |
} |
| 4189 |
|
| 4190 |
parent := "" |
| 4191 |
if star == "*" { |
| 4192 |
parent = topPattern |
| 4193 |
} else if star != "" { |
| 4194 |
DisplayError("-pattern Parent/Child construct is not supported") |
| 4195 |
os.Exit(1) |
| 4196 |
} |
| 4197 |
|
| 4198 |
turbo := false |
| 4199 |
|
| 4200 |
transform := make(map[string]string) |
| 4201 |
|
| 4202 |
// parse nested exploration instruction from command-line arguments |
| 4203 |
cmds := ParseArguments(args, topPattern) |
| 4204 |
if cmds == nil { |
| 4205 |
DisplayError("Problem parsing command-line arguments") |
| 4206 |
os.Exit(1) |
| 4207 |
} |
| 4208 |
|
| 4209 |
// GLOBAL MAP FOR SORT-UNIQ-COUNT HISTOGRAM ARGUMENT |
| 4210 |
|
| 4211 |
histogram := make(map[string]int) |
| 4212 |
|
| 4213 |
// LAUNCH PRODUCER, CONSUMER, AND UNSHUFFLER GOROUTINES |
| 4214 |
|
| 4215 |
// launch producer goroutine to partition XML by pattern |
| 4216 |
xmlq := CreateXMLProducer(topPattern, star, turbo, CreateXMLStreamer(strings.NewReader(xml), nil)) |
| 4217 |
|
| 4218 |
// launch consumer goroutines to parse and explore partitioned XML objects |
| 4219 |
tblq := CreateXMLConsumers(cmds, parent, hd, tl, transform, false, histogram, xmlq) |
| 4220 |
|
| 4221 |
// launch unshuffler goroutine to restore order of results |
| 4222 |
unsq := CreateXMLUnshuffler(tblq) |
| 4223 |
|
| 4224 |
if xmlq == nil || tblq == nil || unsq == nil { |
| 4225 |
DisplayError("Unable to create servers") |
| 4226 |
os.Exit(1) |
| 4227 |
} |
| 4228 |
|
| 4229 |
// DRAIN OUTPUT CHANNEL TO EXECUTE EXTRACTION COMMANDS, RESTORE OUTPUT ORDER WITH HEAP |
| 4230 |
|
| 4231 |
var buffer strings.Builder |
| 4232 |
okay := false |
| 4233 |
last := "" |
| 4234 |
|
| 4235 |
if head != "" { |
| 4236 |
buffer.WriteString(head[:]) |
| 4237 |
buffer.WriteString("\n") |
| 4238 |
} |
| 4239 |
|
| 4240 |
for curr := range unsq { |
| 4241 |
|
| 4242 |
str := curr.Text |
| 4243 |
|
| 4244 |
if str == "" { |
| 4245 |
continue |
| 4246 |
} |
| 4247 |
|
| 4248 |
last = str |
| 4249 |
buffer.WriteString(str) |
| 4250 |
|
| 4251 |
okay = true |
| 4252 |
} |
| 4253 |
|
| 4254 |
// print -histogram results, if populated |
| 4255 |
keys := slices.SortedFunc(maps.Keys(histogram), CompareAlphaOrNumericKeys) |
| 4256 |
|
| 4257 |
for _, str := range keys { |
| 4258 |
|
| 4259 |
count := histogram[str] |
| 4260 |
|
| 4261 |
val := strconv.Itoa(count) |
| 4262 |
buffer.WriteString(val) |
| 4263 |
buffer.WriteString("\t") |
| 4264 |
buffer.WriteString(str) |
| 4265 |
buffer.WriteString("\n") |
| 4266 |
|
| 4267 |
last = "\n" |
| 4268 |
okay = true |
| 4269 |
} |
| 4270 |
|
| 4271 |
if !strings.HasSuffix(last, "\n") { |
| 4272 |
buffer.WriteString("\n") |
| 4273 |
} |
| 4274 |
|
| 4275 |
if tail != "" { |
| 4276 |
buffer.WriteString(tail[:]) |
| 4277 |
buffer.WriteString("\n") |
| 4278 |
} |
| 4279 |
|
| 4280 |
txt := "" |
| 4281 |
if okay { |
| 4282 |
txt = buffer.String() |
| 4283 |
} |
| 4284 |
buffer.Reset() |
| 4285 |
|
| 4286 |
return txt |
| 4287 |
} |