- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在尝试使用 similar_text()
和 in_array()
在 PHP 中制作一个简单的拼写检查和建议程序。我有一个文本文件 dictionary.txt,其中包含英语中的大部分单词。
首先,我将文本文件中的所有单词都换行放入一个数组中。然后在用户输入和提交时,我使用 in_array()
检查他们输入的单词是否在数组中。如果是,那么他们拼写正确。
如果不是,则我使用 similar_text()
在数组中查找与拼写错误的单词接近的单词。
我遇到了两个无法解决的问题,我相信我正在根据 PHP 文档正确使用 in_array()
和 similar_text()
.
第一个问题是,当用户键入并提交文本文件中应该也应该在数组中的单词时,else 会触发,这不应该发生。因为它在文本文件中,所以它应该在数组中,并且 in_array()
应该评估为真。
第二个问题是我收到一个错误,我通过 similar_text()
存储两个词之间相似度百分比的变量未定义。我正在使用它,similar_text()
,就像在文档注释示例中一样;事实上,我在每次比较之前重置和重新定义 $percentageSimilarity
。为什么我会收到未定义的错误消息?
这是我的代码:
<?php
function addTo($line){
return $line;
}
$words = array_map('addTo', file('dictionary.txt'));
if(isset($_GET['checkSpelling'])){
$input = (string)$_GET['checkSpelling'];
$suggestions = array();
if(in_array($input, $words)){
echo "you spelled the word right!";
}
else{
foreach($words as $word){
$percentageSimilarity=0.0;
similar_text($input, $word, $percentageSimilarity);
if($percentageSimilarity>=95){
array_push($suggestions, $word);
}
}
echo "Looks like you spelled that wrong. Here are some suggestions: \n";
foreach($suggestions as $suggestion){
echo $suggestion;
}
}
}
?>
<!Doctype HTMl>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Spell Check</title>
</head>
<body>
<form method="get">
<input type="text" name="checkSpelling" autocomplete="off" autofocus />
</form>
</body>
</html>
最佳答案
将您的添加行更改为
function addTo($line){
return strtolower(trim($line));
}
并将您的输入更改为
$input = strtolower(trim($_GET['checkSpelling']));
文件命令有一个讨厌的习惯,它会留下尾随的换行符,所以你可能不会基于那个进行匹配……trim 应该处理这个问题。其他更改只是为了使其不区分大小写。
关于PHP similar_text() 和 in_array() 没有像他们应该的那样工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16910116/
好的,我正在使用 similar_text() 比较字符串,但结果受到大小写敏感的影响。有没有办法在 php.ini 或其他东西中以某种方式关闭它? 最佳答案 将字符串转换为小写,必须使用 strto
我只是想知道,这里发生了什么。如果我使用这个: var_dump(similar_text('abcd', 'abcdefg', $percent)); //output: int 4 没关系,abc
我刚刚找到了similar_text 函数并正在使用它,但是百分比输出总是让我感到惊讶。请参阅下面的示例。 我试图找到关于 php: similar_text()Docs 中提到的算法的信息。 : "
php similar_text() 函数计算比较两个字符串的相似度,本文章向码农介绍php similar_text() 函数的基本使用方法和基本使用实例,感兴趣的码农可以参考一下。 定义和
PHP 提供了一个极少使用的 similar_text 函数,但此函数非常有用,用于比较两个字符串并返回相似程度的百分比,以下是similar_text () 函数的使用方法: 复制代
我有两个表,分别称为引用表和列表表。两者都有两个字段:id 和 name。 reference: ref_id, ref_name 1, john 2, georges etc... list: ls
我想创建一个简单的搜索引擎来查找用户输入的关键词。我知道我可以使用 strpos 检查字符串中是否存在单词。但是,我希望用户能够拼错这个词。例如, $userInput = "What year di
我正在尝试使用 similar_text() 和 in_array() 在 PHP 中制作一个简单的拼写检查和建议程序。我有一个文本文件 dictionary.txt,其中包含英语中的大部分单词。 首
我目前正在使用 similar_text将字符串与 ~50,000 的列表进行比较,虽然由于比较的数量非常慢,但它仍然有效。比较约 500 个独特的字符串大约需要 11 分钟。 在运行它之前,我确实检
我想合并 MySQL 中的两个表: table_1 -------------------------------------------------------- id | table_1_
为了重写 PHP 的 similar_text 算法,我尝试了几种不同的方法。所有这些都取得了一定的成功,但最终都失败了。 第一次尝试:我试着从 PHP 源代码重写它。 C 对指针的优雅使用使得完全相
我在哪里可以找到比 levenshtein() 和 php similar_text() 方法更准确地评估错位字符拼写的算法? 例子: similar_text('jonas', 'xxjon', $
我是一名优秀的程序员,十分优秀!