lab_4_summarization_textrank package

Submodules

Lab 4 Summarize text using TextRank algorithm

class lab_4_summarization_textrank.main.EncodedSentence

Type alias representing an encoded sentence as a tuple of integers.

class lab_4_summarization_textrank.main.PreprocessedSentence

Type alias representing a preprocessed sentence as a tuple of strings.

exception lab_4_summarization_textrank.main.IncorrectArgumentError

Bases: ValueError

Raised when a provided input has an invalid type or value.

class lab_4_summarization_textrank.main.NaiveSummarizer(graph)

Bases: object

Naive graph summarizer and shared interface for graph-based algorithms.

Parameters:

graph (TextGraph)

_graph

Graph of sentence vertices and their relations.

Type:

TextGraph

__init__(graph)

Initializes a graph summarizer with a similarity graph.

Parameters:

graph (TextGraph) – Built graph of encoded sentences.

Return type:

None

__str__()

Returns a string representation of the summarizer.

Returns:

String representation of the summarizer.

Return type:

str

_get_top_n_sentences(n)

Returns the top n sentences with the highest significance scores.

Parameters:

n (int) – Number of top sentences to return.

Returns:

Top n sentences with the highest significance scores.

Return type:

tuple[Sentence, …]

summarize(n_sentences)

Returns a summary from the current sentence scores.

Parameters:

n_sentences (int) – Number of sentences to include.

Returns:

Selected sentences in source order.

Return type:

str

class lab_4_summarization_textrank.main.Sentence(text, position)

Bases: object

An abstraction over a real-world sentence.

Parameters:
_text

The text of the sentence.

Type:

str

_position

The position of the sentence in the text.

Type:

int

_preprocessed

Preprocessed representation of the sentence.

Type:

PreprocessedSentence

_encoded

Encoded representation of the sentence.

Type:

EncodedSentence

__eq__(other)

Checks equality between two Sentence instances.

Parameters:

other (object) – Another object to compare.

Returns:

True if both are Sentence instances with the same text and position,

otherwise False.

Return type:

bool

__hash__()

Returns a hash value for the Sentence instance.

Returns:

Hash value based on the text and position.

Return type:

int

__init__(text, position)

Initializes a sentence with its text and position.

Parameters:
  • text (str) – Text of the sentence.

  • position (int) – Position of the sentence in the text.

Return type:

None

__str__()

Returns a string representation of the sentence.

Returns:

String representation of the sentence.

Return type:

str

get_encoded()

Returns the encoded representation of the sentence.

Returns:

Encoded sequence of integers.

Return type:

EncodedSentence

get_position()

Returns the position of the sentence in the text.

Returns:

Position of the sentence.

Return type:

int

get_preprocessed()

Returns the preprocessed representation of the sentence.

Returns:

Preprocessed sequence of tokens.

Return type:

PreprocessedSentence

get_text()

Returns the text of the sentence.

Returns:

Text of the sentence.

Return type:

str

set_encoded(encoded_sentence)

Sets the encoded representation of the sentence.

Parameters:

encoded_sentence (EncodedSentence) – Encoded sequence of integers.

Return type:

None

set_preprocessed(preprocessed_sentence)

Sets the preprocessed representation of the sentence.

Parameters:

preprocessed_sentence (PreprocessedSentence) – Preprocessed sequence of tokens.

Return type:

None

class lab_4_summarization_textrank.main.SentenceEncoder

Bases: object

A class to encode string sequences into matching integer sequences.

_word2id

Maps words to integers.

Type:

dict[str, int]

_id2word

Maps integers to words.

Type:

dict[int, str]

last

Counter for the last assigned identifier.

Type:

int

__init__()

Initializes the sentence encoder.

Return type:

None

__str__()

Returns a string representation of the encoder.

Returns:

String representation of the encoder.

Return type:

str

_learn_indices(tokens)

Creates mappings between words and their integer equivalents.

Parameters:

tokens (Sequence[str]) – Sequence of string tokens.

Return type:

None

encode_sentences(sentences)

Encodes the preprocessed tokens of each sentence.

Parameters:

sentences (Sequence[Sentence]) – Sequence of sentences to encode.

Return type:

None

class lab_4_summarization_textrank.main.SentencePreprocessor(stop_words, punctuation)

Bases: object

Class for sentence preprocessing.

Parameters:
_stop_words

Insignificant words to remove from tokens.

Type:

Sequence[str]

_punctuation

Punctuation symbols to remove during text cleaning.

Type:

Sequence[str]

__init__(stop_words, punctuation)

Initializes the text preprocessor.

Parameters:
  • stop_words (Sequence[str]) – Insignificant words to remove from tokens.

  • punctuation (Sequence[str]) – Punctuation symbols to remove during text cleaning.

Return type:

None

__str__()

Returns a string representation of the preprocessor.

Returns:

String representation of the preprocessor.

Return type:

str

_clean_and_tokenize(text)

Removes punctuation, converts text to lowercase, and splits it into tokens.

Parameters:

text (str) – Raw text.

