Maison  >  Article  >  développement back-end  >  换行 - 【PHP】在用PHP来统计一个纯英文的txt的单词的时候,为什么会这种情况?【已解决】

换行 - 【PHP】在用PHP来统计一个纯英文的txt的单词的时候,为什么会这种情况?【已解决】

WBOY
WBOYoriginal
2016-12-01 00:56:351073parcourir

代码如下:

<code><?php /**
* 任一个英文的纯文本文件,统计其中的单词出现的个数。
* Created by PhpStorm.
* User: Paul
* Date: 2016/11/5
* Time: 23:18
*/

$content = file_get_contents('4/Gone with the wind.txt');
$res = count_word($content, 1);
print_r($res);

/**
* 任一个英文的纯文本文件,统计其中的单词出现的个数。
* @param string $string  字符串
* @param int $lower 是否大小写   1:不区分大小写  0:区分大小写
* @return array
*/
function count_word($string, $lower = 0) {
    $string = trim($string);
    if ($lower) {
        $string = strtolower($string);
    }

    //过滤掉一些标点符号
    $string = str_replace(';', '', $string);
    $string = str_replace(',', '', $string);
    $string = str_replace('.', '', $string);
    $string = str_replace('.', '', $string);
    $string = str_replace('‘', '', $string);
    $string = str_replace('?', '', $string);
    $string = str_replace('“', '', $string);
    $string = str_replace('”', '', $string);
    $string = str_replace('―', '', $string);
    $string = str_replace('-', '', $string);
    $string = str_replace('!', '', $string);
    $string = str_replace(':', '', $string);
    $string = str_replace('(', '', $string);
    $string = str_replace(')', '', $string);

    $array = explode(' ', trim($string));

    $res = array();
    foreach ($array as $key=>$value) {
        //过滤掉如I’ll、you’re、masters’s等单词
        if (strpos($value, '’') !== false || strpos($value, "'") !== false) {
            continue;
        }

        //过滤掉空
        if (empty($value) === true) {
            continue;
        }

        if (array_key_exists($value, $res)) {
            $res[$value]++;
        } else {
            $res[$value] = 1;
        }
    }

    //排序
    array_multisort($res, SORT_DESC, SORT_NUMERIC);
    return $res;
}</code>

输出结果:

<code>array(
    [repression] => 1
    [thoroughness] => 1
    [bleached] => 1
    [tow] => 1
    [inspired] => 1
    [uniformwell] => 1
    [panamas] => 1
    [caps
when] => 1
)</code>

不明白为什么会把两个单词给判断成一个单词,txt呢是用sublime打开并且设置编码为UTF-8,没有用电脑自带的文本文档工具打开编辑过,另外呢,过滤标点符号的时候也有加上过滤掉rn来处理,但是没效果,所以代码去掉了。求解为什么会出现这种情况并且如何避免?

回复内容:

代码如下:

<code><?php /**
* 任一个英文的纯文本文件,统计其中的单词出现的个数。
* Created by PhpStorm.
* User: Paul
* Date: 2016/11/5
* Time: 23:18
*/

$content = file_get_contents('4/Gone with the wind.txt');
$res = count_word($content, 1);
print_r($res);

/**
* 任一个英文的纯文本文件,统计其中的单词出现的个数。
* @param string $string  字符串
* @param int $lower 是否大小写   1:不区分大小写  0:区分大小写
* @return array
*/
function count_word($string, $lower = 0) {
    $string = trim($string);
    if ($lower) {
        $string = strtolower($string);
    }

    //过滤掉一些标点符号
    $string = str_replace(';', '', $string);
    $string = str_replace(',', '', $string);
    $string = str_replace('.', '', $string);
    $string = str_replace('.', '', $string);
    $string = str_replace('‘', '', $string);
    $string = str_replace('?', '', $string);
    $string = str_replace('“', '', $string);
    $string = str_replace('”', '', $string);
    $string = str_replace('―', '', $string);
    $string = str_replace('-', '', $string);
    $string = str_replace('!', '', $string);
    $string = str_replace(':', '', $string);
    $string = str_replace('(', '', $string);
    $string = str_replace(')', '', $string);

    $array = explode(' ', trim($string));

    $res = array();
    foreach ($array as $key=>$value) {
        //过滤掉如I’ll、you’re、masters’s等单词
        if (strpos($value, '’') !== false || strpos($value, "'") !== false) {
            continue;
        }

        //过滤掉空
        if (empty($value) === true) {
            continue;
        }

        if (array_key_exists($value, $res)) {
            $res[$value]++;
        } else {
            $res[$value] = 1;
        }
    }

    //排序
    array_multisort($res, SORT_DESC, SORT_NUMERIC);
    return $res;
}</code>

输出结果:

<code>array(
    [repression] => 1
    [thoroughness] => 1
    [bleached] => 1
    [tow] => 1
    [inspired] => 1
    [uniformwell] => 1
    [panamas] => 1
    [caps
when] => 1
)</code>

不明白为什么会把两个单词给判断成一个单词,txt呢是用sublime打开并且设置编码为UTF-8,没有用电脑自带的文本文档工具打开编辑过,另外呢,过滤标点符号的时候也有加上过滤掉rn来处理,但是没效果,所以代码去掉了。求解为什么会出现这种情况并且如何避免?

你的问题应该就出在没有处理换行(和回车)以及那些过滤字符被替换成了'', 应该替换成' '

<code class="php"><?php $content = file_get_contents(__FILE__); //没有你的原始文本, 所以就直接读取文件自身作为样本了
$res = count_word($content, 1);
print_r($res);

/**
* 任一个英文的纯文本文件,统计其中的单词出现的个数。
* @param string $string  字符串
* @param int $lower 是否大小写   1:不区分大小写  0:区分大小写
* @return array
*/
function count_word($string, $lower = 0) {
    $string = trim($string);
    if ($lower) {
        $string = strtolower($string);
    }

    //过滤掉一些标点符号
    $string = str_replace([';',',','.','.','‘','?','“','”','―','-','!',':','(',')',"\r","\n"], ' ', $string);
    $array = explode(' ', $string);

    $res = array();
    foreach ($array as $key=>$value) {
        //过滤掉空
        if (!$value) {
            continue;
        }

        //过滤掉如I’ll、you’re、masters’s等单词
        if (strpos($value, '’') !== false || strpos($value, "'") !== false) {
            continue;
        }

        if (array_key_exists($value, $res)) {
            $res[$value]++;
        } else {
            $res[$value] = 1;
        }
    }

    //排序
    array_multisort($res, SORT_DESC, SORT_NUMERIC);
    return $res;
}</code>

不知道你的文件里的字符串是什么样子的,不过trim函数只会去掉两边的空格(rn),感觉问题会出在这里。

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn