NLTK庫中提供了一些用于評估文本可讀性的方法。下面是一個簡單的示例代碼,演示如何使用NLTK庫中的textstat
模塊來評估文本的可讀性:
import nltk
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
from nltk.text import Text
from textstat.textstat import textstat
# 載入文本
text = "This is a sample text to test readability using NLTK library."
# 分詞
tokens = word_tokenize(text)
# 去除停用詞
stop_words = set(stopwords.words('english'))
filtered_tokens = [word for word in tokens if word.lower() not in stop_words]
# 創建NLTK文本對象
text_nltk = Text(filtered_tokens)
# 計算文本可讀性指標
flesch_reading_ease = textstat.flesch_reading_ease(text)
automated_readability_index = textstat.automated_readability_index(text)
coleman_liau_index = textstat.coleman_liau_index(text)
# 打印結果
print("Flesch Reading Ease Score:", flesch_reading_ease)
print("Automated Readability Index:", automated_readability_index)
print("Coleman-Liau Index:", coleman_liau_index)
運行上述代碼后,將輸出文本的Flesch Reading Ease Score(弗萊施閱讀易度分數)、Automated Readability Index(自動可讀性指數)和Coleman-Liau Index(科爾曼-利奧指數)等可讀性指標。根據這些指標的數值,可以評估文本的可讀性水平。