Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

ดัชนีความอ่านง่าย (Readability Index) ใน Python NLP

Natural Language Processing (NLP) ศึกษาการสร้างและทำความเข้าใจภาษามนุษย์โดยอัตโนมัติ หนึ่งหัวข้อสนุกคือ Readability (ความอ่านง่าย) ว่าข้อความยากหรือง่ายในการอ่านและเข้าใจเพียงใด

Readability Index คือค่าตัวเลขบ่งชี้ความยากง่ายของข้อความ มีหลายสูตรทดสอบ แต่ละสูตรเหมาะกับภาษา/วัตถุประสงค์ต่างกัน

ตารางเปรียบเทียบสูตร Readability ยอดนิยม

สูตรภาษาคำอธิบายและสูตร
Automated Readability Index (ARI) English วัดความเข้าใจง่าย ผลลัพธ์ ≈ ระดับชั้นสหรัฐที่ต้องการ
ARI = 4.71*(chars/words) + 0.5*(words/sentences) - 21.43
Flesch Reading Ease English คะแนนสูง = อ่านง่าย ต่ำ = อ่านยาก
FRE = 206.835 - 1.015*(words/sentences) - 84.6*(syllables/words)
Flesch-Kincaid Grade Level English ผลลัพธ์ = ระดับชั้นสหรัฐ
FKGL = 0.39*(words/sentences) + 11.8*(syllables/words) - 15.59
Coleman-Liau Index English ใช้ตัวอักษรแทนพยางค์
CLI = 5.89*(chars/words) - 30*(sentences/words) - 15.8
Gunning Fog Index English ปีการศึกษาที่ต้องการ
GFI = 0.4*((words/sentences) + 100*(complex_words/words))
Linsear Write English สำหรับคู่มือทางเทคนิค ใช้คำง่าย (≤2 พยางค์) และยาก (≥3 พยางค์)
RIX Western European RIX = (Long words / Sentences) (Long words = ตัวอักษร > 6)
LIX Western European LIX = (words/sentences) + (long_words/words * 100)

ตัวอย่าง: คำนวณ Flesch-Kincaid Grade Level

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
        
        # คำนวณ Grade Level
        G = round((0.39*words)/sentence + (11.8*syllable)/words - 15.59)
        
        if 0 <= G <= 30:
            print('The Readability level is College')
        elif 50 <= G <= 60:
            print('The Readability level is High School')
        elif 90 <= 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

การประยุกต์ใช้

  • ตรวจสอบความเหมาะสมของเนื้อหาสำหรับกลุ่มเป้าหมาย
  • ปรับปรุงการเขียนให้อ่านง่ายขึ้น
  • จัดหมวดหมู่เอกสารตามระดับความยาก
  • วิจัยทางภาษาศาสตร์และการศึกษา

สำหรับภาษาไทย ต้องปรับสูตรให้เหมาะสม เนื่องจากโครงสร้างภาษาแตกต่าง (ไม่มีช่องว่างระหว่างคำ ไม่มีพยางค์ชัดเจนเหมือนอังกฤษ)