coverting multiple sentences to positions - python

i have created a set of code which can convert the sentence into a list of positions.
sentence = "ask not what you can do for your country ask what your country can do for you"
d = {}
i = 0
values = []
for i, word in enumerate(sentence.split(" ")):
if not word in d:
d[word] = (i + 1)
values += [d[word]]
print(values)
i now need for the program to be able to convert multiple sentences
so it would be
sentence = ("ask not what you can do for your country ask what your country can do for you")
sentence2 = ("some people enjoy computing others do not enjoy computing")
sentence3 = ("i will use this as my last sentence as i do not need another sentence")
i need the code to be able to create seperate lists for each of the sentences, without the code being too heavily modified

I think what you are looking for is a function:
def get_positions(sentence):
d = {}
i = 0
values = []
for i, word in enumerate(sentence.split(" ")):
if not word in d:
d[word] = (i + 1)
values += [d[word]]
return values
print get_positions(sentence1)
print get_positions(sentence2)
print get_positions(sentence3)
What this does is create a function that takes a sentences as an argument and then converts that into the list you wanted to construct. Any time you want to get the positions for a sentence you can just call your function with the sentence you want to get the positions for as the argument.
Note that I changed the print at the end of your code to a return value. the return statement is what comes back out of the function when you use it. Basically, you pass in some value, do some computation and then spit out another value.

Related

A Python program to print the longest consecutive chain of words of the same length from a sentence

I got tasked with writing a Python script that would output the longest chain of consecutive words of the same length from a sentence. For example, if the input is "To be or not to be", the output should be "To, be, or".
text = input("Enter text: ")
words = text.replace(",", " ").replace(".", " ").split()
x = 0
same = []
same.append(words[x])
for i in words:
if len(words[x]) == len(words[x+1]):
same.append(words[x+1])
x += 1
elif len(words[x]) != len(words[x+1]):
same = []
x += 1
else:
print("No consecutive words of the same length")
print(words)
print("Longest chain of words with similar length: ", same)
In order to turn the string input into a list of words and to get rid of any punctuation, I used the replace() and split() methods. The first word of this list would then get appended to a new list called "same", which would hold the words with the same length. A for-loop would then compare the lengths of the words one by one, and either append them to this list if their lengths match, or clear the list if they don't.
if len(words[x]) == len(words[x+1]):
~~~~~^^^^^
IndexError: list index out of range
This is the problem I keep getting, and I just can't understand why the index is out of range.
I will be very grateful for any help with solving this issue and fixing the program. Thank you in advance.
using groupby you can get the result as
from itertools import groupby
string = "To be or not to be"
sol = ', '.join(max([list(b) for a, b in groupby(string.split(), key=len)], key=len))
print(sol)
# 'To, be, or'
len() function takes a string as an argument, for instance here in this code according to me first you have to convert the words variable into a list then it might work.
Thank You !!!

If statemts, only if the subject is of a certian class

For an assignment have to turn words into the dog lantin version of them, thats not the problem though, they love to find the conditions for our code to fail, so i think they will attempt to trip us up by having different class types in the auto code. My problem is i want to apply my function to a sentence, I can do it for an indivdual word, but I dont know how to have it apply for every word in the sentece. So i have one function here
def dog_latinify_word(word):
"""Takes a word and returns the dog latin version of the word"""
first_letter = word[0]
first_letter.lower()
vowels = ('a','e','i','o','u','1','2','3','4','5',
'6','7','8','9','0')
dogify_vowel = word + 'woof'
dogify_constant = word[1:] + word[0] + "oof"
if word.startswith(vowels):
result = dogify_vowel
elif first_letter != vowels:
result = dogify_constant
return result
but i dont know how to have this function work on every item in a list, because i need to have each word be "dog latinified"
So I have another function to take the sentence that then splits it so each word is its own item in a list, but when I go to call on this function, it only works for class str, and not list. So in my rambling ways can someone point me in the direction of having this function apply into every item in a list, rather than just a string
my code for the secondary function
def dog_latinify_sentence(sentence):
"""translate a sentence into dog latin"""
str_split = sentence.split(' ')
dogify_sentence = dog_latinify_word(str_split)
return dogify_sentence
sorry for the rambling im very tired
You can fix this by just adding a for loop over the list object like this.
def dog_latinify_sentence(sentence):
"""translate a sentence into dog latin"""
str_split = sentence.split(' ')
dogify_sentence = ""
for s in str_split:
dogify_sentence += dog_latinify_word(s)
return dogify_sentence
Above code removes spaces between words. In case you want to preserve spaces,
dogify_sentence = " ".join([dog_latinify_word(s) for s in str_split])
It just gets results from dog_latinify_word() and stitch them back!

