Skip to main content

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.

  1. Tokenize by word, punctuations, or special characters delimiters using TetunStandardTokenizer().
  2. Tokenize by whitespace delimiter using TetunWhiteSpaceTokenizer().
  3. Tokenize by blank lines as a delimiter using TetunBlankLineTokenizer().

It is also allowed us to tokenize a string (or a text) by:

  1. Strings and numbers and ignore punctuations and special characters using TetunSimpleTokenizer().
  2. 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 use:

pip install tetun-tokenizer

Usage

To use the Tetun tokenizer, from the tetuntokenizer package, import a specific tokenizer class and the tokenize function as follows:

  1. Using TetunStandardTokenizer() to tokenize a string.
from tetuntokenizer.tokenizer import TetunStandardTokenizer

tetun_tokenizer = TetunStandardTokenizer()

string_text = "Ha'u, Gabriel de Jesus, 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", ',', 'Gabriel', 'de', 'Jesus', ',', 'ita-nia', 'maluk', "di'ak", '.', "Ha'u", 'iha', '$', '0.25', 'atu', 'fó', 'ba', 'ita', '.']
  1. Using TetunWhiteSpaceTokenizer() to tokenize a string.
from tetuntokenizer.tokenizer import TetunWhiteSpaceTokenizer

tetun_tokenizer = TetunWhiteSpaceTokenizer()

string_text = "Ha'u, Gabriel de Jesus, 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,", 'Gabriel', 'de', 'Jesus,', 'ita-nia', 'maluk', "di'ak.", "Ha'u", 'iha', '$0.25', 'atu', 'fó', 'ba', 'ita.']
  1. Using TetunBlankLineTokenizer() to tokenize a string.
from tetuntokenizer.tokenizer import TetunBlankLineTokenizer

tetun_tokenizer = TetunBlankLineTokenizer()

string = """
        Ha'u, Gabriel de Jesus, 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, Gabriel de Jesus, ita-nia maluk di'ak.\n            Ha'u iha $0.25 atu fó ba ita.\n            "]
  1. Using TetunSimpleTokenizer() to tokenize a string.
from tetuntokenizer.tokenizer import TetunSimpleTokenizer

tetun_tokenizer = TetunSimpleTokenizer()

string_text = "Ha'u, Gabriel de Jesus, 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", 'Gabriel', 'de', 'Jesus', 'ita-nia', 'maluk', "di'ak", "Ha'u", 'iha', '0.25', 'atu', 'fó', 'ba', 'ita']
  1. Using TetunWordTokenizer() to tokenize a string.
from tetuntokenizer.tokenizer import TetunWordTokenizer

tetun_tokenizer = TetunWordTokenizer()

string_text = "Ha'u, Gabriel de Jesus, 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", 'Gabriel', 'de', 'Jesus', '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
Gabriel
de
Jesus
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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tetun_tokenizer-0.0.3.tar.gz (15.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tetun_tokenizer-0.0.3-py3-none-any.whl (15.6 kB view details)

Uploaded Python 3

File details

Details for the file tetun_tokenizer-0.0.3.tar.gz.

File metadata

  • Download URL: tetun_tokenizer-0.0.3.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

Hashes for tetun_tokenizer-0.0.3.tar.gz
Algorithm Hash digest
SHA256 666d05a45b1ff4c1e02fe0b3b3658ba44ac09682c3aa29f3def68c0b08f3df8e
MD5 a5992ee90d41a0f7ba52e6b18bd8f3d0
BLAKE2b-256 cf363f215fa76a8a35bb5236dcabd9ac4165a43fb69acb9d0c8bf1bd787ad098

See more details on using hashes here.

File details

Details for the file tetun_tokenizer-0.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for tetun_tokenizer-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 ae23eb33a91ca4f5dd0ee31f672ef53b41ac2ac6897a5478403c3efb8739a7ea
MD5 24aa4948dc9b3f29da9155930ba86285
BLAKE2b-256 e7278994952d4c6d6e56f389ba8d614cda9c92d8b2596317b05873106f212681

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page