3
我正在尝试使用Levenshtein距离函数来比较目录中所有其他文档中指定文档的文本。计算Levenshtein距离
我有一个基本的想法,但我不知道如何在PHP中编写代码。我来自C#背景,所以我会尽可能详细地提供。
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;
}
}
你重新实现一些PHP已经有存在的理由? [php levenshtein](http://php.net/manual/en/function.levenshtein.php) – birryree
我不是重新实现levenshtein,我只是用它来比较文档中的原始文本和列表中的其他文档文件在目录中。 – user478636
写一个会话包装是恕我直言的方式去。你可以通过创建一个名为User的类来完成。 – greg0ire