Can't call dictionary function. keyError 0

For my comp sci class I was assigned to make an english to pirate dictionary. The user is prompted to enter a sentence which is then translated to pirate but it isn't working and I'm not sure why. Any help would be appreciated.
eng2pir = {}
eng2pir['sir'] = 'matey'
eng2pir['hotel'] = 'fleabag inn'
eng2pir['restauraunt'] = 'galley'
eng2pir['your'] = 'yer'
eng2pir['hello'] = 'avast'
eng2pir['is'] = 'be'
eng2pir['professor'] = 'foul blaggart'
a = input("Please enter a sentence to be translated into pirate: ")
for x in range(len(a)):
b = a.replace(x, eng2pir[x])
print(b)
Your loop is iterating over range(len(a)), so x will take on an integer value for each individual character in your input. This is off for a couple of reasons:
Your goal is to iterate over words, not characters.
Indexing the dictionary should be done with words, not integers (this is the cause of your error).
Finally, note that .replace() replaces the first occurrence of the searched item in the string. To revise your approach to this problem in a way that still uses that method, consider these two main changes:
Iterate over the keys of the dictionary; the words that could potentially be replaced.
Loop until no such words exist in the input, since replace only does individual changes.
You're iterating over each of the characters in the string input, as the other answer before this has said, replace only replaces the first occurence.
You'd want to do something like this (after you've made your dictionary).
a = input("Please enter a sentence to be translated into pirate: ")
for x in eng2pir:
while x in a:
a = a.replace(x,eng2pir[x])
print(a)
for x in range(len(a)):
b = a.replace(x, eng2pir[x])
because for loop x is int
but eng2pir dict no int key
so output error
#!/usr/bin/env python
# coding:utf-8
'''黄哥Python'''
eng2pir = {}
eng2pir['sir'] = 'matey'
eng2pir['hotel'] = 'fleabag inn'
eng2pir['restauraunt'] = 'galley'
eng2pir['your'] = 'yer'
eng2pir['hello'] = 'avast'
eng2pir['is'] = 'be'
eng2pir['professor'] = 'foul blaggart'
a = input("Please enter a sentence to be translated into pirate:\n ")
lst = a.split()
b = ''
for word in lst:
b += eng2pir.get(word, "")
print(b)

How to find the position of a word in a string / list?

