How to rewrite code - python-3.x

My comment is down below with the code
My program censors words. It works for both one word and many words. I was having trouble making the program work for many words. It would print out the sentence with the space censored too. I found code to make it work though but do not understand it.
sentence = input("Enter a sentence:")
word = input("Enter a word to replace:")
words = word
def censorWord(sentence,word):
# I would like to rewrite this code in a way I can understand and read clearer.
return " ".join(["-"*len(item) if item in word else item for item in sentence.split()])
def censorWords(sentence,words):
words1 = words.split()
for w in words1:
if w in sentence:
return replaceWord(sentence,word)
print(censorWords(sentence,words))

def censorWord (sentence, word):
result = [] #list to store the new sentence words
eachword = sentence.split() #splits each word in the sentence and store in a seperate array element
for item in eachword: #iterates the list until last word
if item == word: #if current list item matches the given word then insert - for length of the word
item = "-"*len(word)
result.append(item) #add the word to the list
return " ".join(result) #join all the words in the list with space in between

You can rewrite:
s = " ".join(["-" * len(item) if item in word else item for item in sentence.split()])
Into:
arr = []
for item in sentence.split():
if item in word:
arr.append("-" * len(item))
else:
arr.append(item)
s = " ".join(arr)
It basically splits sentence by spacing. Then if the current item is in word, then it gets replaced with it's own length in hyphens.

You seem to be a bit confused censorWord() is censoring all words in the sentence and censorWords() looks like it is trying to do the same thing but returns in the middle of processing. Just looking at censorWord():
More descriptive variable naming and breaking the one liner down would probably make it clearer, e.g.:
def redact(word):
return '-'*len(word)
def censorWord(sentence, censored_words):
words = sentence.split()
return " ".join([redact(word) if word in censored_words else word for word in words])
You can always turn this into a for loop but list comprehensions are a common part of python and you should get comfortable with them:
def censorWord(sentence, censored_words):
words = sentence.split()
clean_sentence = []
for word in words:
if word in censored_words:
clean_sentence.append(redact(word))
else:
clean_sentence.append(word)
return " ".join(clean_sentence)

Related

Compressing a sentence in python

so i have this task for school and it states:
develop a program that identifies individual words in a sentence, stores these in a list and replaces each word with the position of that word in the list. The sentence has to inputted by the user of the program.
The example we have to use is:
ask not what your country can do for you ask what you can do for your country
should become:
1,2,3,4,5,6,7,8,9,1,3,9,6,7,8,4,5
I have no idea how to even go about starting this task
You could do something like that:
sentence = input().split(' ')
word_list = []
for i, word in enumerate(sentence, start=1):
if word not in word_list:
word_list.append(word)
to_print = str(len(word_list) - 1)
else:
to_print = str(word_list.index(word))
print(to_print, end=',') if i < len(sentence) else print(to_print, end='')

Finding the position of two words that are the same in a list [duplicate]

This question already has answers here:
How to find all occurrences of an element in a list
(18 answers)
Closed 6 years ago.
So I created this code to ask a person to input a sentence.
After that they type a word in that sentence.
Then the code will output the position that word is in.
print("Type a sentence here")
sentence = input("")
sentence = sentence.split()
print("Now type a word in that sentence.")
word = input('')
if word in sentence:
print("I found",word,"in your sentence.")
else:
print("That word is not in your sentence.")
print(sentence.index(word))
The problem I am having is that if they put two of the same word in the sentence it only outputs the first one. Please can you help.
You could use the built-in enumerate to associate every word in your list sentence with its corresponding position. Then use a list comprehension to get every occurrence of the word in the list.
print([i for i, j in enumerate(sentence) if j == word])
Some further considerations would be that maybe you want to convert your sentence to lower case and remove punctuation before attempting to match your word, such that proper punctuation and capitalization will not trip up your matching. Further, you don't need the '' in input() in order for it to be valid - an empty input() without a prompt is fine.
This pb is solved by this script :
import re
print("Type a sentence here")
sentence = raw_input("")
print("Now type a word in that sentence.")
word = raw_input('')
words = re.sub("[^\w]", " ", sentence).split() # using re
position = 1
list_pos = []
for w in words :
if w == word:
print position
list_pos.append(position)
position += 1
if list_pos:
print("I found",word,"in your sentence.")
else:
print("That word is not in your sentence.")
print list_pos

