Skip to content

Commit

Permalink
models: add RandomManuscriptRevisionModel for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
miltondp committed Jan 1, 2023
1 parent 60b4971 commit d595fcf
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion libs/manubot/ai_editor/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import re
from abc import ABC, abstractmethod
import random

import openai

Expand Down Expand Up @@ -42,7 +43,6 @@ class DummyManuscriptRevisionModel(ManuscriptRevisionModel):
"""
This model does nothing, just returns the same paragraph content with
sentences one after the other separated by a white space (no new lines).
This mimics what a real OpenAI model does.
"""

def __init__(self):
Expand All @@ -56,6 +56,41 @@ def get_prompt(self, paragraph_text, section_name):
return paragraph_text


class RandomManuscriptRevisionModel(ManuscriptRevisionModel):
"""
This model takes a paragraph and randomizes the words. The paragraph has the
sentences one after the other separated by a white space (no new lines).
"""

def __init__(self):
super().__init__()
self.sentence_end_pattern = re.compile(r"\n")

def revise_paragraph(self, paragraph_text: str, section_name: str) -> str:
"""
It takes each sentence of the paragraph and randomizes the words.
"""
paragraph_text = self.sentence_end_pattern.sub(" ", paragraph_text).strip()
sentences = paragraph_text.split(". ")
sentences_revised = []
for sentence in sentences:
words = sentence.split(" ")
words_revised = []
for word in words:
if len(word) > 3:
word_revised = (
"".join(random.sample(word[1:-1], len(word[1:-1]))) + word[-1]
)
else:
word_revised = word
words_revised.append(word_revised)
sentences_revised.append(" ".join(words_revised))
return ". ".join(sentences_revised)

def get_prompt(self, paragraph_text, section_name):
return paragraph_text


class GPT3CompletionModel(ManuscriptRevisionModel):
"""
Revises a paragraphs using GPT-3 completion model. Most of the parameters
Expand Down

0 comments on commit d595fcf

Please sign in to comment.