Skip to main content

Key Word In Context

Locates the lemmas of a query in the context that attests them.

Installation

Requirements

  • Python 3.12 or later

From PyPI

pip install kwic

A search reads with spaCy, whose pipelines are published apart from the library:

python -m spacy download en_core_web_sm

The other engines are extras:

pip install "kwic[stanza]"
pip install "kwic[lemminflect]"

Reading on the graphics card needs CuPy, whose wheel is named after the CUDA release nvidia-smi reports:

pip install "kwic[cuda12x]"
pip install "kwic[cuda13x]"

Usage

A search takes one context and the lemmas to look for.

from kwic import Locator, POS, Query

locator = Locator()

locator.find("She found the keys she had lost.", [Query("find", POS.VERB)])
# (Match(lemma='find', pos=POS.VERB, form='found', word_index=1, offsets=(4, 9)),)

Query

Field Default
lemma Dictionary form to look for, in whatever case
pos None Tag a one-word occurrence must carry; a longer lemma is not narrowed
forms () How else the lemma is written, taken where the engine read another

The engine cuts the lemma into words, and a space and a hyphen are one to it.

locator.find("It was hunky-dory.", [Query("hunky dory")])
# (Match(lemma='hunky dory', pos=POS.ADJ, form='hunky-dory', word_index=2, offsets=(7, 17)),)

A phrasal verb written apart runs from the verb to the particle.

locator.find("She gave the money up.", [Query("give up")])
# (Match(lemma='give up', pos=POS.VERB, form='gave the money up', word_index=1, offsets=(4, 21)),)

Match

Field
lemma The lemma you asked for, as you wrote it
pos The tag it carries
form How it is written
word_index Where it opens among the words, from zero
offsets Where it falls in the text, half-open and in code points

Contexts

A context is a text, or the words it was split into. The second has no range.

locator.find(("She", "found", "the", "keys"), [Query("find")])
# (Match(lemma='find', pos=POS.VERB, form='found', word_index=1, offsets=None),)

Many contexts

find_all takes context and lemmas in pairs, and reads a batch at a time.

for occurrences in locator.find_all(searches):
    ...

Engines

An engine reads the context; the search reads the engine.

from kwic.engines.stanza import StanzaEngine

locator = Locator(StanzaEngine())
Engine Reads with Install
SpacyEngine a spaCy pipeline, its English lemmatiser rules over the tag spacy
StanzaEngine Stanza, a dictionary with a neural model behind it kwic[stanza]
LemmInflectEngine spaCy for the tags, LemmInflect for the lemmas kwic[lemminflect]

An extra is imported from the module wrapping it, so a package without it still loads.

SpacyEngine takes the pipeline to load, StanzaEngine the language. Both parse unless told otherwise:

Parser Buys Costs
spaCy phrasal verbs apart, and several universal tags a tenth of a reading
Stanza phrasal verbs apart half its speed
Locator(SpacyEngine(parse=False))

Either engine reads on the graphics card when told to, which needs the CuPy extra above:

Locator(SpacyEngine("en_core_web_trf", gpu=True))
Asked for Gets Wants
gpu=True a card, or an error where none answers one process, a large batch
gpu=False the processor a small batch, padding being what it costs

Accuracy

English-EWT test section: 2,077 sentences, 417 lemmas, 3,619 occurrences.

Engine Precision Recall F1
StanzaEngine 0.975 0.963 0.969
SpacyEngine, en_core_web_trf 0.981 0.905 0.942
SpacyEngine, en_core_web_lg 0.978 0.891 0.933
SpacyEngine, en_core_web_sm 0.978 0.882 0.928
LemmInflectEngine 0.976 0.872 0.921

Release files for kwic 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for kwic 0.2.1
File Size Uploaded
kwic-0.2.1.tar.gz 15.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for kwic 0.2.1
File Interpreter ABI Platform
kwic-0.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 36.6 kB

Release files / kwic-0.2.1.tar.gz

Download URL kwic-0.2.1.tar.gz
Size 15.3 kB
Tags Source
SHA-256 checksum
How to use checksums
84f36f20e283c02e940e4961fe9f465ce6737c8fd9dcee42fa8f956ed51e0165
BLAKE2b-256 checksum
How to use checksums
19c91ed9ee6e1667ace322cea80f381874bf92100e5ec6f00295fcc4673a4e8d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.9 {"installer":{"name":"uv","version":"0.12.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / kwic-0.2.1-py3-none-any.whl

Download URL kwic-0.2.1-py3-none-any.whl
Size 21.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4cffa7317cc4b96ad203eb4f851b00b2dffe2346f73a17e444db9790aace1d0d
BLAKE2b-256 checksum
How to use checksums
ba06662ce35319da7d90fc534830f0b5b7f2570b50a9f686b32f8cabeffb31b8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.9 {"installer":{"name":"uv","version":"0.12.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page