How to get the words before and after a specific word from a textfile

I defined this function to find the keywords in a text file but, now i want to get a tuple containing the words before and after the key word in the file and i am not sure of how to do that
def findProperWords(paragraphs, excludedWords):
key = [] # an empty list
count = 0
for paragraph in paragraphs: #calling of every paaragraph in the textfile
count += 1 #counting each paragraph
words = list(paragraph.split(' '))
# spliting each paragraph into a list of words
for keys in words:
if len(keys) > 0:
if keys[0] == keys[0].upper():
#checking for words that start with capital letters
if keys.lower() not in excludedWords:
key.append(keys) # creating a list of the key words
index = paragraph.find(keys)
# finding the position of each key word in the textile
Try this but please note that it would only find previous and next words within the paragraph. If you want it to find results in previous/next paragraphs consider creating one big lists of words (if memory limitations permits) or while iterating into a new paragraph update the previous one and when on last iteration of a paragraph save last word for later use.
def findProperWords(paragraphs, excludedWords):
key = [] # an empty list
count = 0
for paragraph in paragraphs: #calling of every paaragraph in the textfile
count += 1 #counting each paragraph
words = list(paragraph.split(' '))
# spliting each paragraph into a list of words
for idx,keys in enumerate(words):
if len(keys) > 0:
if keys[0] == keys[0].upper():
#checking for words that start with capital letters
if keys.lower() not in excludedWords:
key.append(keys) # creating a list of the key words
index = paragraph.find(keys)
# finding the position of each key word in the textile
if idx > 0:
word_before = words[idx-1]
if idx < len(words) -2:
word_after = words[idx+1]

I am relatively new to Python, I was wondering how to see if two words in a string are the same like this:

How do you compare to see if two words side by side in a string are the same. For instance if the string "Hello world, how how are you doing today" was imported how would I write code to say that the word "how" is repeated in that sentence. I know I would start with something like this but have no clue where to go after.
x=input("Please type a sentence.")
x.split()
Python is a great language to start, I suggest using a dictionary. You can split the string into words and then, count and store the occurrence of each word into a dictionary, then call the dictionary to see how many times a word is repeated.
mystring = "Hello world, how how are you doing today"
words = mystring.split()
mydict = {}
for word in words:
if word in mydict:
mydict[word] += 1
else:
mydict[word] = 1
print(mydict['how'])
Update
mystring = "Hello world, how how are you doing today"
words = mystring.split()
lastword = ""
for word in words:
if lastword.lower() == word.lower():
print("The word " + word + " is repeated")
break
lastword = word

parsing words in a document using specific delimiters

I have a document that I'm parsing words from but I want to consider anything that is not a-z, A-Z, 0-9, or an apostrophe, to be white space. How could I do this if I am using the following bit of code before:
ifstream file;
file.open(filePath);
while(file >> word){
listOfWords.push_back(word); // I want to make sure only words with the stated
// range of characters exist in my list.
}
So, for example, the word hor.se would be two elements in my list, "hor" and "se".
Create a list of "whitespace characters" and then each time you encounter a character, check to see if that character is in the list and if so you've started a new word. This example is written in python, but the concept is the same.
def get_words(whitespace_chars, string):
words = []
current_word = ""
for x in range(0, len(string)):
#check to see if we hit the end of a word.
if(string[x] in whitespace_chars and current_word != ""):
words.append(current_word)
current_word = ""
#add current letter to current word.
else:
current_word += string[x]
#if the last letter isnt whitespace then the last word wont be added, so add here.
if(current_word != ""):
words.append(current_word)
return words
return words

Resources