Skip to content

NLP Class

What's the NLP class?

NLP inherits from TextChunk.

NLP is a class designed to give access to relevant data at the different levels (document, sentence, subsentence) in an intuitive way. It allows you to perform quick data exploration, manipulation and analysis. It's also used to perform requests and can save as well as load result as JSON objects.

When a response from the API is received it's stored in a hierarchy of classes: NLP (all data) => Document => Sentence => Subsentence => Token

At each level direct access it's possible to access inferior levels i.e. nlp.sentences gives access to a list of all the Sentence in the current data, while nlp.documents[0].sentences only gives the Sentence of the first Document.

NLP is iterable and will yield Document instances.

Attributes & Properties

NameTypeDescription
documentslist of Document instancesList of all the Document instances
sentenceslist of Sentence instancesDirect access to all of the Sentences instances.
subsentenceslist of Subsentence instancesDirect access to all of the Subsentence instances.
tokenslist of Token instancesDirect access to all Tokens in the subsentence
fieldslist of stringList of all common properties accessible at all levels (token, pos etc.)
fieldsinstance of ClientClient used for performing request to Lettria's API
Common propertiesdepends on propertyProperties allowing access to specific data (pos, token etc.)

NLP methods

Data analysis

Below is an overview list of the methods that can be used to manage data with the API.

MethodDescription
add_document()Submits document to API
reset_data()Saves data from json file
load_results()Loads data from json file
reset_data()Erase data and reinitialise object
add_client()Adds new client / api_key

add_document()

add_document(document, skip_document = False, id=None, verbose=True)

Performs a request to the lettria API using the API_KEY provided. Results are appended as an additional Document instance to the documents attribute.

Parameters:

NameTypeDescriptionOptional
documentstring or list of stringData to be sent to the APIFalse
skip_documentboolWhether to skip the document if there is a problem during processingTrue
idstrId to identify the document, by default an incrementing integer is assigned.True
verboseboolWhether to print additional statements about document processing.True

save_results()

save_results(file = '')

Writes current results to a JSON file. If no file is specified the default path is results_X.json with X being next 'free' integer.

Parameters:

NameTypeDescriptionOptional
filestringPath of file to write in.True

load_results()

load_results(path = 'results_0', reset = False)

Loads results from a JSON file.

Parameters:

NameTypeDescriptionOptional
filestringPath of file to load.True
resetboolWhether to erase current data.True

reset_data()

reset_data()

Erase all data inside NLP and reinitialise documents ids.

Parameters:

NameTypeDescriptionOptional
filestringPath of file to load.True
resetboolWhether to erase current data.True

add_client()

add_client(client = None, api_key = None)

Replaces current client with provided one, or creates a new client using the provided api_key.

Parameters:

NameTypeDescriptionOptional
clientinstance of Client classClient instance to replace the current one.True
api_keystringKey to use for the new client.True

register-sticky