Skip to main content

A dataclass-like structure to make the handling of xtsv token attributes comfortable and transparent

Project description

xtsv-word

This module provides a special dataclass-like structure to handle tokens in xtsv format. It is meant to be used within xtsv modules to make the processing of token attributes (xtsv fields) more comfortable and transparent.

It allows for each token to be represented by a Word object which is initialised simply by passing the token in its xtsv representation (i.e. a list of strings, one for each field in the input stream) to a WordFactory object. This factory object keeps track of the input and target fields of the xtsv module, and assigns the items of the list representing the token in xtsv to the respective Word object attributes which are identified by the name of the corresponding field (i.e. the xtsv column header).

Both the input and the target fields can be accessed as attributes of a Word object, i.e. they can be both retrieved and modified. (The usual use case is to only read input field attributes and to specify target field attributes. However, the Word object does not prevent a user from modifying input field values. This is discouraged but not ruled out by xtsv.) When the xtsv module is done processing a token, the Word object is simply converted into a list which contains the original input fields followed by the target fields, as expected by xtsv.

Disclaimer: This is not an official extension of the xtsv module.

Suggested usage

Install xtsv-word from pip:

python3 -m pip install xtsv-word

or build locally:

make

For example, assuming that the internal app object defined in the xtsv module myXtsvModule is called InternalApp, the input stream contains the fields ['form', 'wsafter'] and myXtsvModule has a single target field: ['syllables']:

  1. Create WordFactory object:
# myXtsvModule.py

from xtsv_word import WordFactory

class InternalApp:
	...
	def prepare_fields(self, field_names):
		self.wf = WordFactory(field_names, self.target_fields)
		# self.target_fields is normally set in InternalApp.__init__()
  1. Use Word object:
class InternalApp:
	...
	def process_sentence(self, sen, field_values):
		return_sen = []
		for tok in sen:
			# Get Word object from factory
			word = self.wf.get_word(tok)

			# process token by getting and setting its attributes, e.g.:
			word.syllables = split_syllables(word.form)
			...
			# alternatively access attributes as dict keys:
			word['syllables'] = '-'.join(word['syllables'])

			# convert Word object to list of fields for xtsv output stream
			return_sen.append(list(word))

		...
		return return_sen

See docstrings for further details.

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

xtsv-word-1.0.1.tar.gz (3.7 kB view details)

Uploaded Source

Built Distribution

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

xtsv_word-1.0.1-py3-none-any.whl (3.8 kB view details)

Uploaded Python 3

File details

Details for the file xtsv-word-1.0.1.tar.gz.

File metadata

  • Download URL: xtsv-word-1.0.1.tar.gz
  • Upload date:
  • Size: 3.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.1 CPython/3.10.4 Linux/5.15.0-48-generic

File hashes

Hashes for xtsv-word-1.0.1.tar.gz
Algorithm Hash digest
SHA256 76b5f5403464b69a3c116abe2da3c0eeca0d41bfd43e9ef4a44e3efc4ce67cc8
MD5 4809a3c781bf309a406bd66097d52656
BLAKE2b-256 d7f8aa30715ecb0b7b1b079b23cb4385ddc789b1ce5265093a4610a3e8b947f5

See more details on using hashes here.

File details

Details for the file xtsv_word-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: xtsv_word-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 3.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.1 CPython/3.10.4 Linux/5.15.0-48-generic

File hashes

Hashes for xtsv_word-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 723ae9c4528a303dc98ee9604692b72dc6b02c911077d1ea2291ca8c8beb9f30
MD5 82d03054df481ca0f3f6d865af14c7be
BLAKE2b-256 254889e5d99710ff550075dabd9bb3f13ed806a140c51886b10837519ec9b29c

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