File size: 3,728 Bytes
e4b9a7b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# -*- coding: utf-8 -*-
# SPDX-FileCopyrightText: 2016-2025 PyThaiNLP Project
# SPDX-FileType: SOURCE
# SPDX-License-Identifier: Apache-2.0
from collections import Counter
from typing import Dict, List

from pythainlp.corpus import thai_stopwords

_STOPWORDS = thai_stopwords()


def rank(words: List[str], exclude_stopwords: bool = False) -> Counter:
    """
    Count word frequencies given a list of Thai words with an option
    to exclude stopwords.

    :param list words: a list of words
    :param bool exclude_stopwords: If this parameter is set to **True**,
                                   exclude stopwords from counting.
                                   Otherwise, the stopwords will be counted.
                                   By default, `exclude_stopwords`is
                                   set to **False**
    :return: a Counter object representing word frequencies in the text
    :rtype: :class:`collections.Counter`

    :Example:

    Include stopwords when counting word frequencies::

        from pythainlp.util import rank

        words = ["บันทึก", "เหตุการณ์", " ", "มี", "การ", "บันทึก", \\
        "เป็น", " ", "ลายลักษณ์อักษร"]

        rank(words)
        # output:
        # Counter(
        #     {
        #         ' ': 2,
        #         'การ': 1,
        #         'บันทึก': 2,
        #         'มี': 1,
        #         'ลายลักษณ์อักษร': 1,
        #         'เป็น': 1,
        #         'เหตุการณ์': 1
        #     })

    Exclude stopwords when counting word frequencies::

        from pythainlp.util import rank

        words = ["บันทึก", "เหตุการณ์", " ", "มี", "การ", "บันทึก", \\
            "เป็น", " ", "ลายลักษณ์อักษร"]

        rank(words)
        # output:
        # Counter(
        #     {
        #         ' ': 2,
        #         'บันทึก': 2,
        #         'ลายลักษณ์อักษร': 1,
        #         'เหตุการณ์': 1
        #     })
    """
    if not words:
        return None

    if exclude_stopwords:
        words = [word for word in words if word not in _STOPWORDS]

    return Counter(words)


def find_keyword(word_list: List[str], min_len: int = 3) -> Dict[str, int]:
    """
    This function counts the frequencies of words in the list
    where stopword is excluded and returns a frequency dictionary.

    :param list word_list: a list of words
    :param int min_len: the minimum frequency for words to be retained

    :return: a dictionary object with key-value pair being words and their raw counts
    :rtype: dict[str, int]

    :Example:
    ::

        from pythainlp.util import find_keyword

        words = ["บันทึก", "เหตุการณ์", "บันทึก", "เหตุการณ์",
                 " ", "มี", "การ", "บันทึก", "เป็น", " ", "ลายลักษณ์อักษร"
                 "และ", "การ", "บันทึก","เสียง","ใน","เหตุการณ์"]

        find_keyword(words)
        # output: {'บันทึก': 4, 'เหตุการณ์': 3}

        find_keyword(words, min_len=1)
        # output: {' ': 2, 'บันทึก': 4, 'ลายลักษณ์อักษรและ': 1,
         'เสียง': 1, 'เหตุการณ์': 3}
    """
    word_list = rank(word_list, exclude_stopwords=True)

    return {k: v for k, v in word_list.items() if v >= min_len}