All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.cequence.openaiscala.task.binary.EliminateBinaryPairsTask.scala Maven / Gradle / Ivy

The newest version!
package io.cequence.openaiscala.task.binary

import io.cequence.openaiscala.task.domain.BinaryTaskCoreSettings

class EliminateBinaryPairsTask(withAssistantSeedPrompt: Boolean = false) extends BinaryStringTask[BinaryTaskCoreSettings] {

  private val answerPrefix = "The final string is"

  override def seedAssistantPrompt = if (withAssistantSeedPrompt) Some(answerPrefix) else None

  private val answerPattern = s"${answerPrefix.toLowerCase}[01\\s]*".r

  override protected val examples = Seq(
    "0101101",
    "0110010",
    "101010",
    "100111"
  )

  override def generatePrompt(
    input: String,
    settings: BinaryTaskCoreSettings
  ) =
    s"""In this task, given a binary string, two adjacent 1s (two 1s that are next to each other) or two adjacent 0s (two 0s that are next to each other) are to be removed. This rule is applied repeatedly until no more 1s or 0s can be removed.${generateExamples(settings)}
       |
       |Now, apply this rule repeatedly to its full extent to the following string:
       |
       |${addSpaces(settings.withSpaces)(input)}
       |
       |At the end of your answer write "$answerPrefix """".stripMargin

  override def evalResult(
    input: String,
    output: String
  ): Option[Int] = {
    val expectedOutputAux = eliminatePairs(input.replaceAll(" ", ""))
    val actualOutput = extractWoSpacesAux(answerPattern, answerPrefix.toLowerCase(), output).map(
      _.replaceAll(" ", "").trim
    )

    actualOutput.map(actualOutputAux =>
      actualOutputAux == expectedOutputAux match {
        case true => 1  // give one point if match
        case false =>
          println(s"expected: $expectedOutputAux != actual: $actualOutputAux")
          0 // otherwise give zero points
      }
    )
  }

  override def expectedOutput(
    input: String,
    settings: BinaryTaskCoreSettings
  ): Option[String] = {
    // remove spaces and eliminate pairs
    val output = eliminatePairs(input.replaceAll(" ", ""))

    Some(addSpaces(settings.withSpaces)(output))
  }

  // aux functions

  override protected def addExample(
    withSpace: Boolean)(
    example: String
  ) = {
    val steps = addExampleAux(withSpace)(example)
    val startingString = addSpaces(withSpace)(example)

    val iterations = if (steps.nonEmpty)
      steps.mkString("")
    else
      s" -> (no pairs to eliminate) -> $startingString"

    startingString + iterations
  }

  protected def addExampleAux(
    withSpace: Boolean)(
    binaryString: String
  ): Seq[String] = {
    def eliminateAux(digit: Char) = {
      val digitPair = s"$digit$digit"
      val binaryStringAux = binaryString.replaceFirst(digitPair, "")
      val output = s" -> (eliminating adjacent ${digit}s) -> ${addSpaces(withSpace)(binaryStringAux)}"
      output +: addExampleAux(withSpace)(binaryStringAux)
    }

    if (binaryString.contains("11"))
      eliminateAux('1')
    else if (binaryString.contains("00"))
      eliminateAux('0')
    else
      Nil
  }

  private def eliminatePairs(
    binaryString: String
  ): String =
  // call recursively until no more 1s or 0s can be removed
    if (binaryString.contains("00") || binaryString.contains("11")) {
      val binaryStringAux = binaryString
        .replaceAll("00", "")
        .replaceAll("11", "")

      eliminatePairs(binaryStringAux)
    } else
      binaryString
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy