Home Backend Development PHP Tutorial Maximum forward matching algorithm PHP implementation

Maximum forward matching algorithm PHP implementation

Jul 29, 2016 am 08:56 AM
feature query

1

<pre class="brush:php;toolbar:false"><span>字符串最大正向匹配算法说明可以参考其他文章,这里只做实现。</span>

Copy after login

1

<span>三个参数:查询词 $query,词典$</span><span>dict</span><span>,最大长度,这里设置为15</span>

Copy after login

1

<span>词典示例:</span>

Copy after login

1

2

<pre class="brush:php;toolbar:false"><span>$dict = </span><span>array</span><span>(

</span><span>'测试字符'</span><span>=></span><span>'</span><span>测试字符</span><span>'</span><span>,</span><span>'测试'</span><span>=></span><span>'测试'</span><span>,</span><span>)</span>

Copy after login

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

<span>public function </span><span>extractWords(</span><span>$query</span><span>,</span><span>$</span><span>dict</span><span>,</span><span>$max_len</span><span>=</span><span>15</span><span>){</span><span><em></em></span><span>$feature </span><span>= </span><span>""</span><span>;

</span><span>$slen</span><span>=</span><span><em>mb_strlen</em></span><span>(</span><span>$query</span><span>,</span><span>'UTF8'</span><span>);

</span><span>$c_bg </span><span>= </span><span>0</span><span>;

</span><span>while</span><span>(</span><span>$c_bg</span><span><</span><span>$slen</span><span>){

</span><span>$matched </span><span>= </span><span>false</span><span>;

</span><span>$c_len </span><span>=((</span><span>$slen</span><span>-</span><span>$c_bg</span><span>)></span><span>$max_len</span><span>)?</span><span>$max_len</span><span>:(</span><span>$slen</span><span>-</span><span>$c_bg</span><span>);

</span><span>$t_str </span><span>= </span><span><em>mb_substr</em></span><span>(</span><span>$query</span><span>, </span><span>$c_bg</span><span>,</span><span>$c_len</span><span>,</span><span>'UTF8'</span><span>);

</span><span>for</span><span>(</span><span>$i</span><span>=</span><span>$c_len</span><span>;</span><span>$i</span><span>></span><span>1</span><span>;</span><span>$i</span><span>--){

</span><span>$ttts </span><span>= </span><span><em>mb_substr</em></span><span>(</span><span>$t_str</span><span>, </span><span>0</span><span>,</span><span>$i</span><span>,</span><span>'UTF8'</span><span>);</span><span>if</span><span>(!</span><span>empty</span><span>(</span><span>$dict</span><span>[</span><span>$ttts</span><span>])){

</span><span><em>//                    echo 'matched = '.$ttts.PHP_EOL;

</em></span><span><em></em></span><span>$matched </span><span>= </span><span>true</span><span>;

</span><span>$c_bg </span><span>+= </span><span>$i</span><span>;

</span><span>if</span><span>(!</span><span>empty</span><span>(</span><span>$feature</span><span>)){

</span><span>$feature</span><span>.=</span><span>","</span><span>;

</span><span>                        }

</span><span>$feature</span><span>.=</span><span>$ttts</span><span>;

</span><span>break</span><span>;

</span><span>                    }</span><span>            }

</span><span>if</span><span>(!</span><span>$matched</span><span>){

</span><span>$c_bg</span><span>++;

</span><span>            }

</span><span>        }

</span><span><em>        echo $feature.PHP_EOL;</em></span><span>    }</span>

Copy after login

The above introduces the PHP implementation of the maximum forward matching algorithm, including some aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

How to reorder multiple columns in Power Query via drag and drop How to reorder multiple columns in Power Query via drag and drop Mar 14, 2024 am 10:55 AM

In this article, we will show you how to reorder multiple columns in PowerQuery by dragging and dropping. Often, when importing data from various sources, columns may not be in the desired order. Reordering columns not only allows you to arrange them in a logical order that suits your analysis or reporting needs, it also improves the readability of your data and speeds up tasks such as filtering, sorting, and performing calculations. How to rearrange multiple columns in Excel? There are many ways to rearrange columns in Excel. You can simply select the column header and drag it to the desired location. However, this approach can become cumbersome when dealing with large tables with many columns. To rearrange columns more efficiently, you can use the enhanced query editor. Enhancing the query