Returns:

Clean lowercase tokens.

Return type:

tuple[str, …]

_preprocess_sentences(sentences)

Preprocesses a sequence of sentences and stores their token representations.

Parameters:

sentences (Sequence[Sentence]) – Sequence of sentences to preprocess.

Return type:

None

_remove_stop_words(tokens)

Removes stop words from a sequence of tokens.

Parameters:

tokens (Sequence[str]) – Sequence of tokens containing stop words.

Returns:

Sequence of tokens without stop words.

Return type:

tuple[str, …]

_split_by_sentence(text)

Splits the provided text into sentences.

Parameters:

text (str) – Raw text to split.

Returns:

Sequence of sentences extracted from the text.

Return type:

tuple[Sentence, …]

get_sentences(text)

Splits the provided text into sentences and preprocesses them.

Parameters:

text (str) – Raw text to split and preprocess.

Returns:

Sequence of preprocessed sentences extracted from the text.

Return type:

tuple[Sentence, …]

preprocess_text(text)

Produces filtered, clean, lowercase tokens from raw text.

Parameters:

text (str) – Raw text.

Returns:

Clean lowercase tokens without stop words.

Return type:

tuple[str, …]

class lab_4_summarization_textrank.main.TextGraph

Bases: object

A class to represent relations between sentences.

_vertices

List of sentence vertices in the graph.

Type:

list[Sentence]

_matrix

Similarity matrix representing edges between vertices.

Type:

list[list[float]]

__init__()

Initializes an empty similarity graph.

Return type:

None

__len__()

Returns the number of sentence vertices in the graph.

Returns:

Number of vertices.

Return type:

int

__str__()

Returns a short description of the graph.

Returns:

Description of the graph.

Return type:

str

_add_edge(vertex1, vertex2)

Adds an edge between two vertices in the graph.

Parameters:
Return type:

None

_add_vertex(vertex)

Adds a vertex and extends the matrix without computing similarity scores.

Parameters:

vertex (Sentence) – Vertex to add to the graph.

Return type:

None

build(sentences)

Builds the graph from preprocessed and encoded sentences.

Parameters:

sentences (Sequence[Sentence]) – Sequence of sentences to add.

Return type:

None

calculate_inout_score(vertex)

Calculates the number of vertices similar to the specified vertex.

Parameters:

vertex (Sentence) – Vertex whose similar vertices should be counted.

Returns:

Number of similar vertices.

Return type:

int

get_similarity_score(vertex1, vertex2)

Returns the similarity score between two vertices.

Parameters:
Returns:

Similarity score between the two vertices.

Return type:

float

get_top_n_sentences(n)

Returns the top n sentences with the highest sum of similarity scores.

Parameters:

n (int) – Number of top sentences to return.

Returns:

Top n sentences with the highest similarity score sum.

Return type:

tuple[Sentence, …]

get_vertices()

Returns all vertices present in the graph.

Returns:

Sequence of vertices in the graph.

Return type:

tuple[Sentence, …]

exception lab_4_summarization_textrank.main.TextGraphError

Bases: IncorrectArgumentError

Raised when an invalid graph operation or vertex lookup occurs.

class lab_4_summarization_textrank.main.TextRankSummarizer(graph)

Bases: NaiveSummarizer

TextRank algorithm for text summarization.

Parameters:

graph (TextGraph)

_damping_factor

Damping factor for score calculation.

Type:

float

_convergence_threshold

Threshold for convergence of scores.

Type:

float

_max_iter

Maximum number of iterations for score calculation.

Type:

int

_scores

Significance scores for each vertex.

Type:

dict[Sentence, float]

__init__(graph)

Initializes a TextRank summarizer with a similarity graph.

Parameters:

graph (TextGraph) – Built graph of encoded sentences.

Return type:

None

__len__()

Returns the number of vertices in the graph.

Returns:

Number of vertices in the graph.

Return type:

int

__str__()

Returns a string representation of the TextRank summarizer.

Returns:

String representation of the TextRank summarizer.

Return type:

str

_get_top_n_sentences(n)

Returns the top n sentences with the highest significance scores.

Parameters:

n (int) – Number of top sentences to return.

Returns:

Top n sentences with the highest significance scores.

Return type:

tuple[Sentence, …]

_update_vertex_score(vertex, incidental_vertices, current_scores)

Updates a vertex significance score using the TextRank formula.

Parameters:
  • vertex (Sentence) – Sentence whose score should be updated.

  • incidental_vertices (Sequence[Sentence]) – Vertices related to the sentence.

  • current_scores (dict[Sentence, float]) – Current scores of all vertices.

Return type:

None

train()

Iteratively calculates significance scores for all vertices until convergence.

Return type:

None

lab_4_summarization_textrank.main.calculate_similarity(sequence, other_sequence)

Calculates the similarity between two sequences using the Jaccard index.

Parameters:
  • sequence (Sequence) – First sequence of items.

  • other_sequence (Sequence) – Second sequence of items.

Returns:

Jaccard similarity score between the two sequences.

Return type:

float