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]"

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))

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.0

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.0
File Size Uploaded
kwic-0.2.0.tar.gz 15.0 kB Details

Built distribution (wheel)

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

Total release size: 36.1 kB

Release files / kwic-0.2.0.tar.gz

Download URL kwic-0.2.0.tar.gz
Size 15.0 kB
Tags Source
SHA-256 checksum
How to use checksums
7a6ef77e4d491c130cd52a02cee64c9588c66381b9b60f18928a78a18307af50
BLAKE2b-256 checksum
How to use checksums
d2644346ca71d9941d230dc214b3762e0a6a797bc90cf6ce7745c3bb34039ed5
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.0-py3-none-any.whl

Download URL kwic-0.2.0-py3-none-any.whl
Size 21.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d886976a3796c9b886956ae99c1d0ccb70adb0d75fd5f212a8054c99336d18fb
BLAKE2b-256 checksum
How to use checksums
9271052c9636ab03945826896d8746d619f1a1ebd9416aaf0c29c42c6b599168
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

0.2.1

2 release files

This release

0.2.0 This release

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