计算词汇出现的次数
有一组非日常的英文词汇,我需要计算在英文文章中出现频次最多的。
于是我最初想到遍历数组,用 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);

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











There are four main error types in PHP: 1.Notice: the slightest, will not interrupt the program, such as accessing undefined variables; 2. Warning: serious than Notice, will not terminate the program, such as containing no files; 3. FatalError: the most serious, will terminate the program, such as calling no function; 4. ParseError: syntax error, will prevent the program from being executed, such as forgetting to add the end tag.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

HTTP request methods include GET, POST, PUT and DELETE, which are used to obtain, submit, update and delete resources respectively. 1. The GET method is used to obtain resources and is suitable for read operations. 2. The POST method is used to submit data and is often used to create new resources. 3. The PUT method is used to update resources and is suitable for complete updates. 4. The DELETE method is used to delete resources and is suitable for deletion operations.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP handles file uploads through the $\_FILES variable. The methods to ensure security include: 1. Check upload errors, 2. Verify file type and size, 3. Prevent file overwriting, 4. Move files to a permanent storage location.

In PHPOOP, self:: refers to the current class, parent:: refers to the parent class, static:: is used for late static binding. 1.self:: is used for static method and constant calls, but does not support late static binding. 2.parent:: is used for subclasses to call parent class methods, and private methods cannot be accessed. 3.static:: supports late static binding, suitable for inheritance and polymorphism, but may affect the readability of the code.
