Compressing a sentence in python - python-3.x

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='')

Related

(Python) Why doesn't my program run anything while my CPU usage skyrockets?

So I was following this tutorial on youtube however when it comes to running the program, nothing shows up on the terminal while my CPU doubles in usage. I am using VScode to run this program. PS. I am very new to this and I always have frequent trouble when it comes to coding in general so i decided to resort in asking questions.
import random
from words import words
import string
def get_valid_word(words):
word = random.choice(words) #randomly chooses something from the list
while '-' in word or '' in word: #helps pick words that have a dash or space in it
word = random.choice(words)
return word.upper() #returns in uppercase
def hangman():
word = get_valid_word(words)
word_letters = set(word) # saves all the letters in the word
alphabet = set(string.ascii_uppercase) #imports predetermined list from english dictionary
used_letters = set() #keeps track of what user guessed
#getting user input
while len(word_letters) > 0: #keep guessing till you have no letters to guess the word
#letters used
#'' .join (['a', 'b', 'cd']) --> 'a, b cd'
print('you have used these letters: ',' '.join(used_letters))
#what the current word is (ie W - R D)
word_list = [letter if letter in used_letters else '-' for letter in word]
print('current word: ',' '.join(word_list))
user_letter = input('Guess a letter: ')
if user_letter in alphabet - used_letters: #letters haven't used
used_letters.add(user_letter) #add the used letters
if user_letter in word_letters: #if the letter that you've just guessed is in the word then you remove that letter from word_letters. So everytime you've guessed correctly from the word letters which is keeping track of all the letters in the word decreases in size.
word_letters.remove(user_letter)
elif user_letter in used_letters:
print('You have used this')
else:
print('Invalid')
#gets here when len(word_letters) == 0
hangman()

In python 3 how can I return a variable to a function properly?

I am currently in school studying python and have a question. I am working on a midterm project that has to take an input, assign it to a list, if the first letter isnt capital - capitalize it..and count the number of words in the sentence.
While my code works.. I can't help but think I handled the arguments into the functions completely wrong. If you could take a look at it and help me out on how I could clean it up that would be excellent.
Please remember - I am new..so explain it like I am 5!
sentence_list = sentList()
sentence = listToString(sentence_list)
sentence = is_cap(sentence)
sentence = fix(sentence)
sentence = count_words(sentence)
def sentList():
sentence_list = []
sentence_list.append(input('Please enter a sentence: '))
return sentence_list
def listToString(sentence_list):
sentence = ""
sentence = ''.join(sentence_list)
return sentence
def is_cap(sentence):
sentence = sentence.capitalize()
return sentence
def fix(sentence):
sentence = sentence + "." if (not sentence.endswith('.')) and (not sentence.endswith('!')) and \
(not sentence.endswith('?')) else sentence
return sentence
def count_words(sentence):
count = len(sentence.split())
print('The number of words in the string are: '+ str(count))
print(sentence)
main()```
first of all, your code is very good as a beginner, good job dude.
so
to make your function run, you need call it after you defined them. but here you put the call at the top of the page.
the reason of that is python read the codes from top to bottom, so when he read the first that call a function that he didn't read 'til this line
the code should be like this:
def sentList():
sentence_list = []
sentence_list.append(input('Please enter a sentence: '))
return sentence_list
def listToString(sentence_list):
sentence = ""
sentence = ''.join(sentence_list)
return sentence
def is_cap(sentence):
sentence = sentence.capitalize()
return sentence
def fix(sentence):
sentence = sentence + "." if (not sentence.endswith('.')) and (not sentence.endswith('!')) and \ (not sentence.endswith('?')) else sentence
return sentence
def count_words(sentence):
count = len(sentence.split())
print('The number of words in the string are: '+ str(count))
print(sentence)
sentence_list = sentList()
sentence = listToString(sentence_list)
sentence = is_cap(sentence)
sentence = fix(sentence)
sentence = count_words(sentence)
I guess that it. if you have any another question. this community will always be here

How to rewrite code

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)

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

stemming. i need to write a code for this

If you search for something in Google and use a word like "running", Google is smart enough to match "run" or "runs" as well. That's because search engines do what's called stemming before matching words.
In English, stemming involves removing common endings from words to produce a base word. It's hard to come up with a complete set of rules that work for all words, but this simplified set does a pretty good job:
If the word starts with a capital letter, output it without changes.
If the word ends in 's', 'ed', or 'ing' remove those letters, but if the resulting stemmed word is only 1 or 2 letters long (e.g. chopping the ing from sing), use the original word.
Your program should read one word of input and print out the corresponding stemmed word. For example:
Enter the word: states
state
Another example interaction with your program is:
Enter the word: rowed
row
Remember that capitalised words should not be stemmed:
Enter the word: James
James
and nor should words that become too short after stemming:
Enter the word: sing
sing
Here is the code:
word = input("Enter the word:")
x = 'ing'
y = 'ed'
z = 's'
first = word[:1]
last = word[-1:]
uppercase = first.upper
if word == uppercase:
print("")
elif (x in word) == True:
word = (word.replace('ing',''))
print(word)
elif (y in word) == True:
word = (word.replace('ed',''))
print(word)
elif (z in word) == True:
word = (word.replace('s',''))
print(word)
I see two options. Either this is a homework question, in which case - please try to solve your own homework.
The other case - you need this in real life. If so, please look at NLTK for Python natural language processing needs. In particular see http://nltk.org/api/nltk.stem.html
Install NLTK toolkit
and try this
from nltk.stem.porter import PorterStemmer
PorterStemmer.stem_word(word)

Resources