Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial 计算词汇出现的次数

计算词汇出现的次数

Jun 23, 2016 pm 02:05 PM

有一组非日常的英文词汇,我需要计算在英文文章中出现频次最多的。
于是我最初想到遍历数组,用 substr_count 依次计算每个词汇出现的次数,但这样就会造成对整篇文章多次重复的扫描。或者将文章也拆分成词汇,从中用数组函数计算交集数量,但依然觉得不理想。

各位有什么想法吗?这个应用其实也就是关键词提取。


回复讨论(解决方案)

拆成数组为何不好,英文入数组很方便啊,起码比中文简单多了
其实不太明白你的需求,纯粹统计 array_count_values 足够方便了

就是说你已经有了一个词库,现在需要在文章里检查词库词的出现次数
如果是的,那么可以使用 trie 算法(我发过的)
只需扫描文章一遍就可以了,当然要先构造词库

就是说你已经有了一个词库,现在需要在文章里检查词库词的出现次数
如果是的,那么可以使用 trie 算法(我发过的)
只需扫描文章一遍就可以了,当然要先构造词库

词库保存为什么格式比较好?mysql,json,xml,纯数组?

如果一篇文章有5kb,词库有1000个单词,那么把这1000个单词逐个foreach,匹配这篇文章,

mysql_query,
json_decode()
simplexml_load_file()
数组

哪个效率更高,更节省资源(CPU,RAM)?

5kb不太可能有1000个单词,全部都是冠词?

即使1000个,量也不算很大,去除重复应该就少很多了,一次数组交集就够了

我的思路是文章拆分为单词数组,array_count_values 就起到统计和去除重复两个功能
然后提取次数一定的部分(次数太少没匹配意义吧?),那剩下就很少了,再与现存词库求交集就足够了

虽然楼主是专指英文词汇,但是你的算法若只限于英文词汇的话,那就没有什么意义了


5kb不太可能有1000个单词,全部都是冠词?

即使1000个,量也不算很大,去除重复应该就少很多了,一次数组交集就够了

我的思路是文章拆分为单词数组,array_count_values 就起到统计和去除重复两个功能
然后提取次数一定的部分(次数太少没匹配意义吧?),那剩下就很少了,再与现存词库求交集就足够了

你说的也有道理
只是我觉得简单问题简单处理,他既然说英文,就按这样去想,没必要太花时间考虑算法
如果他说混杂多语种,估计我也只是旁观不会回这贴了,呵呵

虽然楼主是专指英文词汇,但是你的算法若只限于英文词汇的话,那就没有什么意义了


引用 4 楼 snmr_com 的回复:5kb不太可能有1000个单词,全部都是冠词?

即使1000个,量也不算很大,去除重复应该就少很多了,一次数组交集就够了

我的思路是文章拆分为单词数组,array_count_values 就起到统计和去除重复两个功能
然后提取次数……

版本给的前缀树怎么也没看懂,暂时先选择了多次扫描文章来实现

一个简单的例子

include 'TTrie.php';class wordkey extends TTrie {  function b() {    $t = array_pop($this->buffer);    $this->buffer[] = "<b>$t</b>";  }}$p = new wordkey;$p->set('秦始皇', 'b');$p->set('洛阳', 'b');$t = $p->match('秦始皇东巡洛阳');echo join('', $t);
Copy after login
秦始皇东巡洛阳

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)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

See all articles