<commit_before>/*
 * (C) 2010-2012 ICM UW. All rights reserved.
 */

package pl.edu.icm.coansys.commons.scala

import annotation.tailrec
import collections._
import util.matching.Regex

/**
 * @author <NAME> (<EMAIL>)
 */
object strings {
  /**
   * Returns all rotations of a given string
   */
  def rotations(s: String): IndexedSeq[String] = {
    for {
      b <- 0 to (s.length - 1)
      rot = s.substring(b) + s.substring(0, b)
    } yield rot
  }

  /**
   * Computes HCS (Heaviest Common Subsequence) of two strings using given weight function.
   */
  def hcs(s1: String, s2: String, weight: Char => Double) = {
    val hcsArray = tabulate[Double](s1.length + 1, s2.length + 1) {
      (hcs, i, j) =>
        if (i == 0 || j == 0) {
          0
        } else {
          val eqBonus = if (s1.charAt(i - 1) == s2.charAt(j - 1)) weight(s1.charAt(i - 1)) else 0
          List(hcs(i - 1)(j - 1) + eqBonus, hcs(i - 1)(j), hcs(i)(j - 1)).max
        }
    }

    @tailrec
    def extractHcs(i: Int, j: Int, acc: List[(Int, Int)] = Nil): List[(Int, Int)] = {
      if (i > 0 && j > 0) {
        if (hcsArray(i - 1)(j - 1) >= math.max(hcsArray(i - 1)(j), hcsArray(i)(j - 1))) {
          if (s1.charAt(i - 1) == s2.charAt(j - 1))
            extractHcs(i - 1, j - 1, (i - 1, j - 1) :: acc)
          else
            extractHcs(i - 1, j - 1, acc)
        } else {
          if (hcsArray(i - 1)(j) >= hcsArray(i)(j - 1))
            extractHcs(i - 1, j, acc)
          else
            extractHcs(i, j - 1, acc)
        }
      }
      else {
        acc<commit_msg>fixed hcs and lcs<commit_after>/*
 * (C) 2010-2012 ICM UW. All rights reserved.
 */

package pl.edu.icm.coansys.commons.scala

import annotation.tailrec
import util.matching.Regex

/**
 * @author <NAME> (<EMAIL>)
 */
object strings {
  /**
   * Returns all rotations of a given string
   */
  def rotations(s: String): IndexedSeq[String] = {
    for {
      b <- 0 to (s.length - 1)
      rot = s.substring(b) + s.substring(0, b)
    } yield rot
  }

  /**
   * Computes HCS (Heaviest Common Subsequence) of two strings using given weight function.
   */
  def hcs(s1: String, s2: String, weight: Char => Double) = {
    val sndDim = s2.length + 1
    val hcsArray = Array.ofDim[Double]((s1.length + 1) * (s2.length + 1))
    for (i <- 0 to s1.length) {
      for (j <- 0 to s2.length)
        if (i == 0 || j == 0) {
          hcsArray(i * sndDim + j) = 0
        } else {
          val eqBonus = if (s1.charAt(i - 1) == s2.charAt(j - 1)) weight(s1.charAt(i - 1)) else 0
          hcsArray(i * sndDim + j) = List(hcsArray((i - 1) * sndDim + (j - 1)) + eqBonus, hcsArray((i - 1) * sndDim + (j)), hcsArray(i * sndDim + j - 1)).max
        }
    }

    @tailrec
    def extractHcs(i: Int, j: Int, acc: List[(Int, Int)] = Nil): List[(Int, Int)] = {
      if (i > 0 && j > 0) {
        if (hcsArray((i - 1) * sndDim + (j - 1)) >= math.max(hcsArray((i - 1) * sndDim + (j)), hcsArray((i) * sndDim + (j - 1)))) {
          if (s1.charAt(i - 1) == s2.charAt(j - 1))
            extractHcs(i - 1, j - 1, (i - 1, j - 1) :: acc)
          else
            extractHcs(i - 1, j - 1, acc)
        } else {
          if (hcsArray((i - 1) * sndDim + (j)) >= hcsArray((i) * sndDim + (j - 1)))
            extractHcs(i - 1, j, acc)
          else
            extractHcs(i, j - 1, acc)
        }
      }
      else {
        acc
{'fraction_non_alphanumeric': 0.17613972764949673, 'fraction_numerical': 0.03197158081705151, 'mean_word_length': 1.8158333333333334, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2655887', 'n_tokens_mistral': 1401, 'n_tokens_neox': 1296, 'n_words': 355}