Home php教程 php手册 PHP匹配变音使404页面更加智能化

PHP匹配变音使404页面更加智能化

Jun 13, 2016 am 10:29 AM
404 php for content create match umlaut deal with you Intelligent information program site my own page

创建您自己的 404 错识消息处理程序,为站点内容提供有用的链接和重定向。使用变音匹配(metaphone matching)和一个简单的加权记分文件为输入错误、拼写错误和无效链接生成重定向建议。根据 Web 站点的内容和首选重定向位置定制建议。捕获传入 URL 请求中的各种错误,并通过处理纠正其中的目录、脚本和 HTML 页面名称错误。

关于如何为 404 页面创建有效格式的教程比比皆是。这类教程大多建议在 404 页面中包含静态的建议链接,并将这些链接指向站点的公共区域,比如说首页、下载页面和站点的搜索引擎,前提是要有这些页面。404 页面普遍存在的问题是它们无法反映用户访问该站点的目的。本文将介绍如何构建一个建议生成器和一个方法,用于根据 Web 站点的内容提供更加有用的重定向链接。

现行的 404 处理程序允许我们为各种错误提供一些建议链接,比如说将用户指向站点目录。一些拼写校正程序(比如说 mod_speling ——— 没错,它只有一个 “l”)可用于纠正词典单词中的错误,从而将用户定向到正确的页面。本文中的代码将帮助您构建一个建议生成引擎,它可以根据 Web 站点的内容来处理在词典中无法找到的单词和目录链接。

我们考虑这样一个场景:您在电话会议中听到了一个 Web 页面名称,因此便尝试打开 blegs/DavSmath.html 链接。现行的拼写校正模块无法为此情况提供一个有用的链接。使用本文中的代码,您将能够生成一个 404 页面,并在其中显示建议的有效页面 /blogs/DaveSmith.html。

需求

本世纪生产的任何现代 PC 应该都足以编写和运行本文中的代码。如果您的 Web 页面含有超过 10,000 个不同的页面,那么可能需要大容量的内存、高性能的硬件或足够的耐心。

所提供的 Perl 和 CGI 脚本可以在多种 UNIX® 和 Windows® 平台上运行(请参阅 下载部分。虽然本文将使用 Apache 和一个 CGI 脚本作为建议引擎,但是所构建的工具应该能够在大多数 Web 服务器上正常运行。对于变音匹配,本文将引用 Michael Schwern 编写的 Text::Metaphone 模块。在开始之前,先通过喜好的 CPAN 镜像安装 Text::Metaphone 模块。请参阅 参考资料 获得下载信息。

Web 服务器页面和变音代码

针对输入和拼写错误提供替代建议的主要方法为变音匹配。与 Soundex 语音算法和一些其他算法类似,Metaphone 使用字母数字代码表示单词的发音。但是,与 Soundex 语音算法有所不同,构建语音代码的目的是匹配英文发音的语言可变性。因此,变音代码通常能够更加准确地表示特定的单词,并且为建议库的构建提供了理论基础。

考虑示例 Web 服务器目录中的下列文件。


清单 1. Web 服务器文件
               

以下为引用的内容:
./index.html
./survey.html
./search_tips.html
./about.html
./how.html
./why.html
./who.html
./NathanHarrington.html
./blogs/NathanHarrington.html
./blogs/DaveSmith.html
./blogs/MarkCappel.html

针对这些静态 HTML 文件,我们将使用 buildMetaphoneList.pl 程序为所有扩展名为 .html 的文件创建变音。


清单 2. buildMetaphoneList.pl
               

以下为引用的内容:
#!/usr/bin/perl -w
# buildMetaphoneList.pl - / split filename, 0 score, metaphones

use strict;
use File::Find;
use Text::Metaphone;

find(&htmlOnly,".");

sub htmlOnly
{
  if( $File::Find::name =~ /.html/ )
  {
    my $clipFname = $File::Find::name;
    $clipFname =~ s/.html//g;

    my @slParts = split /, $clipFname;
    shift(@slParts);

    print "$File::Find::name ### 0 ### ";
    for( @slParts ){ print Metaphone($_) . " " }
    print " ";

  }#if a matching .html file

}#htmlOnly sub

buildMetaphoneList.pl 程序只能处理扩展名为 .html 的文件,它将移除文件名中的 .html,然后为完整路径名称的各个部分生成变音。将 buildMetaPhoneList.pl 程序复制到 Web 服务器的根目录下,然后运行命令 perl buildMetaphoneList.pl > metaphonesScore.txt。对于清单 1 中的文件,相应的 metaphonesScore.txt 文件内容如清单 3 所示。

清单 3. metaphonesScore.txt
               

以下为引用的内容:
./index.html ### 0 ### INTKS
./survey.html ### 0 ### SRF
./search_tips.html ### 0 ### SRXTPS
./about.html ### 0 ### ABT
./how.html ### 0 ### H
./why.html ### 0 ### H
./who.html ### 0 ### H
./NathanHarrington.html ### 0 ### N0NHRNKTN
./blogs/NathanHarrington.html ### 0 ### BLKS N0NHRNKTN
./blogs/DaveSmith.html ### 0 ### BLKS TFSM0
./blogs/MarkCappel.html ### 0 ### BLKS MRKKPL

清单 3 中的每一行文字都显示了 Web 服务器根目录下的实际链接、默认作用域和变音代码。注意,how.html、 why.html 和 who.html 都解析为了相同的变音代码。要解决这个不明确的地方,需要修改作用域字段,让链接建议程序以指定的顺序向页面提供链接。比如说,将 “H” 变音条目修改为:

以下为引用的内容:
./how.html ### 100 ### H
./why.html ### 50 ### H
./who.html ### 0 ### H

这样将创建一个直观的链接重排序,并留下空间用于作用域的进一步修改。作用域的数字越大,插入同一变音文件(不过是不同的作用域)的顺序就越靠后。比如说添加一个作用域为 25 的 hoo.html 文件列表,那么它将位于 who.html 条目之上和 why.html 条目之下。

您还可以使用作用域字段区分目录不同而名称相同的文件。比如说,将 ./NathanHarrington.html 一行的的作用域修改为 100,那么类似 nathenHorrington.html 这样的请求会将 ./NathanHarrington.html 链接列在 ./blogs/NathanHarrington.html 页面之前。

选择文件的作用域时,务必要考虑 Web 站点的统计和逻辑访问组件。从日志文件可以看出,用户对 why.html 页面的请求比较频繁,但是如果您认为 how.html 对于用户更为重要,那么只需修改相应的作用域值对排序做出纠正。

构建 CGI 404 处理程序

我们已经生成了适当的变音并为它们指定了相关的作用域值,下一步将构建实际的建议生成器。通常,404 错误消息的原因为链接输入错误或链接本身的问题。以下代码生成的建议将通过以下三个主要测试创建:根据目录结构匹配、使用变音组合匹配,以及当其他方法失败时使用 “包含” 匹配。这三种测试的设计目的是处理大多数 404 错误。MetaphoneSuggest CGI Perl 脚本的开始部分如下所示。

清单 4. MetaphoneSuggest CGI 第 1 部分
               

以下为引用的内容:
#!/usr/bin/perl -w
# MetaphoneSuggest - suggest links for typographical and other errors from 404s
use s
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles