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

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Lepaskan: 2023-03-01 22:24:01
asal
1177 orang telah melayarinya

代码如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

<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>

Salin selepas log masuk
Salin selepas log masuk

输出结果:

1

2

3

4

5

6

7

8

9

10

11

<code>array(

    [repression] => 1

    [thoroughness] => 1

    [bleached] => 1

    [tow] => 1

    [inspired] => 1

    [uniformwell] => 1

    [panamas] => 1

    [caps

when] => 1

)</code>

Salin selepas log masuk
Salin selepas log masuk

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

回复内容:

代码如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

<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>

Salin selepas log masuk
Salin selepas log masuk

输出结果:

1

2

3

4

5

6

7

8

9

10

11

<code>array(

    [repression] => 1

    [thoroughness] => 1

    [bleached] => 1

    [tow] => 1

    [inspired] => 1

    [uniformwell] => 1

    [panamas] => 1

    [caps

when] => 1

)</code>

Salin selepas log masuk
Salin selepas log masuk

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

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

<?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;

}

Salin selepas log masuk

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

Label berkaitan:
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan