Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8883625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:51:35+00:00 2026-06-14T20:51:35+00:00

In an attempt to rewrite PHP’s similar_text algorithm I have tried a few different

  • 0

In an attempt to rewrite PHP’s similar_text algorithm I have tried a few different approaches. All have been moderately successful but ultimately failed.

First attempt: I tried just rewriting it from the PHP source code. C’s elegant use of pointers makes the same exact implementation seemingly impossible to make in Scala and be clean.

Second attempt: I tried rewriting it from a Java function someone posted on PHP similar_text() in java. Unfortunately that function doesn’t work in Java so nevermind porting it over to Scala.

Third (current) attempt: I’m currently attempting to translate this JavaScript implementation into Scala: http://phpjs.org/functions/similar_text/. I’ve used it before in JavaScript and it seems to function properly. My translation (below) into Scala is not functioning properly. It gets you within 1 or 2 similarity indexes but it is typically not 100% to the results of it’s PHP counterpart.

def similartext(first:String,second:String) : Int = {
  if (first == null || second == null) {
    0
  }

  var pos1:Int = 0
  var pos2:Int = 0
  var max:Int = 0
  var sum:Int = 0
  var l:Int = 0

  val firstLength:Int = first.length
  val secondLength:Int = second.length

  for (p <- 0 until firstLength) {
    for (q <- 0 until secondLength) {
      while(p+l < firstLength && q+l < secondLength && (first.charAt(p+l) == second.charAt(q+l))) {
        if (l > max) {
            println("[" + p + "," + q + "," + l + "]" + first.charAt(p+l) + " | " + second.charAt(q+l))
            max = l
            pos1 = p
            pos2 = q
          }
        l += 1
      }
    }
  }

  sum = max;

  if (sum > 0) {
    if (pos1 > 0 && pos2 > 0) {
      sum += similartext(first.substring(0, pos2), second.substring(0, pos2))
    }

    if ((pos1 + max < firstLength) && (pos2 + max < secondLength)) {
      sum += similartext(first.substring(pos1 + max, (pos1 + max) + (firstLength - pos1 - max)), second.substring(pos2 + max, (pos2 + max) + (secondLength - pos2 - max)))
    }
  }

  sum;
}

Tests:

(Scala)val st = similartext("apple","aple") Yields 3
(PHP)$similar = similar_text("apple","aple"); Yields 4

(Scala)val st = similartext("starbucks","stharducks") Yields 8
(PHP)$similar = similar_text("starbucks","stharducks"); Yields 8

(Scala)val st = similartext("hello earth!","hello world!") Yields 10
(PHP)$similar = similar_text("hello earth!","hello world!"); Yields 8

Does anyone have any ideas on what is going wrong here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T20:51:36+00:00Added an answer on June 14, 2026 at 8:51 pm

    Here’s a hint: look very closely at line 28 of the JavaScript version—particularly the last character of the line. That’s where your implementation differs. (You also don’t reset l to zero for every pair of indices, but that’s not the most important problem.)

    Here’s a var-free Scala version, by the way:

    def similarText(x: String, y: String): Int = {
      val indices = for {
        (s, p) <- x.tails.zipWithIndex
        (t, q) <- y.tails.zipWithIndex
        l = ((s zip t) takeWhile Function.tupled(_ == _)).size
      } yield (p, q, l)
      val (pos1, pos2, max) = indices.maxBy(_._3)
    
      if (max == 0) max else max +
        similarText(x take pos1, y take pos2) +
        similarText(x drop (pos1 + max), y drop (pos2 + max))
    }
    

    This is fairly off-the-cuff—I’m sure you could make it more concise and efficient pretty easily.

    And for extra credit: there’s a bug in the JavaScript version—try for example "aabcd" and "abcabcd" and the result won’t be the same as PHP’s (or mine).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

OK I have been messing about with URL rewriting for the last few days
The situation: Using a off-the-shelf PHP application, I have to add in a new
This is an attempt to rewrite some old homework using STL algorithms instead of
We have a rewrite rule that looks like this: RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}
I have a bit of a complicated rewrite scenario that I could use some
I have tried various methods to try and get this working correctly (I am
Hi I have been told by my hosting provider to add this to my
I have to rewrite the core cache model. And this doesn't work. My first
I have .htaccess currently set up to rewrite anything without a period or slash
I have the attached redirect and rewrite rules. Essentially, www.foo.com/vehicles/vehicle-detail.aspx?stockno=123456 is rewritten to www.foo.com/123456.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.