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:
ValueErrorRaised when a provided input has an invalid type or value.
- class lab_4_summarization_textrank.main.NaiveSummarizer(graph)
Bases:
objectNaive graph summarizer and shared interface for graph-based algorithms.
- Parameters:
graph (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:
- _get_top_n_sentences(n)
Returns the top n sentences with the highest significance scores.
- class lab_4_summarization_textrank.main.Sentence(text, position)
Bases:
objectAn abstraction over a real-world sentence.
- _preprocessed
Preprocessed representation of the sentence.
- Type:
- _encoded
Encoded representation of the sentence.
- Type:
- __eq__(other)
Checks equality between two Sentence instances.
- __hash__()
Returns a hash value for the Sentence instance.
- Returns:
Hash value based on the text and position.
- Return type:
- __init__(text, position)
Initializes a sentence with its text and position.
- __str__()
Returns a string representation of the sentence.
- Returns:
String representation of the sentence.
- Return type:
- get_encoded()
Returns the encoded representation of the sentence.
- Returns:
Encoded sequence of integers.
- Return type:
- get_position()
Returns the position of the sentence in the text.
- Returns:
Position of the sentence.
- Return type:
- get_preprocessed()
Returns the preprocessed representation of the sentence.
- Returns:
Preprocessed sequence of tokens.
- Return type:
- 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:
objectA class to encode string sequences into matching integer sequences.
- __init__()
Initializes the sentence encoder.
- Return type:
None
- __str__()
Returns a string representation of the encoder.
- Returns:
String representation of the encoder.
- Return type:
- class lab_4_summarization_textrank.main.SentencePreprocessor(stop_words, punctuation)
Bases:
objectClass for sentence preprocessing.
- __init__(stop_words, punctuation)
Initializes the text preprocessor.
- __str__()
Returns a string representation of the preprocessor.
- Returns:
String representation of the preprocessor.
- Return type:
- _clean_and_tokenize(text)
Removes punctuation, converts text to lowercase, and splits it into tokens.
- _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.
- _split_by_sentence(text)
Splits the provided text into sentences.
- get_sentences(text)
Splits the provided text into sentences and preprocesses them.
- class lab_4_summarization_textrank.main.TextGraph
Bases:
objectA class to represent relations between sentences.
- __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:
- __str__()
Returns a short description of the graph.
- Returns:
Description of the graph.
- Return type:
- _add_edge(vertex1, vertex2)
Adds an edge between two vertices in the graph.
- _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.
- get_similarity_score(vertex1, vertex2)
Returns the similarity score between two vertices.
- get_top_n_sentences(n)
Returns the top n sentences with the highest sum of similarity scores.
- exception lab_4_summarization_textrank.main.TextGraphError
Bases:
IncorrectArgumentErrorRaised when an invalid graph operation or vertex lookup occurs.
- class lab_4_summarization_textrank.main.TextRankSummarizer(graph)
Bases:
NaiveSummarizerTextRank algorithm for text summarization.
- Parameters:
graph (TextGraph)
- __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:
- __str__()
Returns a string representation of the TextRank summarizer.
- Returns:
String representation of the TextRank summarizer.
- Return type:
- _get_top_n_sentences(n)
Returns the top n sentences with the highest significance scores.
- _update_vertex_score(vertex, incidental_vertices, current_scores)
Updates a vertex significance score using the TextRank formula.
- 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: