Код считающий FWR, TTR и FREНаписал код, который считает для английского текста Function Word Ratio, Type-Token Ratiо и Flesch Reading Ease.
!pip install spacy_syllables
!python -m spacy download en_core_web_sm
import spacy
from spacy_syllables import SpacySyllables
# Load the spaCy model
nlp = spacy.load('en_core_web_sm')
nlp.add_pipe('syllables', after='tagger')
def calculate_fwr_ttr_fre(text):
# Process the text with spaCy to tokenize and apply other NLP tasks
doc = nlp(text.lower())
# Count total words, sentences, syllables, and function words
total_words = len(doc)
total_sentences = len(list(doc.sents))
total_syllables = sum(token._.syllables_count for token in doc if token._.syllables_count is not None)
function_words_count = sum(token.is_stop for token in doc)
# Calculate Function Words Ratio
fwr = function_words_count / total_words if total_words > 0 else 0
# Calculate Type-Token Ratio
unique_words = set(token.text for token in doc)
ttr = len(unique_words) / total_words if total_words > 0 else 0
# Calculate Flesch Reading Ease
fre = (206.835 - 1.015 * (total_words / total_sentences) - 84.6 * (total_syllables / total_words)) if total_words > 0 and total_sentences > 0 else 0
return fwr, ttr, fre
# Change filename in code line below
with open('filename.txt', 'r') as file:
# Read the contents of the file
text = file.read()
fwr, ttr, fre = calculate_fwr_ttr_fre(text)
print("Function Words Ratio:", fwr)
print("Type-Token Ratio:", ttr)
print("Flesch Reading Ease:", fre)
В системе должна быть установлена библиотека
spacy. Либо вы можете воспользоваться ссылкой на Colab и запустить скрипт в облаке Google.