Monday, June 7, 2010

Calculate gunning fog index in .Net





"Gunning fog" index is designed to measure the readability of English writing paragraphs. The result shows normally an estimate of the "number of years" of education he/she needs to understand the text on a first reading the text paragraph.



So here is the formula ...

 0.4\left( \left(\frac{\mbox{words}}{\mbox{sentence}}\right) + 100\left(\frac{\mbox{complex words}}{\mbox{words}}\right) \right )


Here is sample code to calculate gunning fog index written in VB.Net

//some variables
Dim inputText As String = txtInput.Text
Dim ltr As String
Dim wordCount As Integer
Dim sentenceCount As Integer
Dim syllableCount As Integer
Dim complexCount As Integer
Dim i As Integer
Dim inWord As Boolean = False
Dim vowels As String = "aeiouy"
TextBox1.Text = ""

' First make the input string case insensitive.
inputText = inputText.ToLower()
For i = 0 To inputText.Length() - 1
ltr = inputText.Substring(i, 1).ToLower
If ltr = "'" Then
   ' do nothing in case of apostrophe
ElseIf (ltr >= "a" And ltr <= "z") Then ' check for alphabetic input
If Not inWord Then ' new word
wordCount += 1 ' so add 1 to count
End If
inWord = True
Else ' could be space, full stop, comma etc
inWord = False ' no longer in word
' check if end of sentence
If (ltr = "." Or ltr = "?" Or ltr = "!" Or ltr = ":" Or ltr = ";") Then
sentenceCount += 1 ' add 1 to count
End If
End If
Next
Dim input As String = inputText.Length() - 1
Dim ch As Char
Dim inVowel As Boolean = False
' syllable count is zero if no input
If (input <>Then
syllableCount = 0
complexCount = 0
End If
' e at end of word doesn't count as a vowel
ch = inputText.Substring(inputText.Length - 1)
If (ch = "e") Then
input -= 1
End If
'For Each ch In inputText
' If (vowels.IndexOf(ch) >= 0) Then
' If Not inVowel Then
' syllableCount += 1
' End If
' inVowel = True
' End If
'Next
Dim word As String
Dim pieces As String()
'Replace all what is not needed here
inputText = inputText.Replace(".", " ")
inputText = inputText.Replace(":", " ")
inputText = inputText.Replace(";", " ")
inputText = inputText.Replace("?", " ")
inputText = inputText.Replace("!", " ")
inputText = inputText.Replace(",", "")
inputText = inputText.Replace("'", " ")
inputText = inputText.Replace(" ", " ")
'Now make pieces
pieces = inputText.Split(" ")
syllableCount = 0
complexCount = 0
For index As Integer = 0 To pieces.Length - 1
word = pieces(index)
word = word.ToLower().Trim()
Dim pattern As String = "[aeiouy]+"
'Handle vowels
Dim count As Integer = Regex.Matches(word, pattern).Count
If word.EndsWith("e") Then
count -= 1
End If
'some special cases
If word.EndsWith("cial") Or word.EndsWith("tia") Or word.EndsWith("cius") Or word.EndsWith("cious") Or word.EndsWith("giu") Or word.EndsWith("ion") Or word.EndsWith("iou") Then 'Or word.EndsWith("sia") Or word.EndsWith("ely") Then
count = 1
End If
syllableCount += count
If count <>Then
count = 1
End If
If count >= 3 Then
complexCount += 1
End If
Next
'Now display all results on GUI
TextBox1.Text += "Words: " & wordCount.ToString() & Environment.NewLine
TextBox1.Text += "Sentences: " & sentenceCount.ToString() & Environment.NewLine
TextBox1.Text += "Syllable: " & syllableCount.ToString() & Environment.NewLine
TextBox1.Text += "Complex Words: " & complexCount.ToString() & Environment.NewLine
TextBox1.Text += "Fog index: " & (0.4 * ((wordCount / sentenceCount) + (100 * (complexCount / wordCount)))).ToString() & Environment.NewLine
TextBox1.Text += "Flesch-Kincaid Reading Age: " & (206.876 - (1.015 * (wordCount / sentenceCount)) - (84.6 * (syllableCount / wordCount))).ToString() & Environment.NewLine

2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Put some more detail on the gunning fog index and also put comments on the source code too :)

    ReplyDelete