Skip to main content

library.qai.utilities

Helper functions and classes for interacting with the rest of the Writer platform. The main components are:

  • qai.issues.make_issue: makes a dictionary that conforms to Writer platform standards
  • qai.spacy_factor.SpacyFactor: a helper class which turns a spaCy Span into an issue
  • qai.server.QRest: a helper class which builds a Sanic REST server for you
  • qai.validation.Validator: a simple validator class that can be used to skip segments without processing them, e.g. for being empty or having HTML. You would only want to import this yourself if you aren't using QRest

If you are building a Sanic app without using QRest, you still may be interested in the following middleware:

  • qai.server.set_valid_segments: stores valid segments to request.ctx.segments and also sets request.ctx.info
  • qai.server.postprocess_issues: changes the string indexing to be utf-16 based, and filters no ops

(See GitHub history for older docs - QAI used to do a lot more!)

Upgrading to v5

  • Analyzer class must now be callable. It will be passed (segment: str, meta: dict, all_info: dict) - segment is the string to analyze, meta is the meta object that was sent, or {} if none was sent, and all_info is the entire payload the server received - in case you need access to clientID or something. Feel free to define def __call__(self, segment: str, meta: dict, _) if you don't expect to need the request.
  • QRest can be passed a Sanic app, or be passed a dictionary which maps issue types to categories (in addition to the default behavior). This is useful for services that handle multiple categories, for which the default behavior doesn't work.
  • QAI has a simpler structure, so all imports look different
  • Configs, Strings, Storage, and Document are gone. The later 2 because they aren't needed anymore, the former 2 because you should manage that yourself. Whitelisting is also gone - just don't make the issues if you don't want them.
  • All issues are created in the v2 format (meaning, the format we switched to after new segmentation - defined here)
  • By default, issue from and until keys are now based on UTF-16 indexing, to make things easier for JS. We add _from_p and _until_p keys for debugging, which are the Python string indexes. This happens as response middleware in QRest.

Usage

You can explicitly create a REST connection like this:

from app import Analyzer

from qai.server import QRest


# setting the category / service name does nothing
# we use the category passed on the request
category = 'service_name'
host = '0.0.0.0'
port = 5000


if __name__ == '__main__':
    analyzer = Analyzer()
    rest_connection = QRest(
      analyzer,
      category=category,
      host=host,
      port=port
    )
    # create a blocking connection:
    rest_connection.connect()

The above will create as many workers as you have cores. This is great, sometimes. For example, there is a known bug where AutoML crashes if you are using more than one worker. So pass workers=1 if this happens

There is also a helper class for turning spaCy Spans into issues the rest of the platform can process:

from spacy.tokens import Span
from qai.spacy_factor import SpacyFactor


MyFactor = SpacyFactor(
    "subject_object_verb_spacing",
    "Keep the subject, verb, and object of a sentence close together to help the reader understand the sentence."
)

Span.set_extension("score", default=0)
Span.set_extension("suggestions", default=[])

doc = nlp("Holders of the Class A and Class B-1 certificates will be entitled to receive on each Payment Date, to the extent monies are available therefor (but not more than the Class A Certificate Balance or Class B-1 Certificate Balance then outstanding), a distribution.")
score = analyze(doc)
if score is not None:
    span = Span(doc, 0, len(doc))  # or whichever tokens/spans are the issue (don't have to worry about character indexes)
    span._.score = score
    span._.suggestions = get_suggestions(doc)
    issue = MyFactor(span)

Installation

pip install qai or poetry add qai

Testing

See Confluence for docs on input format expectations.

scripts/test_qai.sh has some helpful testing functions.

CI/CD

GitHub Actions will push to PyPi when you merge into the main branch.

License

This software is not licensed. If you do not work at Writer, you are not legally allowed to use it. Also, it's just helper functions that really won't help you. If something in it does look interesting, and you would like access or our help, open an issue.

Metadata

Release files for qai 5.1.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 qai 5.1.1
File Size Uploaded
qai-5.1.1.tar.gz 12.3 kB Details

Built distribution (wheel)

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

Total release size: 24.5 kB

Release files / qai-5.1.1.tar.gz

Download URL qai-5.1.1.tar.gz
Size 12.3 kB
Tags Source
SHA-256 checksum
How to use checksums
b25b4ffbb9c7f80ddee08105b28e429a2b9143f31a26e3ffe3a8eee8624a86ae
BLAKE2b-256 checksum
How to use checksums
a74363400be70d47e02b179c91fdfd7c62b73b4adcf62fc6342d6679a8ce80e3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.6 CPython/3.8.2 Linux/5.8.0-1033-azure

Release files / qai-5.1.1-py3-none-any.whl

Download URL qai-5.1.1-py3-none-any.whl
Size 12.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9a3b81494db9745b65661ff91355b4536e5e022594fea8574e899aad48be6487
BLAKE2b-256 checksum
How to use checksums
51e1fedc5a3228a63c09b4cbef903d629de409a3190cb37ee034306c94e8afdd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.6 CPython/3.8.2 Linux/5.8.0-1033-azure

Release history Release notifications | RSS feed

This release

5.1.1 This release

2 release files

5.1.0

2 release files

5.0.0

2 release files

4.9.999

1 release file

4.9.99

1 release file

4.3.1

1 release file

4.2.4

1 release file

4.2.3

1 release file

4.2.2

1 release file

4.2.1

1 release file

4.2.0

1 release file

4.1.0

1 release file

4.0.12

1 release file

4.0.11

1 release file

4.0.10

1 release file

4.0.9

1 release file

4.0.5

1 release file

4.0.4

1 release file

4.0.3

1 release file

4.0.2

1 release file

4.0.1

1 release file

4.0.0

1 release file

3.0.0

1 release file

2.6.1

1 release file

2.5.8

1 release file

2.5.6

1 release file

2.5.5

1 release file

2.5.4

1 release file

2.5.3

1 release file

2.4.2

1 release file

2.4.1

1 release file

2.4.0

1 release file

2.3.6

1 release file

2.3.5

1 release file

2.3.4

1 release file

2.3.3

1 release file

2.3.2

1 release file

2.3.1

1 release file

2.3.0

1 release file

2.2.9

1 release file

2.2.8

1 release file

2.2.7

1 release file

2.2.6

1 release file

2.2.5

1 release file

2.2.4

1 release file

2.2.3

1 release file

2.2.2

1 release file

2.2.1

1 release file

2.2.0

1 release file

2.1.9

1 release file

2.1.8

1 release file

2.1.6

1 release file

2.1.5

1 release file

2.1.4

1 release file

2.1.3

1 release file

2.1.2

1 release file

2.1.1

1 release file

2.1.0

1 release file

2.0.1

1 release file

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