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.2.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.2-py3-none-any.whl (3.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: xtsv-word-1.0.2.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.2.tar.gz
Algorithm Hash digest
SHA256 df1bb90ba82390b0fe77f8f34eb7bcd02a53421a016a9ca69a05aff7e138cf9c
MD5 3808d0be9eaf02948f5bf073846962b7
BLAKE2b-256 ce77d0e6b882c30884e8a3d27b6c9a4b8a9912d493d2c59fdafa333cdfb4b8d5

See more details on using hashes here.

File details

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

File metadata

  • Download URL: xtsv_word-1.0.2-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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 06e6355fc7f839d03cfa4551c4f450273d9b3bb8a0fcb00291bf3a996e695161
MD5 5911fe82070d402902159217fd710ab9
BLAKE2b-256 b77795ec8bb5cd75da5240c2bbd4c1d1a23e289262ce48a47d23fe237df39a68

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