0

I am trying to produce a counter that will tell me how many words are wrong in an essay. I want to add one to counter if check is returned false. Here is what I have EDIT: Essay is a list of words. I took an essay, took out punctuation, made all the letters lower case and then made a list of each individual word. I now want to write a loop that checks each word to see if is proper. It is not, I want a counter that will return how many words are wrong

I have searched around, but can't figure out how to apply stuff to this. I haven't found what will work

The errors I get I run the num_spell_error line ** (python.exe:7804): CRITICAL **: enchant_dict_check: assertion `g_utf8_validate(word, len, NULL)' failed Traceback (most recent call last): File "", line 1, in File "E:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 538, in runfile execfile(filename, namespace) File "C:/Documents and Settings/stephen_beckham/.spyder2/admissions.py", line 49, in num_spel_errs_why = len(whybaylor) - len(filter(dictionary.check, whybaylor)) File "E:\Python27\lib\site-packages\enchant__init__.py", line 577, in check self._raise_error() File "E:\Python27\lib\site-packages\enchant__init__.py", line 551, in _raise_error raise eclass(default) enchant.errors.Error: Unspecified Error

The error I get when I try the for word loop

** (python.exe:7804): CRITICAL **: enchant_dict_check: assertion `g_utf8_validate(word, len, NULL)' failed Traceback (most recent call last): File "", line 1, in File "E:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 538, in runfile execfile(filename, namespace) File "C:/Documents and Settings/stephen_beckham/.spyder2/admissions.py", line 51, in if dictionary.check(word) is False: File "E:\Python27\lib\site-packages\enchant__init__.py", line 577, in check self._raise_error() File "E:\Python27\lib\site-packages\enchant__init__.py", line 551, in _raise_error raise eclass(default) enchant.errors.Error: Unspecified Error

from __future__ import division import csv import re from string import punctuation import enchant

faithwords = ['church', 'christ', 'faith']

dictionary = enchant.Dict("en_US")

with open('2011ShortAnswers.csv', 'rb') as csvfile: data = csv.reader(csvfile, delimiter=",")

writer = csv.writer(open('2011output.csv', 'wb'))

for row in data:

    faithcounter = 0
    grammercounter = 0

    row3 = row[3]
    row3 = row3.lower().replace('  ', ' ')
    row4 = row[4]
    row4 = row4.lower().replace('  ', ' ')

    essay1_sentence = re.split('.', row3)
    essay2_sentence = re.split('.', row4)
    essay1_sentencelen = len(essay1_sentence)
    essay2_sentencelen = len(essay2_sentence)

    for p in list(punctuation):
        row3 = row3.replace(p, '')
        row4 = row4.replace(p, '')

    essay1 = re.split(' ', row3)
    essay2 = re.split(' ', row4)

    essay1len = len(essay1)
    essay2len = len(essay2)

   num_spel_errs_why = len(essay1) - len(filter(dictionary.check, essay1))
    for word in essay1:
        if dictionary.check(word) is False:
            grammercounter = grammercounter + 1
  • 1
    Welcome to StackOverflow! I'm not sure what you're asking here -- can you give us an example of what you'd expect? – Christian Ternus Oct 24 '13 at 16:02
  • 1
    Should it also count misspelling *grammar* as *grammer*? ;) – kojiro Oct 24 '13 at 16:04
  • What is `essay`'s type? Is it already a list of words? If it's a string, you should split it in separate words first. – Nadir Sampaoli Oct 24 '13 at 16:06
  • `num_spel_errs = len(essay) - len(filter(dictionary.check, essay))` – kojiro Oct 24 '13 at 16:11
  • Let's see.. everything looks fine. What happen if you print `grammercounter`? – aIKid Oct 24 '13 at 16:19
  • With your code kojiro, I get an error File "C:/Documents and Settings/stephen_beckham/.spyder2/admissions.py", line 49, in num_spel_errs_why = len(whybaylor) - len(filter(dictionary.check, whybaylor)) – user2884824 Oct 24 '13 at 16:19
  • 1
    @user2884824 hard to say without being able to see more of the traceback. – kojiro Oct 24 '13 at 16:22
  • Does this throw an error or not? If it is, can you provide the traceback? – aIKid Oct 24 '13 at 16:39
  • This error is related to UTF-8 encoding, might possible the string which u r passing in dictionary.check(word) is not a valid unicode string. One more suggestion change condition dictionary.check(word) to (len(word)>0 and dictionary.check(word)). – Saurabh Jain Jul 05 '16 at 15:34

0 Answers0