Utils
In this file implemented the helper function
add_custom_token(nlp, spacial_token, token_attrs)
Add custom token
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nlp |
object
|
pipeline |
required |
spacial_token |
string
|
the string to specially tokenize |
required |
token_attrs |
list[dict]
|
a list of dicts, where each dict describes a token and its attributes. |
required |
Source code in app/utils.py
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | |
generate_guid(k=6)
Generate random id
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
k |
int
|
length of guid |
6
|
Returns:
| Name | Type | Description |
|---|---|---|
guid |
str
|
random id |
Source code in app/utils.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | |
generate_openapi_json(openapi_version='3.1.0')
Generate and save openapi specification of the application
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
openapi_version |
str
|
openapi version specifier |
'3.1.0'
|
Returns:
| Type | Description |
|---|---|
None
|
|
Source code in app/utils.py
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | |
json_load(path, encoding='utf-8')
Load json files from path
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path |
str
|
path to save object |
required |
encoding |
str
|
encoding type |
'utf-8'
|
Returns:
| Name | Type | Description |
|---|---|---|
obj |
(Object) object
|
|
Source code in app/utils.py
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | |
json_save(path, obj)
Save supported files to json format
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path |
str
|
path to save object |
required |
obj |
(Object) object
|
|
required |
Source code in app/utils.py
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | |
pickle_load(path)
Load supported pickle format files
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path |
str: path to save object
|
|
required |
Returns:
| Name | Type | Description |
|---|---|---|
obj |
(Object) any object with any pickle supported format
|
|
Source code in app/utils.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | |
pickle_save(path, obj)
Save supported files in pickle format
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path |
str: path to save object
|
|
required |
obj |
object: any pickle serializable object
|
|
required |
Source code in app/utils.py
32 33 34 35 36 37 38 39 40 41 42 43 44 | |
any_next_words_form_swear_word(cur_word, words_indices, censor_words)
Return True, and the end index of the word in the text,
if any word formed in words_indices is in CENSOR_WORDSET.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cur_word |
str
|
current word |
required |
words_indices |
list[int]
|
words indices |
required |
censor_words |
list[str]
|
censor words |
required |
Returns:
| Name | Type | Description |
|---|---|---|
end_index |
int
|
last index of censor word |
Source code in better_profanity/utils.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | |
get_complete_path_of_file(filename)
Join the path of the current directory with the input filename.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename |
str
|
name of the file |
required |
Returns:
| Name | Type | Description |
|---|---|---|
filepath |
str
|
path of the file |
Source code in better_profanity/utils.py
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | |
get_replacement_for_swear_word(censor_char)
Replace swear words
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
censor_char |
str
|
censor character |
required |
Returns:
| Type | Description |
|---|---|
censor_char : str
|
replaced censor character |
Source code in better_profanity/utils.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | |
read_wordlist(filename)
Return words from a wordlist file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename |
str
|
name of the file |
required |
Returns:
| Name | Type | Description |
|---|---|---|
row |
str
|
words from a wordlist file |
Source code in better_profanity/utils.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | |