如何计算一列中的相似单词 [英] how to count similar words in a column

查看:88
本文介绍了如何计算一列中的相似单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有CSV文件,想统计9000行列中的重复单词。

I have CSV file and would like to count repeated words in a column of 9000 row.


文件示例

谢谢

好​​的

nice

谢谢

不错

谢谢

exemple of the file
thanks
ok
nice
thanks
nice
thanks

所需结果将类似于

the required result would be something like


谢谢= 3,确定= 1,nice = 2。

thanks=3, ok=1, nice=2 .

我找到了以下PHP代码,但无法正常工作,并复制了CSV文件的内容转换为 file.txt 我在做错什么吗?

I found the following PHP code, but I couldn't get it to work, and copied the content of the CSV file into file.txt Am I doing something wrong?

<?php
$file = (''C:\Users\wnmb4793\Desktop\Test\file.txt'');

$fh = fopen($file, 'rb');

$tag = array();
while($col = fgetcsv($fh)) {

if (isset($tag[$col[2]])) {
$tag[$col[2]]++;
}
else {
$tag[$col[2]] = 1;
}
?>


推荐答案

只有几个错误。您的代码有效。

Just a few mistakes. Your code works.

$file = 'C:\Users\wnmb4793\Desktop\Test\file.txt';

$fh = fopen($file, 'rb');

$tag = array();
while($col = fgetcsv($fh)) 
{
  $value = $col[0]; // change 0 to column number you need, 0 - first 
  if ( isset($tag[$value]) ) 
    $tag[$value]++;
    else 
    $tag[$value] = 1;
}

print_r($tag);

结果:

Array
(
    [thanks] => 3
    [ok] => 1
    [nice] => 2
)

这篇关于如何计算一列中的相似单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