AbdurRahim Sharif February 2016

How to check if sentence contains words only from a file

I am trying to create a program that checks if a string entered by the user contains only words from a text file, which will contain all words from the English dictionary. This will remove any slang language. If you have any other way of doing this, please let me know as I am relatively new to python. Thanks in advance.

Answers


Carlos February 2016

  1. Take the dictionary, put all the words from it in some sort of hash set.
  2. Take the sentence, split it into words, hash each word. Check if the hash occurs in the hash set.


Jason Heddle February 2016

First, read all the words into a string, then separate the words into a list splitting by spaces:

words = []

with open('data.txt', 'r') as myfile:
     data = myfile.read().replace('\n', '')

words = data.split()

Then check if your word is in the list:

if checkWord.lower() in words:
    wordCheck = True


Kazi Nazmul Haque Shezan February 2016

def slang_remover(PATH_TO_DICTIONARY_TEXT_FILE):

    #Opening the Dictionary textfile which contain all dictionary words except slangs.
    mydictionaryfile=open(PATH_TO_DICTIONARY_TEXT_FILE)

    #Reading the whole dictionary as a text.
    alltext_from_dictionary=mydictionaryfile.read()

    #Getting the sentence from the User.
    user_sentence=raw_input("Give me the sentence!")

    #Spliting User sentence into word so that check for slang and remove it.
    splited_user_sentence=user_sentence.split()
    slang_removed_splited_user_sentence=[word for word in splited_user_sentence if word in alltext_from_dictionary ]
    slang_removed_user_sentence=' '.join(slang_removed_splited_user_sentence)

    print "Here is the slang removed sentence"
    return slang_removed_user_sentence

Call it like: slang_remover("dictionary.txt")

Post Status

Asked in February 2016
Viewed 3,647 times
Voted 10
Answered 3 times

Search




Leave an answer