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)[https://github.com/nytud/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. The 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 this 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 finished with processing a token, the Word value is simply converted into a list which contains the original input fields followed by the target fields, as expected by xtsv.

Note that this is not an official extension of the xtsv module.

Suggested usage

Install xtsv-word from pip:

python3 -m pip install xtsv-word

Assuming the internal app object defined in the xtsv module myXtsvModule is called InternalApp, the input stream contains the fields ['form', 'wsafter'] and the module 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):
		wf = WordFactors(field_names, self.target_fields)
		# self.target_fields is normally set in InternalApp.__init__()
  1. Use Word object:
class InternalApp:
	...
	def process_sentence(sen, field_values):
		return_sen = []
		for tok in sen:
			# Get Word object form factory
			word = 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.0.tar.gz (4.2 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.0-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: xtsv-word-1.0.0.tar.gz
  • Upload date:
  • Size: 4.2 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.0.tar.gz
Algorithm Hash digest
SHA256 4502715444465c1a74a53329ae88c4db9fc34539493d9aa2d3c9fcfe3be3e5a3
MD5 b8560f3c529081aa506a1f897b2bab60
BLAKE2b-256 3b38a344f49a1307415cbcf20aa9c69b3fa4149c483a239faac4dffc87f67bdf

See more details on using hashes here.

File details

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

File metadata

  • Download URL: xtsv_word-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 5.2 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 62208fab0a5785550dabd72a7f84dfd30e289693d324bc44cd4c9e034a005be8
MD5 aab89f03d9b6abdf0f06ba9186e5d836
BLAKE2b-256 17115e78b74c7905bf408037a50b842ca137c4236eec86d45e2f480bf6b694dc

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