Tetun tokenizer package
Project description
Project description
Tetun Tokenizer
Tetun tokenizer is a Python package for tokenizing a string (or a text) into tokens. There are several tokenization alternatives in the following.
- Tokenize by word, punctuations, or special characters delimiters using
TetunStandardTokenizer(). - Tokenize by whitespace delimiter using
TetunWhiteSpaceTokenizer(). - Tokenize by blank lines as a delimiter using
TetunBlankLineTokenizer().
It is also allowed us to tokenize a string (or a text) by:
- Strings and numbers and ignore punctuations and special characters using
TetunSimpleTokenizer(). - Strings only and ignore numbers, punctuations, and special characters using
TetunWordTokenizer().
Installation
To install Tetun tokenizer, run the following command:
python3 -m pip install tetun-tokenizer
or simply run:
pip install tetun-tokenizer
Usage
To use the Tetun tokenizer, from the tetuntokenizer package, import tokenizer module then call a specific tokenizer class. Instantiate the imported class and then use a tokenize function as follows:
- Using
TetunStandardTokenizer()to tokenize a string.
from tetuntokenizer.tokenizer import TetunStandardTokenizer
tetun_tokenizer = TetunStandardTokenizer()
string_text = "Ha'u mak ita-nia maluk di'ak. Ha'u iha $0.25 atu fó ba ita."
output = tetun_tokenizer.tokenize(string_text)
print(output)
The output will be:
["Ha'u", 'mak', 'ita-nia', 'maluk', "di'ak", '.', "Ha'u", 'iha', '$', '0.25', 'atu', 'fó', 'ba', 'ita', '.']
- Using
TetunWhiteSpaceTokenizer()to tokenize a string.
from tetuntokenizer.tokenizer import TetunWhiteSpaceTokenizer
tetun_tokenizer = TetunWhiteSpaceTokenizer()
string_text = "Ha'u mak ita-nia maluk di'ak. Ha'u iha $0.25 atu fó ba ita."
output = tetun_tokenizer.tokenize(string_text)
print(output)
The output will be:
["Ha'u", 'mak', 'ita-nia', 'maluk', "di'ak.", "Ha'u", 'iha', '$0.25', 'atu', 'fó', 'ba', 'ita.']
- Using
TetunBlankLineTokenizer()to tokenize a string.
from tetuntokenizer.tokenizer import TetunBlankLineTokenizer
tetun_tokenizer = TetunBlankLineTokenizer()
string = """
Ha'u mak ita-nia maluk di'ak.
Ha'u iha $0.25 atu fó ba ita.
"""
output = tetun_tokenizer.tokenize(string_text)
print(output)
The output will be:
["\n Ha'u mak ita-nia maluk di'ak.\n Ha'u iha $0.25 atu fó ba ita.\n "]
- Using
TetunSimpleTokenizer()to tokenize a string.
from tetuntokenizer.tokenizer import TetunSimpleTokenizer
tetun_tokenizer = TetunSimpleTokenizer()
string_text = "Ha'u mak ita-nia maluk di'ak. Ha'u iha $0.25 atu fó ba ita."
output = tetun_tokenizer.tokenize(string_text)
print(output)
The output will be:
["Ha'u", 'mak', 'ita-nia', 'maluk', "di'ak", "Ha'u", 'iha', '0.25', 'atu', 'fó', 'ba', 'ita']
- Using
TetunWordTokenizer()to tokenize a string.
from tetuntokenizer.tokenizer import TetunWordTokenizer
tetun_tokenizer = TetunWordTokenizer()
string_text = "Ha'u mak ita-nia maluk di'ak. Ha'u iha $0.25 atu fó ba ita."
output = tetun_tokenizer.tokenize(string_text)
print(output)
The output will be:
["Ha'u", 'mak', 'ita-nia', 'maluk', "di'ak", "Ha'u", 'iha', 'atu', 'fó', 'ba', 'ita']
To print the resulting string to the console, with each element on a new line, you can use for loop or simply use join as follows:
print('\n'.join(output))
The output will be:
Ha'u
mak
ita-nia
maluk
di'ak
Ha'u
iha
atu
fó
ba
ita
You can also use the tokenizer to tokenize a text from a file. Here is an example:
# Assume that we use Path instead of a string for the file path
from pathlib import Path
from tetuntokenizer.tokenizer import TetunSimpleTokenizer
file_path = Path("myfile/example.txt")
try:
with corpus_path.open('r', encoding='utf-8') as f:
contents = [line.strip() for line in f]
except FileNotFoundError:
print(f"File not found at: {corpus_path}")
# You can also lowercase the contents before tokenizing them.
lowercase_contents = contents.lower()
tetun_tokenizer = TetunSimpleTokenizer()
output = '\n'.join(tetun_tokenizer.tokenize(str(lowercase_contents)))
print(output)
There are a few more ways to read file contents that you can use to achieve the same output.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tetun_tokenizer-0.0.6.tar.gz.
File metadata
- Download URL: tetun_tokenizer-0.0.6.tar.gz
- Upload date:
- Size: 15.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8df9f4c1eddcbf8ca37a55386a3393529c207d0fda6c442ddd94f4ed81a0fe86
|
|
| MD5 |
d126b7139684cb9ba99e788cd91538a6
|
|
| BLAKE2b-256 |
fcd367ffcfb58042d68660902f67526d238c6c86870a04193b2c147aeb83c225
|
File details
Details for the file tetun_tokenizer-0.0.6-py3-none-any.whl.
File metadata
- Download URL: tetun_tokenizer-0.0.6-py3-none-any.whl
- Upload date:
- Size: 15.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
84cb850829d1619fe2979383996c5eb44a3c213bb4e278783560258ab6736d98
|
|
| MD5 |
1a54e355c934bd5d5ab8abdca5c345ff
|
|
| BLAKE2b-256 |
1a153973ef5a6aa646c11cb0da4365420aa03c1ddc7ef1f47dca88c51e5412c6
|