Original words rewritten: An unexpected discovery is that what was originally regarded as a bug is actually a feature in the design of Protobuf Original words rewritten: An unexpected discovery is that what was originally regarded as a bug is actually a feature in the design of Protobuf May 09, 2023 pm 04:22 PM

Hi everyone, I'm awesome. Recently, in our project, we used protobuf format as a carrier for storing data. I accidentally buried a big hole for myself, but it took me a long time to discover it. Introduction to protobuf The full name of protobuf is Protocalbuffers. It was developed by Google and is a cross-language, cross-platform, and scalable mechanism for serializing data. Similar to XML, but smaller, faster, and simpler. You only need to define once how you want your data to be structured, and then you can use its generation tools to generate source code that includes some serialization and deserialization operations. Can be easily written from a variety of data streams and using a variety of programming languages

React Query database plug-in: how to import and export data React Query database plug-in: how to import and export data Sep 26, 2023 pm 05:37 PM

ReactQuery database plug-in: Methods to implement data import and export, specific code examples are required. With the widespread application of ReactQuery in front-end development, more and more developers are beginning to use it to manage data. In actual development, we often need to export data to local files or import data from local files into the database. In order to implement these functions more conveniently, you can use the ReactQuery database plug-in. The ReactQuery database plugin provides a series of methods

How to split data to NTFS using Power Query How to split data to NTFS using Power Query Mar 15, 2024 am 11:00 AM

This article will introduce how to use PowerQuery to split data into rows. When exporting data from other systems or sources, it is common to encounter situations where the data is stored in cells combining multiple values. With PowerQuery, we can easily split such data into rows, making the data easier to process and analyze. This can happen if the user doesn't understand Excel's rules and accidentally enters multiple data into a cell, or if the data is not formatted correctly when copying/pasting it from other sources. Processing this data requires additional steps to extract and organize the information for analysis or reporting. How to split data in PowerQuery? PowerQuery transformations can be based on a variety of different factors such as word

Query intent recognition based on knowledge enhancement and pre-trained large model Query intent recognition based on knowledge enhancement and pre-trained large model May 19, 2023 pm 02:01 PM

1. Background Introduction Enterprise digitalization is a hot topic in recent years. It refers to the use of new generation digital technologies such as artificial intelligence, big data, and cloud computing to change the business model of enterprises, thereby promoting new growth in enterprise business. Enterprise digitalization generally includes the digitalization of business operations and the digitalization of enterprise management. This sharing mainly introduces the digitalization of enterprise management level. Information digitization, simply put, means reading, writing, storing and transmitting information in a digital way. From the previous paper documents to the current electronic documents and online collaborative documents, information digitization has become the new normal in today's office. Currently, Alibaba uses DingTalk Documents and Yuque Documents for business collaboration, and the number of online documents has reached more than 20 million. In addition, many companies will internally

React Query Database Plugin: Strategies for Data Backup and Restore React Query Database Plugin: Strategies for Data Backup and Restore Sep 28, 2023 pm 11:22 PM

ReactQuery database plug-in: Strategies to implement data backup and restoration, specific code examples are required Introduction: In modern web development, data backup and restoration is a very important task. Especially when using state management tools like ReactQuery, we need to ensure data security and reliability. This article will introduce a database plug-in based on ReactQuery to implement data backup and restore strategies, and provide specific code examples. ReactQu

React Query Database Plugin: Tips for Data Compression and Decompression React Query Database Plugin: Tips for Data Compression and Decompression Sep 26, 2023 pm 08:03 PM

ReactQuery database plugin: Tips for implementing data compression and decompression, specific code examples required Introduction: In modern web application development, processing large amounts of data queries is a common task. ReactQuery is a powerful library that provides a simple and intuitive way to manage data queries and state. Although ReactQuery itself is already very good, when dealing with large amounts of data, we may need to consider some additional tricks to improve performance and optimize storage space. This article will introduce

React Query Database Plugin: Strategies for Data Merging and Splitting React Query Database Plugin: Strategies for Data Merging and Splitting Sep 26, 2023 am 09:01 AM

ReactQuery database plug-in: Strategies to implement data merging and splitting, specific code examples are required Title: ReactQuery database plug-in: Strategies to implement data merging and splitting Introduction: In modern web development, data request and management are a very important part . ReactQuery is a very popular data query and management library in the React ecosystem. It provides a concise way to manage data requests, caching and status in applications, while integrating with React

See all articles