| 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: normal.go |
| 26 |
// |
| 27 |
// Author: Jonathan Kans |
| 28 |
// |
| 29 |
// ========================================================================== |
| 30 |
|
| 31 |
package eutils |
| 32 |
|
| 33 |
import ( |
| 34 |
"html" |
| 35 |
"os" |
| 36 |
"strings" |
| 37 |
) |
| 38 |
|
| 39 |
var combiningAccents = map[string]string{ |
| 40 |
"Combining Grave Accent": "", |
| 41 |
"Combining Acute Accent": "", |
| 42 |
"Combining Circumflex Accent": "", |
| 43 |
"Combining Tilde": "", |
| 44 |
"Combining Macron": "", |
| 45 |
"Combining Overline": "", |
| 46 |
"Combining Breve": "", |
| 47 |
"Combining Dot Above": "", |
| 48 |
"Combining Diaeresis": "", |
| 49 |
"Combining Hook Above": "", |
| 50 |
"Combining Ring Above": "", |
| 51 |
"Combining Double Acute Accent": "", |
| 52 |
"Combining Caron": "", |
| 53 |
"Combining Vertical Line Above": "", |
| 54 |
"Combining Double Vertical Line Above": "", |
| 55 |
"Combining Double Grave Accent": "", |
| 56 |
"Combining Candrabindu": "", |
| 57 |
"Combining Inverted Breve": "", |
| 58 |
"Combining Turned Comma Above": "", |
| 59 |
"Combining Comma Above": "", |
| 60 |
"Combining Reversed Comma Above": "", |
| 61 |
"Combining Comma Above Right": "", |
| 62 |
"Combining Grave Accent Below": "", |
| 63 |
"Combining Acute Accent Below": "", |
| 64 |
"Combining Left Tack Below": "", |
| 65 |
"Combining Right Tack Below": "", |
| 66 |
"Combining Left Angle Above": "", |
| 67 |
"Combining Horn": "", |
| 68 |
"Combining Left Half Ring Below": "", |
| 69 |
"Combining Up Tack Below": "", |
| 70 |
"Combining Down Tack Below": "", |
| 71 |
"Combining Plus Sign Below": "", |
| 72 |
"Combining Minus Sign Below": "", |
| 73 |
"Combining Palatalized Hook Below": "", |
| 74 |
"Combining Retroflex Hook Below": "", |
| 75 |
"Combining Dot Below": "", |
| 76 |
"Combining Diaeresis Below": "", |
| 77 |
"Combining Ring Below": "", |
| 78 |
"Combining Comma Below": "", |
| 79 |
"Combining Cedilla": "", |
| 80 |
"Combining Ogonek": "", |
| 81 |
"Combining Vertical Line Below": "", |
| 82 |
"Combining Bridge Below": "", |
| 83 |
"Combining Inverted Double Arch Below": "", |
| 84 |
"Combining Caron Below": "", |
| 85 |
"Combining Circumflex Accent Below": "", |
| 86 |
"Combining Breve Below": "", |
| 87 |
"Combining Inverted Breve Below": "", |
| 88 |
"Combining Tilde Below": "", |
| 89 |
"Combining Macron Below": "", |
| 90 |
"Combining Low Line": "", |
| 91 |
"Combining Double Low Line": "", |
| 92 |
"Combining Tilde Overlay": "", |
| 93 |
"Combining Short Stroke Overlay": "", |
| 94 |
"Combining Long Stroke Overlay": "", |
| 95 |
"Combining Short Solidus Overlay": "", |
| 96 |
"Combining Long Solidus Overlay": "", |
| 97 |
"Combining Right Half Ring Below": "", |
| 98 |
"Combining Inverted Bridge Below": "", |
| 99 |
"Combining Square Below": "", |
| 100 |
"Combining Seagull Below": "", |
| 101 |
"Combining X Above": "", |
| 102 |
"Combining Vertical Tilde": "", |
| 103 |
"Combining Double Overline": "", |
| 104 |
"Combining Grave Tone Mark": "", |
| 105 |
"Combining Acute Tone Mark": "", |
| 106 |
"Combining Greek Perispomeni": "", |
| 107 |
"Combining Greek Koronis": "", |
| 108 |
"Combining Greek Dialytika Tonos": "", |
| 109 |
"Combining Greek Ypogegrammeni": "", |
| 110 |
"Combining Bridge Above": "", |
| 111 |
"Combining Equals Sign Below": "", |
| 112 |
"Combining Double Vertical Line Below": "", |
| 113 |
"Combining Left Angle Below": "", |
| 114 |
"Combining Not Tilde Above": "", |
| 115 |
"Combining Homothetic Above": "", |
| 116 |
"Combining Almost Equal To Above": "", |
| 117 |
"Combining Left Right Arrow Below": "", |
| 118 |
"Combining Upwards Arrow Below": "", |
| 119 |
"Combining Grapheme Joiner": "", |
| 120 |
"Combining Right Arrowhead Above": "", |
| 121 |
"Combining Left Half Ring Above": "", |
| 122 |
"Combining Fermata": "", |
| 123 |
"Combining X Below": "", |
| 124 |
"Combining Left Arrowhead Below": "", |
| 125 |
"Combining Right Arrowhead Below": "", |
| 126 |
"Combining Right Arrowhead And Up Arrowhead Below": "", |
| 127 |
"Combining Right Half Ring Above": "", |
| 128 |
"Combining Dot Above Right": "", |
| 129 |
"Combining Asterisk Below": "", |
| 130 |
"Combining Double Ring Below": "", |
| 131 |
"Combining Zigzag Above": "", |
| 132 |
"Combining Double Breve Below": "", |
| 133 |
"Combining Double Breve": "", |
| 134 |
"Combining Double Macron": "", |
| 135 |
"Combining Double Macron Below": "", |
| 136 |
"Combining Double Tilde": "", |
| 137 |
"Combining Double Inverted Breve": "", |
| 138 |
"Combining Double Rightwards Arrow Below": "", |
| 139 |
"Combining Latin Small Letter A": "", |
| 140 |
"Combining Latin Small Letter E": "", |
| 141 |
"Combining Latin Small Letter I": "", |
| 142 |
"Combining Latin Small Letter O": "", |
| 143 |
"Combining Latin Small Letter U": "", |
| 144 |
"Combining Latin Small Letter C": "", |
| 145 |
"Combining Latin Small Letter D": "", |
| 146 |
"Combining Latin Small Letter H": "", |
| 147 |
"Combining Latin Small Letter M": "", |
| 148 |
"Combining Latin Small Letter R": "", |
| 149 |
"Combining Latin Small Letter T": "", |
| 150 |
"Combining Latin Small Letter V": "", |
| 151 |
"Combining Latin Small Letter X": "", |
| 152 |
} |
| 153 |
|
| 154 |
// first tier - fuse content strings within tag, suppressing all inner mixed-content tags |
| 155 |
var fuseInPMC = map[string]bool{ |
| 156 |
"p": true, |
| 157 |
"aff": true, |
| 158 |
"corresp": true, |
| 159 |
"author-notes": true, |
| 160 |
"disp-formula": true, |
| 161 |
"mixed-citation": true, |
| 162 |
} |
| 163 |
|
| 164 |
// second tier - remove tag and contents |
| 165 |
var dropInPMC = map[string]bool{ |
| 166 |
"xref": true, |
| 167 |
} |
| 168 |
|
| 169 |
// third tier - remove specific internal mixed-content tags |
| 170 |
var skipTagInPMC = map[string]bool{ |
| 171 |
"bold": true, |
| 172 |
"italic": true, |
| 173 |
"sub": true, |
| 174 |
"sup": true, |
| 175 |
"underline": true, |
| 176 |
"ext-link": true, |
| 177 |
"list-item": true, |
| 178 |
} |
| 179 |
|
| 180 |
// NormalizeXML adjusts Entrez XML fields to conform to common conventions |
| 181 |
func NormalizeXML(rdr <-chan XMLBlock, db string) <-chan string { |
| 182 |
|
| 183 |
if rdr == nil || db == "" { |
| 184 |
return nil |
| 185 |
} |
| 186 |
|
| 187 |
// force -strict cleanup flag for most databases (even after CreateReader and CreateTokenizer are called) |
| 188 |
switch db { |
| 189 |
case "bioc", "biocollections", "clinvar", "dbvar", "gap", "gapplus", "grasp", "pccompound", "pcsubstance": |
| 190 |
doMixed = true |
| 191 |
doStrict = false |
| 192 |
default: |
| 193 |
doStrict = true |
| 194 |
doMixed = false |
| 195 |
} |
| 196 |
allowEmbed = true |
| 197 |
contentMods = true |
| 198 |
|
| 199 |
out := make(chan string, chanDepth) |
| 200 |
if out == nil { |
| 201 |
DisplayError("Unable to create normalize channel") |
| 202 |
os.Exit(1) |
| 203 |
} |
| 204 |
|
| 205 |
normalizeXML := func(rdr <-chan XMLBlock, out chan<- string) { |
| 206 |
|
| 207 |
// close channel when all chunks have been sent |
| 208 |
defer close(out) |
| 209 |
|
| 210 |
tknq := CreateTokenizer(rdr) |
| 211 |
|
| 212 |
if tknq == nil { |
| 213 |
DisplayError("Unable to create normalize tokenizer") |
| 214 |
os.Exit(1) |
| 215 |
} |
| 216 |
|
| 217 |
var buffer strings.Builder |
| 218 |
|
| 219 |
count := 0 |
| 220 |
|
| 221 |
uid := "" |
| 222 |
isDocsum := false |
| 223 |
penultName := "" |
| 224 |
prevName := "" |
| 225 |
|
| 226 |
// mfix (in misc.go) is a *strings.Replacer for removing mixed content tags |
| 227 |
// rfix (in clean.go) is a *strings.Replacer that reencodes <, >, and & to <, >, and & |
| 228 |
|
| 229 |
cleanCombiningAccent := func(name string) string { |
| 230 |
|
| 231 |
goOn := true |
| 232 |
|
| 233 |
// loop to handle multiple combining accent phrases in a single string |
| 234 |
for goOn { |
| 235 |
|
| 236 |
// flank with spaces to catch unbracketed accent phrase at beginning or end |
| 237 |
name = " " + name + " " |
| 238 |
|
| 239 |
goOn = false |
| 240 |
if strings.Index(name, " Combining ") >= 0 { |
| 241 |
for ky, rep := range combiningAccents { |
| 242 |
pos := strings.Index(name, " "+ky+" ") |
| 243 |
if pos >= 0 { |
| 244 |
ln := len(ky) + 2 |
| 245 |
lft := name[0:pos] |
| 246 |
rgt := name[pos+ln:] |
| 247 |
name = lft + rep + rgt |
| 248 |
goOn = true |
| 249 |
} |
| 250 |
} |
| 251 |
} |
| 252 |
if strings.Index(name, "[Combining ") >= 0 { |
| 253 |
for ky, rep := range combiningAccents { |
| 254 |
pos := strings.Index(name, "["+ky+"]") |
| 255 |
if pos >= 0 { |
| 256 |
ln := len(ky) + 2 |
| 257 |
lft := name[0:pos] |
| 258 |
rgt := name[pos+ln:] |
| 259 |
name = lft + rep + rgt |
| 260 |
goOn = true |
| 261 |
} |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
name = strings.TrimSpace(name) |
| 266 |
name = CompressRunsOfSpaces(name) |
| 267 |
} |
| 268 |
|
| 269 |
return name |
| 270 |
} |
| 271 |
|
| 272 |
cleanPubmedEncoding := func(name string) string { |
| 273 |
|
| 274 |
// convert <b> - µ - &#x...; - remove mixed content tags |
| 275 |
name = RemoveHTMLDecorations(name) |
| 276 |
// reencode < and > to avoid breaking XML |
| 277 |
name = encodeAngleBracketsAndAmpersand(name) |
| 278 |
|
| 279 |
name = strings.TrimSpace(name) |
| 280 |
name = CompressRunsOfSpaces(name) |
| 281 |
|
| 282 |
return name |
| 283 |
} |
| 284 |
|
| 285 |
for tkn := range tknq { |
| 286 |
|
| 287 |
tag := tkn.Tag |
| 288 |
name := tkn.Name |
| 289 |
attr := tkn.Attr |
| 290 |
|
| 291 |
switch tag { |
| 292 |
case STARTTAG: |
| 293 |
if name == "Id" && uid != "" { |
| 294 |
uid = "" |
| 295 |
} |
| 296 |
if uid != "" { |
| 297 |
// if object after DocumentSummary is not already Id, create Id from rescued attribute |
| 298 |
buffer.WriteString("<Id>\n") |
| 299 |
buffer.WriteString(uid) |
| 300 |
buffer.WriteString("\n</Id>\n") |
| 301 |
// clear until next docsum |
| 302 |
uid = "" |
| 303 |
} |
| 304 |
if name == "DocumentSummary" { |
| 305 |
isDocsum = true |
| 306 |
atts := ParseAttributes(attr) |
| 307 |
for i := 0; i < len(atts)-1; i += 2 { |
| 308 |
if atts[i] == "uid" { |
| 309 |
// store uid from DocumentSummary |
| 310 |
uid = atts[i+1] |
| 311 |
// if uid found, remove all attributes |
| 312 |
attr = "" |
| 313 |
} |
| 314 |
} |
| 315 |
} |
| 316 |
buffer.WriteString("<") |
| 317 |
buffer.WriteString(name) |
| 318 |
if attr != "" { |
| 319 |
attr = strings.TrimSpace(attr) |
| 320 |
attr = CompressRunsOfSpaces(attr) |
| 321 |
buffer.WriteString(" ") |
| 322 |
buffer.WriteString(attr) |
| 323 |
} |
| 324 |
buffer.WriteString(">\n") |
| 325 |
penultName = prevName |
| 326 |
prevName = name |
| 327 |
case SELFTAG: |
| 328 |
buffer.WriteString("<") |
| 329 |
buffer.WriteString(name) |
| 330 |
if attr != "" { |
| 331 |
attr = strings.TrimSpace(attr) |
| 332 |
attr = CompressRunsOfSpaces(attr) |
| 333 |
buffer.WriteString(" ") |
| 334 |
buffer.WriteString(attr) |
| 335 |
} |
| 336 |
buffer.WriteString("/>\n") |
| 337 |
case STOPTAG: |
| 338 |
buffer.WriteString("</") |
| 339 |
buffer.WriteString(name) |
| 340 |
buffer.WriteString(">\n") |
| 341 |
penultName = name |
| 342 |
case CONTENTTAG: |
| 343 |
if isDocsum { |
| 344 |
if db == "pubmed" && prevName == "Title" { |
| 345 |
if strings.ContainsAny(name, "<>&") { |
| 346 |
ctype := tkn.Cont |
| 347 |
name = CleanupContents(name, (ctype&ASCII) != 0, (ctype&ER) != 0, (ctype&MIXED) != 0) |
| 348 |
name = cleanPubmedEncoding(name) |
| 349 |
} |
| 350 |
} else if db == "pubmed" && penultName == "Author" && prevName == "Name" { |
| 351 |
if strings.Index(name, "Combining ") >= 0 || strings.Index(name, "[Combining ") >= 0 { |
| 352 |
name = cleanCombiningAccent(name) |
| 353 |
} |
| 354 |
} else if db == "gene" && prevName == "Summary" { |
| 355 |
if strings.Contains(name, "&") { |
| 356 |
if HasFlankingSpace(name) { |
| 357 |
name = strings.TrimSpace(name) |
| 358 |
} |
| 359 |
name = html.UnescapeString(name) |
| 360 |
// reencode < and > to avoid breaking XML |
| 361 |
if strings.ContainsAny(name, "<>&") { |
| 362 |
name = rfix.Replace(name) |
| 363 |
} |
| 364 |
} |
| 365 |
} else if (db == "biosample" && prevName == "SampleData") || |
| 366 |
(db == "medgen" && prevName == "ConceptMeta") || |
| 367 |
(db == "sra" && prevName == "ExpXml") || |
| 368 |
(db == "sra" && prevName == "Runs") { |
| 369 |
if strings.Contains(name, "<") && strings.Contains(name, ">") { |
| 370 |
if HasFlankingSpace(name) { |
| 371 |
name = strings.TrimSpace(name) |
| 372 |
} |
| 373 |
name = html.UnescapeString(name) |
| 374 |
} |
| 375 |
} |
| 376 |
} else { |
| 377 |
if db == "pubmed" { |
| 378 |
ctype := tkn.Cont |
| 379 |
name = CleanupContents(name, (ctype&ASCII) != 0, (ctype&ER) != 0, (ctype&MIXED) != 0) |
| 380 |
if HasFlankingSpace(name) { |
| 381 |
name = strings.TrimSpace(name) |
| 382 |
} |
| 383 |
if prevName == "ArticleTitle" || (penultName == "Abstract" && prevName == "AbstractText") { |
| 384 |
name = cleanPubmedEncoding(name) |
| 385 |
} |
| 386 |
if (penultName == "Author" && prevName == "LastName") || |
| 387 |
(penultName == "LastName" && prevName == "ForeName") || |
| 388 |
(penultName == "AffiliationInfo" && prevName == "Affiliation") { |
| 389 |
if strings.Index(name, "Combining ") >= 0 || strings.Index(name, "[Combining ") >= 0 { |
| 390 |
name = cleanCombiningAccent(name) |
| 391 |
} |
| 392 |
name = cleanPubmedEncoding(name) |
| 393 |
} |
| 394 |
} else if db == "bioc" { |
| 395 |
name = CleanupContents(name, true, true, true) |
| 396 |
if HasFlankingSpace(name) { |
| 397 |
name = strings.TrimSpace(name) |
| 398 |
} |
| 399 |
name = html.UnescapeString(name) |
| 400 |
// reencode < and > to avoid breaking XML |
| 401 |
if strings.ContainsAny(name, "<>&") { |
| 402 |
name = rfix.Replace(name) |
| 403 |
} |
| 404 |
} |
| 405 |
} |
| 406 |
// content normally printed |
| 407 |
if HasFlankingSpace(name) { |
| 408 |
name = strings.TrimSpace(name) |
| 409 |
} |
| 410 |
buffer.WriteString(name) |
| 411 |
buffer.WriteString("\n") |
| 412 |
case CDATATAG: |
| 413 |
if isDocsum { |
| 414 |
if db == "assembly" && prevName == "Meta" { |
| 415 |
if strings.Contains(name, "<") && strings.Contains(name, ">") { |
| 416 |
// if CDATA contains embedded XML, simply remove CDATA wrapper |
| 417 |
if HasFlankingSpace(name) { |
| 418 |
name = strings.TrimSpace(name) |
| 419 |
} |
| 420 |
buffer.WriteString(name) |
| 421 |
buffer.WriteString("\n") |
| 422 |
} |
| 423 |
} else if db == "gtr" && prevName == "Extra" { |
| 424 |
// remove entire CDATA contents |
| 425 |
} |
| 426 |
} |
| 427 |
case COMMENTTAG: |
| 428 |
if !isDocsum { |
| 429 |
if db == "sra" { |
| 430 |
if strings.Contains(name, "<") && strings.Contains(name, ">") { |
| 431 |
// if comment contains embedded XML, remove comment wrapper and trim to leading < bracket |
| 432 |
pos := strings.Index(name, "<") |
| 433 |
if pos > 0 { |
| 434 |
name = name[pos:] |
| 435 |
} |
| 436 |
if HasFlankingSpace(name) { |
| 437 |
name = strings.TrimSpace(name) |
| 438 |
} |
| 439 |
buffer.WriteString(name) |
| 440 |
buffer.WriteString("\n") |
| 441 |
} |
| 442 |
} |
| 443 |
} |
| 444 |
case DOCTYPETAG: |
| 445 |
doctype := strings.TrimSpace(name) |
| 446 |
if strings.HasPrefix(doctype, "<") { |
| 447 |
doctype = doctype[1:] |
| 448 |
} |
| 449 |
if strings.HasPrefix(doctype, "!") { |
| 450 |
doctype = doctype[1:] |
| 451 |
} |
| 452 |
if strings.HasPrefix(doctype, "DOCTYPE") { |
| 453 |
doctype = doctype[7:] |
| 454 |
} |
| 455 |
if strings.HasPrefix(doctype, " ") { |
| 456 |
doctype = doctype[1:] |
| 457 |
} |
| 458 |
doctype = strings.TrimSuffix(doctype, ">") |
| 459 |
doctype = strings.TrimSpace(doctype) |
| 460 |
|
| 461 |
buffer.WriteString("<!DOCTYPE ") |
| 462 |
buffer.WriteString(doctype) |
| 463 |
buffer.WriteString(">") |
| 464 |
case PROCESSTAG: |
| 465 |
case NOTAG: |
| 466 |
case ISCLOSED: |
| 467 |
// now handled at end of calling function |
| 468 |
default: |
| 469 |
} |
| 470 |
|
| 471 |
count++ |
| 472 |
if count > 1000 { |
| 473 |
count = 0 |
| 474 |
txt := buffer.String() |
| 475 |
if txt != "" { |
| 476 |
// send current result through output channel |
| 477 |
out <- txt |
| 478 |
} |
| 479 |
buffer.Reset() |
| 480 |
} |
| 481 |
} |
| 482 |
|
| 483 |
txt := buffer.String() |
| 484 |
if txt != "" { |
| 485 |
// send remaining result through output channel |
| 486 |
out <- txt |
| 487 |
} |
| 488 |
} |
| 489 |
|
| 490 |
// launch single normalize goroutine |
| 491 |
go normalizeXML(rdr, out) |
| 492 |
|
| 493 |
return out |
| 494 |
} |