| """ |
| adapted from chemdataextractor.text.normalize |
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| Tools for normalizing text. |
| https://github.com/mcs07/ChemDataExtractor |
| :copyright: Copyright 2016 by Matt Swain. |
| :license: MIT |
| |
| Permission is hereby granted, free of charge, to any person obtaining |
| a copy of this software and associated documentation files (the |
| 'Software'), to deal in the Software without restriction, including |
| without limitation the rights to use, copy, modify, merge, publish, |
| distribute, sublicense, and/or sell copies of the Software, and to |
| permit persons to whom the Software is furnished to do so, subject to |
| the following conditions: |
| |
| The above copyright notice and this permission notice shall be |
| included in all copies or substantial portions of the Software. |
| |
| THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, |
| EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| """ |
|
|
| |
| CONTROLS = { |
| '\u0001', '\u0002', '\u0003', '\u0004', '\u0005', '\u0006', '\u0007', '\u0008', '\u000e', '\u000f', '\u0011', |
| '\u0012', '\u0013', '\u0014', '\u0015', '\u0016', '\u0017', '\u0018', '\u0019', '\u001a', '\u001b', |
| } |
| |
| |
|
|
|
|
| |
| HYPHENS = { |
| '-', |
| 'β', |
| 'β', |
| 'β', |
| 'β', |
| 'β', |
| 'β', |
| 'β', |
| } |
|
|
| |
| MINUSES = { |
| '-', |
| 'β', |
| 'οΌ', |
| 'β»', |
| } |
|
|
| |
| PLUSES = { |
| '+', |
| 'οΌ', |
| 'βΊ', |
| } |
|
|
| |
| SLASHES = { |
| '/', |
| 'β', |
| 'β', |
| } |
|
|
| |
| TILDES = { |
| '~', |
| 'Λ', |
| 'β', |
| 'βΌ', |
| 'β½', |
| 'βΏ', |
| 'γ', |
| 'ο½', |
| } |
|
|
| |
| APOSTROPHES = { |
| "'", |
| 'β', |
| 'Υ', |
| 'κ', |
| 'κ', |
| 'οΌ', |
| } |
|
|
| |
| SINGLE_QUOTES = { |
| "'", |
| 'β', |
| 'β', |
| 'β', |
| 'β', |
|
|
| } |
|
|
| |
| DOUBLE_QUOTES = { |
| '"', |
| 'β', |
| 'β', |
| 'β', |
| 'β', |
| } |
|
|
| |
| ACCENTS = { |
| '`', |
| 'Β΄', |
| } |
|
|
| |
| PRIMES = { |
| 'β²', |
| 'β³', |
| 'β΄', |
| 'β΅', |
| 'βΆ', |
| 'β·', |
| 'β', |
| } |
|
|
| |
| QUOTES = APOSTROPHES | SINGLE_QUOTES | DOUBLE_QUOTES | ACCENTS | PRIMES |
|
|
| def normalize_text(text: str): |
| for control in CONTROLS: |
| text = text.replace(control, '') |
| text = text.replace('\u000b', ' ').replace('\u000c', ' ').replace(u'\u0085', ' ') |
|
|
| for hyphen in HYPHENS | MINUSES: |
| text = text.replace(hyphen, '-') |
| text = text.replace('\u00ad', '') |
|
|
| for double_quote in DOUBLE_QUOTES: |
| text = text.replace(double_quote, '"') |
| for single_quote in (SINGLE_QUOTES | APOSTROPHES | ACCENTS): |
| text = text.replace(single_quote, "'") |
| text = text.replace('β²', "'") |
| text = text.replace('β΅', "'") |
| text = text.replace('β³', "''") |
| text = text.replace('βΆ', "''") |
| text = text.replace('β΄', "'''") |
| text = text.replace('β·', "'''") |
| text = text.replace('β', "''''") |
|
|
| text = text.replace('β¦', '...').replace(' . . . ', ' ... ') |
|
|
| for slash in SLASHES: |
| text = text.replace(slash, '/') |
|
|
| |
| |
|
|
| return text |
|
|