| import re | |
| def pre_caption(caption, max_words = 64): | |
| caption_raw = caption | |
| caption = re.sub( | |
| r"([,.'!?\"()*#:;~])", | |
| ' ', | |
| caption.lower(), | |
| ).replace('-', ' ').replace('/', ' ').replace('<person>', 'person') | |
| caption = re.sub( | |
| r"\s{2,}", | |
| ' ', | |
| caption, | |
| ) | |
| caption = caption.rstrip('\n') | |
| caption = caption.strip(' ') | |
| # truncate caption | |
| caption_words = caption.split(' ') | |
| if len(caption_words) > max_words: | |
| caption = ' '.join(caption_words[:max_words]) | |
| if not len(caption): | |
| raise ValueError(f"pre_caption yields invalid text (raw: {caption_raw})") | |
| return caption | |
| def pre_caption_list(captionList, max_words = 64): | |
| newCaptionList = [pre_caption(caption, max_words) for caption in captionList] | |
| return newCaptionList |