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.1.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.1.0
File Size Uploaded
kwic-0.1.0.tar.gz 14.9 kB Details

Built distribution (wheel)

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

Total release size: 35.6 kB

Release files / kwic-0.1.0.tar.gz

Download URL kwic-0.1.0.tar.gz
Size 14.9 kB
Tags Source
SHA-256 checksum
How to use checksums
78c35b29f76ca98d6f434d903122f3845818816d3eae24225f2f44e9e922d6fc
BLAKE2b-256 checksum
How to use checksums
965893c420b2579a6b8980f0e3ea4b821d9aa881659512413f6e41f612525953
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.8 {"installer":{"name":"uv","version":"0.12.8","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.1.0-py3-none-any.whl

Download URL kwic-0.1.0-py3-none-any.whl
Size 20.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2ba221e657b22388bbe2dc676b8d3ca2e59effbe8e26d3d40fa39338134069af
BLAKE2b-256 checksum
How to use checksums
ece74df83cc480816181db11e7ff5db45df753b409cc35f3354c81164c3cd025
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.8 {"installer":{"name":"uv","version":"0.12.8","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

0.2.0

2 release files

This release

0.1.0 This release

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