text
stringlengths
0
15.3k
if self._num_highlights is None or self._num_highlights < 0:
self._num_highlights = random.randint(1, _NUM_HIGHLIGHTED_SECTIONS)
self._description_pattern = 'Highlight at least {num_highlights} sections in your answer with ' + 'markdown, i.e. *highlighted section*.'
return self._description_pattern.format(num_highlights=self._num_highlights)
def get_instruction_args(self):
return {'num_highlights': self._num_highlights}
def get_instruction_args_keys(self):
return ['num_highlights']
def check_following(self, value):
num_highlights = 0
highlights = re.findall('\\*[^\\n\\*]*\\*', value)
double_highlights = re.findall('\\*\\*[^\\n\\*]*\\*\\*', value)
for highlight in highlights:
if highlight.strip('*').strip():
num_highlights += 1
for highlight in double_highlights:
if highlight.removeprefix('**').removesuffix('**').strip():
num_highlights += 1
return num_highlights >= self._num_highlights
class SectionChecker(Instruction):
def build_description(self, *, section_spliter=None, num_sections=None):
self._section_spliter = section_spliter.strip() if isinstance(section_spliter, str) else section_spliter
if self._section_spliter is None:
self._section_spliter = random.choice(_SECTION_SPLITER)
self._num_sections = num_sections
if self._num_sections is None or self._num_sections < 0:
self._num_sections = random.randint(1, _NUM_SECTIONS)
self._description_pattern = 'Your response must have {num_sections} sections. Mark the beginning ' + 'of each section with {section_spliter} X, such as:\n' + '{section_spliter} 1\n' + '[content of section 1]\n' + '{section_spliter} 2\n' + '[content of section 2]'
return self._description_pattern.format(num_sections=self._num_sections, section_spliter=self._section_spliter)
def get_instruction_args(self):
return {'section_spliter': self._section_spliter, 'num_sections': self._num_sections}
def get_instruction_args_keys(self):
return ['section_spliter', 'num_sections']
def check_following(self, value):
section_splitter_patten = '\\s?' + self._section_spliter + '\\s?\\d+\\s?'
sections = re.split(section_splitter_patten, value)
num_sections = len(sections) - 1
return num_sections >= self._num_sections
class ParagraphChecker(Instruction):
def build_description(self, *, num_paragraphs=None):
self._num_paragraphs = num_paragraphs
if self._num_paragraphs is None or self._num_paragraphs < 0:
self._num_paragraphs = random.randint(1, _NUM_PARAGRAPHS)
self._description_pattern = 'There should be {num_paragraphs} paragraphs. ' + 'Paragraphs are separated with the markdown divider: ***'
return self._description_pattern.format(num_paragraphs=self._num_paragraphs)
def get_instruction_args(self):
return {'num_paragraphs': self._num_paragraphs}
def get_instruction_args_keys(self):
return ['num_paragraphs']
def check_following(self, value):
paragraphs = re.split('\\s?\\*\\*\\*\\s?', value)
num_paragraphs = len(paragraphs)
for (index, paragraph) in enumerate(paragraphs):
if not paragraph.strip():
if index == 0 or index == len(paragraphs) - 1:
num_paragraphs -= 1
else:
return False
return num_paragraphs == self._num_paragraphs
class PostscriptChecker(Instruction):
def build_description(self, *, postscript_marker=None):
self._postscript_marker = postscript_marker.strip() if isinstance(postscript_marker, str) else postscript_marker
if self._postscript_marker is None:
self._postscript_marker = random.choice(_POSTSCRIPT_MARKER)
self._description_pattern = 'At the end of your response, please explicitly add a postscript ' + 'starting with {postscript}'
return self._description_pattern.format(postscript=self._postscript_marker)
def get_instruction_args(self):
return {'postscript_marker': self._postscript_marker}
def get_instruction_args_keys(self):
return ['postscript_marker']
def check_following(self, value):
value = value.lower()
if self._postscript_marker == 'P.P.S':
postscript_pattern = '\\s*p\\.\\s?p\\.\\s?s.*$'
elif self._postscript_marker == 'P.S.':
postscript_pattern = '\\s*p\\.\\s?s\\..*$'
else:
postscript_pattern = '\\s*' + self._postscript_marker.lower() + '.*$'
postscript = re.findall(postscript_pattern, value, flags=re.MULTILINE)
return True if postscript else False
class RephraseChecker(Instruction):