| 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: misc.go |
| 26 |
// |
| 27 |
// Author: Jonathan Kans |
| 28 |
// |
| 29 |
// ========================================================================== |
| 30 |
|
| 31 |
package eutils |
| 32 |
|
| 33 |
import ( |
| 34 |
"cmp" |
| 35 |
"encoding/hex" |
| 36 |
"golang.org/x/text/cases" |
| 37 |
"golang.org/x/text/language" |
| 38 |
"html" |
| 39 |
"slices" |
| 40 |
"strconv" |
| 41 |
"strings" |
| 42 |
"sync" |
| 43 |
"unicode" |
| 44 |
) |
| 45 |
|
| 46 |
// constants for markup policy |
| 47 |
const ( |
| 48 |
NOMARKUP = iota |
| 49 |
FUSE |
| 50 |
SPACE |
| 51 |
PERIOD |
| 52 |
CONCISE |
| 53 |
BRACKETS |
| 54 |
MARKDOWN |
| 55 |
SLASH |
| 56 |
TAGS |
| 57 |
TERSE |
| 58 |
) |
| 59 |
|
| 60 |
// removes mixed content tags |
| 61 |
var mfix *strings.Replacer |
| 62 |
|
| 63 |
// lower-case version of combiningAccents |
| 64 |
var lcBadAccents map[string]bool |
| 65 |
|
| 66 |
var invisibleRunes = map[rune]bool{ |
| 67 |
0x00A0: true, // NO-BREAK SPACE |
| 68 |
0x00AD: true, // SOFT HYPHEN |
| 69 |
0x034F: true, // COMBINING GRAPHEME JOINER |
| 70 |
0x061C: true, // ARABIC LETTER MARK |
| 71 |
0x115F: true, // HANGUL CHOSEONG FILLER |
| 72 |
0x1160: true, // HANGUL JUNGSEONG FILLER |
| 73 |
0x17B4: true, // KHMER VOWEL INHERENT AQ |
| 74 |
0x17B5: true, // KHMER VOWEL INHERENT AA |
| 75 |
0x180E: true, // MONGOLIAN VOWEL SEPARATOR |
| 76 |
0x2000: true, // EN QUAD |
| 77 |
0x2001: true, // EM QUAD |
| 78 |
0x2002: true, // EN SPACE |
| 79 |
0x2003: true, // EM SPACE |
| 80 |
0x2004: true, // THREE-PER-EM SPACE |
| 81 |
0x2005: true, // FOUR-PER-EM SPACE |
| 82 |
0x2006: true, // SIX-PER-EM SPACE |
| 83 |
0x2007: true, // FIGURE SPACE |
| 84 |
0x2008: true, // PUNCTUATION SPACE |
| 85 |
0x2009: true, // THIN SPACE |
| 86 |
0x200A: true, // HAIR SPACE |
| 87 |
0x200B: true, // ZERO WIDTH SPACE |
| 88 |
0x200C: true, // ZERO WIDTH NON-JOINER |
| 89 |
0x200D: true, // ZERO WIDTH JOINER |
| 90 |
0x200E: true, // LEFT-TO-RIGHT MARK |
| 91 |
0x200F: true, // RIGHT-TO-LEFT MARK |
| 92 |
0x202F: true, // NARROW NO-BREAK SPACE |
| 93 |
0x205F: true, // MEDIUM MATHEMATICAL SPACE |
| 94 |
0x2060: true, // WORD JOINER |
| 95 |
0x2061: true, // FUNCTION APPLICATION |
| 96 |
0x2062: true, // INVISIBLE TIMES |
| 97 |
0x2063: true, // INVISIBLE SEPARATOR |
| 98 |
0x2064: true, // INVISIBLE PLUS |
| 99 |
0x206A: true, // INHIBIT SYMMETRIC SWAPPING |
| 100 |
0x206B: true, // ACTIVATE SYMMETRIC SWAPPING |
| 101 |
0x206C: true, // INHIBIT ARABIC FORM SHAPING |
| 102 |
0x206D: true, // ACTIVATE ARABIC FORM SHAPING |
| 103 |
0x206E: true, // NATIONAL DIGIT SHAPES |
| 104 |
0x206F: true, // NOMINAL DIGIT SHAPES |
| 105 |
0x3000: true, // IDEOGRAPHIC SPACE |
| 106 |
0x2800: true, // BRAILLE PATTERN BLANK |
| 107 |
0x3164: true, // HANGUL FILLER |
| 108 |
0xFEFF: true, // ZERO WIDTH NO-BREAK SPACE |
| 109 |
0xFFA0: true, // HALFWIDTH HANGUL FILLER |
| 110 |
} |
| 111 |
|
| 112 |
var subScriptRunes = map[rune]rune{ |
| 113 |
0x2080: '0', |
| 114 |
0x2081: '1', |
| 115 |
0x2082: '2', |
| 116 |
0x2083: '3', |
| 117 |
0x2084: '4', |
| 118 |
0x2085: '5', |
| 119 |
0x2086: '6', |
| 120 |
0x2087: '7', |
| 121 |
0x2088: '8', |
| 122 |
0x2089: '9', |
| 123 |
0x208A: '+', |
| 124 |
0x208B: '-', |
| 125 |
0x208C: '=', |
| 126 |
0x208D: '(', |
| 127 |
0x208E: ')', |
| 128 |
0x2090: 'a', |
| 129 |
0x2091: 'e', |
| 130 |
0x2092: 'o', |
| 131 |
0x2093: 'x', |
| 132 |
0x2094: 'e', |
| 133 |
0x2095: 'h', |
| 134 |
0x2096: 'k', |
| 135 |
0x2097: 'l', |
| 136 |
0x2098: 'm', |
| 137 |
0x2099: 'n', |
| 138 |
0x209A: 'p', |
| 139 |
0x209B: 's', |
| 140 |
0x209C: 't', |
| 141 |
} |
| 142 |
|
| 143 |
var superScriptRunes = map[rune]rune{ |
| 144 |
0x00B2: '2', |
| 145 |
0x00B3: '3', |
| 146 |
0x00B9: '1', |
| 147 |
0x2070: '0', |
| 148 |
0x2071: '1', |
| 149 |
0x2074: '4', |
| 150 |
0x2075: '5', |
| 151 |
0x2076: '6', |
| 152 |
0x2077: '7', |
| 153 |
0x2078: '8', |
| 154 |
0x2079: '9', |
| 155 |
0x207A: '+', |
| 156 |
0x207B: '-', |
| 157 |
0x207C: '=', |
| 158 |
0x207D: '(', |
| 159 |
0x207E: ')', |
| 160 |
0x207F: 'n', |
| 161 |
} |
| 162 |
|
| 163 |
var isStopWord = map[string]bool{ |
| 164 |
"a": true, |
| 165 |
"about": true, |
| 166 |
"above": true, |
| 167 |
"abs": true, |
| 168 |
"accordingly": true, |
| 169 |
"across": true, |
| 170 |
"after": true, |
| 171 |
"afterwards": true, |
| 172 |
"again": true, |
| 173 |
"against": true, |
| 174 |
"all": true, |
| 175 |
"almost": true, |
| 176 |
"alone": true, |
| 177 |
"along": true, |
| 178 |
"already": true, |
| 179 |
"also": true, |
| 180 |
"although": true, |
| 181 |
"always": true, |
| 182 |
"am": true, |
| 183 |
"among": true, |
| 184 |
"amongst": true, |
| 185 |
"an": true, |
| 186 |
"analyze": true, |
| 187 |
"and": true, |
| 188 |
"another": true, |
| 189 |
"any": true, |
| 190 |
"anyhow": true, |
| 191 |
"anyone": true, |
| 192 |
"anything": true, |
| 193 |
"anywhere": true, |
| 194 |
"applicable": true, |
| 195 |
"apply": true, |
| 196 |
"are": true, |
| 197 |
"arise": true, |
| 198 |
"around": true, |
| 199 |
"as": true, |
| 200 |
"assume": true, |
| 201 |
"at": true, |
| 202 |
"be": true, |
| 203 |
"became": true, |
| 204 |
"because": true, |
| 205 |
"become": true, |
| 206 |
"becomes": true, |
| 207 |
"becoming": true, |
| 208 |
"been": true, |
| 209 |
"before": true, |
| 210 |
"beforehand": true, |
| 211 |
"being": true, |
| 212 |
"below": true, |
| 213 |
"beside": true, |
| 214 |
"besides": true, |
| 215 |
"between": true, |
| 216 |
"beyond": true, |
| 217 |
"both": true, |
| 218 |
"but": true, |
| 219 |
"by": true, |
| 220 |
"came": true, |
| 221 |
"can": true, |
| 222 |
"cannot": true, |
| 223 |
"cc": true, |
| 224 |
"cm": true, |
| 225 |
"come": true, |
| 226 |
"compare": true, |
| 227 |
"could": true, |
| 228 |
"de": true, |
| 229 |
"dealing": true, |
| 230 |
"department": true, |
| 231 |
"depend": true, |
| 232 |
"did": true, |
| 233 |
"discover": true, |
| 234 |
"dl": true, |
| 235 |
"do": true, |
| 236 |
"does": true, |
| 237 |
"done": true, |
| 238 |
"due": true, |
| 239 |
"during": true, |
| 240 |
"each": true, |
| 241 |
"ec": true, |
| 242 |
"ed": true, |
| 243 |
"effected": true, |
| 244 |
"eg": true, |
| 245 |
"either": true, |
| 246 |
"else": true, |
| 247 |
"elsewhere": true, |
| 248 |
"enough": true, |
| 249 |
"especially": true, |
| 250 |
"et": true, |
| 251 |
"etc": true, |
| 252 |
"ever": true, |
| 253 |
"every": true, |
| 254 |
"everyone": true, |
| 255 |
"everything": true, |
| 256 |
"everywhere": true, |
| 257 |
"except": true, |
| 258 |
"find": true, |
| 259 |
"for": true, |
| 260 |
"found": true, |
| 261 |
"from": true, |
| 262 |
"further": true, |
| 263 |
"gave": true, |
| 264 |
"get": true, |
| 265 |
"give": true, |
| 266 |
"go": true, |
| 267 |
"gone": true, |
| 268 |
"got": true, |
| 269 |
"gov": true, |
| 270 |
"had": true, |
| 271 |
"has": true, |
| 272 |
"have": true, |
| 273 |
"having": true, |
| 274 |
"he": true, |
| 275 |
"hence": true, |
| 276 |
"her": true, |
| 277 |
"here": true, |
| 278 |
"hereafter": true, |
| 279 |
"hereby": true, |
| 280 |
"herein": true, |
| 281 |
"hereupon": true, |
| 282 |
"hers": true, |
| 283 |
"herself": true, |
| 284 |
"him": true, |
| 285 |
"himself": true, |
| 286 |
"his": true, |
| 287 |
"how": true, |
| 288 |
"however": true, |
| 289 |
"hr": true, |
| 290 |
"i": true, |
| 291 |
"ie": true, |
| 292 |
"if": true, |
| 293 |
"ii": true, |
| 294 |
"iii": true, |
| 295 |
"immediately": true, |
| 296 |
"importance": true, |
| 297 |
"important": true, |
| 298 |
"in": true, |
| 299 |
"inc": true, |
| 300 |
"incl": true, |
| 301 |
"indeed": true, |
| 302 |
"into": true, |
| 303 |
"investigate": true, |
| 304 |
"is": true, |
| 305 |
"it": true, |
| 306 |
"its": true, |
| 307 |
"itself": true, |
| 308 |
"just": true, |
| 309 |
"keep": true, |
| 310 |
"kept": true, |
| 311 |
"kg": true, |
| 312 |
"km": true, |
| 313 |
"last": true, |
| 314 |
"latter": true, |
| 315 |
"latterly": true, |
| 316 |
"lb": true, |
| 317 |
"ld": true, |
| 318 |
"letter": true, |
| 319 |
"like": true, |
| 320 |
"ltd": true, |
| 321 |
"made": true, |
| 322 |
"mainly": true, |
| 323 |
"make": true, |
| 324 |
"many": true, |
| 325 |
"may": true, |
| 326 |
"me": true, |
| 327 |
"meanwhile": true, |
| 328 |
"mg": true, |
| 329 |
"might": true, |
| 330 |
"ml": true, |
| 331 |
"mm": true, |
| 332 |
"mo": true, |
| 333 |
"more": true, |
| 334 |
"moreover": true, |
| 335 |
"most": true, |
| 336 |
"mostly": true, |
| 337 |
"mr": true, |
| 338 |
"much": true, |
| 339 |
"mug": true, |
| 340 |
"must": true, |
| 341 |
"my": true, |
| 342 |
"myself": true, |
| 343 |
"namely": true, |
| 344 |
"nearly": true, |
| 345 |
"necessarily": true, |
| 346 |
"neither": true, |
| 347 |
"never": true, |
| 348 |
"nevertheless": true, |
| 349 |
"next": true, |
| 350 |
"no": true, |
| 351 |
"nobody": true, |
| 352 |
"noone": true, |
| 353 |
"nor": true, |
| 354 |
"normally": true, |
| 355 |
"nos": true, |
| 356 |
"not": true, |
| 357 |
"noted": true, |
| 358 |
"nothing": true, |
| 359 |
"now": true, |
| 360 |
"nowhere": true, |
| 361 |
"obtained": true, |
| 362 |
"of": true, |
| 363 |
"off": true, |
| 364 |
"often": true, |
| 365 |
"on": true, |
| 366 |
"only": true, |
| 367 |
"onto": true, |
| 368 |
"or": true, |
| 369 |
"other": true, |
| 370 |
"others": true, |
| 371 |
"otherwise": true, |
| 372 |
"ought": true, |
| 373 |
"our": true, |
| 374 |
"ours": true, |
| 375 |
"ourselves": true, |
| 376 |
"out": true, |
| 377 |
"over": true, |
| 378 |
"overall": true, |
| 379 |
"owing": true, |
| 380 |
"own": true, |
| 381 |
"oz": true, |
| 382 |
"particularly": true, |
| 383 |
"per": true, |
| 384 |
"perhaps": true, |
| 385 |
"pm": true, |
| 386 |
"pmid": true, |
| 387 |
"precede": true, |
| 388 |
"predominantly": true, |
| 389 |
"present": true, |
| 390 |
"presently": true, |
| 391 |
"previously": true, |
| 392 |
"primarily": true, |
| 393 |
"promptly": true, |
| 394 |
"pt": true, |
| 395 |
"quickly": true, |
| 396 |
"quite": true, |
| 397 |
"quot": true, |
| 398 |
"rather": true, |
| 399 |
"readily": true, |
| 400 |
"really": true, |
| 401 |
"recently": true, |
| 402 |
"refs": true, |
| 403 |
"regarding": true, |
| 404 |
"relate": true, |
| 405 |
"said": true, |
| 406 |
"same": true, |
| 407 |
"seem": true, |
| 408 |
"seemed": true, |
| 409 |
"seeming": true, |
| 410 |
"seems": true, |
| 411 |
"seen": true, |
| 412 |
"seriously": true, |
| 413 |
"several": true, |
| 414 |
"shall": true, |
| 415 |
"she": true, |
| 416 |
"should": true, |
| 417 |
"show": true, |
| 418 |
"showed": true, |
| 419 |
"shown": true, |
| 420 |
"shows": true, |
| 421 |
"significantly": true, |
| 422 |
"since": true, |
| 423 |
"slightly": true, |
| 424 |
"so": true, |
| 425 |
"some": true, |
| 426 |
"somehow": true, |
| 427 |
"someone": true, |
| 428 |
"something": true, |
| 429 |
"sometime": true, |
| 430 |
"sometimes": true, |
| 431 |
"somewhat": true, |
| 432 |
"somewhere": true, |
| 433 |
"soon": true, |
| 434 |
"specifically": true, |
| 435 |
"still": true, |
| 436 |
"strongly": true, |
| 437 |
"studied": true, |
| 438 |
"studies": true, |
| 439 |
"study": true, |
| 440 |
"sub": true, |
| 441 |
"substantially": true, |
| 442 |
"such": true, |
| 443 |
"sufficiently": true, |
| 444 |
"take": true, |
| 445 |
"tell": true, |
| 446 |
"th": true, |
| 447 |
"than": true, |
| 448 |
"that": true, |
| 449 |
"the": true, |
| 450 |
"their": true, |
| 451 |
"theirs": true, |
| 452 |
"them": true, |
| 453 |
"themselves": true, |
| 454 |
"then": true, |
| 455 |
"thence": true, |
| 456 |
"there": true, |
| 457 |
"thereafter": true, |
| 458 |
"thereby": true, |
| 459 |
"therefore": true, |
| 460 |
"therein": true, |
| 461 |
"thereupon": true, |
| 462 |
"these": true, |
| 463 |
"they": true, |
| 464 |
"this": true, |
| 465 |
"thorough": true, |
| 466 |
"those": true, |
| 467 |
"though": true, |
| 468 |
"through": true, |
| 469 |
"throughout": true, |
| 470 |
"thru": true, |
| 471 |
"thus": true, |
| 472 |
"to": true, |
| 473 |
"together": true, |
| 474 |
"too": true, |
| 475 |
"toward": true, |
| 476 |
"towards": true, |
| 477 |
"try": true, |
| 478 |
"type": true, |
| 479 |
"ug": true, |
| 480 |
"under": true, |
| 481 |
"unless": true, |
| 482 |
"until": true, |
| 483 |
"up": true, |
| 484 |
"upon": true, |
| 485 |
"us": true, |
| 486 |
"use": true, |
| 487 |
"used": true, |
| 488 |
"usefully": true, |
| 489 |
"usefulness": true, |
| 490 |
"using": true, |
| 491 |
"usually": true, |
| 492 |
"various": true, |
| 493 |
"very": true, |
| 494 |
"via": true, |
| 495 |
"was": true, |
| 496 |
"we": true, |
| 497 |
"were": true, |
| 498 |
"what": true, |
| 499 |
"whatever": true, |
| 500 |
"when": true, |
| 501 |
"whence": true, |
| 502 |
"whenever": true, |
| 503 |
"where": true, |
| 504 |
"whereafter": true, |
| 505 |
"whereas": true, |
| 506 |
"whereby": true, |
| 507 |
"wherein": true, |
| 508 |
"whereupon": true, |
| 509 |
"wherever": true, |
| 510 |
"whether": true, |
| 511 |
"which": true, |
| 512 |
"while": true, |
| 513 |
"whither": true, |
| 514 |
"who": true, |
| 515 |
"whoever": true, |
| 516 |
"whom": true, |
| 517 |
"whose": true, |
| 518 |
"why": true, |
| 519 |
"will": true, |
| 520 |
"with": true, |
| 521 |
"within": true, |
| 522 |
"without": true, |
| 523 |
"wk": true, |
| 524 |
"would": true, |
| 525 |
"wt": true, |
| 526 |
"yet": true, |
| 527 |
"you": true, |
| 528 |
"your": true, |
| 529 |
"yours": true, |
| 530 |
"yourself": true, |
| 531 |
"yourselves": true, |
| 532 |
"yr": true, |
| 533 |
} |
| 534 |
|
| 535 |
var htmlRepair = map[string]string{ |
| 536 |
"&lt;b&gt;": "<b>", |
| 537 |
"&lt;i&gt;": "<i>", |
| 538 |
"&lt;u&gt;": "<u>", |
| 539 |
"&lt;/b&gt;": "</b>", |
| 540 |
"&lt;/i&gt;": "</i>", |
| 541 |
"&lt;/u&gt;": "</u>", |
| 542 |
"&lt;b/&gt;": "<b/>", |
| 543 |
"&lt;i/&gt;": "<i/>", |
| 544 |
"&lt;u/&gt;": "<u/>", |
| 545 |
"&lt;b /&gt;": "<b/>", |
| 546 |
"&lt;i /&gt;": "<i/>", |
| 547 |
"&lt;u /&gt;": "<u/>", |
| 548 |
"&lt;sub&gt;": "<sub>", |
| 549 |
"&lt;sup&gt;": "<sup>", |
| 550 |
"&lt;/sub&gt;": "</sub>", |
| 551 |
"&lt;/sup&gt;": "</sup>", |
| 552 |
"&lt;sub/&gt;": "<sub/>", |
| 553 |
"&lt;sup/&gt;": "<sup/>", |
| 554 |
"&lt;sub /&gt;": "<sub/>", |
| 555 |
"&lt;sup /&gt;": "<sup/>", |
| 556 |
"<b>": "<b>", |
| 557 |
"<i>": "<i>", |
| 558 |
"<u>": "<u>", |
| 559 |
"</b>": "</b>", |
| 560 |
"</i>": "</i>", |
| 561 |
"</u>": "</u>", |
| 562 |
"<b/>": "<b/>", |
| 563 |
"<i/>": "<i/>", |
| 564 |
"<u/>": "<u/>", |
| 565 |
"<b />": "<b/>", |
| 566 |
"<i />": "<i/>", |
| 567 |
"<u />": "<u/>", |
| 568 |
"<sub>": "<sub>", |
| 569 |
"<sup>": "<sup>", |
| 570 |
"</sub>": "</sub>", |
| 571 |
"</sup>": "</sup>", |
| 572 |
"<sub/>": "<sub/>", |
| 573 |
"<sup/>": "<sup/>", |
| 574 |
"<sub />": "<sub/>", |
| 575 |
"<sup />": "<sup/>", |
| 576 |
} |
| 577 |
|
| 578 |
var hyphenatedPrefixes = map[string]bool{ |
| 579 |
"anti": true, |
| 580 |
"bi": true, |
| 581 |
"co": true, |
| 582 |
"contra": true, |
| 583 |
"counter": true, |
| 584 |
"de": true, |
| 585 |
"di": true, |
| 586 |
"extra": true, |
| 587 |
"infra": true, |
| 588 |
"inter": true, |
| 589 |
"intra": true, |
| 590 |
"micro": true, |
| 591 |
"mid": true, |
| 592 |
"mono": true, |
| 593 |
"multi": true, |
| 594 |
"non": true, |
| 595 |
"over": true, |
| 596 |
"peri": true, |
| 597 |
"post": true, |
| 598 |
"pre": true, |
| 599 |
"pro": true, |
| 600 |
"proto": true, |
| 601 |
"pseudo": true, |
| 602 |
"re": true, |
| 603 |
"semi": true, |
| 604 |
"sub": true, |
| 605 |
"super": true, |
| 606 |
"supra": true, |
| 607 |
"tetra": true, |
| 608 |
"trans": true, |
| 609 |
"tri": true, |
| 610 |
"ultra": true, |
| 611 |
"un": true, |
| 612 |
"under": true, |
| 613 |
"whole": true, |
| 614 |
} |
| 615 |
|
| 616 |
var primedPrefixes = map[string]bool{ |
| 617 |
"5": true, |
| 618 |
"3": true, |
| 619 |
} |
| 620 |
|
| 621 |
var primedSuffix = map[string]bool{ |
| 622 |
"s": true, |
| 623 |
} |
| 624 |
|
| 625 |
var revComp = map[rune]rune{ |
| 626 |
'A': 'T', |
| 627 |
'B': 'V', |
| 628 |
'C': 'G', |
| 629 |
'D': 'H', |
| 630 |
'G': 'C', |
| 631 |
'H': 'D', |
| 632 |
'K': 'M', |
| 633 |
'M': 'K', |
| 634 |
'N': 'N', |
| 635 |
'R': 'Y', |
| 636 |
'S': 'S', |
| 637 |
'T': 'A', |
| 638 |
'U': 'A', |
| 639 |
'V': 'B', |
| 640 |
'W': 'W', |
| 641 |
'X': 'X', |
| 642 |
'Y': 'R', |
| 643 |
'a': 't', |
| 644 |
'b': 'v', |
| 645 |
'c': 'g', |
| 646 |
'd': 'h', |
| 647 |
'g': 'c', |
| 648 |
'h': 'd', |
| 649 |
'k': 'm', |
| 650 |
'm': 'k', |
| 651 |
'n': 'n', |
| 652 |
'r': 'y', |
| 653 |
's': 's', |
| 654 |
't': 'a', |
| 655 |
'u': 'a', |
| 656 |
'v': 'b', |
| 657 |
'w': 'w', |
| 658 |
'x': 'x', |
| 659 |
'y': 'r', |
| 660 |
} |
| 661 |
|
| 662 |
var aaTo3 = map[string]string{ |
| 663 |
"*": "Ter", |
| 664 |
"-": "Gap", |
| 665 |
"A": "Ala", |
| 666 |
"B": "Asx", |
| 667 |
"C": "Cys", |
| 668 |
"D": "Asp", |
| 669 |
"E": "Glu", |
| 670 |
"F": "Phe", |
| 671 |
"G": "Gly", |
| 672 |
"H": "His", |
| 673 |
"I": "Ile", |
| 674 |
"J": "Xle", |
| 675 |
"K": "Lys", |
| 676 |
"L": "Leu", |
| 677 |
"M": "Met", |
| 678 |
"N": "Asn", |
| 679 |
"O": "Pyl", |
| 680 |
"P": "Pro", |
| 681 |
"Q": "Gln", |
| 682 |
"R": "Arg", |
| 683 |
"S": "Ser", |
| 684 |
"T": "Thr", |
| 685 |
"U": "Sec", |
| 686 |
"V": "Val", |
| 687 |
"W": "Trp", |
| 688 |
"X": "Xxx", |
| 689 |
"Y": "Tyr", |
| 690 |
"Z": "Glx", |
| 691 |
"a": "Ala", |
| 692 |
"b": "Asx", |
| 693 |
"c": "Cys", |
| 694 |
"d": "Asp", |
| 695 |
"e": "Glu", |
| 696 |
"f": "Phe", |
| 697 |
"g": "Gly", |
| 698 |
"h": "His", |
| 699 |
"i": "Ile", |
| 700 |
"j": "Xle", |
| 701 |
"k": "Lys", |
| 702 |
"l": "Leu", |
| 703 |
"m": "Met", |
| 704 |
"n": "Asn", |
| 705 |
"o": "Pyl", |
| 706 |
"p": "Pro", |
| 707 |
"q": "Gln", |
| 708 |
"r": "Arg", |
| 709 |
"s": "Ser", |
| 710 |
"t": "Thr", |
| 711 |
"u": "Sec", |
| 712 |
"v": "Val", |
| 713 |
"w": "Trp", |
| 714 |
"x": "Xxx", |
| 715 |
"y": "Tyr", |
| 716 |
"z": "Glx", |
| 717 |
} |
| 718 |
|
| 719 |
var aaTo1 = map[string]string{ |
| 720 |
"*": "*", |
| 721 |
"-": "-", |
| 722 |
"Ala": "A", |
| 723 |
"Arg": "R", |
| 724 |
"Asn": "N", |
| 725 |
"Asp": "D", |
| 726 |
"Asx": "B", |
| 727 |
"Cys": "C", |
| 728 |
"Gap": "-", |
| 729 |
"Gln": "Q", |
| 730 |
"Glu": "E", |
| 731 |
"Glx": "Z", |
| 732 |
"Gly": "G", |
| 733 |
"His": "H", |
| 734 |
"Ile": "I", |
| 735 |
"Leu": "L", |
| 736 |
"Lys": "K", |
| 737 |
"Met": "M", |
| 738 |
"Phe": "F", |
| 739 |
"Pro": "P", |
| 740 |
"Pyl": "O", |
| 741 |
"Sec": "U", |
| 742 |
"Ser": "S", |
| 743 |
"Ter": "*", |
| 744 |
"Thr": "T", |
| 745 |
"Trp": "W", |
| 746 |
"Tyr": "Y", |
| 747 |
"Val": "V", |
| 748 |
"Xle": "J", |
| 749 |
"Xxx": "X", |
| 750 |
"ala": "A", |
| 751 |
"arg": "R", |
| 752 |
"asn": "N", |
| 753 |
"asp": "D", |
| 754 |
"asx": "B", |
| 755 |
"cys": "C", |
| 756 |
"gap": "-", |
| 757 |
"gln": "Q", |
| 758 |
"glu": "E", |
| 759 |
"glx": "Z", |
| 760 |
"gly": "G", |
| 761 |
"his": "H", |
| 762 |
"ile": "I", |
| 763 |
"leu": "L", |
| 764 |
"lys": "K", |
| 765 |
"met": "M", |
| 766 |
"phe": "F", |
| 767 |
"pro": "P", |
| 768 |
"pyl": "O", |
| 769 |
"sec": "U", |
| 770 |
"ser": "S", |
| 771 |
"ter": "*", |
| 772 |
"thr": "T", |
| 773 |
"trp": "W", |
| 774 |
"tyr": "Y", |
| 775 |
"val": "V", |
| 776 |
"xle": "J", |
| 777 |
"xxx": "X", |
| 778 |
} |
| 779 |
|
| 780 |
var expandNuc = map[string]string{ |
| 781 |
"A": "A", |
| 782 |
"B": "CGT", |
| 783 |
"C": "C", |
| 784 |
"D": "AGT", |
| 785 |
"G": "G", |
| 786 |
"H": "ACT", |
| 787 |
"K": "GT", |
| 788 |
"M": "AC", |
| 789 |
"N": "ACGT", |
| 790 |
"R": "AG", |
| 791 |
"S": "GC", |
| 792 |
"T": "T", |
| 793 |
"V": "ACG", |
| 794 |
"W": "AT", |
| 795 |
"X": "ACGT", |
| 796 |
"Y": "CT", |
| 797 |
"a": "a", |
| 798 |
"b": "cgt", |
| 799 |
"c": "c", |
| 800 |
"d": "agt", |
| 801 |
"g": "g", |
| 802 |
"h": "act", |
| 803 |
"k": "gt", |
| 804 |
"m": "ac", |
| 805 |
"n": "acgt", |
| 806 |
"r": "ag", |
| 807 |
"s": "gc", |
| 808 |
"t": "t", |
| 809 |
"v": "acg", |
| 810 |
"w": "at", |
| 811 |
"x": "acgt", |
| 812 |
"y": "ct", |
| 813 |
} |
| 814 |
|
| 815 |
/* |
| 816 |
var conv = []string{"A", "C", "G", "T"} |
| 817 |
for i := 0; i < 4; i++ { |
| 818 |
for j := 0; j < 4; j++ { |
| 819 |
for k := 0; k < 4; k++ { |
| 820 |
for l := 0; l < 4; l++ { |
| 821 |
base := conv[i] + conv[j] + conv[k] + conv[l] |
| 822 |
idx := i*64 + j*16 + k*4 + l |
| 823 |
fmt.Fprintf(os.Stdout, "\t%d: \"%s\",\n", idx, base) |
| 824 |
} |
| 825 |
} |
| 826 |
} |
| 827 |
} |
| 828 |
*/ |
| 829 |
|
| 830 |
var ncbi2naToIupac = map[int]string{ |
| 831 |
0: "AAAA", |
| 832 |
1: "AAAC", |
| 833 |
2: "AAAG", |
| 834 |
3: "AAAT", |
| 835 |
4: "AACA", |
| 836 |
5: "AACC", |
| 837 |
6: "AACG", |
| 838 |
7: "AACT", |
| 839 |
8: "AAGA", |
| 840 |
9: "AAGC", |
| 841 |
10: "AAGG", |
| 842 |
11: "AAGT", |
| 843 |
12: "AATA", |
| 844 |
13: "AATC", |
| 845 |
14: "AATG", |
| 846 |
15: "AATT", |
| 847 |
16: "ACAA", |
| 848 |
17: "ACAC", |
| 849 |
18: "ACAG", |
| 850 |
19: "ACAT", |
| 851 |
20: "ACCA", |
| 852 |
21: "ACCC", |
| 853 |
22: "ACCG", |
| 854 |
23: "ACCT", |
| 855 |
24: "ACGA", |
| 856 |
25: "ACGC", |
| 857 |
26: "ACGG", |
| 858 |
27: "ACGT", |
| 859 |
28: "ACTA", |
| 860 |
29: "ACTC", |
| 861 |
30: "ACTG", |
| 862 |
31: "ACTT", |
| 863 |
32: "AGAA", |
| 864 |
33: "AGAC", |
| 865 |
34: "AGAG", |
| 866 |
35: "AGAT", |
| 867 |
36: "AGCA", |
| 868 |
37: "AGCC", |
| 869 |
38: "AGCG", |
| 870 |
39: "AGCT", |
| 871 |
40: "AGGA", |
| 872 |
41: "AGGC", |
| 873 |
42: "AGGG", |
| 874 |
43: "AGGT", |
| 875 |
44: "AGTA", |
| 876 |
45: "AGTC", |
| 877 |
46: "AGTG", |
| 878 |
47: "AGTT", |
| 879 |
48: "ATAA", |
| 880 |
49: "ATAC", |
| 881 |
50: "ATAG", |
| 882 |
51: "ATAT", |
| 883 |
52: "ATCA", |
| 884 |
53: "ATCC", |
| 885 |
54: "ATCG", |
| 886 |
55: "ATCT", |
| 887 |
56: "ATGA", |
| 888 |
57: "ATGC", |
| 889 |
58: "ATGG", |
| 890 |
59: "ATGT", |
| 891 |
60: "ATTA", |
| 892 |
61: "ATTC", |
| 893 |
62: "ATTG", |
| 894 |
63: "ATTT", |
| 895 |
64: "CAAA", |
| 896 |
65: "CAAC", |
| 897 |
66: "CAAG", |
| 898 |
67: "CAAT", |
| 899 |
68: "CACA", |
| 900 |
69: "CACC", |
| 901 |
70: "CACG", |
| 902 |
71: "CACT", |
| 903 |
72: "CAGA", |
| 904 |
73: "CAGC", |
| 905 |
74: "CAGG", |
| 906 |
75: "CAGT", |
| 907 |
76: "CATA", |
| 908 |
77: "CATC", |
| 909 |
78: "CATG", |
| 910 |
79: "CATT", |
| 911 |
80: "CCAA", |
| 912 |
81: "CCAC", |
| 913 |
82: "CCAG", |
| 914 |
83: "CCAT", |
| 915 |
84: "CCCA", |
| 916 |
85: "CCCC", |
| 917 |
86: "CCCG", |
| 918 |
87: "CCCT", |
| 919 |
88: "CCGA", |
| 920 |
89: "CCGC", |
| 921 |
90: "CCGG", |
| 922 |
91: "CCGT", |
| 923 |
92: "CCTA", |
| 924 |
93: "CCTC", |
| 925 |
94: "CCTG", |
| 926 |
95: "CCTT", |
| 927 |
96: "CGAA", |
| 928 |
97: "CGAC", |
| 929 |
98: "CGAG", |
| 930 |
99: "CGAT", |
| 931 |
100: "CGCA", |
| 932 |
101: "CGCC", |
| 933 |
102: "CGCG", |
| 934 |
103: "CGCT", |
| 935 |
104: "CGGA", |
| 936 |
105: "CGGC", |
| 937 |
106: "CGGG", |
| 938 |
107: "CGGT", |
| 939 |
108: "CGTA", |
| 940 |
109: "CGTC", |
| 941 |
110: "CGTG", |
| 942 |
111: "CGTT", |
| 943 |
112: "CTAA", |
| 944 |
113: "CTAC", |
| 945 |
114: "CTAG", |
| 946 |
115: "CTAT", |
| 947 |
116: "CTCA", |
| 948 |
117: "CTCC", |
| 949 |
118: "CTCG", |
| 950 |
119: "CTCT", |
| 951 |
120: "CTGA", |
| 952 |
121: "CTGC", |
| 953 |
122: "CTGG", |
| 954 |
123: "CTGT", |
| 955 |
124: "CTTA", |
| 956 |
125: "CTTC", |
| 957 |
126: "CTTG", |
| 958 |
127: "CTTT", |
| 959 |
128: "GAAA", |
| 960 |
129: "GAAC", |
| 961 |
130: "GAAG", |
| 962 |
131: "GAAT", |
| 963 |
132: "GACA", |
| 964 |
133: "GACC", |
| 965 |
134: "GACG", |
| 966 |
135: "GACT", |
| 967 |
136: "GAGA", |
| 968 |
137: "GAGC", |
| 969 |
138: "GAGG", |
| 970 |
139: "GAGT", |
| 971 |
140: "GATA", |
| 972 |
141: "GATC", |
| 973 |
142: "GATG", |
| 974 |
143: "GATT", |
| 975 |
144: "GCAA", |
| 976 |
145: "GCAC", |
| 977 |
146: "GCAG", |
| 978 |
147: "GCAT", |
| 979 |
148: "GCCA", |
| 980 |
149: "GCCC", |
| 981 |
150: "GCCG", |
| 982 |
151: "GCCT", |
| 983 |
152: "GCGA", |
| 984 |
153: "GCGC", |
| 985 |
154: "GCGG", |
| 986 |
155: "GCGT", |
| 987 |
156: "GCTA", |
| 988 |
157: "GCTC", |
| 989 |
158: "GCTG", |
| 990 |
159: "GCTT", |
| 991 |
160: "GGAA", |
| 992 |
161: "GGAC", |
| 993 |
162: "GGAG", |
| 994 |
163: "GGAT", |
| 995 |
164: "GGCA", |
| 996 |
165: "GGCC", |
| 997 |
166: "GGCG", |
| 998 |
167: "GGCT", |
| 999 |
168: "GGGA", |
| 1000 |
169: "GGGC", |
| 1001 |
170: "GGGG", |
| 1002 |
171: "GGGT", |
| 1003 |
172: "GGTA", |
| 1004 |
173: "GGTC", |
| 1005 |
174: "GGTG", |
| 1006 |
175: "GGTT", |
| 1007 |
176: "GTAA", |
| 1008 |
177: "GTAC", |
| 1009 |
178: "GTAG", |
| 1010 |
179: "GTAT", |
| 1011 |
180: "GTCA", |
| 1012 |
181: "GTCC", |
| 1013 |
182: "GTCG", |
| 1014 |
183: "GTCT", |
| 1015 |
184: "GTGA", |
| 1016 |
185: "GTGC", |
| 1017 |
186: "GTGG", |
| 1018 |
187: "GTGT", |
| 1019 |
188: "GTTA", |
| 1020 |
189: "GTTC", |
| 1021 |
190: "GTTG", |
| 1022 |
191: "GTTT", |
| 1023 |
192: "TAAA", |
| 1024 |
193: "TAAC", |
| 1025 |
194: "TAAG", |
| 1026 |
195: "TAAT", |
| 1027 |
196: "TACA", |
| 1028 |
197: "TACC", |
| 1029 |
198: "TACG", |
| 1030 |
199: "TACT", |
| 1031 |
200: "TAGA", |
| 1032 |
201: "TAGC", |
| 1033 |
202: "TAGG", |
| 1034 |
203: "TAGT", |
| 1035 |
204: "TATA", |
| 1036 |
205: "TATC", |
| 1037 |
206: "TATG", |
| 1038 |
207: "TATT", |
| 1039 |
208: "TCAA", |
| 1040 |
209: "TCAC", |
| 1041 |
210: "TCAG", |
| 1042 |
211: "TCAT", |
| 1043 |
212: "TCCA", |
| 1044 |
213: "TCCC", |
| 1045 |
214: "TCCG", |
| 1046 |
215: "TCCT", |
| 1047 |
216: "TCGA", |
| 1048 |
217: "TCGC", |
| 1049 |
218: "TCGG", |
| 1050 |
219: "TCGT", |
| 1051 |
220: "TCTA", |
| 1052 |
221: "TCTC", |
| 1053 |
222: "TCTG", |
| 1054 |
223: "TCTT", |
| 1055 |
224: "TGAA", |
| 1056 |
225: "TGAC", |
| 1057 |
226: "TGAG", |
| 1058 |
227: "TGAT", |
| 1059 |
228: "TGCA", |
| 1060 |
229: "TGCC", |
| 1061 |
230: "TGCG", |
| 1062 |
231: "TGCT", |
| 1063 |
232: "TGGA", |
| 1064 |
233: "TGGC", |
| 1065 |
234: "TGGG", |
| 1066 |
235: "TGGT", |
| 1067 |
236: "TGTA", |
| 1068 |
237: "TGTC", |
| 1069 |
238: "TGTG", |
| 1070 |
239: "TGTT", |
| 1071 |
240: "TTAA", |
| 1072 |
241: "TTAC", |
| 1073 |
242: "TTAG", |
| 1074 |
243: "TTAT", |
| 1075 |
244: "TTCA", |
| 1076 |
245: "TTCC", |
| 1077 |
246: "TTCG", |
| 1078 |
247: "TTCT", |
| 1079 |
248: "TTGA", |
| 1080 |
249: "TTGC", |
| 1081 |
250: "TTGG", |
| 1082 |
251: "TTGT", |
| 1083 |
252: "TTTA", |
| 1084 |
253: "TTTC", |
| 1085 |
254: "TTTG", |
| 1086 |
255: "TTTT", |
| 1087 |
} |
| 1088 |
|
| 1089 |
/* |
| 1090 |
var conv = []string{"N", "A", "C", "M", "G", "R", "S", "V", "T", "W", "Y", "H", "K", "D", "B", "N"} |
| 1091 |
for i := 0; i < 16; i++ { |
| 1092 |
for j := 0; j < 16; j++ { |
| 1093 |
base := conv[i] + conv[j] |
| 1094 |
idx := i*16 + j |
| 1095 |
fmt.Fprintf(os.Stdout, "\t%d: \"%s\",\n", idx, base) |
| 1096 |
} |
| 1097 |
} |
| 1098 |
*/ |
| 1099 |
|
| 1100 |
var ncbi4naToIupac = map[int]string{ |
| 1101 |
0: "NN", |
| 1102 |
1: "NA", |
| 1103 |
2: "NC", |
| 1104 |
3: "NM", |
| 1105 |
4: "NG", |
| 1106 |
5: "NR", |
| 1107 |
6: "NS", |
| 1108 |
7: "NV", |
| 1109 |
8: "NT", |
| 1110 |
9: "NW", |
| 1111 |
10: "NY", |
| 1112 |
11: "NH", |
| 1113 |
12: "NK", |
| 1114 |
13: "ND", |
| 1115 |
14: "NB", |
| 1116 |
15: "NN", |
| 1117 |
16: "AN", |
| 1118 |
17: "AA", |
| 1119 |
18: "AC", |
| 1120 |
19: "AM", |
| 1121 |
20: "AG", |
| 1122 |
21: "AR", |
| 1123 |
22: "AS", |
| 1124 |
23: "AV", |
| 1125 |
24: "AT", |
| 1126 |
25: "AW", |
| 1127 |
26: "AY", |
| 1128 |
27: "AH", |
| 1129 |
28: "AK", |
| 1130 |
29: "AD", |
| 1131 |
30: "AB", |
| 1132 |
31: "AN", |
| 1133 |
32: "CN", |
| 1134 |
33: "CA", |
| 1135 |
34: "CC", |
| 1136 |
35: "CM", |
| 1137 |
36: "CG", |
| 1138 |
37: "CR", |
| 1139 |
38: "CS", |
| 1140 |
39: "CV", |
| 1141 |
40: "CT", |
| 1142 |
41: "CW", |
| 1143 |
42: "CY", |
| 1144 |
43: "CH", |
| 1145 |
44: "CK", |
| 1146 |
45: "CD", |
| 1147 |
46: "CB", |
| 1148 |
47: "CN", |
| 1149 |
48: "MN", |
| 1150 |
49: "MA", |
| 1151 |
50: "MC", |
| 1152 |
51: "MM", |
| 1153 |
52: "MG", |
| 1154 |
53: "MR", |
| 1155 |
54: "MS", |
| 1156 |
55: "MV", |
| 1157 |
56: "MT", |
| 1158 |
57: "MW", |
| 1159 |
58: "MY", |
| 1160 |
59: "MH", |
| 1161 |
60: "MK", |
| 1162 |
61: "MD", |
| 1163 |
62: "MB", |
| 1164 |
63: "MN", |
| 1165 |
64: "GN", |
| 1166 |
65: "GA", |
| 1167 |
66: "GC", |
| 1168 |
67: "GM", |
| 1169 |
68: "GG", |
| 1170 |
69: "GR", |
| 1171 |
70: "GS", |
| 1172 |
71: "GV", |
| 1173 |
72: "GT", |
| 1174 |
73: "GW", |
| 1175 |
74: "GY", |
| 1176 |
75: "GH", |
| 1177 |
76: "GK", |
| 1178 |
77: "GD", |
| 1179 |
78: "GB", |
| 1180 |
79: "GN", |
| 1181 |
80: "RN", |
| 1182 |
81: "RA", |
| 1183 |
82: "RC", |
| 1184 |
83: "RM", |
| 1185 |
84: "RG", |
| 1186 |
85: "RR", |
| 1187 |
86: "RS", |
| 1188 |
87: "RV", |
| 1189 |
88: "RT", |
| 1190 |
89: "RW", |
| 1191 |
90: "RY", |
| 1192 |
91: "RH", |
| 1193 |
92: "RK", |
| 1194 |
93: "RD", |
| 1195 |
94: "RB", |
| 1196 |
95: "RN", |
| 1197 |
96: "SN", |
| 1198 |
97: "SA", |
| 1199 |
98: "SC", |
| 1200 |
99: "SM", |
| 1201 |
100: "SG", |
| 1202 |
101: "SR", |
| 1203 |
102: "SS", |
| 1204 |
103: "SV", |
| 1205 |
104: "ST", |
| 1206 |
105: "SW", |
| 1207 |
106: "SY", |
| 1208 |
107: "SH", |
| 1209 |
108: "SK", |
| 1210 |
109: "SD", |
| 1211 |
110: "SB", |
| 1212 |
111: "SN", |
| 1213 |
112: "VN", |
| 1214 |
113: "VA", |
| 1215 |
114: "VC", |
| 1216 |
115: "VM", |
| 1217 |
116: "VG", |
| 1218 |
117: "VR", |
| 1219 |
118: "VS", |
| 1220 |
119: "VV", |
| 1221 |
120: "VT", |
| 1222 |
121: "VW", |
| 1223 |
122: "VY", |
| 1224 |
123: "VH", |
| 1225 |
124: "VK", |
| 1226 |
125: "VD", |
| 1227 |
126: "VB", |
| 1228 |
127: "VN", |
| 1229 |
128: "TN", |
| 1230 |
129: "TA", |
| 1231 |
130: "TC", |
| 1232 |
131: "TM", |
| 1233 |
132: "TG", |
| 1234 |
133: "TR", |
| 1235 |
134: "TS", |
| 1236 |
135: "TV", |
| 1237 |
136: "TT", |
| 1238 |
137: "TW", |
| 1239 |
138: "TY", |
| 1240 |
139: "TH", |
| 1241 |
140: "TK", |
| 1242 |
141: "TD", |
| 1243 |
142: "TB", |
| 1244 |
143: "TN", |
| 1245 |
144: "WN", |
| 1246 |
145: "WA", |
| 1247 |
146: "WC", |
| 1248 |
147: "WM", |
| 1249 |
148: "WG", |
| 1250 |
149: "WR", |
| 1251 |
150: "WS", |
| 1252 |
151: "WV", |
| 1253 |
152: "WT", |
| 1254 |
153: "WW", |
| 1255 |
154: "WY", |
| 1256 |
155: "WH", |
| 1257 |
156: "WK", |
| 1258 |
157: "WD", |
| 1259 |
158: "WB", |
| 1260 |
159: "WN", |
| 1261 |
160: "YN", |
| 1262 |
161: "YA", |
| 1263 |
162: "YC", |
| 1264 |
163: "YM", |
| 1265 |
164: "YG", |
| 1266 |
165: "YR", |
| 1267 |
166: "YS", |
| 1268 |
167: "YV", |
| 1269 |
168: "YT", |
| 1270 |
169: "YW", |
| 1271 |
170: "YY", |
| 1272 |
171: "YH", |
| 1273 |
172: "YK", |
| 1274 |
173: "YD", |
| 1275 |
174: "YB", |
| 1276 |
175: "YN", |
| 1277 |
176: "HN", |
| 1278 |
177: "HA", |
| 1279 |
178: "HC", |
| 1280 |
179: "HM", |
| 1281 |
180: "HG", |
| 1282 |
181: "HR", |
| 1283 |
182: "HS", |
| 1284 |
183: "HV", |
| 1285 |
184: "HT", |
| 1286 |
185: "HW", |
| 1287 |
186: "HY", |
| 1288 |
187: "HH", |
| 1289 |
188: "HK", |
| 1290 |
189: "HD", |
| 1291 |
190: "HB", |
| 1292 |
191: "HN", |
| 1293 |
192: "KN", |
| 1294 |
193: "KA", |
| 1295 |
194: "KC", |
| 1296 |
195: "KM", |
| 1297 |
196: "KG", |
| 1298 |
197: "KR", |
| 1299 |
198: "KS", |
| 1300 |
199: "KV", |
| 1301 |
200: "KT", |
| 1302 |
201: "KW", |
| 1303 |
202: "KY", |
| 1304 |
203: "KH", |
| 1305 |
204: "KK", |
| 1306 |
205: "KD", |
| 1307 |
206: "KB", |
| 1308 |
207: "KN", |
| 1309 |
208: "DN", |
| 1310 |
209: "DA", |
| 1311 |
210: "DC", |
| 1312 |
211: "DM", |
| 1313 |
212: "DG", |
| 1314 |
213: "DR", |
| 1315 |
214: "DS", |
| 1316 |
215: "DV", |
| 1317 |
216: "DT", |
| 1318 |
217: "DW", |
| 1319 |
218: "DY", |
| 1320 |
219: "DH", |
| 1321 |
220: "DK", |
| 1322 |
221: "DD", |
| 1323 |
222: "DB", |
| 1324 |
223: "DN", |
| 1325 |
224: "BN", |
| 1326 |
225: "BA", |
| 1327 |
226: "BC", |
| 1328 |
227: "BM", |
| 1329 |
228: "BG", |
| 1330 |
229: "BR", |
| 1331 |
230: "BS", |
| 1332 |
231: "BV", |
| 1333 |
232: "BT", |
| 1334 |
233: "BW", |
| 1335 |
234: "BY", |
| 1336 |
235: "BH", |
| 1337 |
236: "BK", |
| 1338 |
237: "BD", |
| 1339 |
238: "BB", |
| 1340 |
239: "BN", |
| 1341 |
240: "NN", |
| 1342 |
241: "NA", |
| 1343 |
242: "NC", |
| 1344 |
243: "NM", |
| 1345 |
244: "NG", |
| 1346 |
245: "NR", |
| 1347 |
246: "NS", |
| 1348 |
247: "NV", |
| 1349 |
248: "NT", |
| 1350 |
249: "NW", |
| 1351 |
250: "NY", |
| 1352 |
251: "NH", |
| 1353 |
252: "NK", |
| 1354 |
253: "ND", |
| 1355 |
254: "NB", |
| 1356 |
255: "NN", |
| 1357 |
} |
| 1358 |
|
| 1359 |
// CleanDoctype accepts full doctype objects or just the inner contents |
| 1360 |
func CleanDoctype(doctype string) string { |
| 1361 |
|
| 1362 |
doctype = strings.TrimSpace(doctype) |
| 1363 |
doctype = strings.TrimPrefix(doctype, "<") |
| 1364 |
doctype = strings.TrimPrefix(doctype, "!") |
| 1365 |
doctype = strings.TrimPrefix(doctype, "DOCTYPE") |
| 1366 |
doctype = strings.TrimSuffix(doctype, ">") |
| 1367 |
doctype = strings.TrimSpace(doctype) |
| 1368 |
|
| 1369 |
doctype = "<!DOCTYPE " + doctype + ">" |
| 1370 |
|
| 1371 |
return doctype |
| 1372 |
} |
| 1373 |
|
| 1374 |
// CleanXmltype accepts full xml objects or just the inner contents |
| 1375 |
func CleanXmltype(xmltype string) string { |
| 1376 |
|
| 1377 |
xmltype = strings.TrimSpace(xmltype) |
| 1378 |
xmltype = strings.TrimPrefix(xmltype, "<") |
| 1379 |
xmltype = strings.TrimPrefix(xmltype, "?") |
| 1380 |
xmltype = strings.TrimPrefix(xmltype, "xml") |
| 1381 |
xmltype = strings.TrimSuffix(xmltype, ">") |
| 1382 |
xmltype = strings.TrimSuffix(xmltype, "?") |
| 1383 |
xmltype = strings.TrimSpace(xmltype) |
| 1384 |
|
| 1385 |
xmltype = "<?xml " + xmltype + " ?>" |
| 1386 |
|
| 1387 |
return xmltype |
| 1388 |
} |
| 1389 |
|
| 1390 |
// use RelaxString to convert non-alphanumeric characters to spaces |
| 1391 |
|
| 1392 |
// CleanupAuthor fixes misused letters and accents |
| 1393 |
func CleanupAuthor(str string, wrp bool) string { |
| 1394 |
|
| 1395 |
if str == "" { |
| 1396 |
return str |
| 1397 |
} |
| 1398 |
|
| 1399 |
str = strings.Replace(str, "\n", " ", -1) |
| 1400 |
|
| 1401 |
str = FixMisusedLetters(str, false, true, false) |
| 1402 |
str = TransformAccents(str, false, false) |
| 1403 |
// convert numeric encoding to apostrophe |
| 1404 |
str = strings.Replace(str, "'", "'", -1) |
| 1405 |
// remove space following apostrophe |
| 1406 |
str = strings.Replace(str, "' ", "'", -1) |
| 1407 |
// but leave apostrophe present in result |
| 1408 |
|
| 1409 |
if HasUnicodeMarkup(str) { |
| 1410 |
str = RepairUnicodeMarkup(str, SPACE) |
| 1411 |
} |
| 1412 |
if HasAngleOrAmpersandEncoding(str) { |
| 1413 |
str = RepairTableMarkup(str, SPACE) |
| 1414 |
str = RemoveHTMLDecorations(str) |
| 1415 |
if wrp { |
| 1416 |
str = encodeAngleBracketsAndAmpersand(str) |
| 1417 |
} |
| 1418 |
} |
| 1419 |
if HasBadSpace(str) { |
| 1420 |
str = CleanupBadSpaces(str) |
| 1421 |
} |
| 1422 |
if HasAdjacentSpaces(str) { |
| 1423 |
str = CompressRunsOfSpaces(str) |
| 1424 |
} |
| 1425 |
if HasExtraSpaces(str) { |
| 1426 |
str = RemoveExtraSpaces(str) |
| 1427 |
} |
| 1428 |
|
| 1429 |
return str |
| 1430 |
} |
| 1431 |
|
| 1432 |
// CleanupBadSpaces converts non-ASCII Unicode spaces to ASCII space |
| 1433 |
func CleanupBadSpaces(str string) string { |
| 1434 |
|
| 1435 |
var buffer strings.Builder |
| 1436 |
|
| 1437 |
for _, ch := range str { |
| 1438 |
if ch < 128 { |
| 1439 |
buffer.WriteRune(ch) |
| 1440 |
continue |
| 1441 |
} |
| 1442 |
|
| 1443 |
if unicode.IsSpace(ch) { |
| 1444 |
buffer.WriteRune(' ') |
| 1445 |
} else if ch >= 0x0080 && ch <= 0x009F { |
| 1446 |
// absorb control characters |
| 1447 |
} else if ch == 0x00A0 { |
| 1448 |
// no-break space |
| 1449 |
buffer.WriteRune(' ') |
| 1450 |
} else if ch >= 0x2002 && ch <= 0x200B { |
| 1451 |
// special spaces (en space through zero width space) |
| 1452 |
buffer.WriteRune(' ') |
| 1453 |
} else if ch == 0x202F { |
| 1454 |
// narrow no-break space |
| 1455 |
buffer.WriteRune(' ') |
| 1456 |
} else if ch == 0x3000 { |
| 1457 |
// ideographic space |
| 1458 |
buffer.WriteRune(' ') |
| 1459 |
} else if ch >= 0xE000 && ch <= 0xF8FF { |
| 1460 |
// unassigned values (private use) |
| 1461 |
buffer.WriteRune('(') |
| 1462 |
buffer.WriteRune('?') |
| 1463 |
buffer.WriteRune(')') |
| 1464 |
} else if ch == 0xFEFF { |
| 1465 |
// zero width no-break space |
| 1466 |
buffer.WriteRune(' ') |
| 1467 |
} else { |
| 1468 |
buffer.WriteRune(ch) |
| 1469 |
} |
| 1470 |
} |
| 1471 |
|
| 1472 |
return buffer.String() |
| 1473 |
} |
| 1474 |
|
| 1475 |
// CleanupBasic convert superscripts and subscripts |
| 1476 |
func CleanupBasic(str string, wrp bool) string { |
| 1477 |
|
| 1478 |
if str == "" { |
| 1479 |
return str |
| 1480 |
} |
| 1481 |
|
| 1482 |
str = strings.Replace(str, "\n", " ", -1) |
| 1483 |
|
| 1484 |
if HasUnicodeMarkup(str) { |
| 1485 |
str = RepairUnicodeMarkup(str, SPACE) |
| 1486 |
} |
| 1487 |
if HasAngleOrAmpersandEncoding(str) { |
| 1488 |
str = RepairTableMarkup(str, SPACE) |
| 1489 |
str = RemoveHTMLDecorations(str) |
| 1490 |
if wrp { |
| 1491 |
str = encodeAngleBracketsAndAmpersand(str) |
| 1492 |
} |
| 1493 |
} |
| 1494 |
if HasBadSpace(str) { |
| 1495 |
str = CleanupBadSpaces(str) |
| 1496 |
} |
| 1497 |
if HasAdjacentSpaces(str) { |
| 1498 |
str = CompressRunsOfSpaces(str) |
| 1499 |
} |
| 1500 |
if HasExtraSpaces(str) { |
| 1501 |
str = RemoveExtraSpaces(str) |
| 1502 |
} |
| 1503 |
|
| 1504 |
return str |
| 1505 |
} |
| 1506 |
|
| 1507 |
// CleanupContents performs optional operations on XML content strings |
| 1508 |
func CleanupContents(str string, ascii, amper, mixed bool) string { |
| 1509 |
|
| 1510 |
if doCompress { |
| 1511 |
if !allowEmbed { |
| 1512 |
if ascii && HasBadSpace(str) { |
| 1513 |
str = CleanupBadSpaces(str) |
| 1514 |
} |
| 1515 |
} |
| 1516 |
if HasAdjacentSpacesOrNewline(str) { |
| 1517 |
str = CompressRunsOfSpaces(str) |
| 1518 |
} |
| 1519 |
} |
| 1520 |
if allowEmbed { |
| 1521 |
if amper { |
| 1522 |
str = CleanupEncodedMarkup(str) |
| 1523 |
} |
| 1524 |
} |
| 1525 |
if doScript { |
| 1526 |
if mixed && HasAngleOrAmpersandEncoding(str) { |
| 1527 |
str = RepairScriptMarkup(str, CONCISE) |
| 1528 |
} |
| 1529 |
} |
| 1530 |
if doMathML { |
| 1531 |
if mixed && HasAngleOrAmpersandEncoding(str) { |
| 1532 |
str = RepairMathMLMarkup(str, mathMLFix) |
| 1533 |
} |
| 1534 |
} |
| 1535 |
if doStrict { |
| 1536 |
if ascii { |
| 1537 |
if HasUnicodeMarkup(str) { |
| 1538 |
str = RepairUnicodeMarkup(str, TAGS) |
| 1539 |
mixed = true |
| 1540 |
} |
| 1541 |
} |
| 1542 |
if mixed || amper { |
| 1543 |
if HasAngleOrAmpersandEncoding(str) { |
| 1544 |
str = RepairScriptMarkup(str, CONCISE) |
| 1545 |
str = RepairTableMarkup(str, SPACE) |
| 1546 |
// call RepairScriptMarkup before RemoveEmbeddedMarkup |
| 1547 |
str = RemoveEmbeddedMarkup(str, NOMARKUP) |
| 1548 |
} |
| 1549 |
} |
| 1550 |
if ascii && HasBadSpace(str) { |
| 1551 |
str = CleanupBadSpaces(str) |
| 1552 |
} |
| 1553 |
if HasAdjacentSpaces(str) { |
| 1554 |
str = CompressRunsOfSpaces(str) |
| 1555 |
} |
| 1556 |
// Remove MathML artifact |
| 1557 |
if NeedsTightening(str) { |
| 1558 |
str = TightenParentheses(str) |
| 1559 |
} |
| 1560 |
} |
| 1561 |
if doMixed { |
| 1562 |
if ascii { |
| 1563 |
if HasUnicodeMarkup(str) { |
| 1564 |
str = RepairUnicodeMarkup(str, TAGS) |
| 1565 |
mixed = true |
| 1566 |
} |
| 1567 |
} |
| 1568 |
if mixed { |
| 1569 |
str = DoTrimFlankingHTML(str) |
| 1570 |
} |
| 1571 |
if ascii && HasBadSpace(str) { |
| 1572 |
str = CleanupBadSpaces(str) |
| 1573 |
} |
| 1574 |
if HasAdjacentSpaces(str) { |
| 1575 |
str = CompressRunsOfSpaces(str) |
| 1576 |
} |
| 1577 |
} |
| 1578 |
if deAccent { |
| 1579 |
if ascii { |
| 1580 |
str = TransformAccents(str, true, false) |
| 1581 |
if HasAdjacentSpaces(str) { |
| 1582 |
str = CompressRunsOfSpaces(str) |
| 1583 |
} |
| 1584 |
} |
| 1585 |
} |
| 1586 |
if deSymbol { |
| 1587 |
if ascii { |
| 1588 |
str = FixMisusedLetters(str, false, true, false) |
| 1589 |
str = TransformAccents(str, true, false) |
| 1590 |
if HasAdjacentSpaces(str) { |
| 1591 |
str = CompressRunsOfSpaces(str) |
| 1592 |
} |
| 1593 |
} |
| 1594 |
} |
| 1595 |
if doASCII { |
| 1596 |
if ascii { |
| 1597 |
str = UnicodeToASCII(str) |
| 1598 |
} |
| 1599 |
} |
| 1600 |
|
| 1601 |
if HasFlankingSpace(str) { |
| 1602 |
str = strings.TrimSpace(str) |
| 1603 |
} |
| 1604 |
|
| 1605 |
return str |
| 1606 |
} |
| 1607 |
|
| 1608 |
// CleanupEncodedMarkup removes ampersand-encoded markup |
| 1609 |
func CleanupEncodedMarkup(str string) string { |
| 1610 |
|
| 1611 |
// convert <sup> to <sup> (html subset) |
| 1612 |
// convert &#181; to µ (but not further - use html.UnescapeString) |
| 1613 |
// convert &amp;amp;amp;amp;amp;amp;lt; to < |
| 1614 |
// remove </sub><sub> or </sup><sup> (internals) |
| 1615 |
|
| 1616 |
var buffer strings.Builder |
| 1617 |
|
| 1618 |
lookAhead := func(txt string, to int) string { |
| 1619 |
mx := len(txt) |
| 1620 |
if to > mx { |
| 1621 |
to = mx |
| 1622 |
} |
| 1623 |
pos := strings.Index(txt[:to], "gt;") |
| 1624 |
if pos > 0 { |
| 1625 |
to = pos + 3 |
| 1626 |
} |
| 1627 |
return txt[:to] |
| 1628 |
} |
| 1629 |
|
| 1630 |
skip := 0 |
| 1631 |
|
| 1632 |
for i, ch := range str { |
| 1633 |
if skip > 0 { |
| 1634 |
skip-- |
| 1635 |
continue |
| 1636 |
} |
| 1637 |
if ch == '<' { |
| 1638 |
// remove internal tags in runs of subscripts or superscripts |
| 1639 |
if strings.HasPrefix(str[i:], "</sub><sub>") || strings.HasPrefix(str[i:], "</sup><sup>") { |
| 1640 |
skip = 10 |
| 1641 |
continue |
| 1642 |
} |
| 1643 |
buffer.WriteRune(ch) |
| 1644 |
continue |
| 1645 |
} else if ch != '&' { |
| 1646 |
buffer.WriteRune(ch) |
| 1647 |
continue |
| 1648 |
} else if strings.HasPrefix(str[i:], "<") { |
| 1649 |
sub := lookAhead(str[i:], 14) |
| 1650 |
txt, ok := htmlRepair[sub] |
| 1651 |
if ok { |
| 1652 |
adv := len(sub) - 1 |
| 1653 |
// do not convert if flanked by spaces - it may be a scientific symbol, |
| 1654 |
// e.g., fragments <i> in PMID 9698410, or escaped <b> and <d> tags used |
| 1655 |
// to indicate stem position in letters in PMID 21892341 |
| 1656 |
if i < 1 || str[i-1] != ' ' || !strings.HasPrefix(str[i+adv:], "; ") { |
| 1657 |
buffer.WriteString(txt) |
| 1658 |
skip = adv |
| 1659 |
continue |
| 1660 |
} |
| 1661 |
} |
| 1662 |
} else if strings.HasPrefix(str[i:], "&") { |
| 1663 |
if strings.HasPrefix(str[i:], "&lt;") { |
| 1664 |
sub := lookAhead(str[i:], 22) |
| 1665 |
txt, ok := htmlRepair[sub] |
| 1666 |
if ok { |
| 1667 |
buffer.WriteString(txt) |
| 1668 |
skip = len(sub) - 1 |
| 1669 |
continue |
| 1670 |
} else { |
| 1671 |
buffer.WriteString("<") |
| 1672 |
skip = 7 |
| 1673 |
continue |
| 1674 |
} |
| 1675 |
} else if strings.HasPrefix(str[i:], "&gt;") { |
| 1676 |
buffer.WriteString(">") |
| 1677 |
skip = 7 |
| 1678 |
continue |
| 1679 |
} else { |
| 1680 |
skip = 4 |
| 1681 |
j := i + 5 |
| 1682 |
// remove runs of multiply-encoded ampersands |
| 1683 |
for strings.HasPrefix(str[j:], "amp;") { |
| 1684 |
skip += 4 |
| 1685 |
j += 4 |
| 1686 |
} |
| 1687 |
// then look for special symbols used in PubMed records |
| 1688 |
if strings.HasPrefix(str[j:], "lt;") { |
| 1689 |
buffer.WriteString("<") |
| 1690 |
skip += 3 |
| 1691 |
} else if strings.HasPrefix(str[j:], "gt;") { |
| 1692 |
buffer.WriteString(">") |
| 1693 |
skip += 3 |
| 1694 |
} else if strings.HasPrefix(str[j:], "frac") { |
| 1695 |
buffer.WriteString("&frac") |
| 1696 |
skip += 4 |
| 1697 |
} else if strings.HasPrefix(str[j:], "plusmn") { |
| 1698 |
buffer.WriteString("±") |
| 1699 |
skip += 6 |
| 1700 |
} else if strings.HasPrefix(str[j:], "acute") { |
| 1701 |
buffer.WriteString("´") |
| 1702 |
skip += 5 |
| 1703 |
} else if strings.HasPrefix(str[j:], "aacute") { |
| 1704 |
buffer.WriteString("á") |
| 1705 |
skip += 6 |
| 1706 |
} else if strings.HasPrefix(str[j:], "rsquo") { |
| 1707 |
buffer.WriteString("&rsquo") |
| 1708 |
skip += 5 |
| 1709 |
} else if strings.HasPrefix(str[j:], "lsquo") { |
| 1710 |
buffer.WriteString("&lsquo") |
| 1711 |
skip += 5 |
| 1712 |
} else if strings.HasPrefix(str[j:], "micro") { |
| 1713 |
buffer.WriteString("µ") |
| 1714 |
skip += 5 |
| 1715 |
} else if strings.HasPrefix(str[j:], "oslash") { |
| 1716 |
buffer.WriteString("ø") |
| 1717 |
skip += 6 |
| 1718 |
} else if strings.HasPrefix(str[j:], "kgr") { |
| 1719 |
buffer.WriteString("&kgr") |
| 1720 |
skip += 3 |
| 1721 |
} else if strings.HasPrefix(str[j:], "apos") { |
| 1722 |
buffer.WriteString("&apos") |
| 1723 |
skip += 4 |
| 1724 |
} else if strings.HasPrefix(str[j:], "quot") { |
| 1725 |
buffer.WriteString(""") |
| 1726 |
skip += 4 |
| 1727 |
} else if strings.HasPrefix(str[j:], "alpha") { |
| 1728 |
buffer.WriteString("&alpha") |
| 1729 |
skip += 5 |
| 1730 |
} else if strings.HasPrefix(str[j:], "beta") { |
| 1731 |
buffer.WriteString("&beta") |
| 1732 |
skip += 4 |
| 1733 |
} else if strings.HasPrefix(str[j:], "gamma") { |
| 1734 |
buffer.WriteString("&gamma") |
| 1735 |
skip += 5 |
| 1736 |
} else if strings.HasPrefix(str[j:], "Delta") { |
| 1737 |
buffer.WriteString("&Delta") |
| 1738 |
skip += 5 |
| 1739 |
} else if strings.HasPrefix(str[j:], "phi") { |
| 1740 |
buffer.WriteString("&phi") |
| 1741 |
skip += 3 |
| 1742 |
} else if strings.HasPrefix(str[j:], "ge") { |
| 1743 |
buffer.WriteString("&ge") |
| 1744 |
skip += 2 |
| 1745 |
} else if strings.HasPrefix(str[j:], "sup2") { |
| 1746 |
buffer.WriteString("²") |
| 1747 |
skip += 4 |
| 1748 |
} else if strings.HasPrefix(str[j:], "#") { |
| 1749 |
buffer.WriteString("&") |
| 1750 |
} else { |
| 1751 |
buffer.WriteString("&") |
| 1752 |
} |
| 1753 |
continue |
| 1754 |
} |
| 1755 |
} |
| 1756 |
|
| 1757 |
// if loop not continued by any preceding test, print character |
| 1758 |
buffer.WriteRune(ch) |
| 1759 |
} |
| 1760 |
|
| 1761 |
return buffer.String() |
| 1762 |
} |
| 1763 |
|
| 1764 |
// CleanupPlain removes embedded mixed-content markup tags |
| 1765 |
func CleanupPlain(str string, wrp bool) string { |
| 1766 |
|
| 1767 |
if str == "" { |
| 1768 |
return str |
| 1769 |
} |
| 1770 |
|
| 1771 |
str = strings.Replace(str, "\n", " ", -1) |
| 1772 |
|
| 1773 |
str = RemoveEmbeddedMarkup(str, NOMARKUP) |
| 1774 |
str = TransformAccents(str, false, false) |
| 1775 |
|
| 1776 |
if HasUnicodeMarkup(str) { |
| 1777 |
str = RepairUnicodeMarkup(str, SPACE) |
| 1778 |
} |
| 1779 |
if HasAngleOrAmpersandEncoding(str) { |
| 1780 |
str = RepairTableMarkup(str, SPACE) |
| 1781 |
str = RemoveHTMLDecorations(str) |
| 1782 |
if wrp { |
| 1783 |
str = encodeAngleBracketsAndAmpersand(str) |
| 1784 |
} |
| 1785 |
} |
| 1786 |
if HasBadSpace(str) { |
| 1787 |
str = CleanupBadSpaces(str) |
| 1788 |
} |
| 1789 |
if HasAdjacentSpaces(str) { |
| 1790 |
str = CompressRunsOfSpaces(str) |
| 1791 |
} |
| 1792 |
if HasExtraSpaces(str) { |
| 1793 |
str = RemoveExtraSpaces(str) |
| 1794 |
} |
| 1795 |
|
| 1796 |
return str |
| 1797 |
} |
| 1798 |
|
| 1799 |
// CleanupProse converts text to ASCII |
| 1800 |
func CleanupProse(str string, wrp bool) string { |
| 1801 |
|
| 1802 |
if str == "" { |
| 1803 |
return str |
| 1804 |
} |
| 1805 |
|
| 1806 |
str = strings.Replace(str, "\n", " ", -1) |
| 1807 |
|
| 1808 |
// CleanupBadSpaces must be called before TransformAccents, to convert Unicode En Space converted from   |
| 1809 |
if HasBadSpace(str) { |
| 1810 |
str = CleanupBadSpaces(str) |
| 1811 |
} |
| 1812 |
|
| 1813 |
if wrp { |
| 1814 |
str = html.UnescapeString(str) |
| 1815 |
} |
| 1816 |
|
| 1817 |
str = RemoveEmbeddedMarkup(str, SPACE) |
| 1818 |
str = TransformAccents(str, false, false) |
| 1819 |
str = FixMisusedLetters(str, true, false, true) |
| 1820 |
str = TransformAccents(str, false, false) |
| 1821 |
|
| 1822 |
if wrp { |
| 1823 |
str = html.EscapeString(str) |
| 1824 |
} |
| 1825 |
|
| 1826 |
if HasUnicodeMarkup(str) { |
| 1827 |
str = RepairUnicodeMarkup(str, SPACE) |
| 1828 |
} |
| 1829 |
if HasAngleOrAmpersandEncoding(str) { |
| 1830 |
str = RepairTableMarkup(str, SPACE) |
| 1831 |
str = RemoveHTMLDecorations(str) |
| 1832 |
if wrp { |
| 1833 |
str = encodeAngleBracketsAndAmpersand(str) |
| 1834 |
} |
| 1835 |
} |
| 1836 |
if HasBadSpace(str) { |
| 1837 |
str = CleanupBadSpaces(str) |
| 1838 |
} |
| 1839 |
if HasAdjacentSpaces(str) { |
| 1840 |
str = CompressRunsOfSpaces(str) |
| 1841 |
} |
| 1842 |
if HasExtraSpaces(str) { |
| 1843 |
str = RemoveExtraSpaces(str) |
| 1844 |
} |
| 1845 |
|
| 1846 |
return str |
| 1847 |
} |
| 1848 |
|
| 1849 |
// CleanupQuery performs optional operations on XML query strings |
| 1850 |
func CleanupQuery(str string, exactMatch, removeBrackets bool) string { |
| 1851 |
|
| 1852 |
if exactMatch { |
| 1853 |
str = html.EscapeString(str) |
| 1854 |
} |
| 1855 |
|
| 1856 |
// cleanup string |
| 1857 |
if IsNotASCII(str) { |
| 1858 |
str = TransformAccents(str, true, false) |
| 1859 |
if HasUnicodeMarkup(str) { |
| 1860 |
str = RepairUnicodeMarkup(str, SPACE) |
| 1861 |
} |
| 1862 |
} |
| 1863 |
|
| 1864 |
if exactMatch { |
| 1865 |
str = strings.ToLower(str) |
| 1866 |
} |
| 1867 |
|
| 1868 |
if HasBadSpace(str) { |
| 1869 |
str = CleanupBadSpaces(str) |
| 1870 |
} |
| 1871 |
|
| 1872 |
if removeBrackets { |
| 1873 |
if HasAngleOrAmpersandEncoding(str) { |
| 1874 |
str = CleanupEncodedMarkup(str) |
| 1875 |
str = RepairScriptMarkup(str, SPACE) |
| 1876 |
str = RepairMathMLMarkup(str, SPACE) |
| 1877 |
// RemoveEmbeddedMarkup must be called before UnescapeString, which was suppressed in ExploreElements |
| 1878 |
str = RemoveEmbeddedMarkup(str, NOMARKUP) |
| 1879 |
} |
| 1880 |
} |
| 1881 |
|
| 1882 |
if HasAmpOrNotASCII(str) { |
| 1883 |
str = html.UnescapeString(str) |
| 1884 |
} |
| 1885 |
|
| 1886 |
if IsNotASCII(str) { |
| 1887 |
if !exactMatch { |
| 1888 |
str = UnicodeToASCII(str) |
| 1889 |
} |
| 1890 |
} |
| 1891 |
if HasAdjacentSpaces(str) { |
| 1892 |
str = CompressRunsOfSpaces(str) |
| 1893 |
} |
| 1894 |
|
| 1895 |
return str |
| 1896 |
} |
| 1897 |
|
| 1898 |
// CleanupSimple normalizes accented letters, spell Greek letters |
| 1899 |
func CleanupSimple(str string, wrp bool) string { |
| 1900 |
|
| 1901 |
if str == "" { |
| 1902 |
return str |
| 1903 |
} |
| 1904 |
|
| 1905 |
str = strings.Replace(str, "\n", " ", -1) |
| 1906 |
|
| 1907 |
str = TransformAccents(str, true, false) |
| 1908 |
|
| 1909 |
if HasUnicodeMarkup(str) { |
| 1910 |
str = RepairUnicodeMarkup(str, SPACE) |
| 1911 |
} |
| 1912 |
if HasAngleOrAmpersandEncoding(str) { |
| 1913 |
str = RepairTableMarkup(str, SPACE) |
| 1914 |
str = RemoveHTMLDecorations(str) |
| 1915 |
if wrp { |
| 1916 |
str = encodeAngleBracketsAndAmpersand(str) |
| 1917 |
} |
| 1918 |
} |
| 1919 |
if HasBadSpace(str) { |
| 1920 |
str = CleanupBadSpaces(str) |
| 1921 |
} |
| 1922 |
if HasAdjacentSpaces(str) { |
| 1923 |
str = CompressRunsOfSpaces(str) |
| 1924 |
} |
| 1925 |
if HasExtraSpaces(str) { |
| 1926 |
str = RemoveExtraSpaces(str) |
| 1927 |
} |
| 1928 |
|
| 1929 |
return str |
| 1930 |
} |
| 1931 |
|
| 1932 |
// CompareAlphaOrNumericKeys is called by slices.Sort{ed}Func to sort strings in alphabetical or numeric order |
| 1933 |
func CompareAlphaOrNumericKeys(i, j string) int { |
| 1934 |
|
| 1935 |
if IsAllDigits(i) && IsAllDigits(j) { |
| 1936 |
// numeric sort on strings checks lengths first |
| 1937 |
lni, lnj := len(i), len(j) |
| 1938 |
// shorter string is numerically lower, assuming no leading zeros |
| 1939 |
if lni < lnj { |
| 1940 |
return -1 |
| 1941 |
} |
| 1942 |
if lni > lnj { |
| 1943 |
return 1 |
| 1944 |
} |
| 1945 |
// same length allows comparison of numeric values without |
| 1946 |
// needing expensive string-to-integer conversion |
| 1947 |
} |
| 1948 |
|
| 1949 |
// same length or non-numeric, can now do string comparison on contents |
| 1950 |
return cmp.Compare(i, j) |
| 1951 |
} |
| 1952 |
|
| 1953 |
// CompressRunsOfSpaces turns runs of spaces into a single space |
| 1954 |
func CompressRunsOfSpaces(str string) string { |
| 1955 |
|
| 1956 |
// return strings.Join(strings.Fields(str), " ") |
| 1957 |
|
| 1958 |
whiteSpace := false |
| 1959 |
var buffer strings.Builder |
| 1960 |
|
| 1961 |
for _, ch := range str { |
| 1962 |
if ch < 127 && inBlank[ch] { |
| 1963 |
if !whiteSpace { |
| 1964 |
buffer.WriteRune(' ') |
| 1965 |
} |
| 1966 |
whiteSpace = true |
| 1967 |
} else { |
| 1968 |
buffer.WriteRune(ch) |
| 1969 |
whiteSpace = false |
| 1970 |
} |
| 1971 |
} |
| 1972 |
|
| 1973 |
return buffer.String() |
| 1974 |
} |
| 1975 |
|
| 1976 |
// ConvertSlash maps backslash-prefixed letter to ASCII control character |
| 1977 |
func ConvertSlash(str string) string { |
| 1978 |
|
| 1979 |
if str == "" { |
| 1980 |
return str |
| 1981 |
} |
| 1982 |
|
| 1983 |
length := len(str) |
| 1984 |
res := make([]byte, length+1, length+1) |
| 1985 |
|
| 1986 |
isSlash := false |
| 1987 |
idx := 0 |
| 1988 |
for _, ch := range str { |
| 1989 |
if isSlash { |
| 1990 |
switch ch { |
| 1991 |
case 'n': |
| 1992 |
// line feed |
| 1993 |
res[idx] = '\n' |
| 1994 |
case 'r': |
| 1995 |
// carriage return |
| 1996 |
res[idx] = '\r' |
| 1997 |
case 't': |
| 1998 |
// horizontal tab |
| 1999 |
res[idx] = '\t' |
| 2000 |
case 'f': |
| 2001 |
// form feed |
| 2002 |
res[idx] = '\f' |
| 2003 |
case 'a': |
| 2004 |
// audible bell from terminal (undocumented) |
| 2005 |
res[idx] = '\x07' |
| 2006 |
default: |
| 2007 |
res[idx] = byte(ch) |
| 2008 |
} |
| 2009 |
idx++ |
| 2010 |
isSlash = false |
| 2011 |
} else if ch == '\\' { |
| 2012 |
isSlash = true |
| 2013 |
} else { |
| 2014 |
res[idx] = byte(ch) |
| 2015 |
idx++ |
| 2016 |
} |
| 2017 |
} |
| 2018 |
|
| 2019 |
res = res[0:idx] |
| 2020 |
|
| 2021 |
return string(res) |
| 2022 |
} |
| 2023 |
|
| 2024 |
// DoTrimFlankingHTML removes HTML decorations at beginning or end of string |
| 2025 |
func DoTrimFlankingHTML(str string) string { |
| 2026 |
|
| 2027 |
badPrefix := [10]string{ |
| 2028 |
"<i></i>", |
| 2029 |
"<b></b>", |
| 2030 |
"<u></u>", |
| 2031 |
"<sup></sup>", |
| 2032 |
"<sub></sub>", |
| 2033 |
"</i>", |
| 2034 |
"</b>", |
| 2035 |
"</u>", |
| 2036 |
"</sup>", |
| 2037 |
"</sub>", |
| 2038 |
} |
| 2039 |
|
| 2040 |
badSuffix := [10]string{ |
| 2041 |
"<i></i>", |
| 2042 |
"<b></b>", |
| 2043 |
"<u></u>", |
| 2044 |
"<sup></sup>", |
| 2045 |
"<sub></sub>", |
| 2046 |
"<i>", |
| 2047 |
"<b>", |
| 2048 |
"<u>", |
| 2049 |
"<sup>", |
| 2050 |
"<sub>", |
| 2051 |
} |
| 2052 |
|
| 2053 |
if strings.Contains(str, "<") { |
| 2054 |
goOn := true |
| 2055 |
for goOn { |
| 2056 |
goOn = false |
| 2057 |
for _, tag := range badPrefix { |
| 2058 |
if strings.HasPrefix(str, tag) { |
| 2059 |
str = str[len(tag):] |
| 2060 |
goOn = true |
| 2061 |
} |
| 2062 |
} |
| 2063 |
for _, tag := range badSuffix { |
| 2064 |
if strings.HasSuffix(str, tag) { |
| 2065 |
str = str[:len(str)-len(tag)] |
| 2066 |
goOn = true |
| 2067 |
} |
| 2068 |
} |
| 2069 |
} |
| 2070 |
} |
| 2071 |
|
| 2072 |
return str |
| 2073 |
} |
| 2074 |
|
| 2075 |
// FixSpecialCases fixes hyphenated or primed prefixes and suffixes for indexing |
| 2076 |
func FixSpecialCases(str string) string { |
| 2077 |
|
| 2078 |
var arry []string |
| 2079 |
var buffer strings.Builder |
| 2080 |
|
| 2081 |
terms := strings.Fields(str) |
| 2082 |
|
| 2083 |
for _, item := range terms { |
| 2084 |
|
| 2085 |
buffer.Reset() |
| 2086 |
|
| 2087 |
for i, ch := range item { |
| 2088 |
if ch == '-' { |
| 2089 |
_, ok := hyphenatedPrefixes[item[0:i]] |
| 2090 |
if ok { |
| 2091 |
continue |
| 2092 |
} |
| 2093 |
} else if ch == '\'' { |
| 2094 |
_, ok := primedPrefixes[item[0:i]] |
| 2095 |
if ok { |
| 2096 |
buffer.WriteString("_prime ") |
| 2097 |
continue |
| 2098 |
} |
| 2099 |
_, ok = primedSuffix[item[i:]] |
| 2100 |
if ok { |
| 2101 |
continue |
| 2102 |
} |
| 2103 |
} |
| 2104 |
buffer.WriteRune(ch) |
| 2105 |
} |
| 2106 |
|
| 2107 |
item = buffer.String() |
| 2108 |
|
| 2109 |
arry = append(arry, item) |
| 2110 |
} |
| 2111 |
|
| 2112 |
// reconstruct string from transformed words |
| 2113 |
str = strings.Join(arry, " ") |
| 2114 |
|
| 2115 |
return str |
| 2116 |
} |
| 2117 |
|
| 2118 |
// FlattenMathML removes embedded MathML structure |
| 2119 |
func FlattenMathML(str string, policy int) string { |
| 2120 |
|
| 2121 |
findNextXMLBlock := func(txt string) (int, int, bool) { |
| 2122 |
|
| 2123 |
beg := strings.Index(txt, "<") |
| 2124 |
if beg < 0 { |
| 2125 |
return -1, -1, false |
| 2126 |
} |
| 2127 |
end := strings.Index(txt, ">") |
| 2128 |
if end < 0 { |
| 2129 |
return -1, -1, false |
| 2130 |
} |
| 2131 |
end++ |
| 2132 |
return beg, end, true |
| 2133 |
} |
| 2134 |
|
| 2135 |
var arry []string |
| 2136 |
|
| 2137 |
for { |
| 2138 |
beg, end, ok := findNextXMLBlock(str) |
| 2139 |
if !ok { |
| 2140 |
break |
| 2141 |
} |
| 2142 |
pfx := str[:beg] |
| 2143 |
pfx = strings.TrimSpace(pfx) |
| 2144 |
if pfx != "" { |
| 2145 |
arry = append(arry, pfx) |
| 2146 |
} |
| 2147 |
tmp := str[beg:end] |
| 2148 |
tmp = strings.TrimSpace(tmp) |
| 2149 |
str = str[end:] |
| 2150 |
} |
| 2151 |
|
| 2152 |
switch policy { |
| 2153 |
case PERIOD: |
| 2154 |
case SPACE: |
| 2155 |
str = strings.Join(arry, " ") |
| 2156 |
case CONCISE: |
| 2157 |
case BRACKETS: |
| 2158 |
case MARKDOWN: |
| 2159 |
case SLASH: |
| 2160 |
case TAGS: |
| 2161 |
case TERSE: |
| 2162 |
str = strings.Join(arry, "") |
| 2163 |
} |
| 2164 |
|
| 2165 |
str = strings.TrimSpace(str) |
| 2166 |
|
| 2167 |
// str = RemoveEmbeddedMarkup(str, NOMARKUP) |
| 2168 |
|
| 2169 |
return str |
| 2170 |
} |
| 2171 |
|
| 2172 |
// GenBankToMedlineAuthors changes punctuation to make authors searchable |
| 2173 |
func GenBankToMedlineAuthors(name string) string { |
| 2174 |
|
| 2175 |
if name == "" { |
| 2176 |
return name |
| 2177 |
} |
| 2178 |
|
| 2179 |
// start with "Smith-Jones,J.-P." |
| 2180 |
// change comma to space |
| 2181 |
name = strings.Replace(name, ",", " ", -1) |
| 2182 |
// remove all periods |
| 2183 |
name = strings.Replace(name, ".", "", -1) |
| 2184 |
name = strings.TrimSpace(name) |
| 2185 |
idx := strings.LastIndex(name, " ") |
| 2186 |
if idx >= 0 { |
| 2187 |
// remove hyphen only from initials |
| 2188 |
rgt := name[idx:] |
| 2189 |
lft := name[:idx] |
| 2190 |
rgt = strings.Replace(rgt, "-", "", -1) |
| 2191 |
lft = strings.TrimSpace(lft) |
| 2192 |
rgt = strings.TrimSpace(rgt) |
| 2193 |
name = lft + " " + rgt |
| 2194 |
} |
| 2195 |
// end with "Smith-Jones JP" |
| 2196 |
|
| 2197 |
return name |
| 2198 |
} |
| 2199 |
|
| 2200 |
// HasAdjacentSpaces reports if CompressRunsOfSpaces is needed |
| 2201 |
func HasAdjacentSpaces(str string) bool { |
| 2202 |
|
| 2203 |
whiteSpace := false |
| 2204 |
|
| 2205 |
for _, ch := range str { |
| 2206 |
if ch == ' ' || ch == '\n' { |
| 2207 |
if whiteSpace { |
| 2208 |
return true |
| 2209 |
} |
| 2210 |
whiteSpace = true |
| 2211 |
} else { |
| 2212 |
whiteSpace = false |
| 2213 |
} |
| 2214 |
} |
| 2215 |
|
| 2216 |
return false |
| 2217 |
} |
| 2218 |
|
| 2219 |
// HasAdjacentSpacesOrNewline reports if CompressRunsOfSpaces is needed |
| 2220 |
func HasAdjacentSpacesOrNewline(str string) bool { |
| 2221 |
|
| 2222 |
whiteSpace := false |
| 2223 |
|
| 2224 |
for _, ch := range str { |
| 2225 |
if ch == '\n' { |
| 2226 |
return true |
| 2227 |
} |
| 2228 |
if ch == ' ' { |
| 2229 |
if whiteSpace { |
| 2230 |
return true |
| 2231 |
} |
| 2232 |
whiteSpace = true |
| 2233 |
} else { |
| 2234 |
whiteSpace = false |
| 2235 |
} |
| 2236 |
} |
| 2237 |
|
| 2238 |
return false |
| 2239 |
} |
| 2240 |
|
| 2241 |
// HasAmpOrNotASCII reports if UnescapeString is needed |
| 2242 |
func HasAmpOrNotASCII(str string) bool { |
| 2243 |
|
| 2244 |
for _, ch := range str { |
| 2245 |
if ch == '&' || ch > 127 { |
| 2246 |
return true |
| 2247 |
} |
| 2248 |
} |
| 2249 |
|
| 2250 |
return false |
| 2251 |
} |
| 2252 |
|
| 2253 |
// HasAngleOrAmpersandEncoding reports angle brackets or ampersand encodings |
| 2254 |
func HasAngleOrAmpersandEncoding(str string) bool { |
| 2255 |
|
| 2256 |
hasAmp := false |
| 2257 |
hasSemi := false |
| 2258 |
|
| 2259 |
for _, ch := range str { |
| 2260 |
if ch == '<' || ch == '>' { |
| 2261 |
return true |
| 2262 |
} else if ch == '&' { |
| 2263 |
hasAmp = true |
| 2264 |
} else if ch == ';' { |
| 2265 |
hasSemi = true |
| 2266 |
} |
| 2267 |
} |
| 2268 |
|
| 2269 |
if hasAmp && hasSemi { |
| 2270 |
/* |
| 2271 |
if strings.Contains(str, "<") || |
| 2272 |
strings.Contains(str, ">") || |
| 2273 |
strings.Contains(str, "&") { |
| 2274 |
return true |
| 2275 |
} |
| 2276 |
*/ |
| 2277 |
return true |
| 2278 |
} |
| 2279 |
|
| 2280 |
return false |
| 2281 |
} |
| 2282 |
|
| 2283 |
// HasBadSpace matches Unicode spaces other than ASCII space |
| 2284 |
func HasBadSpace(str string) bool { |
| 2285 |
|
| 2286 |
for _, ch := range str { |
| 2287 |
if ch > 127 { |
| 2288 |
if unicode.IsSpace(ch) { |
| 2289 |
return true |
| 2290 |
} |
| 2291 |
// control characters |
| 2292 |
if ch >= 0x0080 && ch <= 0x009F { |
| 2293 |
return true |
| 2294 |
} |
| 2295 |
// no-break space |
| 2296 |
if ch == 0x00A0 { |
| 2297 |
return true |
| 2298 |
} |
| 2299 |
// special spaces (en space through zero width space) |
| 2300 |
if ch >= 0x2002 && ch <= 0x200B { |
| 2301 |
return true |
| 2302 |
} |
| 2303 |
// narrow no-break space |
| 2304 |
if ch == 0x202F { |
| 2305 |
return true |
| 2306 |
} |
| 2307 |
// ideographic space |
| 2308 |
if ch == 0x3000 { |
| 2309 |
return true |
| 2310 |
} |
| 2311 |
// unassigned values (private use) |
| 2312 |
if ch >= 0xE000 && ch <= 0xF8FF { |
| 2313 |
return true |
| 2314 |
} |
| 2315 |
// zero width no-break space |
| 2316 |
if ch == 0xFEFF { |
| 2317 |
return true |
| 2318 |
} |
| 2319 |
} |
| 2320 |
} |
| 2321 |
|
| 2322 |
return false |
| 2323 |
} |
| 2324 |
|
| 2325 |
// HasCombiningAccent reports on spelled-out combining accents |
| 2326 |
func HasCombiningAccent(str string) bool { |
| 2327 |
|
| 2328 |
// will search with components of lower-case combiningAccents map |
| 2329 |
str = strings.ToLower(str) |
| 2330 |
|
| 2331 |
// do not require whole-phrase context or leading square bracket |
| 2332 |
if strings.Index(str, "combining") >= 0 { |
| 2333 |
for ky := range lcBadAccents { |
| 2334 |
pos := strings.Index(str, ky) |
| 2335 |
if pos >= 0 { |
| 2336 |
return true |
| 2337 |
} |
| 2338 |
} |
| 2339 |
} |
| 2340 |
|
| 2341 |
return false |
| 2342 |
} |
| 2343 |
|
| 2344 |
// HasCommaOrSemicolon reports on comma, semicolon, or hyphen |
| 2345 |
func HasCommaOrSemicolon(str string) bool { |
| 2346 |
|
| 2347 |
for _, ch := range str { |
| 2348 |
if ch == ',' || ch == ';' || ch == '-' { |
| 2349 |
return true |
| 2350 |
} |
| 2351 |
} |
| 2352 |
|
| 2353 |
return false |
| 2354 |
} |
| 2355 |
|
| 2356 |
// HasExtraSpaces determines whether RemoveExtraSpaces needs to be called |
| 2357 |
func HasExtraSpaces(str string) bool { |
| 2358 |
|
| 2359 |
if len(str) < 2 { |
| 2360 |
return false |
| 2361 |
} |
| 2362 |
|
| 2363 |
var prev rune |
| 2364 |
|
| 2365 |
for _, ch := range str { |
| 2366 |
if prev == '(' && ch == ' ' { |
| 2367 |
return true |
| 2368 |
} |
| 2369 |
if prev == ' ' && ch == ')' { |
| 2370 |
return true |
| 2371 |
} |
| 2372 |
if prev == '-' && ch == ' ' { |
| 2373 |
return true |
| 2374 |
} |
| 2375 |
if prev == ' ' && ch == ',' { |
| 2376 |
return true |
| 2377 |
} |
| 2378 |
prev = ch |
| 2379 |
} |
| 2380 |
|
| 2381 |
return false |
| 2382 |
} |
| 2383 |
|
| 2384 |
// HasFlankingSpace returns true if string starts or ends with whitespace |
| 2385 |
func HasFlankingSpace(str string) bool { |
| 2386 |
|
| 2387 |
if str == "" { |
| 2388 |
return false |
| 2389 |
} |
| 2390 |
|
| 2391 |
ch := str[0] |
| 2392 |
if ch < 127 && inBlank[ch] { |
| 2393 |
return true |
| 2394 |
} |
| 2395 |
|
| 2396 |
strlen := len(str) |
| 2397 |
ch = str[strlen-1] |
| 2398 |
if ch < 127 && inBlank[ch] { |
| 2399 |
return true |
| 2400 |
} |
| 2401 |
|
| 2402 |
return false |
| 2403 |
} |
| 2404 |
|
| 2405 |
// HasHyphenOrApostrophe reports if string may need indexing preprocessing |
| 2406 |
func HasHyphenOrApostrophe(str string) bool { |
| 2407 |
|
| 2408 |
for _, ch := range str { |
| 2409 |
if ch == '-' || ch == '\'' { |
| 2410 |
return true |
| 2411 |
} |
| 2412 |
} |
| 2413 |
|
| 2414 |
return false |
| 2415 |
} |
| 2416 |
|
| 2417 |
// HasInvisibleUnicode checks for invisible characters |
| 2418 |
func HasInvisibleUnicode(str string) bool { |
| 2419 |
|
| 2420 |
for _, ch := range str { |
| 2421 |
if invisibleRunes[ch] { |
| 2422 |
return true |
| 2423 |
} |
| 2424 |
} |
| 2425 |
|
| 2426 |
return false |
| 2427 |
} |
| 2428 |
|
| 2429 |
// HasLeadingUnicodeDash checks for Unicode dash at beginning of argument |
| 2430 |
func HasLeadingUnicodeDash(str string) (rune, bool) { |
| 2431 |
|
| 2432 |
// intentional Unicode dash at beginning of non-command argument must be escaped by a backslash |
| 2433 |
|
| 2434 |
for _, ch := range str { |
| 2435 |
|
| 2436 |
// test 32-bit rune |
| 2437 |
if ch >= 2010 && ch <= 8213 { |
| 2438 |
return ch, true |
| 2439 |
} |
| 2440 |
|
| 2441 |
if ch == '-' { |
| 2442 |
// keep checking for dash after leading hyphen |
| 2443 |
continue |
| 2444 |
} |
| 2445 |
|
| 2446 |
// return once past hyphens |
| 2447 |
break |
| 2448 |
} |
| 2449 |
|
| 2450 |
return 0, false |
| 2451 |
} |
| 2452 |
|
| 2453 |
// HasSpaceOrHyphen checks for multiple words |
| 2454 |
func HasSpaceOrHyphen(str string) bool { |
| 2455 |
|
| 2456 |
for _, ch := range str { |
| 2457 |
if ch == ' ' || ch == '-' { |
| 2458 |
return true |
| 2459 |
} |
| 2460 |
} |
| 2461 |
|
| 2462 |
return false |
| 2463 |
} |
| 2464 |
|
| 2465 |
// HasUnicodeMarkup checks for Unicode superscript or subscript characters |
| 2466 |
func HasUnicodeMarkup(str string) bool { |
| 2467 |
|
| 2468 |
for _, ch := range str { |
| 2469 |
if ch <= 127 { |
| 2470 |
continue |
| 2471 |
} |
| 2472 |
// check for Unicode superscript or subscript characters |
| 2473 |
if ch == 0x00B2 || ch == 0x00B3 || ch == 0x00B9 || (ch >= 0x2070 && ch <= 0x208E) { |
| 2474 |
return true |
| 2475 |
} |
| 2476 |
} |
| 2477 |
|
| 2478 |
return false |
| 2479 |
} |
| 2480 |
|
| 2481 |
// HTMLAhead returns length if next symbol is mixed-content markup tag |
| 2482 |
func HTMLAhead(text string, idx, txtlen int) int { |
| 2483 |
|
| 2484 |
// record position of < character |
| 2485 |
start := idx |
| 2486 |
|
| 2487 |
// at start of element |
| 2488 |
idx++ |
| 2489 |
if idx >= txtlen { |
| 2490 |
return 0 |
| 2491 |
} |
| 2492 |
ch := text[idx] |
| 2493 |
|
| 2494 |
if ch == '/' { |
| 2495 |
// skip past end tag symbol |
| 2496 |
idx++ |
| 2497 |
ch = text[idx] |
| 2498 |
} |
| 2499 |
|
| 2500 |
// all embedded markup tags start with a lower-case letter |
| 2501 |
if ch < 'a' || ch > 'z' { |
| 2502 |
// except for DispFormula in PubmedArticle |
| 2503 |
if ch == 'D' && strings.HasPrefix(text[idx:], "DispFormula") { |
| 2504 |
for ch != '>' { |
| 2505 |
idx++ |
| 2506 |
ch = text[idx] |
| 2507 |
} |
| 2508 |
return idx + 1 - start |
| 2509 |
} |
| 2510 |
|
| 2511 |
// otherwise not a recognized markup tag |
| 2512 |
return 0 |
| 2513 |
} |
| 2514 |
|
| 2515 |
idx++ |
| 2516 |
ch = text[idx] |
| 2517 |
for inLower[ch] { |
| 2518 |
idx++ |
| 2519 |
ch = text[idx] |
| 2520 |
} |
| 2521 |
|
| 2522 |
// if tag name was not all lower-case, then exit |
| 2523 |
if ch >= 'A' && ch <= 'Z' { |
| 2524 |
return 0 |
| 2525 |
} |
| 2526 |
|
| 2527 |
// skip to end of element, past any attributes or slash character |
| 2528 |
for ch != '>' { |
| 2529 |
idx++ |
| 2530 |
ch = text[idx] |
| 2531 |
} |
| 2532 |
|
| 2533 |
// return number of characters to advance to skip this markup tag |
| 2534 |
return idx + 1 - start |
| 2535 |
} |
| 2536 |
|
| 2537 |
// HTMLRepair unexpands ampersand encoding to mixed-content formatting symbols |
| 2538 |
func HTMLRepair(str string) (string, bool) { |
| 2539 |
|
| 2540 |
res, ok := htmlRepair[str] |
| 2541 |
|
| 2542 |
return res, ok |
| 2543 |
} |
| 2544 |
|
| 2545 |
// IsAllCaps matches upper-case |
| 2546 |
func IsAllCaps(str string) bool { |
| 2547 |
|
| 2548 |
for _, ch := range str { |
| 2549 |
if !unicode.IsUpper(ch) { |
| 2550 |
return false |
| 2551 |
} |
| 2552 |
} |
| 2553 |
|
| 2554 |
return true |
| 2555 |
} |
| 2556 |
|
| 2557 |
// IsAllCapsOrDigits matches upper-case or digits |
| 2558 |
func IsAllCapsOrDigits(str string) bool { |
| 2559 |
|
| 2560 |
for _, ch := range str { |
| 2561 |
if !unicode.IsUpper(ch) && !unicode.IsDigit(ch) { |
| 2562 |
return false |
| 2563 |
} |
| 2564 |
} |
| 2565 |
|
| 2566 |
return true |
| 2567 |
} |
| 2568 |
|
| 2569 |
// IsAllDigits matches only digits |
| 2570 |
func IsAllDigits(str string) bool { |
| 2571 |
|
| 2572 |
for _, ch := range str { |
| 2573 |
if !unicode.IsDigit(ch) { |
| 2574 |
return false |
| 2575 |
} |
| 2576 |
} |
| 2577 |
|
| 2578 |
return true |
| 2579 |
} |
| 2580 |
|
| 2581 |
// IsAllDigitsOrPeriod accepts digits or a decimal point |
| 2582 |
func IsAllDigitsOrPeriod(str string) bool { |
| 2583 |
|
| 2584 |
for _, ch := range str { |
| 2585 |
if !unicode.IsDigit(ch) && ch != '.' { |
| 2586 |
return false |
| 2587 |
} |
| 2588 |
} |
| 2589 |
|
| 2590 |
return true |
| 2591 |
} |
| 2592 |
|
| 2593 |
// IsAllLetters matches only letters |
| 2594 |
func IsAllLetters(str string) bool { |
| 2595 |
|
| 2596 |
for _, ch := range str { |
| 2597 |
if !unicode.IsLetter(ch) { |
| 2598 |
return false |
| 2599 |
} |
| 2600 |
} |
| 2601 |
|
| 2602 |
return true |
| 2603 |
} |
| 2604 |
|
| 2605 |
// IsAllLettersOrDigits matches letters or digits (added for completeness) |
| 2606 |
func IsAllLettersOrDigits(str string) bool { |
| 2607 |
|
| 2608 |
for _, ch := range str { |
| 2609 |
if !unicode.IsLetter(ch) && !unicode.IsDigit(ch) { |
| 2610 |
return false |
| 2611 |
} |
| 2612 |
} |
| 2613 |
|
| 2614 |
return true |
| 2615 |
} |
| 2616 |
|
| 2617 |
// IsMixedCaps matches if word has both upper- and lower-case letters |
| 2618 |
// other than initial capital followed by all lower-case |
| 2619 |
func IsMixedCaps(str string) bool { |
| 2620 |
|
| 2621 |
numLower, nonInitialUpper := 0, 0 |
| 2622 |
|
| 2623 |
for i, ch := range str { |
| 2624 |
if unicode.IsLower(ch) { |
| 2625 |
numLower++ |
| 2626 |
} |
| 2627 |
if i > 0 && unicode.IsUpper(ch) { |
| 2628 |
nonInitialUpper++ |
| 2629 |
} |
| 2630 |
} |
| 2631 |
|
| 2632 |
if numLower > 0 && nonInitialUpper > 0 { |
| 2633 |
return true |
| 2634 |
} |
| 2635 |
|
| 2636 |
return false |
| 2637 |
} |
| 2638 |
|
| 2639 |
// IsNotASCII returns true for any non-ASCII Unicode character greater than 7-bits |
| 2640 |
func IsNotASCII(str string) bool { |
| 2641 |
|
| 2642 |
for _, ch := range str { |
| 2643 |
if ch > 127 { |
| 2644 |
return true |
| 2645 |
} |
| 2646 |
} |
| 2647 |
|
| 2648 |
return false |
| 2649 |
} |
| 2650 |
|
| 2651 |
// IsNotJustWhitespace returns true on any visible or non-whitespace control character |
| 2652 |
func IsNotJustWhitespace(str string) bool { |
| 2653 |
|
| 2654 |
for _, ch := range str { |
| 2655 |
if ch > 127 || !inBlank[ch] { |
| 2656 |
return true |
| 2657 |
} |
| 2658 |
} |
| 2659 |
|
| 2660 |
return false |
| 2661 |
} |
| 2662 |
|
| 2663 |
var plock sync.RWMutex |
| 2664 |
|
| 2665 |
// IsStopWord returns true for a stop word |
| 2666 |
func IsStopWord(str string) bool { |
| 2667 |
|
| 2668 |
plock.RLock() |
| 2669 |
isSW := isStopWord[str] |
| 2670 |
plock.RUnlock() |
| 2671 |
|
| 2672 |
return isSW |
| 2673 |
} |
| 2674 |
|
| 2675 |
// IsUnicodeSubsc matches Unicode subscript characters |
| 2676 |
func IsUnicodeSubsc(ch rune) bool { |
| 2677 |
return ch >= 0x2080 && ch <= 0x209C |
| 2678 |
} |
| 2679 |
|
| 2680 |
// IsUnicodeSuper matches Unicode superscript characters |
| 2681 |
func IsUnicodeSuper(ch rune) bool { |
| 2682 |
return ch == 0x00B2 || ch == 0x00B3 || ch == 0x00B9 || (ch >= 0x2070 && ch <= 0x207F) |
| 2683 |
} |
| 2684 |
|
| 2685 |
// Ncbi2naToIupac converts a hex-encoded ncbi2na binary nucleotide sequence to IUPAC |
| 2686 |
func Ncbi2naToIupac(str string) string { |
| 2687 |
|
| 2688 |
if str == "" { |
| 2689 |
return "" |
| 2690 |
} |
| 2691 |
|
| 2692 |
var buffer strings.Builder |
| 2693 |
|
| 2694 |
src := []byte(str) |
| 2695 |
dst := make([]byte, hex.DecodedLen(len(src))) |
| 2696 |
|
| 2697 |
n, err := hex.Decode(dst, src) |
| 2698 |
if err != nil { |
| 2699 |
return "" |
| 2700 |
} |
| 2701 |
|
| 2702 |
dst = dst[:n] |
| 2703 |
for _, byt := range dst { |
| 2704 |
tmp := ncbi2naToIupac[int(byt)] |
| 2705 |
buffer.WriteString(tmp) |
| 2706 |
} |
| 2707 |
|
| 2708 |
return buffer.String() |
| 2709 |
} |
| 2710 |
|
| 2711 |
// Ncbi4naToIupac converts a hex-encoded ncbi4na binary nucleotide sequence to IUPAC |
| 2712 |
func Ncbi4naToIupac(str string) string { |
| 2713 |
|
| 2714 |
if str == "" { |
| 2715 |
return "" |
| 2716 |
} |
| 2717 |
|
| 2718 |
var buffer strings.Builder |
| 2719 |
|
| 2720 |
src := []byte(str) |
| 2721 |
dst := make([]byte, hex.DecodedLen(len(src))) |
| 2722 |
|
| 2723 |
n, err := hex.Decode(dst, src) |
| 2724 |
if err != nil { |
| 2725 |
return "" |
| 2726 |
} |
| 2727 |
|
| 2728 |
dst = dst[:n] |
| 2729 |
for _, byt := range dst { |
| 2730 |
tmp := ncbi4naToIupac[int(byt)] |
| 2731 |
buffer.WriteString(tmp) |
| 2732 |
} |
| 2733 |
|
| 2734 |
return buffer.String() |
| 2735 |
} |
| 2736 |
|
| 2737 |
// NeedsTightening determines whether TightenParentheses needs to be called |
| 2738 |
func NeedsTightening(str string) bool { |
| 2739 |
|
| 2740 |
if len(str) < 2 { |
| 2741 |
return false |
| 2742 |
} |
| 2743 |
|
| 2744 |
var prev rune |
| 2745 |
|
| 2746 |
for _, ch := range str { |
| 2747 |
if prev == '(' && ch == ' ' { |
| 2748 |
return true |
| 2749 |
} |
| 2750 |
if prev == ' ' && ch == ')' { |
| 2751 |
return true |
| 2752 |
} |
| 2753 |
prev = ch |
| 2754 |
} |
| 2755 |
|
| 2756 |
return false |
| 2757 |
} |
| 2758 |
|
| 2759 |
// NormalizeAuthor is used for citation matching |
| 2760 |
func NormalizeAuthor(str string) string { |
| 2761 |
|
| 2762 |
if str == "" { |
| 2763 |
return str |
| 2764 |
} |
| 2765 |
|
| 2766 |
str = FixMisusedLetters(str, true, true, false) |
| 2767 |
str = TransformAccents(str, false, false) |
| 2768 |
|
| 2769 |
// convert numeric encoding to apostrophe |
| 2770 |
str = strings.Replace(str, "'", "'", -1) |
| 2771 |
// remove space following apostrophe |
| 2772 |
str = strings.Replace(str, "' ", "'", -1) |
| 2773 |
// then remove apostrophe to match indexing logic |
| 2774 |
str = strings.Replace(str, "'", "", -1) |
| 2775 |
|
| 2776 |
if HasUnicodeMarkup(str) { |
| 2777 |
str = RepairUnicodeMarkup(str, SPACE) |
| 2778 |
} |
| 2779 |
if HasAngleOrAmpersandEncoding(str) { |
| 2780 |
str = RepairTableMarkup(str, SPACE) |
| 2781 |
// if wrp // str = encodeAngleBracketsAndAmpersand(str) |
| 2782 |
} |
| 2783 |
if HasBadSpace(str) { |
| 2784 |
str = CleanupBadSpaces(str) |
| 2785 |
} |
| 2786 |
str = strings.Replace(str, "(", " ", -1) |
| 2787 |
str = strings.Replace(str, ")", " ", -1) |
| 2788 |
if HasAdjacentSpaces(str) { |
| 2789 |
str = CompressRunsOfSpaces(str) |
| 2790 |
} |
| 2791 |
if HasExtraSpaces(str) { |
| 2792 |
str = RemoveExtraSpaces(str) |
| 2793 |
} |
| 2794 |
|
| 2795 |
// remove jr and sr suffix |
| 2796 |
if strings.Index(str, " ") != strings.LastIndex(str, " ") { |
| 2797 |
str = strings.TrimSuffix(str, " jr") |
| 2798 |
str = strings.TrimSuffix(str, " Jr") |
| 2799 |
str = strings.TrimSuffix(str, " sr") |
| 2800 |
str = strings.TrimSuffix(str, " Sr") |
| 2801 |
} |
| 2802 |
|
| 2803 |
// normalize remaining punctuation |
| 2804 |
str = GenBankToMedlineAuthors(str) |
| 2805 |
|
| 2806 |
// truncate to single initial |
| 2807 |
pos := strings.LastIndex(str, " ") |
| 2808 |
if pos > 0 && len(str) > pos+2 { |
| 2809 |
str = str[:pos+2] |
| 2810 |
} |
| 2811 |
|
| 2812 |
str = CompressRunsOfSpaces(str) |
| 2813 |
str = strings.TrimSpace(str) |
| 2814 |
|
| 2815 |
return str |
| 2816 |
} |
| 2817 |
|
| 2818 |
// NormalizeJournal is used for citation matching |
| 2819 |
func NormalizeJournal(str string) string { |
| 2820 |
|
| 2821 |
if str == "" { |
| 2822 |
return str |
| 2823 |
} |
| 2824 |
|
| 2825 |
// convert numeric encoding to apostrophe |
| 2826 |
str = strings.Replace(str, "'", "'", -1) |
| 2827 |
// remove apostrophe, but do not remove trailing space |
| 2828 |
str = strings.Replace(str, "'", "", -1) |
| 2829 |
|
| 2830 |
// convert period to space |
| 2831 |
str = strings.Replace(str, ".", " ", -1) |
| 2832 |
|
| 2833 |
// convert " & " to " and " |
| 2834 |
str = strings.Replace(str, " & ", " and ", -1) |
| 2835 |
|
| 2836 |
words := strings.FieldsFunc(str, func(c rune) bool { |
| 2837 |
// split at non-alphanumeric characters |
| 2838 |
return (!unicode.IsLetter(c) && !unicode.IsDigit(c)) || c > 127 |
| 2839 |
}) |
| 2840 |
|
| 2841 |
// stop words to lower case unless first word of journal name or all capital letters or mixed case |
| 2842 |
var arry []string |
| 2843 |
|
| 2844 |
csr := cases.Title(language.English) |
| 2845 |
|
| 2846 |
isFirst := true |
| 2847 |
for _, item := range words { |
| 2848 |
|
| 2849 |
if !IsAllCaps(item) && !IsMixedCaps(item) { |
| 2850 |
item = strings.ToLower(item) |
| 2851 |
if !IsStopWord(item) || isFirst { |
| 2852 |
item = csr.String(item) |
| 2853 |
} |
| 2854 |
} |
| 2855 |
|
| 2856 |
arry = append(arry, item) |
| 2857 |
isFirst = false |
| 2858 |
} |
| 2859 |
|
| 2860 |
str = strings.Join(arry, " ") |
| 2861 |
|
| 2862 |
str = CompressRunsOfSpaces(str) |
| 2863 |
str = strings.TrimSpace(str) |
| 2864 |
|
| 2865 |
return str |
| 2866 |
} |
| 2867 |
|
| 2868 |
// NormalizePage is used for citation matching |
| 2869 |
func NormalizePage(str string) string { |
| 2870 |
|
| 2871 |
if str == "" { |
| 2872 |
return str |
| 2873 |
} |
| 2874 |
|
| 2875 |
// keep starting page before hyphen |
| 2876 |
str, _ = SplitInTwoLeft(str, "-") |
| 2877 |
|
| 2878 |
str = CompressRunsOfSpaces(str) |
| 2879 |
str = strings.TrimSpace(str) |
| 2880 |
|
| 2881 |
return str |
| 2882 |
} |
| 2883 |
|
| 2884 |
// NormalizeTitle is used for citation matching |
| 2885 |
func NormalizeTitle(str string) string { |
| 2886 |
|
| 2887 |
if str == "" { |
| 2888 |
return str |
| 2889 |
} |
| 2890 |
|
| 2891 |
str = FixMisusedLetters(str, true, false, true) |
| 2892 |
str = TransformAccents(str, false, false) |
| 2893 |
if HasUnicodeMarkup(str) { |
| 2894 |
str = RepairUnicodeMarkup(str, SPACE) |
| 2895 |
} |
| 2896 |
if HasAngleOrAmpersandEncoding(str) { |
| 2897 |
str = RepairTableMarkup(str, SPACE) |
| 2898 |
// if wrp // str = encodeAngleBracketsAndAmpersand(str) |
| 2899 |
} |
| 2900 |
if HasAmpOrNotASCII(str) { |
| 2901 |
str = html.UnescapeString(str) |
| 2902 |
} |
| 2903 |
if HasBadSpace(str) { |
| 2904 |
str = CleanupBadSpaces(str) |
| 2905 |
} |
| 2906 |
if HasAdjacentSpaces(str) { |
| 2907 |
str = CompressRunsOfSpaces(str) |
| 2908 |
} |
| 2909 |
if HasExtraSpaces(str) { |
| 2910 |
str = RemoveExtraSpaces(str) |
| 2911 |
} |
| 2912 |
|
| 2913 |
str = strings.Replace(str, "(", " ", -1) |
| 2914 |
str = strings.Replace(str, ")", " ", -1) |
| 2915 |
|
| 2916 |
str = strings.Replace(str, "_", " ", -1) |
| 2917 |
|
| 2918 |
str = CompressRunsOfSpaces(str) |
| 2919 |
str = strings.TrimSpace(str) |
| 2920 |
|
| 2921 |
return str |
| 2922 |
} |
| 2923 |
|
| 2924 |
// ParseIndex populates the XMLFind structure for matching by identifier |
| 2925 |
func ParseIndex(indx string) *XMLFind { |
| 2926 |
|
| 2927 |
if indx == "" { |
| 2928 |
return &XMLFind{} |
| 2929 |
} |
| 2930 |
|
| 2931 |
// parse parent/element@attribute^version index |
| 2932 |
prnt, match := SplitInTwoRight(indx, "/") |
| 2933 |
match, versn := SplitInTwoLeft(match, "^") |
| 2934 |
match, attrib := SplitInTwoLeft(match, "@") |
| 2935 |
|
| 2936 |
return &XMLFind{Index: indx, Parent: prnt, Match: match, Attrib: attrib, Versn: versn} |
| 2937 |
} |
| 2938 |
|
| 2939 |
// PrepareForIndexing performs cleanup and normalization of index and query strings |
| 2940 |
func PrepareForIndexing(str string, doHomoglyphs, isAuthor, isProse, spellGreek, reEncode bool) string { |
| 2941 |
|
| 2942 |
if IsNotASCII(str) { |
| 2943 |
str = FixMisusedLetters(str, doHomoglyphs, isAuthor, isProse) |
| 2944 |
str = TransformAccents(str, spellGreek, reEncode) |
| 2945 |
if HasUnicodeMarkup(str) { |
| 2946 |
str = RepairUnicodeMarkup(str, SPACE) |
| 2947 |
} |
| 2948 |
} |
| 2949 |
|
| 2950 |
str = strings.ToLower(str) |
| 2951 |
|
| 2952 |
if HasBadSpace(str) { |
| 2953 |
str = CleanupBadSpaces(str) |
| 2954 |
} |
| 2955 |
if HasAngleOrAmpersandEncoding(str) { |
| 2956 |
str = CleanupEncodedMarkup(str) |
| 2957 |
str = RepairTableMarkup(str, SPACE) |
| 2958 |
str = RepairScriptMarkup(str, SPACE) |
| 2959 |
str = RepairMathMLMarkup(str, SPACE) |
| 2960 |
// RemoveEmbeddedMarkup must be called before UnescapeString, which was suppressed in ExploreElements |
| 2961 |
str = RemoveEmbeddedMarkup(str, SPACE) |
| 2962 |
} |
| 2963 |
|
| 2964 |
if HasAmpOrNotASCII(str) { |
| 2965 |
str = html.UnescapeString(str) |
| 2966 |
str = strings.ToLower(str) |
| 2967 |
} |
| 2968 |
|
| 2969 |
if HasAdjacentSpaces(str) { |
| 2970 |
str = CompressRunsOfSpaces(str) |
| 2971 |
} |
| 2972 |
|
| 2973 |
str = strings.Replace(str, "(", " ", -1) |
| 2974 |
str = strings.Replace(str, ")", " ", -1) |
| 2975 |
|
| 2976 |
str = strings.Replace(str, "_", " ", -1) |
| 2977 |
|
| 2978 |
if HasHyphenOrApostrophe(str) { |
| 2979 |
str = FixSpecialCases(str) |
| 2980 |
} |
| 2981 |
|
| 2982 |
str = strings.Replace(str, "_", " ", -1) |
| 2983 |
str = strings.Replace(str, "-", " ", -1) |
| 2984 |
str = strings.Replace(str, "+", " ", -1) |
| 2985 |
str = strings.Replace(str, "~", " ", -1) |
| 2986 |
|
| 2987 |
// remove trailing punctuation from each word |
| 2988 |
var arry []string |
| 2989 |
|
| 2990 |
terms := strings.Fields(str) |
| 2991 |
for _, item := range terms { |
| 2992 |
max := len(item) |
| 2993 |
for max > 1 { |
| 2994 |
ch := item[max-1] |
| 2995 |
if ch != '.' && ch != ',' && ch != ':' && ch != ';' { |
| 2996 |
break |
| 2997 |
} |
| 2998 |
// trim trailing period, comma, colon, and semicolon |
| 2999 |
item = item[:max-1] |
| 3000 |
// continue checking for runs of punctuation at end |
| 3001 |
max-- |
| 3002 |
} |
| 3003 |
if item == "" { |
| 3004 |
continue |
| 3005 |
} |
| 3006 |
arry = append(arry, item) |
| 3007 |
} |
| 3008 |
|
| 3009 |
// rejoin into string |
| 3010 |
str = strings.Join(arry, " ") |
| 3011 |
|
| 3012 |
return str |
| 3013 |
} |
| 3014 |
|
| 3015 |
// RelaxString removes all punctuation from patterns and search text |
| 3016 |
func RelaxString(str string) string { |
| 3017 |
|
| 3018 |
if str == "" { |
| 3019 |
return str |
| 3020 |
} |
| 3021 |
|
| 3022 |
terms := strings.FieldsFunc(str, func(c rune) bool { |
| 3023 |
// split at non-alphanumeric characters |
| 3024 |
return (!unicode.IsLetter(c) && !unicode.IsDigit(c)) || c > 127 |
| 3025 |
}) |
| 3026 |
|
| 3027 |
str = strings.Join(terms, " ") |
| 3028 |
|
| 3029 |
str = CompressRunsOfSpaces(str) |
| 3030 |
str = strings.TrimSpace(str) |
| 3031 |
|
| 3032 |
return str |
| 3033 |
} |
| 3034 |
|
| 3035 |
// RemoveEmbeddedMarkup removes internal mixed-content sections |
| 3036 |
func RemoveEmbeddedMarkup(str string, policy int) string { |
| 3037 |
|
| 3038 |
inContent := true |
| 3039 |
shouldPad := false |
| 3040 |
var ( |
| 3041 |
curr rune |
| 3042 |
prev rune |
| 3043 |
buffer strings.Builder |
| 3044 |
) |
| 3045 |
|
| 3046 |
for _, ch := range str { |
| 3047 |
if ch == '<' { |
| 3048 |
inContent = false |
| 3049 |
} else if ch == '>' { |
| 3050 |
if !inContent { |
| 3051 |
shouldPad = true |
| 3052 |
} |
| 3053 |
inContent = true |
| 3054 |
} else if inContent { |
| 3055 |
prev = curr |
| 3056 |
curr = ch |
| 3057 |
if shouldPad { |
| 3058 |
if unicode.IsLetter(prev) && unicode.IsLetter(curr) { |
| 3059 |
switch policy { |
| 3060 |
case SPACE: |
| 3061 |
buffer.WriteRune(' ') |
| 3062 |
} |
| 3063 |
} else if unicode.IsDigit(prev) && unicode.IsDigit(curr) { |
| 3064 |
switch policy { |
| 3065 |
case SPACE: |
| 3066 |
buffer.WriteRune(' ') |
| 3067 |
} |
| 3068 |
} |
| 3069 |
} |
| 3070 |
shouldPad = false |
| 3071 |
buffer.WriteRune(ch) |
| 3072 |
} |
| 3073 |
} |
| 3074 |
|
| 3075 |
return buffer.String() |
| 3076 |
} |
| 3077 |
|
| 3078 |
// RemoveCommaOrSemicolon replaces comma or semicolon with space |
| 3079 |
func RemoveCommaOrSemicolon(str string) string { |
| 3080 |
|
| 3081 |
str = strings.ToLower(str) |
| 3082 |
|
| 3083 |
if HasCommaOrSemicolon(str) { |
| 3084 |
str = strings.Replace(str, ",", " ", -1) |
| 3085 |
str = strings.Replace(str, ";", " ", -1) |
| 3086 |
str = CompressRunsOfSpaces(str) |
| 3087 |
} |
| 3088 |
str = strings.TrimSpace(str) |
| 3089 |
str = strings.TrimRight(str, ".?:") |
| 3090 |
|
| 3091 |
return str |
| 3092 |
} |
| 3093 |
|
| 3094 |
// RemoveExtraSpaces removes spaces inside parentheses, after hyphen, and before comma |
| 3095 |
func RemoveExtraSpaces(str string) string { |
| 3096 |
|
| 3097 |
if len(str) < 2 { |
| 3098 |
return str |
| 3099 |
} |
| 3100 |
|
| 3101 |
var ( |
| 3102 |
buffer strings.Builder |
| 3103 |
prev rune |
| 3104 |
) |
| 3105 |
|
| 3106 |
for _, ch := range str { |
| 3107 |
if prev == '(' && ch == ' ' { |
| 3108 |
ch = '(' |
| 3109 |
} else if prev == ' ' && ch == ')' { |
| 3110 |
ch = ')' |
| 3111 |
} else if prev == '-' && ch == ' ' { |
| 3112 |
ch = '-' |
| 3113 |
} else if prev == ' ' && ch == ',' { |
| 3114 |
ch = ',' |
| 3115 |
} else if prev != 0 { |
| 3116 |
buffer.WriteRune(prev) |
| 3117 |
} |
| 3118 |
prev = ch |
| 3119 |
} |
| 3120 |
|
| 3121 |
buffer.WriteRune(prev) |
| 3122 |
|
| 3123 |
return buffer.String() |
| 3124 |
} |
| 3125 |
|
| 3126 |
// RemoveHTMLDecorations removes mixed-content formatting decorations |
| 3127 |
func RemoveHTMLDecorations(str string) string { |
| 3128 |
|
| 3129 |
// unescape converts <b> to <b> |
| 3130 |
// also µ to Greek mu character |
| 3131 |
// also &#x...; where x is followed by a hex value |
| 3132 |
str = html.UnescapeString(str) |
| 3133 |
// remove mixed content tags |
| 3134 |
str = mfix.Replace(str) |
| 3135 |
|
| 3136 |
return str |
| 3137 |
} |
| 3138 |
|
| 3139 |
// RepairMathMLMarkup removes MathML embedded markup symbols |
| 3140 |
func RepairMathMLMarkup(str string, policy int) string { |
| 3141 |
|
| 3142 |
str = strings.Replace(str, "> <mml:", "><mml:", -1) |
| 3143 |
str = strings.Replace(str, "> </mml:", "></mml:", -1) |
| 3144 |
|
| 3145 |
findNextMathBlock := func(txt string) (int, int, bool) { |
| 3146 |
|
| 3147 |
beg := strings.Index(txt, "<DispFormula") |
| 3148 |
if beg < 0 { |
| 3149 |
return -1, -1, false |
| 3150 |
} |
| 3151 |
end := strings.Index(txt, "</DispFormula>") |
| 3152 |
if end < 0 { |
| 3153 |
return -1, -1, false |
| 3154 |
} |
| 3155 |
end += 14 |
| 3156 |
return beg, end, true |
| 3157 |
} |
| 3158 |
|
| 3159 |
var arry []string |
| 3160 |
|
| 3161 |
for { |
| 3162 |
beg, end, ok := findNextMathBlock(str) |
| 3163 |
if !ok { |
| 3164 |
break |
| 3165 |
} |
| 3166 |
pfx := str[:beg] |
| 3167 |
pfx = strings.TrimSpace(pfx) |
| 3168 |
arry = append(arry, pfx) |
| 3169 |
tmp := str[beg:end] |
| 3170 |
if strings.HasPrefix(tmp, "<DispFormula") { |
| 3171 |
tmp = FlattenMathML(tmp, policy) |
| 3172 |
} |
| 3173 |
tmp = strings.TrimSpace(tmp) |
| 3174 |
arry = append(arry, tmp) |
| 3175 |
str = str[end:] |
| 3176 |
} |
| 3177 |
|
| 3178 |
str = strings.TrimSpace(str) |
| 3179 |
arry = append(arry, str) |
| 3180 |
|
| 3181 |
return strings.Join(arry, " ") |
| 3182 |
} |
| 3183 |
|
| 3184 |
// RepairScriptMarkup converts mixed-content subscript and superscript tags |
| 3185 |
func RepairScriptMarkup(str string, policy int) string { |
| 3186 |
|
| 3187 |
var buffer strings.Builder |
| 3188 |
|
| 3189 |
skip := 0 |
| 3190 |
|
| 3191 |
for i, ch := range str { |
| 3192 |
if skip > 0 { |
| 3193 |
skip-- |
| 3194 |
continue |
| 3195 |
} |
| 3196 |
if ch == '<' { |
| 3197 |
if strings.HasPrefix(str[i:], "<sub>") { |
| 3198 |
switch policy { |
| 3199 |
case PERIOD: |
| 3200 |
case SPACE: |
| 3201 |
case CONCISE: |
| 3202 |
case BRACKETS: |
| 3203 |
buffer.WriteRune('(') |
| 3204 |
case MARKDOWN: |
| 3205 |
buffer.WriteRune('~') |
| 3206 |
} |
| 3207 |
skip = 4 |
| 3208 |
continue |
| 3209 |
} |
| 3210 |
if strings.HasPrefix(str[i:], "<sup>") { |
| 3211 |
switch policy { |
| 3212 |
case PERIOD: |
| 3213 |
case SPACE: |
| 3214 |
case CONCISE: |
| 3215 |
buffer.WriteRune('[') |
| 3216 |
case BRACKETS: |
| 3217 |
buffer.WriteRune('[') |
| 3218 |
case MARKDOWN: |
| 3219 |
buffer.WriteRune('^') |
| 3220 |
} |
| 3221 |
skip = 4 |
| 3222 |
continue |
| 3223 |
} |
| 3224 |
if strings.HasPrefix(str[i:], "</sub>") { |
| 3225 |
if strings.HasPrefix(str[i+6:], "<sup>") { |
| 3226 |
switch policy { |
| 3227 |
case PERIOD: |
| 3228 |
buffer.WriteRune('.') |
| 3229 |
case SPACE: |
| 3230 |
buffer.WriteRune(' ') |
| 3231 |
case CONCISE: |
| 3232 |
buffer.WriteRune('[') |
| 3233 |
case BRACKETS: |
| 3234 |
buffer.WriteRune(')') |
| 3235 |
buffer.WriteRune('[') |
| 3236 |
case MARKDOWN: |
| 3237 |
buffer.WriteRune('~') |
| 3238 |
buffer.WriteRune('^') |
| 3239 |
} |
| 3240 |
skip = 10 |
| 3241 |
continue |
| 3242 |
} |
| 3243 |
switch policy { |
| 3244 |
case PERIOD: |
| 3245 |
case SPACE: |
| 3246 |
case CONCISE: |
| 3247 |
case BRACKETS: |
| 3248 |
buffer.WriteRune(')') |
| 3249 |
case MARKDOWN: |
| 3250 |
buffer.WriteRune('~') |
| 3251 |
} |
| 3252 |
skip = 5 |
| 3253 |
continue |
| 3254 |
} |
| 3255 |
if strings.HasPrefix(str[i:], "</sup>") { |
| 3256 |
if strings.HasPrefix(str[i+6:], "<sub>") { |
| 3257 |
switch policy { |
| 3258 |
case PERIOD: |
| 3259 |
buffer.WriteRune('.') |
| 3260 |
case SPACE: |
| 3261 |
buffer.WriteRune(' ') |
| 3262 |
case CONCISE: |
| 3263 |
buffer.WriteRune(']') |
| 3264 |
case BRACKETS: |
| 3265 |
buffer.WriteRune(']') |
| 3266 |
buffer.WriteRune('(') |
| 3267 |
case MARKDOWN: |
| 3268 |
buffer.WriteRune('^') |
| 3269 |
buffer.WriteRune('~') |
| 3270 |
} |
| 3271 |
skip = 10 |
| 3272 |
continue |
| 3273 |
} |
| 3274 |
switch policy { |
| 3275 |
case PERIOD: |
| 3276 |
case SPACE: |
| 3277 |
case CONCISE: |
| 3278 |
buffer.WriteRune(']') |
| 3279 |
case BRACKETS: |
| 3280 |
buffer.WriteRune(']') |
| 3281 |
case MARKDOWN: |
| 3282 |
buffer.WriteRune('^') |
| 3283 |
} |
| 3284 |
skip = 5 |
| 3285 |
continue |
| 3286 |
} |
| 3287 |
} |
| 3288 |
|
| 3289 |
buffer.WriteRune(ch) |
| 3290 |
} |
| 3291 |
|
| 3292 |
return buffer.String() |
| 3293 |
} |
| 3294 |
|
| 3295 |
// RepairTableMarkup removes HTML table element tags |
| 3296 |
func RepairTableMarkup(str string, policy int) string { |
| 3297 |
|
| 3298 |
str = strings.Replace(str, "<tr>", " ", -1) |
| 3299 |
str = strings.Replace(str, "<td>", " ", -1) |
| 3300 |
str = strings.Replace(str, "</tr>", " ", -1) |
| 3301 |
str = strings.Replace(str, "</td>", " ", -1) |
| 3302 |
|
| 3303 |
return str |
| 3304 |
} |
| 3305 |
|
| 3306 |
// RepairUnicodeMarkup handles Unicode superscript and subscript symbols |
| 3307 |
func RepairUnicodeMarkup(str string, policy int) string { |
| 3308 |
|
| 3309 |
type MarkupType int |
| 3310 |
|
| 3311 |
const ( |
| 3312 |
NOSCRIPT MarkupType = iota |
| 3313 |
SUPSCRIPT |
| 3314 |
SUBSCRIPT |
| 3315 |
PLAINDIGIT |
| 3316 |
) |
| 3317 |
|
| 3318 |
var buffer strings.Builder |
| 3319 |
|
| 3320 |
// to improve readability, keep track of switches between numeric types, add period at transitions when converting to plain ASCII |
| 3321 |
level := NOSCRIPT |
| 3322 |
|
| 3323 |
for _, ch := range str { |
| 3324 |
if ch > 127 { |
| 3325 |
rn, ok := superScriptRunes[ch] |
| 3326 |
if ok { |
| 3327 |
ch = rn |
| 3328 |
switch level { |
| 3329 |
case NOSCRIPT: |
| 3330 |
switch policy { |
| 3331 |
case PERIOD: |
| 3332 |
case SPACE: |
| 3333 |
case CONCISE: |
| 3334 |
buffer.WriteRune('[') |
| 3335 |
case BRACKETS: |
| 3336 |
buffer.WriteRune('[') |
| 3337 |
case MARKDOWN: |
| 3338 |
buffer.WriteRune('^') |
| 3339 |
case SLASH: |
| 3340 |
case TAGS: |
| 3341 |
buffer.WriteString("<sup>") |
| 3342 |
} |
| 3343 |
case SUPSCRIPT: |
| 3344 |
switch policy { |
| 3345 |
case PERIOD: |
| 3346 |
case SPACE: |
| 3347 |
case CONCISE: |
| 3348 |
case BRACKETS: |
| 3349 |
case MARKDOWN: |
| 3350 |
case SLASH: |
| 3351 |
case TAGS: |
| 3352 |
} |
| 3353 |
case SUBSCRIPT: |
| 3354 |
switch policy { |
| 3355 |
case PERIOD: |
| 3356 |
buffer.WriteRune('.') |
| 3357 |
case SPACE: |
| 3358 |
buffer.WriteRune(' ') |
| 3359 |
case CONCISE: |
| 3360 |
buffer.WriteRune('[') |
| 3361 |
case BRACKETS: |
| 3362 |
buffer.WriteRune(')') |
| 3363 |
buffer.WriteRune('[') |
| 3364 |
case MARKDOWN: |
| 3365 |
buffer.WriteRune('~') |
| 3366 |
buffer.WriteRune('^') |
| 3367 |
case SLASH: |
| 3368 |
buffer.WriteRune('\\') |
| 3369 |
case TAGS: |
| 3370 |
buffer.WriteString("</sub>") |
| 3371 |
buffer.WriteString("<sup>") |
| 3372 |
} |
| 3373 |
case PLAINDIGIT: |
| 3374 |
switch policy { |
| 3375 |
case PERIOD: |
| 3376 |
buffer.WriteRune('.') |
| 3377 |
case SPACE: |
| 3378 |
buffer.WriteRune(' ') |
| 3379 |
case CONCISE: |
| 3380 |
buffer.WriteRune('[') |
| 3381 |
case BRACKETS: |
| 3382 |
buffer.WriteRune('[') |
| 3383 |
case MARKDOWN: |
| 3384 |
buffer.WriteRune('^') |
| 3385 |
case SLASH: |
| 3386 |
buffer.WriteRune('\\') |
| 3387 |
case TAGS: |
| 3388 |
buffer.WriteString("<sup>") |
| 3389 |
} |
| 3390 |
} |
| 3391 |
level = SUPSCRIPT |
| 3392 |
buffer.WriteRune(ch) |
| 3393 |
continue |
| 3394 |
} |
| 3395 |
rn, ok = subScriptRunes[ch] |
| 3396 |
if ok { |
| 3397 |
ch = rn |
| 3398 |
switch level { |
| 3399 |
case NOSCRIPT: |
| 3400 |
switch policy { |
| 3401 |
case PERIOD: |
| 3402 |
case SPACE: |
| 3403 |
case CONCISE: |
| 3404 |
case BRACKETS: |
| 3405 |
buffer.WriteRune('(') |
| 3406 |
case MARKDOWN: |
| 3407 |
buffer.WriteRune('~') |
| 3408 |
case SLASH: |
| 3409 |
case TAGS: |
| 3410 |
buffer.WriteString("<sub>") |
| 3411 |
} |
| 3412 |
case SUPSCRIPT: |
| 3413 |
switch policy { |
| 3414 |
case PERIOD: |
| 3415 |
buffer.WriteRune('.') |
| 3416 |
case SPACE: |
| 3417 |
buffer.WriteRune(' ') |
| 3418 |
case CONCISE: |
| 3419 |
buffer.WriteRune(']') |
| 3420 |
case BRACKETS: |
| 3421 |
buffer.WriteRune(']') |
| 3422 |
buffer.WriteRune('(') |
| 3423 |
case MARKDOWN: |
| 3424 |
buffer.WriteRune('^') |
| 3425 |
buffer.WriteRune('~') |
| 3426 |
case SLASH: |
| 3427 |
buffer.WriteRune('/') |
| 3428 |
case TAGS: |
| 3429 |
buffer.WriteString("</sup>") |
| 3430 |
buffer.WriteString("<sub>") |
| 3431 |
} |
| 3432 |
case SUBSCRIPT: |
| 3433 |
switch policy { |
| 3434 |
case PERIOD: |
| 3435 |
case SPACE: |
| 3436 |
case CONCISE: |
| 3437 |
case BRACKETS: |
| 3438 |
case MARKDOWN: |
| 3439 |
case SLASH: |
| 3440 |
case TAGS: |
| 3441 |
} |
| 3442 |
case PLAINDIGIT: |
| 3443 |
switch policy { |
| 3444 |
case PERIOD: |
| 3445 |
buffer.WriteRune('.') |
| 3446 |
case SPACE: |
| 3447 |
buffer.WriteRune(' ') |
| 3448 |
case CONCISE: |
| 3449 |
case BRACKETS: |
| 3450 |
buffer.WriteRune('(') |
| 3451 |
case MARKDOWN: |
| 3452 |
buffer.WriteRune('~') |
| 3453 |
case SLASH: |
| 3454 |
buffer.WriteRune('/') |
| 3455 |
case TAGS: |
| 3456 |
buffer.WriteString("<sub>") |
| 3457 |
} |
| 3458 |
} |
| 3459 |
level = SUBSCRIPT |
| 3460 |
buffer.WriteRune(ch) |
| 3461 |
continue |
| 3462 |
} |
| 3463 |
switch level { |
| 3464 |
case NOSCRIPT: |
| 3465 |
switch policy { |
| 3466 |
case PERIOD: |
| 3467 |
case SPACE: |
| 3468 |
case CONCISE: |
| 3469 |
case BRACKETS: |
| 3470 |
case MARKDOWN: |
| 3471 |
case SLASH: |
| 3472 |
case TAGS: |
| 3473 |
} |
| 3474 |
case SUPSCRIPT: |
| 3475 |
switch policy { |
| 3476 |
case PERIOD: |
| 3477 |
case SPACE: |
| 3478 |
case CONCISE: |
| 3479 |
buffer.WriteRune(']') |
| 3480 |
case BRACKETS: |
| 3481 |
buffer.WriteRune(']') |
| 3482 |
case MARKDOWN: |
| 3483 |
buffer.WriteRune('^') |
| 3484 |
case SLASH: |
| 3485 |
case TAGS: |
| 3486 |
buffer.WriteString("</sup>") |
| 3487 |
} |
| 3488 |
case SUBSCRIPT: |
| 3489 |
switch policy { |
| 3490 |
case PERIOD: |
| 3491 |
case SPACE: |
| 3492 |
case CONCISE: |
| 3493 |
case BRACKETS: |
| 3494 |
buffer.WriteRune(')') |
| 3495 |
case MARKDOWN: |
| 3496 |
buffer.WriteRune('~') |
| 3497 |
case SLASH: |
| 3498 |
case TAGS: |
| 3499 |
buffer.WriteString("</sub>") |
| 3500 |
} |
| 3501 |
case PLAINDIGIT: |
| 3502 |
switch policy { |
| 3503 |
case PERIOD: |
| 3504 |
case SPACE: |
| 3505 |
case CONCISE: |
| 3506 |
case BRACKETS: |
| 3507 |
case MARKDOWN: |
| 3508 |
case SLASH: |
| 3509 |
case TAGS: |
| 3510 |
} |
| 3511 |
} |
| 3512 |
level = NOSCRIPT |
| 3513 |
} else if ch >= '0' && ch <= '9' { |
| 3514 |
switch level { |
| 3515 |
case NOSCRIPT: |
| 3516 |
switch policy { |
| 3517 |
case PERIOD: |
| 3518 |
case SPACE: |
| 3519 |
case CONCISE: |
| 3520 |
case BRACKETS: |
| 3521 |
case MARKDOWN: |
| 3522 |
case SLASH: |
| 3523 |
case TAGS: |
| 3524 |
} |
| 3525 |
case SUPSCRIPT: |
| 3526 |
switch policy { |
| 3527 |
case PERIOD: |
| 3528 |
buffer.WriteRune('.') |
| 3529 |
case SPACE: |
| 3530 |
buffer.WriteRune(' ') |
| 3531 |
case CONCISE: |
| 3532 |
buffer.WriteRune(']') |
| 3533 |
case BRACKETS: |
| 3534 |
buffer.WriteRune(']') |
| 3535 |
case MARKDOWN: |
| 3536 |
buffer.WriteRune('^') |
| 3537 |
case SLASH: |
| 3538 |
buffer.WriteRune('/') |
| 3539 |
case TAGS: |
| 3540 |
buffer.WriteString("</sup>") |
| 3541 |
} |
| 3542 |
case SUBSCRIPT: |
| 3543 |
switch policy { |
| 3544 |
case PERIOD: |
| 3545 |
buffer.WriteRune('.') |
| 3546 |
case SPACE: |
| 3547 |
buffer.WriteRune(' ') |
| 3548 |
case CONCISE: |
| 3549 |
case BRACKETS: |
| 3550 |
buffer.WriteRune(')') |
| 3551 |
case MARKDOWN: |
| 3552 |
buffer.WriteRune('~') |
| 3553 |
case SLASH: |
| 3554 |
buffer.WriteRune('\\') |
| 3555 |
case TAGS: |
| 3556 |
buffer.WriteString("</sub>") |
| 3557 |
} |
| 3558 |
case PLAINDIGIT: |
| 3559 |
switch policy { |
| 3560 |
case PERIOD: |
| 3561 |
case SPACE: |
| 3562 |
case CONCISE: |
| 3563 |
case BRACKETS: |
| 3564 |
case MARKDOWN: |
| 3565 |
case SLASH: |
| 3566 |
case TAGS: |
| 3567 |
} |
| 3568 |
} |
| 3569 |
level = PLAINDIGIT |
| 3570 |
} else { |
| 3571 |
switch level { |
| 3572 |
case NOSCRIPT: |
| 3573 |
switch policy { |
| 3574 |
case PERIOD: |
| 3575 |
case SPACE: |
| 3576 |
case CONCISE: |
| 3577 |
case BRACKETS: |
| 3578 |
case MARKDOWN: |
| 3579 |
case SLASH: |
| 3580 |
case TAGS: |
| 3581 |
} |
| 3582 |
case SUPSCRIPT: |
| 3583 |
switch policy { |
| 3584 |
case PERIOD: |
| 3585 |
case SPACE: |
| 3586 |
case CONCISE: |
| 3587 |
buffer.WriteRune(']') |
| 3588 |
case BRACKETS: |
| 3589 |
buffer.WriteRune(']') |
| 3590 |
case MARKDOWN: |
| 3591 |
buffer.WriteRune('^') |
| 3592 |
case SLASH: |
| 3593 |
case TAGS: |
| 3594 |
buffer.WriteString("</sup>") |
| 3595 |
} |
| 3596 |
case SUBSCRIPT: |
| 3597 |
switch policy { |
| 3598 |
case PERIOD: |
| 3599 |
case SPACE: |
| 3600 |
case CONCISE: |
| 3601 |
case BRACKETS: |
| 3602 |
buffer.WriteRune(')') |
| 3603 |
case MARKDOWN: |
| 3604 |
buffer.WriteRune('~') |
| 3605 |
case SLASH: |
| 3606 |
case TAGS: |
| 3607 |
buffer.WriteString("</sub>") |
| 3608 |
} |
| 3609 |
case PLAINDIGIT: |
| 3610 |
switch policy { |
| 3611 |
case PERIOD: |
| 3612 |
case SPACE: |
| 3613 |
case CONCISE: |
| 3614 |
case BRACKETS: |
| 3615 |
case MARKDOWN: |
| 3616 |
case SLASH: |
| 3617 |
case TAGS: |
| 3618 |
} |
| 3619 |
} |
| 3620 |
level = NOSCRIPT |
| 3621 |
} |
| 3622 |
buffer.WriteRune(ch) |
| 3623 |
} |
| 3624 |
|
| 3625 |
switch level { |
| 3626 |
case NOSCRIPT: |
| 3627 |
switch policy { |
| 3628 |
case PERIOD: |
| 3629 |
case SPACE: |
| 3630 |
case CONCISE: |
| 3631 |
case BRACKETS: |
| 3632 |
case MARKDOWN: |
| 3633 |
case SLASH: |
| 3634 |
case TAGS: |
| 3635 |
} |
| 3636 |
case SUPSCRIPT: |
| 3637 |
switch policy { |
| 3638 |
case PERIOD: |
| 3639 |
case SPACE: |
| 3640 |
case CONCISE: |
| 3641 |
buffer.WriteRune(']') |
| 3642 |
case BRACKETS: |
| 3643 |
buffer.WriteRune(']') |
| 3644 |
case MARKDOWN: |
| 3645 |
buffer.WriteRune('^') |
| 3646 |
case SLASH: |
| 3647 |
case TAGS: |
| 3648 |
buffer.WriteString("</sup>") |
| 3649 |
} |
| 3650 |
case SUBSCRIPT: |
| 3651 |
switch policy { |
| 3652 |
case PERIOD: |
| 3653 |
case SPACE: |
| 3654 |
case CONCISE: |
| 3655 |
case BRACKETS: |
| 3656 |
buffer.WriteRune(')') |
| 3657 |
case MARKDOWN: |
| 3658 |
buffer.WriteRune('~') |
| 3659 |
case SLASH: |
| 3660 |
case TAGS: |
| 3661 |
buffer.WriteString("</sub>") |
| 3662 |
} |
| 3663 |
case PLAINDIGIT: |
| 3664 |
switch policy { |
| 3665 |
case PERIOD: |
| 3666 |
case SPACE: |
| 3667 |
case CONCISE: |
| 3668 |
case BRACKETS: |
| 3669 |
case MARKDOWN: |
| 3670 |
case SLASH: |
| 3671 |
case TAGS: |
| 3672 |
} |
| 3673 |
} |
| 3674 |
|
| 3675 |
return buffer.String() |
| 3676 |
} |
| 3677 |
|
| 3678 |
// ReverseString creates a mirror-image of letters in a string |
| 3679 |
func ReverseString(str string) string { |
| 3680 |
|
| 3681 |
if str == "" { |
| 3682 |
return str |
| 3683 |
} |
| 3684 |
|
| 3685 |
runes := []rune(str) |
| 3686 |
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 { |
| 3687 |
runes[i], runes[j] = runes[j], runes[i] |
| 3688 |
} |
| 3689 |
rev := string(runes) |
| 3690 |
|
| 3691 |
return rev |
| 3692 |
} |
| 3693 |
|
| 3694 |
// SlidingSlices passes a sliding window over a sequence |
| 3695 |
func SlidingSlices(str string, num int) []string { |
| 3696 |
|
| 3697 |
if str == "" { |
| 3698 |
return nil |
| 3699 |
} |
| 3700 |
|
| 3701 |
var arry []string |
| 3702 |
|
| 3703 |
fst, lst, max := 0, num, len(str)+1 |
| 3704 |
|
| 3705 |
for lst < max { |
| 3706 |
sub := str[fst:lst] |
| 3707 |
arry = append(arry, sub) |
| 3708 |
fst++ |
| 3709 |
lst++ |
| 3710 |
} |
| 3711 |
|
| 3712 |
return arry |
| 3713 |
} |
| 3714 |
|
| 3715 |
// SortStringByWords sorts the individual words in a string |
| 3716 |
func SortStringByWords(str string) string { |
| 3717 |
|
| 3718 |
str = RemoveCommaOrSemicolon(str) |
| 3719 |
|
| 3720 |
// check for multiple words |
| 3721 |
if HasSpaceOrHyphen(str) { |
| 3722 |
flds := strings.Fields(str) |
| 3723 |
slices.Sort(flds) |
| 3724 |
str = strings.Join(flds, " ") |
| 3725 |
str = strings.Replace(str, "-", " ", -1) |
| 3726 |
str = CompressRunsOfSpaces(str) |
| 3727 |
str = strings.TrimRight(str, ".?:") |
| 3728 |
} |
| 3729 |
|
| 3730 |
return str |
| 3731 |
} |
| 3732 |
|
| 3733 |
// SplitInTwoLeft loads the first argument if no delimiter is present |
| 3734 |
func SplitInTwoLeft(str, chr string) (string, string) { |
| 3735 |
|
| 3736 |
slash := strings.SplitN(str, chr, 2) |
| 3737 |
if len(slash) > 1 { |
| 3738 |
return slash[0], slash[1] |
| 3739 |
} |
| 3740 |
|
| 3741 |
return str, "" |
| 3742 |
} |
| 3743 |
|
| 3744 |
// SplitInTwoRight loads the second argument if no delimiter is present |
| 3745 |
func SplitInTwoRight(str, chr string) (string, string) { |
| 3746 |
|
| 3747 |
slash := strings.SplitN(str, chr, 2) |
| 3748 |
if len(slash) > 1 { |
| 3749 |
return slash[0], slash[1] |
| 3750 |
} |
| 3751 |
|
| 3752 |
return "", str |
| 3753 |
} |
| 3754 |
|
| 3755 |
// TightenParentheses removes spaces inside parentheses |
| 3756 |
func TightenParentheses(str string) string { |
| 3757 |
|
| 3758 |
if len(str) < 2 { |
| 3759 |
return str |
| 3760 |
} |
| 3761 |
|
| 3762 |
var ( |
| 3763 |
buffer strings.Builder |
| 3764 |
prev rune |
| 3765 |
) |
| 3766 |
|
| 3767 |
for _, ch := range str { |
| 3768 |
if prev == '(' && ch == ' ' { |
| 3769 |
ch = '(' |
| 3770 |
} else if prev == ' ' && ch == ')' { |
| 3771 |
ch = ')' |
| 3772 |
} else if prev != 0 { |
| 3773 |
buffer.WriteRune(prev) |
| 3774 |
} |
| 3775 |
prev = ch |
| 3776 |
} |
| 3777 |
|
| 3778 |
buffer.WriteRune(prev) |
| 3779 |
|
| 3780 |
return buffer.String() |
| 3781 |
} |
| 3782 |
|
| 3783 |
// TrimBOM removes leading Byte Order Mark, if present |
| 3784 |
func TrimBOM(str string) string { |
| 3785 |
|
| 3786 |
if str == "" { |
| 3787 |
return str |
| 3788 |
} |
| 3789 |
|
| 3790 |
for i, ch := range str { |
| 3791 |
|
| 3792 |
if i == 0 { |
| 3793 |
if ch == 65279 { |
| 3794 |
// next offset is size of BOM |
| 3795 |
continue |
| 3796 |
} |
| 3797 |
// otherwise break out of loop |
| 3798 |
break |
| 3799 |
} |
| 3800 |
|
| 3801 |
// trim with size of BOM (should be 3) |
| 3802 |
str = str[i:] |
| 3803 |
|
| 3804 |
// break to return trimmed string |
| 3805 |
break |
| 3806 |
} |
| 3807 |
|
| 3808 |
return str |
| 3809 |
} |
| 3810 |
|
| 3811 |
// UnicodeToASCII converts nonASCII to ampersand-pound-x encoding |
| 3812 |
func UnicodeToASCII(str string) string { |
| 3813 |
|
| 3814 |
// convert to &#x...; representation |
| 3815 |
|
| 3816 |
var buffer strings.Builder |
| 3817 |
|
| 3818 |
for _, ch := range str { |
| 3819 |
if ch > 127 { |
| 3820 |
s := strconv.QuoteToASCII(string(ch)) |
| 3821 |
s = strings.ToUpper(s[3:7]) |
| 3822 |
for { |
| 3823 |
if !strings.HasPrefix(s, "0") { |
| 3824 |
break |
| 3825 |
} |
| 3826 |
s = s[1:] |
| 3827 |
} |
| 3828 |
buffer.WriteString("&#x") |
| 3829 |
buffer.WriteString(s) |
| 3830 |
buffer.WriteRune(';') |
| 3831 |
continue |
| 3832 |
} |
| 3833 |
buffer.WriteRune(ch) |
| 3834 |
} |
| 3835 |
|
| 3836 |
return buffer.String() |
| 3837 |
} |
| 3838 |
|
| 3839 |
// initialize mfix replacer before non-init functions are called |
| 3840 |
func init() { |
| 3841 |
|
| 3842 |
mfix = strings.NewReplacer( |
| 3843 |
"<b>", "", |
| 3844 |
"<i>", "", |
| 3845 |
"<u>", "", |
| 3846 |
"</b>", "", |
| 3847 |
"</i>", "", |
| 3848 |
"</u>", "", |
| 3849 |
"<b/>", "", |
| 3850 |
"<i/>", "", |
| 3851 |
"<u/>", "", |
| 3852 |
"<sup>", "", |
| 3853 |
"<sub>", "", |
| 3854 |
"</sup>", "", |
| 3855 |
"</sub>", "", |
| 3856 |
"<sup/>", "", |
| 3857 |
"<sub/>", "", |
| 3858 |
) |
| 3859 |
|
| 3860 |
lcBadAccents = make(map[string]bool) |
| 3861 |
|
| 3862 |
for ky := range combiningAccents { |
| 3863 |
ky = strings.ToLower(ky) |
| 3864 |
lcBadAccents[ky] = true |
| 3865 |
} |
| 3866 |
|
| 3867 |
} |