This repository has been archived by the owner on Nov 3, 2023. It is now read-only.
/
strings.py
158 lines (133 loc) · 4.89 KB
/
strings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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
152
153
154
155
156
157
158
#!/usr/bin/env python3
# Copyright (c) Facebook, Inc. and its affiliates.
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
"""
Utility functions and classes for handling text strings.
"""
import os
import sys as _sys
def normalize_reply(text: str, version=1) -> str:
"""
Standardize the capitalization and punctuation spacing of the input text.
Version 1: Fix sentence start casing, and punctuation.
Version 2: Add trailing period, if missing.
"""
switch_list = [(' .', '.'), (' ,', ','), (' ?', '?'), (' !', '!'), (" ' ", "'")]
# add spaces so that words and punctuation can be seaprated
new_text = text.lower()
# normalize in case of human:
for new, old in switch_list:
new_text = new_text.replace(old, new).replace(' ', ' ')
# split on punctuation to find sentence boundaries
# capitalize stuff
tokens = new_text.split(' ')
for i in range(len(tokens)):
if i == 0:
tokens[i] = uppercase(tokens[i])
elif tokens[i] in ('i', "i'm", "i've", "i'll", "i'd"):
tokens[i] = uppercase(tokens[i])
elif tokens[i] in '?.!' and i < len(tokens) - 1:
tokens[i + 1] = uppercase(tokens[i + 1])
new_text = ' '.join(tokens)
new_text = ' ' + new_text + ' '
for tup in switch_list:
new_text = new_text.replace(tup[0], tup[1])
# get rid of surrounding whitespace
new_text = new_text.strip()
new_text = new_text.replace(' ', ' ')
if version > 1 and new_text and new_text[-1] not in '!.?)"\'':
new_text += '.'
return new_text
def uppercase(string: str) -> str:
"""
Make the first character of the string uppercase, if the string is non-empty.
"""
if len(string) == 0:
return string
else:
return string[0].upper() + string[1:]
def name_to_classname(name: str) -> str:
words = name.split('_')
class_name = ''
for w in words:
# capitalize the first letter
class_name += w[0].upper() + w[1:]
return class_name
def colorize(text, style):
try:
# if we're in ipython it's okay to use colors
__IPYTHON__
USE_COLORS = True
except NameError:
USE_COLORS = _sys.stdout.isatty()
if not USE_COLORS:
return text
colorstyle = os.environ.get('PARLAI_COLORSTYLE')
RESET = '\033[0;0m'
if style == 'red':
return '\033[0;31m' + text + RESET
if style == 'yellow':
return '\033[0;93m' + text + RESET
if style == 'green':
return '\033[0;32m' + text + RESET
if style == 'blue':
return '\033[0;34m' + text + RESET
if style == 'brightblack':
return '\033[0;90m' + text + RESET
if colorstyle is None or colorstyle.lower() == 'steamroller':
BLUE = '\033[1;94m'
BOLD_LIGHT_GRAY_NOBK = '\033[1m'
LIGHT_GRAY_NOBK = '\033[0m'
MAGENTA = '\033[0;95m'
HIGHLIGHT_RED_NOBK = '\033[1;31m'
HIGHLIGHT_BLUE_NOBK = '\033[0;34m'
if style == 'highlight':
return HIGHLIGHT_RED_NOBK + text + RESET
if style == 'highlight2':
return HIGHLIGHT_BLUE_NOBK + text + RESET
elif style == 'text':
return LIGHT_GRAY_NOBK + text + RESET
elif style == 'bold_text':
return BOLD_LIGHT_GRAY_NOBK + text + RESET
elif style == 'labels' or style == 'eval_labels':
return BLUE + text + RESET
elif style == 'label_candidates':
return LIGHT_GRAY_NOBK + text + RESET
elif style == 'id':
return LIGHT_GRAY_NOBK + text + RESET
elif style == 'text2':
return MAGENTA + text + RESET
elif style == 'field':
return HIGHLIGHT_BLUE_NOBK + text + RESET
else:
return MAGENTA + text + RESET
if colorstyle.lower() == 'spermwhale':
BLUE = '\033[1;94m'
BOLD_LIGHT_GRAY = '\033[1;37;40m'
LIGHT_GRAY = '\033[0;37;40m'
MAGENTA = '\033[0;95m'
HIGHLIGHT_RED = '\033[1;37;41m'
HIGHLIGHT_BLUE = '\033[1;37;44m'
if style == 'highlight':
return HIGHLIGHT_RED + text + RESET
if style == 'highlight2':
return HIGHLIGHT_BLUE + text + RESET
elif style == 'text':
return LIGHT_GRAY + text + RESET
elif style == 'bold_text':
return BOLD_LIGHT_GRAY + text + RESET
elif style == 'labels' or style == 'eval_labels':
return BLUE + text + RESET
elif style == 'label_candidates':
return LIGHT_GRAY + text + RESET
elif style == 'id':
return LIGHT_GRAY + text + RESET
elif style == 'text2':
return MAGENTA + text + RESET
elif style == 'field':
return HIGHLIGHT_BLUE + text + RESET
else:
return MAGENTA + text + RESET
# No colorstyle specified/found.
return text