Skip to main content

A tool for information and relation extraction using regular expressions

Project description

Text-To-Relations

Text-To-Relations: a tool for Information and Relation Extraction

Text-To-Relations is built around a two-layer pipeline:

  1. Entity recognitionRegexString provides a simple interface for building complex regular expressions that identify typed entity spans (e.g. a stamp ID, a denomination, a perforation value) in raw text.
  2. Relation extractionExtractionPhaseABC and its supporting machinery take those entity spans and find relationships between them based on proximity in token space (e.g. "a StampID followed within 4 tokens by a Denomination"). You define which entity types to link and how close together they must appear; the framework handles the rest.

The two layers are independent: you can use RegexString alone for entity recognition tasks, or combine both layers for full relation extraction.

Source code at GitHub text_to_relations.

Installation

After setting up your virtual environment:

pip install text_to_relations

If your Python version is less than 3.11, also run:

pip install --upgrade pip
pip install typing_extensions

Text-To-Relations requires Spacy:

pip install -U spacy
python -m spacy download en_core_web_lg

Text-To-Relations has been tested on:

  • Python 3.9.18 and Python 3.11.6 on MacOS Sequoia 15.2
  • Python 3.10.12 on Ubuntu 22

Quick Start

from text_to_relations import RegexString

text = "The sky is bright blue and the leaves are dark green or just brown."

# Match individual colors
colors = RegexString(['red', 'blue', 'green', 'brown'], whole_word=True)

# Optionally prepend a qualifier
qualifiers = RegexString(['bright', 'dark', 'dull'], whole_word=True, optional=True)

color_phrase = RegexString.concat_with_word_distances(
    qualifiers, colors,
    min_nbr_words=0,
    max_nbr_words=0)
print(color_phrase.get_match_triples(text))
# [('bright blue', 11, 22), ('dark green', 40, 50), ('brown', 57, 62)]

get_match_triples() returns a list of (matched_text, start_offset, end_offset) tuples.

The key classes — RegexString, Annotation, TokenAnn, SentenceAnn, and ExtractionPhaseABC — are all importable directly from text_to_relations.

For Experienced Regex Users

If you are comfortable writing raw regular expressions, RegexString may not add much value for entity recognition on its own. But see comments on relation extraction, below.

By default the constructor escapes all match strings via re.escape(), so metacharacters like \d+ are treated as literals. Pass escape=False to use regex syntax directly in match_strs while still getting all the constructor features — whole_word, optional, prepend, append, and the OR-ing machinery:

number_rs = RegexString([r'\d+'], escape=False)
digits_or_lower = RegexString([r'\d+', r'[a-z]+'], escape=False)
number_word = RegexString([r'\d+'], escape=False, whole_word=True)

Use from_regex() only when you need to pass a complete hand-written regex that cannot be expressed as a list of alternates — for example, when combining two already-built RegexString objects:

perf_combined_rs = RegexString.from_regex(
    f'(?:{imperf_rs.get_regex_str()}|{perf_sized_rs.get_regex_str()})')

Where the framework pays off for everyone, including experienced regex users, is relation extraction. Consider linking a stamp ID to its denomination when they appear within four tokens of each other. In raw regex:

import re
pattern = r'(#\s\d+(?:\w+)?)(?:\s\S+){0,4}\s(\d\d?(?:c|¢))'
matches = re.findall(pattern, text)
# matches is a list of (stamp_id, denomination) tuples -- but unlabeled,
# unfiltered, and with no structure beyond what re.findall() provides

With the framework:

chain = [
    ChainLink(start_type='StampID', start_property='StampID',
              min_distance=0, max_distance=4,
              end_type='Denomination', end_property='Denomination'),
]
phase = SimpleExtractionPhase(relation_name='StampDescription',
                               regex_patterns=regex_patterns, chain=chain)
results = phase.find_match(text)
# results is a list of Annotation objects with labeled properties

Extending the raw regex approach to four entities — each pair with its own distance constraint — means chaining the pattern into one long, nearly unreadable expression, and then writing additional code to label, filter, and structure the output. With the framework, each new entity is one more dict entry and one more ChainLink, each self-contained and labeled — complexity grows linearly and readably. For a full four-entity example, see examples/extract_stamp_description.py.

Further Reading

For a full walkthrough, including entity recognition and relation extraction examples, see TUTORIAL.md.

A working end-to-end relation extraction example can be found in examples/extract_stamp_description.py. Run it with:

python -m examples.extract_stamp_description

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

text_to_relations-0.1.3.tar.gz (24.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

text_to_relations-0.1.3-py3-none-any.whl (26.1 kB view details)

Uploaded Python 3

File details

Details for the file text_to_relations-0.1.3.tar.gz.

File metadata

  • Download URL: text_to_relations-0.1.3.tar.gz
  • Upload date:
  • Size: 24.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.11.6

File hashes

Hashes for text_to_relations-0.1.3.tar.gz
Algorithm Hash digest
SHA256 5248ec3d011dc8aca0ee6de7a137a0a08158693353c3966c5c19ba5aad0776c8
MD5 7a8bc73829fa36b36d3cb6bf29f26a60
BLAKE2b-256 62f3a6d82e77ae416e2684538422acfcf4e5cfcdc4e2c49175595d00a68d8f13

See more details on using hashes here.

File details

Details for the file text_to_relations-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for text_to_relations-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 ff2eba7fe468d80fd28da60e6956bdad73a94faf231bc1c324b5539903a454b8
MD5 142eddf68545b75b72bb6ec26b354686
BLAKE2b-256 c49b43ef9ffc67d716e1cf8b892b5d9e91fc255579064a9fd106a647047e1f0f

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