|
| 1 | +""" |
| 2 | +Functions which have been copied from TorchText to remove TorchServe's |
| 3 | +dependency on TorchText |
| 4 | +
|
| 5 | +from torchtext.data.utils import ngrams_iterator, get_tokenizer |
| 6 | +
|
| 7 | +""" |
| 8 | + |
| 9 | +import re |
| 10 | + |
| 11 | + |
| 12 | +def ngrams_iterator(token_list, ngrams): |
| 13 | + """Return an iterator that yields the given tokens and their ngrams. |
| 14 | +
|
| 15 | + Args: |
| 16 | + token_list: A list of tokens |
| 17 | + ngrams: the number of ngrams. |
| 18 | +
|
| 19 | + Examples: |
| 20 | + >>> token_list = ['here', 'we', 'are'] |
| 21 | + >>> list(ngrams_iterator(token_list, 2)) |
| 22 | + >>> ['here', 'here we', 'we', 'we are', 'are'] |
| 23 | + """ |
| 24 | + |
| 25 | + def _get_ngrams(n): |
| 26 | + return zip(*[token_list[i:] for i in range(n)]) |
| 27 | + |
| 28 | + for x in token_list: |
| 29 | + yield x |
| 30 | + for n in range(2, ngrams + 1): |
| 31 | + for x in _get_ngrams(n): |
| 32 | + yield " ".join(x) |
| 33 | + |
| 34 | + |
| 35 | +_patterns = [ |
| 36 | + r"\'", |
| 37 | + r"\"", |
| 38 | + r"\.", |
| 39 | + r"<br \/>", |
| 40 | + r",", |
| 41 | + r"\(", |
| 42 | + r"\)", |
| 43 | + r"\!", |
| 44 | + r"\?", |
| 45 | + r"\;", |
| 46 | + r"\:", |
| 47 | + r"\s+", |
| 48 | +] |
| 49 | + |
| 50 | +_replacements = [ |
| 51 | + " ' ", |
| 52 | + "", |
| 53 | + " . ", |
| 54 | + " ", |
| 55 | + " , ", |
| 56 | + " ( ", |
| 57 | + " ) ", |
| 58 | + " ! ", |
| 59 | + " ? ", |
| 60 | + " ", |
| 61 | + " ", |
| 62 | + " ", |
| 63 | +] |
| 64 | + |
| 65 | +_patterns_dict = list((re.compile(p), r) for p, r in zip(_patterns, _replacements)) |
| 66 | + |
| 67 | + |
| 68 | +def _basic_english_normalize(line): |
| 69 | + r""" |
| 70 | + Basic normalization for a line of text. |
| 71 | + Normalization includes |
| 72 | + - lowercasing |
| 73 | + - complete some basic text normalization for English words as follows: |
| 74 | + add spaces before and after '\'' |
| 75 | + remove '\"', |
| 76 | + add spaces before and after '.' |
| 77 | + replace '<br \/>'with single space |
| 78 | + add spaces before and after ',' |
| 79 | + add spaces before and after '(' |
| 80 | + add spaces before and after ')' |
| 81 | + add spaces before and after '!' |
| 82 | + add spaces before and after '?' |
| 83 | + replace ';' with single space |
| 84 | + replace ':' with single space |
| 85 | + replace multiple spaces with single space |
| 86 | +
|
| 87 | + Returns a list of tokens after splitting on whitespace. |
| 88 | + """ |
| 89 | + |
| 90 | + line = line.lower() |
| 91 | + for pattern_re, replaced_str in _patterns_dict: |
| 92 | + line = pattern_re.sub(replaced_str, line) |
| 93 | + return line.split() |
| 94 | + |
| 95 | + |
| 96 | +def _split_tokenizer(x): # noqa: F821 |
| 97 | + return x.split() |
| 98 | + |
| 99 | + |
| 100 | +def get_tokenizer(tokenizer, language="en"): |
| 101 | + r""" |
| 102 | + Generate tokenizer function for a string sentence. |
| 103 | +
|
| 104 | + Args: |
| 105 | + tokenizer: the name of tokenizer function. If None, it returns split() |
| 106 | + function, which splits the string sentence by space. |
| 107 | + If basic_english, it returns _basic_english_normalize() function, |
| 108 | + which normalize the string first and split by space. If a callable |
| 109 | + function, it will return the function. If a tokenizer library |
| 110 | + (e.g. spacy, moses, toktok, revtok, subword), it returns the |
| 111 | + corresponding library. |
| 112 | + language: Default en |
| 113 | +
|
| 114 | + Examples: |
| 115 | + >>> tokenizer = get_tokenizer("basic_english") |
| 116 | + >>> tokens = tokenizer("You can now install TorchText using pip!") |
| 117 | + >>> tokens |
| 118 | + >>> ['you', 'can', 'now', 'install', 'torchtext', 'using', 'pip', '!'] |
| 119 | +
|
| 120 | + """ |
| 121 | + |
| 122 | + # default tokenizer is string.split(), added as a module function for serialization |
| 123 | + if tokenizer is None: |
| 124 | + return _split_tokenizer |
| 125 | + |
| 126 | + if tokenizer == "basic_english": |
| 127 | + if language != "en": |
| 128 | + raise ValueError("Basic normalization is only available for Enlish(en)") |
| 129 | + return _basic_english_normalize |
0 commit comments