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

  • Home
  • SEARCH
  • 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 6801061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:03:13+00:00 2026-05-26T19:03:13+00:00

I’m trying to make a class that uses the Levenshtein distance function to compare

  • 0

I’m trying to make a class that uses the Levenshtein distance function to compare the text from a specified document amongst all the other documents in a directory.

I have the basic idea in mind but I don’t know how to code it in PHP. I am from a C# background so I’ll provide as much detail as possible.

class ComputeLevenshtein
{
   public $filePathList = new Array(); //The array that stores the absolute path of all documents within a specified directory
   public $directory;
   public $filePath; //This is the document that will be compared for each document in a directory

    public function __construct() {
        $this->directory = //;  
       /* I'm stuck here, once a user registers, a separate directory is 
          named after the user. I need to be able to read the username 
          from the Session Variable once the user logs in. 
          I'll just have to pass it in as a parameter. 
          Do I have to create a session wrapper? 
          If it's too complex, 
          then I'll just start off with a static directory */
    }

        // Returns the array containing each filePath for every document in a directory.
        function computeFilePathList($directory) 
        {
           for each file in Directory
           {
             $filepath = file.FilePath(); //store the filepath in a variable
             $this->filePathList.add($filePath) //add the filepath to the array
           }

        }  

        function ($docFilePath) // returns the Levenshtein Distance
        {

            for each path in filePathList
            {
              $input= readDoc($docFilePath);
              $lev = levenshtein($input, readDoc($path));
            }

            return $lev;
        }

    function readDoc($docFilePath) // Returns the raw text of that doc
    {
      //I Have the code for reading the doc in a seperate function
      return $text;
    }
}
  • 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-05-26T19:03:13+00:00Added an answer on May 26, 2026 at 7:03 pm

    How about this:

    class Levenshtein
    {
        private $_p = array(); 
    
        public function __construct($input, $compare)
        {
            $this->_p['input']   = $input;
            $this->_p['compare'] = $compare; // string to check against
        }
    
        public function __get($property)
        {
            if (array_key_exists($property, $this->_p)) {
                return $this->_p[$property];
            }
    
            if (!isset($this->_p['dist']) && $property === 'dist') {
                $this->_p['dist'] = levenshtein($this->_p['input'],
                                                $this->_p['compare']);
                return $this->_p['dist'];
            }
        }
    }
    
    class DirectoryLevenshtein
    {
        private $_directory;
        private $_filePath;
        private $_distances = array();
    
        public function __construct($directoryPath, $filePath = null)
        {
            if (!is_dir($directoryPath)) {
                throw new Exception("Path '$directoryPath' does not exist");
            }
    
            if (substr($directoryPath, -1) !== '/') {
                $directoryPath .= '/';
            }
    
            $this->_directory = $directoryPath;
    
            if ($filePath !== null) {
                if (!$this->setFilePath($filePath)) {
                    throw new Exception("File '$filePath' is not readable");
                }
            }
        }
    
        public function __get($file)
        {
            if (array_key_exists($file, $this->_distances)) {
                return $this->_distances[$file];
            }
    
            if (is_readable($this->_directory . $file)) {
                if (empty($this->_filePath)) {
                    return null;
                }
    
                $input   = file_get_contents($this->_filePath);
                $compare = file_get_contents($this->_directory . $file);
                $this->_distances[$file] = new Levenshtein($input, $compare);
                return $this->_distances[$file];
            }
        }
    
        public function getDirectoryContents()
        {
            $files = scandir($this->_directory);
    
            while ($files[0] === '.' || $files[0] === '..') {
                array_shift($files);
            }
    
            return $files;
        }
    
        public function setFilePath($filePath)
        {
            if (empty($this->_filePath) && is_readable($filePath)) {
                $this->_filePath = $filePath;
                return true;
            }
    
            return false;
        }
    }
    

    To use it do something like the following:

    // could user session wrapper instead
    $userDir = '/path/to/user/dirs/' . $_SESSION['user'];
    // file to compare all files with 
    $filePath = /path/to/file.txt
    
    $dirLev = new DirectoryLevenshtein($userDir, $filePath);
    
    // Files in directory
    $files = $dirLev->getDirectoryContents();
    
    // Distances
    foreach ($files as $file) {
        echo "$file: {$dirLev->file->dist}\n";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I have a bunch of posts stored in text files formatted in yaml/textile (from
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has

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.