tglstemmer 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Andrian Lloyd Maagma
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,147 @@
1
+ Metadata-Version: 2.4
2
+ Name: tglstemmer
3
+ Version: 0.1.0
4
+ Summary: A library for Tagalog word stemming.
5
+ Author-email: Andrian Lloyd Maagma <maagmaandrian@gmail.com>
6
+ License: MIT License
7
+
8
+ Copyright (c) 2025 Andrian Lloyd Maagma
9
+
10
+ Permission is hereby granted, free of charge, to any person obtaining a copy
11
+ of this software and associated documentation files (the "Software"), to deal
12
+ in the Software without restriction, including without limitation the rights
13
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
+ copies of the Software, and to permit persons to whom the Software is
15
+ furnished to do so, subject to the following conditions:
16
+
17
+ The above copyright notice and this permission notice shall be included in all
18
+ copies or substantial portions of the Software.
19
+
20
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26
+ SOFTWARE.
27
+
28
+ Project-URL: homepage, https://github.com/andrianllmm/tglstemmer
29
+ Project-URL: issues, https://github.com/andrianllmm/tglstemmer/issues
30
+ Keywords: stemmer,tagalog,affix removal
31
+ Classifier: Development Status :: 3 - Alpha
32
+ Classifier: License :: OSI Approved :: MIT License
33
+ Classifier: Programming Language :: Python :: 3.10
34
+ Classifier: Operating System :: OS Independent
35
+ Requires-Python: >=3.10
36
+ Description-Content-Type: text/markdown
37
+ License-File: LICENSE
38
+ Requires-Dist: nltk>=3.8.1
39
+ Requires-Dist: tabulate>=0.9.0
40
+ Dynamic: license-file
41
+
42
+ # TglStemmer
43
+
44
+ **A Python library for Tagalog word stemming**
45
+
46
+ ## About
47
+
48
+ TglStemmer is a library that finds the root form of
49
+ <a href="https://www.ethnologue.com/language/tgl" target="_blank">Tagalog</a>
50
+ words. It works on inflected words, even those with mixed Tagalog-English
51
+ (Taglish) terms or those not found in dictionaries. It removes affixes, reduces
52
+ repeated syllables, and applies transformation rules to find possible root
53
+ forms. These are filtered using a list of valid words and conditions. The best
54
+ root is then chosen based on how much was changed during the process.
55
+
56
+ ## Installation
57
+
58
+ ```sh
59
+ pip install tglstemmer
60
+ ```
61
+
62
+ ## Usage
63
+
64
+ TglStemmer acts as a standalone library that can be imported via
65
+ `from tglstemmer import stemmer`.
66
+
67
+ Use `get_stem` to get the root of a word. This takes a word and returns its stem
68
+ as a `Stem` object (basically a string with affixes, reduplication,
69
+ transformations, etc. as additional attributes).
70
+
71
+ ```python
72
+ stem = stemmer.get_stem("nagsulat")
73
+ print(stem)
74
+ # Output: 'sulat'
75
+ ```
76
+
77
+ Since `get_stem` returns a `Stem` object, the properties used in the stemming
78
+ process can be accessed as attributes.
79
+
80
+ ```python
81
+ prefix = stem.pre
82
+ print(prefix)
83
+ # Output: 'nag'
84
+
85
+ suffix = stem.suf
86
+ print(suffix)
87
+ # Output: None
88
+ ```
89
+
90
+ Use `get_stems()` to get the root of each word in a text. This takes a text and
91
+ returns the stem of each word as a list of `Stem` objects.
92
+
93
+ ```python
94
+ stems = stemmer.get_stems("nagsulat, binasa, at punitin")
95
+ print(stems)
96
+ # Output: ['sulat', 'basa', 'at', 'punit']
97
+ ```
98
+
99
+ Use `get_stem_candidates` to get all the stem candidates of a word. This takes a
100
+ word and returns the possible stems as a list of `Stem` objects. This is helpful
101
+ for loose checking considering candidate selection is not perfect.
102
+
103
+ ```python
104
+ candidates = stemmer.get_stem_candidates("pinakamahusay't")
105
+ print(candidates)
106
+ # Output: ['husay', 'mahusay', 'pinakamahusay']
107
+ ```
108
+
109
+ ## Accuracy
110
+
111
+ The accuracy was tested using a list of stems and their corresponding
112
+ inflections. The list is manually derived from the examples from the book
113
+ [Balarila ng Wikang Pambansa (Santos, 1939)](https://tl.wikipedia.org/wiki/Balarila_ng_Wikang_Pambansa),
114
+ particularly in sections "Palabuuan ng mga Salita" (pp. 28-34) and "Mga Sangkap
115
+ ng Pananalita" (pp. 35-37). This is not a "gold" standard dataset but is chosen
116
+ for testing as the book provides varied examples of inflections during its
117
+ discussion about the process of affixation. Each inflection was stemmed by
118
+ TglStemmer and then compared to the original stem. The test included 266
119
+ stem-inflection pairs.
120
+
121
+ | Metric | Value |
122
+ | ------------------- | ------ |
123
+ | Accuracy | 75.19% |
124
+ | Correct Attempts | 200 |
125
+ | Incorrect Attempts | 66 |
126
+ | Understemming Avg | 0.69 |
127
+ | Overstemming Avg | 0.12 |
128
+ | Understemming Total | 184 |
129
+ | Overstemming Total | 33 |
130
+
131
+ ## Development
132
+
133
+ This project uses [uv](https://docs.astral.sh/uv/) for dependency management.
134
+
135
+ Clone the repo and sync dependencies (including dev and test groups):
136
+
137
+ ```sh
138
+ git clone https://github.com/andrianllmm/tagalog-stemmer.git
139
+ cd tagalog-stemmer
140
+ uv sync --all-groups
141
+ ```
142
+
143
+ Run the tests:
144
+
145
+ ```sh
146
+ uv run pytest
147
+ ```
@@ -0,0 +1,106 @@
1
+ # TglStemmer
2
+
3
+ **A Python library for Tagalog word stemming**
4
+
5
+ ## About
6
+
7
+ TglStemmer is a library that finds the root form of
8
+ <a href="https://www.ethnologue.com/language/tgl" target="_blank">Tagalog</a>
9
+ words. It works on inflected words, even those with mixed Tagalog-English
10
+ (Taglish) terms or those not found in dictionaries. It removes affixes, reduces
11
+ repeated syllables, and applies transformation rules to find possible root
12
+ forms. These are filtered using a list of valid words and conditions. The best
13
+ root is then chosen based on how much was changed during the process.
14
+
15
+ ## Installation
16
+
17
+ ```sh
18
+ pip install tglstemmer
19
+ ```
20
+
21
+ ## Usage
22
+
23
+ TglStemmer acts as a standalone library that can be imported via
24
+ `from tglstemmer import stemmer`.
25
+
26
+ Use `get_stem` to get the root of a word. This takes a word and returns its stem
27
+ as a `Stem` object (basically a string with affixes, reduplication,
28
+ transformations, etc. as additional attributes).
29
+
30
+ ```python
31
+ stem = stemmer.get_stem("nagsulat")
32
+ print(stem)
33
+ # Output: 'sulat'
34
+ ```
35
+
36
+ Since `get_stem` returns a `Stem` object, the properties used in the stemming
37
+ process can be accessed as attributes.
38
+
39
+ ```python
40
+ prefix = stem.pre
41
+ print(prefix)
42
+ # Output: 'nag'
43
+
44
+ suffix = stem.suf
45
+ print(suffix)
46
+ # Output: None
47
+ ```
48
+
49
+ Use `get_stems()` to get the root of each word in a text. This takes a text and
50
+ returns the stem of each word as a list of `Stem` objects.
51
+
52
+ ```python
53
+ stems = stemmer.get_stems("nagsulat, binasa, at punitin")
54
+ print(stems)
55
+ # Output: ['sulat', 'basa', 'at', 'punit']
56
+ ```
57
+
58
+ Use `get_stem_candidates` to get all the stem candidates of a word. This takes a
59
+ word and returns the possible stems as a list of `Stem` objects. This is helpful
60
+ for loose checking considering candidate selection is not perfect.
61
+
62
+ ```python
63
+ candidates = stemmer.get_stem_candidates("pinakamahusay't")
64
+ print(candidates)
65
+ # Output: ['husay', 'mahusay', 'pinakamahusay']
66
+ ```
67
+
68
+ ## Accuracy
69
+
70
+ The accuracy was tested using a list of stems and their corresponding
71
+ inflections. The list is manually derived from the examples from the book
72
+ [Balarila ng Wikang Pambansa (Santos, 1939)](https://tl.wikipedia.org/wiki/Balarila_ng_Wikang_Pambansa),
73
+ particularly in sections "Palabuuan ng mga Salita" (pp. 28-34) and "Mga Sangkap
74
+ ng Pananalita" (pp. 35-37). This is not a "gold" standard dataset but is chosen
75
+ for testing as the book provides varied examples of inflections during its
76
+ discussion about the process of affixation. Each inflection was stemmed by
77
+ TglStemmer and then compared to the original stem. The test included 266
78
+ stem-inflection pairs.
79
+
80
+ | Metric | Value |
81
+ | ------------------- | ------ |
82
+ | Accuracy | 75.19% |
83
+ | Correct Attempts | 200 |
84
+ | Incorrect Attempts | 66 |
85
+ | Understemming Avg | 0.69 |
86
+ | Overstemming Avg | 0.12 |
87
+ | Understemming Total | 184 |
88
+ | Overstemming Total | 33 |
89
+
90
+ ## Development
91
+
92
+ This project uses [uv](https://docs.astral.sh/uv/) for dependency management.
93
+
94
+ Clone the repo and sync dependencies (including dev and test groups):
95
+
96
+ ```sh
97
+ git clone https://github.com/andrianllmm/tagalog-stemmer.git
98
+ cd tagalog-stemmer
99
+ uv sync --all-groups
100
+ ```
101
+
102
+ Run the tests:
103
+
104
+ ```sh
105
+ uv run pytest
106
+ ```
@@ -0,0 +1,52 @@
1
+ [build-system]
2
+ requires = ["setuptools"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "tglstemmer"
7
+ version = "0.1.0"
8
+ description = "A library for Tagalog word stemming."
9
+ readme = "README.md"
10
+ license = {file = "LICENSE"}
11
+ authors = [
12
+ {name = "Andrian Lloyd Maagma", email = "maagmaandrian@gmail.com"}
13
+ ]
14
+ keywords = ["stemmer", "tagalog", "affix removal"]
15
+ classifiers = [
16
+ "Development Status :: 3 - Alpha",
17
+ "License :: OSI Approved :: MIT License",
18
+ "Programming Language :: Python :: 3.10",
19
+ "Operating System :: OS Independent",
20
+ ]
21
+ requires-python = ">=3.10"
22
+
23
+ dependencies = [
24
+ "nltk>=3.8.1",
25
+ "tabulate>=0.9.0",
26
+ ]
27
+
28
+ [dependency-groups]
29
+ dev = [
30
+ "commitizen>=4.16.5",
31
+ "git-cliff>=2.13.1",
32
+ "pre-commit>=4.6.1",
33
+ "pytest>=9.1.1",
34
+ "pytest-cov>=7.1.0",
35
+ "ruff>=0.16.0",
36
+ "twine>=5.1.1",
37
+ "ty>=0.0.64",
38
+ ]
39
+
40
+ [project.urls]
41
+ homepage = "https://github.com/andrianllmm/tglstemmer"
42
+ issues = "https://github.com/andrianllmm/tglstemmer/issues"
43
+
44
+ [tool.setuptools]
45
+ package-data = {"tglstemmer" = ["resources/*.txt", "resources/affixes/*.txt"]}
46
+
47
+ [[tool.uv.index]]
48
+ name = "testpypi"
49
+ url = "https://test.pypi.org/simple/"
50
+ publish-url = "https://test.pypi.org/legacy/"
51
+ explicit = true
52
+
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1 @@
1
+ """A package for Tagalog word stemming."""
File without changes
@@ -0,0 +1,27 @@
1
+ import os
2
+
3
+ script_dir = os.path.dirname(os.path.realpath(__file__))
4
+
5
+
6
+ def get_affixes(type, folder_path=None):
7
+ """Get list of affixes from file.
8
+
9
+ Args:
10
+ type (str): Type of affixes to get. Value can only be:
11
+ - 'pre': Prefixes
12
+ - 'in': Infixes
13
+ - 'suf': Suffixes
14
+ folder_path (str, optional): Path to the folder containing .txt files of Tagalog affixes.
15
+
16
+ Returns:
17
+ list: A list of affixes of chosen type.
18
+ """
19
+ if folder_path is None:
20
+ folder_path = os.path.join(script_dir, "../resources/affixes/")
21
+ with open(os.path.join(folder_path, f"{type}fixes.txt")) as in_file:
22
+ return sorted([affix.strip() for affix in in_file], key=len)
23
+
24
+
25
+ PREFIXES = get_affixes("pre")
26
+ INFIXES = get_affixes("in")
27
+ SUFFIXES = get_affixes("suf")
@@ -0,0 +1,2 @@
1
+ VOWELS = set("aeiou")
2
+ CONSONANTS = set("bcdfghjklmnpqrstvwxyz")
@@ -0,0 +1,39 @@
1
+ from ..stem import Stem
2
+
3
+
4
+ def replace_letter(token, index, letter):
5
+ """Replaces a letter in a token.
6
+
7
+ Args:
8
+ token (str): Word to be updated.
9
+ index (int): Index of the letter to be replaced.
10
+ letter (str): Letter used to replace.
11
+
12
+ Returns:
13
+ str: The updated word.
14
+ """
15
+ token_as_list = list(token)
16
+
17
+ token_as_list[index] = letter
18
+
19
+ return Stem("".join(token_as_list), **token.__dict__)
20
+
21
+
22
+ def swap_letters(token, index1, index2):
23
+ """Swaps two letters in a token.
24
+
25
+ Args:
26
+ token (str): Word to be updated.
27
+ index1 (int): Index of the first letter to be swapped.
28
+ index2 (int): Index of the second letter to be swapped.
29
+
30
+ Returns:
31
+ str: The updated word.
32
+ """
33
+ token_as_list = list(token)
34
+
35
+ index1_letter = token_as_list[index1]
36
+ token_as_list[index1] = token_as_list[index2]
37
+ token_as_list[index2] = index1_letter
38
+
39
+ return Stem("".join(token_as_list), **token.__dict__)
@@ -0,0 +1,63 @@
1
+ from .alphabet import CONSONANTS, VOWELS
2
+
3
+
4
+ def is_valid(token, valid_words=None):
5
+ """Checks if token is valid against a list of words.
6
+
7
+ Args:
8
+ token (str): Any word to be tested.
9
+ valid_words (list): List of valid words. Defaults to None.
10
+
11
+ Returns:
12
+ str/bool: The token if token is valid, False otherwise.
13
+ """
14
+ if not valid_words:
15
+ return token
16
+
17
+ if token in valid_words:
18
+ return token
19
+
20
+ return False
21
+
22
+
23
+ def is_acceptable(token):
24
+ """Checks if token is acceptable against certain acceptability conditions.
25
+
26
+ Args:
27
+ token (str): Any word to be tested.
28
+
29
+ Returns:
30
+ str/bool: The token if token is acceptable, False otherwise.
31
+ """
32
+ if is_vowel(token[0]):
33
+ if len(token) == 2 or (len(token) >= 3 and any(is_consonant(c) for c in token)):
34
+ return token
35
+ elif is_consonant(token[0]) and (
36
+ len(token) == 3 or (len(token) >= 4 and any(is_vowel(c) for c in token))
37
+ ):
38
+ return token
39
+ return False
40
+
41
+
42
+ def is_vowel(*substring):
43
+ """Checks if a substring is a consonant.
44
+
45
+ Args:
46
+ *substring: Substrings to be tested.
47
+
48
+ Returns:
49
+ bool: True if substring is consonant, False otherwise.
50
+ """
51
+ return all(letter.lower() in VOWELS for letter in "".join(substring))
52
+
53
+
54
+ def is_consonant(*substring):
55
+ """Checks if a substring is a consonant.
56
+
57
+ Args:
58
+ *substring: Substrings to be tested.
59
+
60
+ Returns:
61
+ bool: True if substring is consonant, False otherwise.
62
+ """
63
+ return all(letter.lower() in CONSONANTS for letter in "".join(substring))
@@ -0,0 +1,17 @@
1
+ import os
2
+
3
+ script_dir = os.path.dirname(os.path.realpath(__file__))
4
+
5
+
6
+ def get_words(file_path=None):
7
+ """Get list of words from a Tagalog word list txt file.
8
+
9
+ Args:
10
+ file_path (str, optional): Path to the Tagalog word lsit txt file. Defaults to '<script_dir>/../resources/tgl_wordlist.txt'.
11
+ Returns:
12
+ list: A list of words.
13
+ """
14
+ if file_path is None:
15
+ file_path = os.path.join(script_dir, "../resources/tgl_wordlist.txt")
16
+ with open(file_path) as in_file:
17
+ return [word.strip().lower() for word in in_file]
@@ -0,0 +1,102 @@
1
+ i
2
+ ka
3
+ ika
4
+ pa
5
+ pang
6
+ pan
7
+ pam
8
+ pina
9
+ ipa
10
+ ipapa
11
+ paka
12
+ pakaka
13
+ pana
14
+ panana
15
+ pama
16
+ pamama
17
+ pinaka
18
+ pinakama
19
+ ma
20
+ mang
21
+ man
22
+ mam
23
+ mina
24
+ mai
25
+ maii
26
+ maka
27
+ makaka
28
+ makakapag
29
+ makapa
30
+ makapag
31
+ na
32
+ nang
33
+ nan
34
+ nam
35
+ nai
36
+ naii
37
+ naka
38
+ nakaka
39
+ nakakapag
40
+ nakapa
41
+ nakapag
42
+ napa
43
+ napapa
44
+ napaka
45
+ napag
46
+ sa
47
+ sang
48
+ san
49
+ sam
50
+ ni
51
+ mag
52
+ magka
53
+ magkaka
54
+ magpa
55
+ magpapa
56
+ nag
57
+ nagka
58
+ nagkaka
59
+ nagpa
60
+ nagpapa
61
+ pag
62
+ pagka
63
+ pagkaka
64
+ pagpa
65
+ pagpapa
66
+ pinag
67
+ ipag
68
+ ipinag
69
+ pinagka
70
+ pinagkaka
71
+ ipinagka
72
+ ipinagkaka
73
+ tag
74
+ tig
75
+ may
76
+ maki
77
+ makipag
78
+ makipagka
79
+ makikipag
80
+ makikipagka
81
+ naki
82
+ nakipag
83
+ nakipagka
84
+ nakikipag
85
+ nakikipagka
86
+ paki
87
+ pakiki
88
+ pakipag
89
+ pakipagka
90
+ pakikipag
91
+ pakikipagka
92
+ pala
93
+ mala
94
+ tiga
95
+ tigapag
96
+ taga
97
+ tagapag
98
+ sing
99
+ sin
100
+ sim
101
+ kasing
102
+ di-
@@ -0,0 +1,19 @@
1
+ an
2
+ in
3
+ han
4
+ hin
5
+ nan
6
+ anan
7
+ nin
8
+ anin
9
+ ang
10
+ ing
11
+ ita
12
+ ito
13
+ dor
14
+ syon
15
+ siyon
16
+ ng
17
+ g
18
+ 't
19
+ 'y