I'm writing a function where the user inputs a word, then inputs a string, and the function identifies all occurrences and the position of that word in a string (although it's actually converted to a list halfway through).
The code I have currently is only identifying the first occurrence of the word, and none further. It also doesn't identify the word if the word is the first word in the string, returning an empty list. It will also say the word's actual position - 1, as the first word is counted as zero.
I have attempted to curb this problem in two ways, the first of which doing a aString.insert(0, ' '), the second of which doing for i in __: if i == int: i += 1. Neither of these work.
Also, when doing the .insert, I tried putting a character in the space instead of, well, a space (as this part doesn't get printed anyway), but that didn't work.
Here is the code:
def wordlocator(word):
yourWord = word
print("You have chosen the following word: " +yourWord)
aString = input("What string would you like to search for the given word?")
aString = aString.lower()
aString = aString.split()
b = [(i, j) for i, j in enumerate(aString)]
c = [(i, x) for i, x in b if x == yourWord]
return c
The output I'm looking for is that if someone did...
wordlocator("word")
"please input a string generic message" "that is a word"
"4, word"
Currently that would work, but it'd print "3, word". If the string was "that is a word and this is also a word" then it'd ignore the further occurrence of "word".
edit: Got it working now, used a simpler piece of code. Thanks for your help all!
Try this:
def wordlocator(word):
yourWord = word
print("You have chosen the following word: " +yourWord)
aString = raw_input("What string would you like to search for the given word?")
aString = aString.lower()
aString = aString.split()
b = [(i+1, j) for i, j in enumerate(aString) if j == yourWord.lower()]
return b
print wordlocator('word')
note that the list comprehension can be filtered on just the match you are looking for. Actually I just changed it
I get this:
What string would you like to search for the given word?This word is not that word is it?
[(2, 'word'), (6, 'word')]
Note that the index is off by one if that matters, add one to x in the comprehension
A new test:
You have chosen the following word: word
What string would you like to search for the given word?word is the word
[(1, 'word'), (4, 'word')]

Python: Finding the word that shows up the most?

I'm trying to get my program to report the word that shows up the most in a text file. For example, if I type "Hello I like pie because they are like so good" the program should print out "like occurred the most." I get this error when executing Option 3: KeyError: 'h'
#Prompt the user to enter a block of text.
done = False
textInput = ""
while(done == False):
nextInput= input()
if nextInput== "EOF":
break
else:
textInput += nextInput
#Prompt the user to select an option from the Text Analyzer Menu.
print("Welcome to the Text Analyzer Menu! Select an option by typing a number"
"\n1. shortest word"
"\n2. longest word"
"\n3. most common word"
"\n4. left-column secret message!"
"\n5. fifth-words secret message!"
"\n6. word count"
"\n7. quit")
#Set option to 0.
option = 0
#Use the 'while' to keep looping until the user types in Option 7.
while option !=7:
option = int(input())
#The error occurs in this specific section of the code.
#If the user selects Option 3,
elif option == 3:
word_counter = {}
for word in textInput:
if word in textInput:
word_counter[word] += 1
else:
word_counter[word] = 1
print("The word that showed up the most was: ", word)
I think you may want to do:
for word in textInput.split():
...
Currently, you are just iterating through every character in the textInput. So to iterate through every word, we must first split the string up into an array of words. By default .split() splits on whitespace, but you can change this by just passing a delimeter to split().
Also, you need to check if the word is in your dictionary, not in your original string. So try:
if word in word_counter:
...
Then, to find the entry with the highest occurrences:
highest_word = ""
highest_value = 0
for k,v in word_counter.items():
if v > highest_value:
highest_value = v
highest_word = k
Then, just print out the value of highest_word and highest_value.
To keep track of ties, just keep a list of the highest words. If we find a higher occurrence, clear the list and continue rebuilding. Here is the full program so far:
textInput = "He likes eating because he likes eating"
word_counter = {}
for word in textInput.split():
if word in word_counter:
word_counter[word] += 1
else:
word_counter[word] = 1
highest_words = []
highest_value = 0
for k,v in word_counter.items():
# if we find a new value, create a new list,
# add the entry and update the highest value
if v > highest_value:
highest_words = []
highest_words.append(k)
highest_value = v
# else if the value is the same, add it
elif v == highest_value:
highest_words.append(k)
# print out the highest words
for word in highest_words:
print word
Instead of rolling your own counter, a better idea is to use Counters in the collections module.
>>> input = 'blah and stuff and things and stuff'
>>> from collections import Counter
>>> c = Counter(input.split())
>>> c.most_common()
[('and', 3), ('stuff', 2), ('things', 1), ('blah', 1)]
Also, as a general code style thing, please avoid adding comments like this:
#Set option to 0.
option = 0
It makes your code less readable, not more.
The original answer is certainly correct, but you may want to keep in mind that it will not show you 'ties for first'. A sentence like
A life in the present is a present itself.
Will only reveal either 'a' or 'present' to be the number one hit. In fact, since dictionaries are (generally) unordered, the result you see may not even be the first word that's repeated multiple times.
If you need to report on multiples, might I suggest the following:
1) Use your current method of key-value pairs for 'word':'hits'.
2) Determine the greatest value for 'hits'.
3) Check for the number of values that equal the greatest number of hits, and add those keys to a list.
4) Iterate through the list to display the words with the greatest number of hits.
Par example:
greatestNumber = 0
# establish the highest number for wordCounter.values()
for hits in wordCounter.values():
if hits > greatestNumber:
greatestNumber = hits
topWords = []
#find the keys that are paired to that value and add them to a list
#we COULD just print them as we iterate, but I would argue that this
#makes this function do too much
for word in wordCounter.keys():
if wordCounter[word] == greatestNumber:
topWords.append(word)
#now reveal the results
print "The words that showed up the most, with %d hits:" % greatestNumber
for word in topWords:
print word
Depending on Python 2.7 or Python 3, your mileage (and syntax) may vary. But ideally - IMHO - you'd first want to determine the greatest number of hits and then just go back and add the relevant entries to a new list.
EDIT -- you should probably just go with the Counters module as suggested in a different answer. I didn't even know that was something Python just came prepared to do. Haha don't accept my answer unless you necessarily have to write your own counter! There's already a module for that, it seems.
With Python 3.6+ you can use statistics.mode:
>>> from statistics import mode
>>> mode('Hello I like pie because they are like so good'.split())
'like'
I'm not too keen on Python, but on your last print statement, shouldn't you have a %s?
i.e.: print("The word that showed up the most was: %s", word)

Categories