php - 操作数组并获取单词出现次数

标签 php mysql sql

我正在对 Mysql 数据进行数学运算。

对于给定标签c,单词w的概率方程如下

P(w/c) = count of words w where tag is c / (total count of words where tag is c + distinct words from all tags)

我可以从表中检索值,但问题在于计算单词数。

记录是这样的(pos是词性,即名词形容词)

seq     hash                            post                                                               pos                                           tag           cat
1   78bb622642f6a28681ca15676045cabd    Marketing in every web application is essential no...   web, application, essential, big, SEO, full, stack...   marketing   Discussion
2   ec31a76d380590fff71cf7b10db0f084    I have just launched my new startup on web analyti...   new, startup, web, expert, SEO, digital     marketing   Discussion

我不熟悉 php + mysql 的字数统计。我需要帮助

  1. 如何计算 pos 中不同的单词,其中 tag = t
  2. 如何计算 pos 中 tag = t 的所有单词
  3. 如何从 pos 中计算单词 w,其中 tag = 'marketting' 并且 pos 包含单词 w

最佳答案

获取带有所需标签的 pos:

SELECT pos FROM table WHERE tag = "tag_you_want"

然后在 PHP 中计算一个特定的单词:

echo substr_count($string, 'word');

统计 pos 中的所有单词:

echo count(str_word_count($string));

关于php - 操作数组并获取单词出现次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24386214/

相关文章:

php - 在 laravel for sqlite 中找不到 pdo 异常驱动程序

php - 外部 CSS 链接在 codeIgniter 中不起作用

PHP - 文本字符串中的关键字匹配 - 如何提高返回关键字的准确性?

mysql - 触发器不尊重自动提交选项?

sql - 帮助基本的sql查询

sql - 小时、月份等的开始

javascript - php脚本回显中的双引号

python - 如何将mysql中的列值存储到python变量?

java - 如何修复类文件是 Java 8 但最大支持是 Java 7

sql - R 和 SQL : Importing date and time from SQL to R