io.cequence.openaiscala.task.CompletionTaskHelper.scala Maven / Gradle / Ivy
The newest version!
package io.cequence.openaiscala.task
import scala.util.Random
import scala.util.matching.Regex
trait CompletionTaskHelper {
protected def generateInputUniformDistAux(stringSize: Int) =
(1 to stringSize).map(_ =>
if (Random.nextBoolean()) "1" else "0"
).mkString("")
protected def generateInputDensityUniformDistAux(stringSize: Int) = {
val onesCount = Random.nextInt(stringSize + 1)
val shuffledIndeces = Random.shuffle(List.range(0, stringSize))
val onesIndeces = shuffledIndeces.take(onesCount).toSet
(0 to stringSize - 1).map(index =>
if (onesIndeces.contains(index)) "1" else "0"
).mkString("")
}
protected def extractAux(
pattern: Regex,
prefixes: Seq[String],
text: String
): Option[String] =
pattern.findAllIn(text.toLowerCase()).toList.lastOption.map(result =>
prefixes.foldLeft(result)((acc, prefix) => acc.stripPrefix(prefix).trim)
)
protected def extractWoSpacesAux(
pattern: Regex,
prefixes: Seq[String],
text: String
): Option[String] =
// filter out spaces
extractAux(pattern, prefixes, text).map(_.filterNot(_ == ' '))
protected def extractWoSpacesAux(
pattern: Regex,
prefix: String,
text: String
): Option[String] =
// filter out spaces
extractWoSpacesAux(pattern, Seq(prefix), text)
}