Python中的可讀性指數(NLP)?
自然語言處理是研究自然人類語言的自動化生成和理解。隨著計算機技術融入當今幾乎所有行業,這正成為越來越有趣的問題。我們將研究自然語言處理中的一個特定領域:可讀性。這涉及到確定文字可讀性的主題。這表明閱讀或理解文字的難易程度。
可讀性指數是一個數值,表示閱讀和理解文字的難易程度。有幾種不同的可讀性測試,它們有不同的應用領域。
"可讀性描述了文件易於閱讀的程度" [13]。存在許多不同的測試 [9] 來計算可讀性。可讀性測試“被認為是對閱讀難易程度的預測,但不是確定可讀性的唯一方法”。
有些測試是語言中性的,但有些測試更適合某些語言。瞭解不同的可讀性測試對我們至關重要。
| 可讀性測試 − | 目標語言 − | 簡短描述和公式 − |
|---|---|---|
| 自動化可讀性指數 (ARI) | 英語 | 旨在衡量文字的可理解性。輸出是理解文字所需的大致美國年級水平。ARI = 4.71 * (characters/words) + 0.5 * (words/sentence) -21.43 |
弗萊施可讀性易度指數 | 英語 | 旨在指示一段文字理解的難易程度。分數越高,表示材料越容易閱讀;分數越低,表示越難閱讀。FRE = 206.835 − 1.015*(total words/ total sentences) − 84.6 * (total syllables/ total words) |
| 弗萊施-金凱德等級 | 英語 | 旨在指示一段文字理解的難易程度。結果是一個與美國年級水平相對應的數字。FKGL = 0.39 * (total words/ total sentences) + 11.8 (total syllables/ total words) -15.59 |
| 科爾曼-利亞烏指數 | 英語 | 旨在衡量文字的可理解性。輸出是理解文字所需的大致美國年級水平。CLI = (5.89 * (characters/ words)) − (30 *(sentences/words)) − 15.8 |
| 岡寧霧度指數 | 英語 | 旨在衡量英語書寫樣本的可讀性。所得指數表示一個人需要多少年的正規教育(美國年級)才能在第一次閱讀時輕鬆理解文字。GFI = 0.4 * (( words/ sentence) + 100 * (complex words/ words)) |
| Linsear Write | 英語 | 一種英語文字的可讀性指標,由空軍開發,以幫助他們計算其技術手冊的可讀性。公式來自維基百科。
|
| 可讀性指數 (RIX) | 西歐語言 | 這很有用,因為它可以用於任何西歐語言的文件 [3]。輸出的分數在0(非常容易)到55+(非常困難)之間。RIX = (Long Words/ Sentences) (long words = words where number of characters > 6) |
| Lesbarhets Index (LIX) | 西歐語言 | 這很有用,因為它可以用於任何西歐語言的文件 [2][3]。輸出的指數表示一個年級水平。指數低於0.1為一年級,而7.2及以上為大學水平。LIX = (total words/ total sentences) + (long words/ total words * 100) (long words = words where number of characters > 6) |
例如,以下是透過弗萊施指數確定文字檔案可讀性的程式。
假設
| 弗萊施指數 | 文字檔案閱讀年級 |
|---|---|
| 0-30 | 大學 |
| 50-60 | 高中 |
| 90-100 | 四年級 |
從上文中,弗萊施-金凱德等級公式用於計算等效的年級水平 G −
FKGL = 0.39 * (總詞數/總句數) + 11.8 (總音節數/總詞數) -15.59
程式碼
import os
dire = os.getcwd()
listOfdir = os.listdir(dire)
while True:
UserFileName = input('Enter file name:')
if (UserFileName in listOfdir) and (UserFileName.endswith(".txt")):
InputFile = open(UserFileName,'r')
text = InputFile.read()
sentence = text.count('.') + text.count('!') + text.count(';') + text.count(':') + text.count('?')
words = len(text.split())
syllable = 0
for word in text.split():
for vowel in ['a','e','i','o','u']:
syllable + = word.count(vowel)
for ending in ['es','ed','e']:
if word.endswith(ending):
syllable - = 1
if word.endswith('le'):
syllable + = 1
G = round((0.39*words)/sentence+ (11.8*syllable)/words-15.59)
if G > = 0 and G < = 30:
print ('The Readability level is College')
elif G > = 50 and G < = 60:
print ('The Readability level is High School')
elif G > = 90 and G < = 100:
print ('The Readability level is fourth grade')
print ('This text has %d words' %(words))
elif UserFileName not in listOfdir:
print ('This text file does not exist in current directory')
elif not(UserFileName.endswith('.txt')):
print ('This is not a text file.')輸出
Enter file name:dataVisualization.txt The Readability level is College This text has 64 words
廣告
資料結構
網路
關係資料庫管理系統(RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP