PHP 中的编辑
我已经在模型中编写了这些函数(我正在使用 CodeIgniter)。
function getLocalIngrdname()
{
$this->load->database();
$query = $this->db->get('onl_local_ingrd');
foreach($query->result() as $row)
$allingrd[]=$row->ingrd_localname;
return $allingrd;
}
function getCloseIngrdname($ingrdname,$localname)
{
$this->load->database();
$query = $this->db->get('onl_ingrd');
foreach($query->result() as $row)
$allingrd[]=$row->ingrd_name;
foreach($localname as $row)
$allingrd[]=$row;
$shortest=-1;
foreach ($allingrd as $ingrd) {
$lev = levenshtein($ingrdname, $ingrd);
if ($lev <= $shortest || $shortest < 0) {
$closeword = $ingrd;
$shortest = $lev;
}
}
return $closeword;
}
该函数位于控制器中
function getResult() { $this->load->model('searchRecipe_model'); $ingrdname = $this->input->post('ingrdname'); $output[]=2; $localnames[]=$this->searchRecipe_model->getLocalIngrdname(); $output[]=$this->searchRecipe_model->getCloseIngrdname($ingrdname,$localnames); echo json_encode($localnames); }
$allingrd
是一个成分名称数组。我在 javascript 警报消息中显示 $closeword
。 如果我给出$lev=levenshtein($ingrdname,$allingrd[0])
,它就可以完美工作;但是,它不在循环中工作。有什么想法吗?提前致谢。
I have written these function in a model (I am using CodeIgniter).
function getLocalIngrdname()
{
$this->load->database();
$query = $this->db->get('onl_local_ingrd');
foreach($query->result() as $row)
$allingrd[]=$row->ingrd_localname;
return $allingrd;
}
function getCloseIngrdname($ingrdname,$localname)
{
$this->load->database();
$query = $this->db->get('onl_ingrd');
foreach($query->result() as $row)
$allingrd[]=$row->ingrd_name;
foreach($localname as $row)
$allingrd[]=$row;
$shortest=-1;
foreach ($allingrd as $ingrd) {
$lev = levenshtein($ingrdname, $ingrd);
if ($lev <= $shortest || $shortest < 0) {
$closeword = $ingrd;
$shortest = $lev;
}
}
return $closeword;
}
This function is in controller
function getResult() { $this->load->model('searchRecipe_model'); $ingrdname = $this->input->post('ingrdname'); $output[]=2; $localnames[]=$this->searchRecipe_model->getLocalIngrdname(); $output[]=$this->searchRecipe_model->getCloseIngrdname($ingrdname,$localnames); echo json_encode($localnames); }
$allingrd
is an array of ingredient names. I display the $closeword
in javascript alert message.
If I give$lev=levenshtein($ingrdname,$allingrd[0])
it works perfectly; however, it's not working in the loop. Any ideas as to why? Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许您有一些长度超过 255 个字符的字符串?在这些情况下,levenshtein() 函数返回 -1,这意味着根据您的算法,最后一个此类字符串将始终是“最短的”。如果这是您的问题,您需要以这种方式丢弃 $lev 等于 -1 的值:
请注意,$closeword 可能并不总是被设置(例如,如果 $allingrd 中的所有字符串都超过 255 个字符,或者如果 $allingrd 为空)。不要忘记在循环之前将 $closeword 初始化为合理的值,例如 false、null 或空字符串。
Maybe you have som strings which are longer than 255 chars? The levenshtein() function returns -1 in those cases, which means the last such string will always be "the shortest" according to your algorithm. If this is your problem, you'll need to discard values where $lev equals -1 in this way:
Note that $closeword may not always be set (if, for example, all strings in $allingrd is longer than 255 chars, or if $allingrd is empty). Don't forget to initialise $closeword to something sensible before the loop, for example false, null or an empty string.
我认为你想要做的是:
现在 $closeword 是一个数组。您只是在每次循环运行时重新声明 $closeword 的值。现在,您将把值放入数组中。最短的美元也一样。
I think that what you're trying to do is:
Now $closeword is an array. You were just redeclaring the value of $closeword, for each time your loop was running. Now, you will throw the values inside an array. Same for $shortest.
请注意 PHP 的 levenshtein 页面上如何声明
$shortest
为-1
。这非常重要,否则您将永远无法满足if
语句的要求。这应该可以解决问题。
Notice how on PHP's levenshtein page they declare
$shortest
as-1
. This is pretty important or else you'll never meet the requirements of theif
statement.This should do the trick.