File size: 1,893 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
# -*- coding: utf-8 -*-
# SPDX-FileCopyrightText: 2016-2025 PyThaiNLP Project
# SPDX-FileType: SOURCE
# SPDX-License-Identifier: Apache-2.0
"""
Thai unigram word frequency from OSCAR Corpus (words tokenized using ICU)

Credit: Korakot Chaovavanich
https://web.facebook.com/groups/colab.thailand/permalink/1524070061101680/
"""

__all__ = ["word_freqs", "unigram_word_freqs"]

from collections import defaultdict
from typing import List, Tuple

from pythainlp.corpus import get_corpus_path

_OSCAR_FILENAME = "oscar_icu"


def word_freqs() -> List[Tuple[str, int]]:
    """
    Get word frequency from OSCAR Corpus (words tokenized using ICU)
    """
    freqs: list[tuple[str, int]] = []
    path = get_corpus_path(_OSCAR_FILENAME)
    if not path:
        return freqs
    path = str(path)

    with open(path, "r", encoding="utf-8-sig") as f:
        lines = list(f.readlines())
        del lines[0]
        for line in lines:
            temp = line.strip().split(",")
            if len(temp) >= 2:
                if temp[0] != " " and '"' not in temp[0]:
                    freqs.append((temp[0], int(temp[1])))
                elif temp[0] == " ":
                    freqs.append(("<s/>", int(temp[1])))

    return freqs


def unigram_word_freqs() -> dict[str, int]:
    """
    Get unigram word frequency from OSCAR Corpus (words tokenized using ICU)
    """
    freqs: dict[str, int] = defaultdict(int)
    path = get_corpus_path(_OSCAR_FILENAME)
    if not path:
        return freqs
    path = str(path)

    with open(path, "r", encoding="utf-8-sig") as fh:
        lines = list(fh.readlines())
        del lines[0]
        for i in lines:
            temp = i.strip().split(",")
            if temp[0] != " " and '"' not in temp[0]:
                freqs[temp[0]] = int(temp[-1])
            elif temp[0] == " ":
                freqs["<s/>"] = int(temp[-1])

    return freqs