94.8k views
1 vote
Write a function countOrange that counts the occurrences of the word Orange (or orange) as it refers to a fruit. However, the word Orange can mean other things. You can be sure that the fruit is being referenced if at least one of the following words is no more than distance 3 away from an occurrence of Orange (or orange): [eat, fruit, slice, peel, juice]

1 Answer

5 votes

Answer:

#Section 1

def countOrange(text):

import string

lowercase = text.lower()

s=lowercase.translate(str.maketrans('', '', string.punctuation))

a= s.split()

test = ['eat', 'fruit', 'slice', 'peel', 'juice']

i=0

#Section 2

for word in a:

if word == 'orange':

print('')

print((a[((a.index(word))-3):((a.index(word))+4)]))

checkFruit = (a[((a.index(word))-3):((a.index(word))+4)])

for e in checkFruit:

if e in test:

i = i+1

break

a.remove('orange')

'''I had to pop the word out of the list because that's the only way i could see all the occurrences of orange and work with them individually '''

print('\\Orange is refered to as a fruit ',i,' Times in the Text')

#Testing the function

stringg= "cocoanut oil is a really cool add but when working with a slice of orange and that came up with penelopy talking abput peel that sweet orange I want juice. Orange juice is the best. orange colour is my facourite because it is an orange."

countOrange(stringg)

Step-by-step explanation:

#Section 1

In this section we define the function and import the string module.

The string module is imported because some work will have to be done on the text you'll pass to the function first before conditional statements and other methods can be used on it.

The First string operation carried out on the text is to convert it to a lower case, because this time we want orange not minding if the first character is in upper case. This will help simplify your job.

Then using translate we strip away every punctuation and finally, convert the text to a list of items, that we can iterate over.

we also declare a test list that contains all the elements that need to be at least 3 places close to the word orange to make it a fruit.

Also, the variable 'i' that will store how many times an orange fruit is referenced is also created.

#Section 2

After all the operations that the string has gone through, in this section the string is now a list called 'a'.

In this section we made use of a FOR loop, an IF statement, splice, and break.

  • for word in a:

if word == 'orange':

The above for and if loop check each word in the text if it is 'orange'.

  • checkFruit = (a[((a.index(word))-3):((a.index(word))+4)])

This line of code gets the index of the word 'orange' with index(word), and creates a new list with a range 3 words before and 3 words after the word orange.

  • for e in checkFruit:

if e in test:

i = i+1

break

For every word in the new list IF the word is in the test list then that orange we just found is a fruit. How this works is that it iterates through every word in the checkFruit list and compares them to each word in the Test list and as soon as it sees a match it breaks.

The break is important so that it won't count more than once.

  • a.remove('orange')

I actually commented the explanation in the code because this is the most important point.

'''I had to pop the word out of the list because that's the only way i could see all the occurrences of orange and work with them individually '''

  • print('\\Orange is refered to as a fruit ',i,' Times in the Text')

Finally we print the number of times orange shows up as a fruit

#Testing the function

I just passed a random string into the function for you to see how the code behaves, check attachment.

Write a function countOrange that counts the occurrences of the word Orange (or orange-example-1
User Martin Gergov
by
5.2k points