0

I'm a beginner in programming with python, and I've got a question which may have an easy answer.

So I have a dictionary of words which is imported from a .txt file, next my program asks you to type a sentence, and then it saves every word which you've typed into another list.

I have to write a program which checks if every word of a list named sentence_list is in the list which is named words. If the word is not present in the dictionary I have to put it in another list which is being filled by all the words that are mistyped or not in the dictionary.

For easier understanding, my program should work something like that:

Type your sentence:
My naeme is John, I lieve in Italy, which is beatiful country.
['naeme', 'lieve', 'beatiful']

This is what I could do until now:

words = open("dictionary.txt", encoding="latin2").read().lower().split()

sentence=input("Type your sentence: ")

import re

sentence_list = re.findall("\w+", sentence.lower())

I know I have to do something with for, but for is different in python that it is in Javascript, which I am familiar with.

Eric
  • 95,302
  • 53
  • 242
  • 374

2 Answers2

3

Using sets

You could use sets to find all of the words not in the dictionary list.

>>> set([1,2,3]).difference([2,3])
set([1])

Note that this will not include duplicates.

So for you, it would be something like this (if you need the result to be a list):

misspelled_word_list = list( set(sentence_list).difference(words) )

Using for

Since you are required to use for, here is an alternate (less efficient) approach:

misspelled_word_list = []
for word in sentence_list:
    if (not word in misspelled_word_list) and (not word in words):
        misspelled_word_list.append(word)

You just loop over the words in sentence_list and check whether they are in your words list or not.

Matthew Adams
  • 9,426
  • 3
  • 27
  • 43
  • Thanks, but I think it's required for me to use for –  Oct 20 '12 at 15:43
  • I guess I should have asked you what you'd tried first since this is pretty clearly homework... – Matthew Adams Oct 20 '12 at 15:52
  • It works like a charm, thanks. But now I have to make sure that the words in my list of misspelled words don't repeat.For example: My naem is John and his naem is Mark. Word naem should be in the list of misspelled words only once. I should use if? –  Oct 20 '12 at 16:18
  • Yes. I checked whether `word` wasn't in the list `words` by using `if not word in words`. How could you use similar code to check whether `word` wasn't in `misspelled_word_list`? – Matthew Adams Oct 20 '12 at 16:58
  • I wrote: if word in misspelled_word_list: misspelled_word_list.remove(word) but now the result is [countrey, naem] instead of [naem, countrey] –  Oct 20 '12 at 17:10
  • Right idea, I edited my answer to include the other `if` condition. – Matthew Adams Oct 20 '12 at 17:12
  • I finally did it. Thank you very much @MatthewAdams. What kind of literature would you recommend to me coming from javascript? I feel like a retard, thanks. –  Oct 20 '12 at 17:17
  • I'm not sure. A little googling turned up [this page](http://hg.toolness.com/python-for-js-programmers/raw-file/tip/PythonForJsProgrammers.html). I skimmed it and it seems accurate. – Matthew Adams Oct 20 '12 at 17:39
  • Not that the right person will see this, but why the unupvote? – Matthew Adams Oct 22 '12 at 07:59
1

Here's a one-liner using for, that generates a set of mispelled words:

mispelled = { word for word in sentence if word not in dictionary }

I've renamed your variables for clarity

Eric
  • 95,302
  • 53
  • 242
  • 374