58 lines
2.7 KiB
Python
58 lines
2.7 KiB
Python
"""Признаки пунктуации и оформления высказывания."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import re
|
|
|
|
import pandas as pd
|
|
|
|
from .base import as_list, safe_div, word_tokens
|
|
|
|
|
|
def extract(row: pd.Series) -> dict[str, float]:
|
|
text = str(row.get("text", ""))
|
|
sentences = as_list(row, "sentences")
|
|
n_sentences = max(len(sentences), 1)
|
|
n_words = max(len(word_tokens(as_list(row, "tokens"))), 1)
|
|
|
|
ellipsis_count = len(re.findall(r"\.{3,}", text))
|
|
single_periods = len(re.findall(r"(?<!\.)\.(?!\.)", text))
|
|
exclamations = text.count("!")
|
|
questions = text.count("?")
|
|
commas = text.count(",")
|
|
colons = text.count(":")
|
|
semicolons = text.count(";")
|
|
apostrophes = text.count("'")
|
|
quotes = text.count('"')
|
|
dashes = text.count("-")
|
|
parentheses = text.count("(") + text.count(")")
|
|
multi_exclamation = len(re.findall(r"!{2,}", text))
|
|
multi_question = len(re.findall(r"\?{2,}", text))
|
|
mixed = len(re.findall(r"[!?]{2,}", text))
|
|
|
|
sentence_texts = [" ".join(sentence) for sentence in sentences if sentence]
|
|
question_sentences = sum(1 for sentence in sentence_texts if sentence.rstrip().endswith("?"))
|
|
exclamation_sentences = sum(1 for sentence in sentence_texts if sentence.rstrip().endswith("!"))
|
|
|
|
return {
|
|
"punct_comma_per_sentence": safe_div(commas, n_sentences),
|
|
"punct_period_per_sentence": safe_div(single_periods, n_sentences),
|
|
"punct_exclamation_per_sentence": safe_div(exclamations, n_sentences),
|
|
"punct_question_per_sentence": safe_div(questions, n_sentences),
|
|
"punct_colon_per_sentence": safe_div(colons, n_sentences),
|
|
"punct_semicolon_per_sentence": safe_div(semicolons, n_sentences),
|
|
"punct_ellipsis_per_sentence": safe_div(ellipsis_count, n_sentences),
|
|
"punct_apostrophe_per_sentence": safe_div(apostrophes, n_sentences),
|
|
"punct_quote_per_sentence": safe_div(quotes, n_sentences),
|
|
"punct_dash_per_sentence": safe_div(dashes, n_sentences),
|
|
"punct_parentheses_per_sentence": safe_div(parentheses, n_sentences),
|
|
"punct_multi_exclamation_per_sentence": safe_div(multi_exclamation, n_sentences),
|
|
"punct_multi_question_per_sentence": safe_div(multi_question, n_sentences),
|
|
"punct_mixed_per_sentence": safe_div(mixed, n_sentences),
|
|
"punct_question_sentence_rate": safe_div(question_sentences, n_sentences),
|
|
"punct_exclamation_sentence_rate": safe_div(exclamation_sentences, n_sentences),
|
|
"punct_comma_per_token": safe_div(commas, n_words),
|
|
"punct_ellipsis_per_token": safe_div(ellipsis_count, n_words),
|
|
"punct_apostrophe_per_token": safe_div(apostrophes, n_words),
|
|
}
|