Created
October 2, 2018 15:37
-
-
Save sp90/436c904908b00ea031bd700072402ae3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# First we def (define) a function that takes 2 arguments: | |
# 1. argument is the text to look for occurances | |
# 2. argument is the word_to_count | |
def count_word_in_text(text, word_to_count): | |
# The count of our word that we're counting | |
count = 0 | |
# The text index looking from | |
start = 0 | |
# Setting all the text to lowercase to match different casing | |
lowercase_text = text.lower() | |
# Setting the word to count to lowercase to match all the occurences in the loop | |
lowercase_word_to_count = word_to_count.lower() | |
while True: | |
# Here we find the index of the word in our text | |
# Ex | |
# text = 'Hello world' | |
# word_to_count = 'Hello' | |
# then index of word start is 0 because the word Hello starts on index 0 in the text | |
# if the word_to_count = 'world' - the index of word start is 6 because the word world | |
# starts on the 6. location in the text | |
start = lowercase_text.find(lowercase_word_to_count, start) + 1 | |
# if start (index of the word) is above 0 then increment the count of words by 1 | |
if start > 0: | |
# here we increment the count | |
count += 1 | |
# Else which meanes the word dosn't exist | |
else: | |
# We just return the final count | |
return count | |
# Here we save the test text into a variable test_text | |
test_text = ''' | |
To Sherlock Holmes she is always the woman. I have seldom heard him | |
mention her under any other name. In his eyes she eclipses and | |
predominates the whole of her sex. It was not that he felt any | |
emotion akin to love for Irene Adler. All emotions, and that one | |
particularly, were abhorrent to his cold, precise but admirably | |
balanced mind. He was, I take it, the most perfect reasoning and | |
observing machine that the world has seen, but as a lover he would | |
have placed himself in a false position. He never spoke of the softer | |
passions, save with a gibe and a sneer. They were admirable things | |
for the observer--excellent for drawing the veil from men's motives | |
and actions. But for the trained reasoner to admit such intrusions | |
into his own delicate and finely adjusted temperament was to | |
introduce a distracting factor which might throw a doubt upon all his | |
mental results. Grit in a sensitive instrument, or a crack in one of | |
his own high-power lenses, would not be more disturbing than a strong | |
emotion in a nature such as his. And yet there was but one woman to | |
him, and that woman was the late Irene Adler, of dubious and | |
questionable memory. | |
''' | |
# Here we count the word "woman" | |
count_one = count_word_in_text(test_text, "woman") | |
print(count_one) | |
# Here we count the word "and" | |
count_two = count_word_in_text(test_text, "and") | |
print(count_two) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment