Finding certain word in text

Viewed 125

I have a simple question about finding certain word in text. I want to find the lines of text which contain only "dog" word (not "doggy","doggo" etc.) and print them. I just want to use the simplest capabilities of Python.

Note: this task can't use regex or re library

import sys

word = "dog"

for line in sys.stdin:
  x = line.find(word)
  if x != -1:
    print (line)

What should I change in this code?

5 Answers

Try this if it helps:

  import re

  for line in sys.stdin:
      y= re.findall('\\bdog\\b', line)
      if(len(y)!=0):
          print(line)

Later I saw the comment, that you can't use regex or re.

Then, try another approach of loops:

   fixed_word="dog"
   for line in sys.stdin:
       word_list=line.split(' ')
       for word in word_list:
           if(word.lower() == fixed_word):
               print(line)

Using Python map()

How to find a certain word or words and returns sentence using python map() without importing any libraries like re or others.

sentence = ['hi little doggy doggo', 'what is a doggo', 'Benji is my dog'] 
words = ['dog']


 def find(sentence, words): 
        res = list(map(lambda x: all(map(lambda y:y in x.split(), 
                                            words)), sentence)) 
        return [sentence[i] for i in range(0, len(res)) if res[i]] 
        



print(find(sentence, words)) 

['Benji is my dog']

You could also find multiple words too if you need.

words = ['doggo', 'doggy'] 

print(find(sentence, words)) 



['hi little doggy doggo']

Note that this approach fails if dots or commas are part of the sentence. If the last sentence was 'Benji is my dog. He is a good boy.', the split-function returns 'dog.' and consequently does not exactly match 'dog' anymore.

You can look for all instances of "dog". Then, make sure the character before and after the match aren't letters. The code below is not particularly efficient, but you can come up with a more efficient algorithm to implement the same idea.

lines = ["I have a dog", "I have a dog.", "I have a dog, a cat, and a parakeet.", "Doggy dog hotdog", "Hotdog doggy"]
word = "dog"
alphabet = {chr(i) for i in range(97, 97+26)}
for line in lines:
    x = 0
    while x >= 0: # loop while word is found in line
        x = line.find(word, x) # Look for match after last match
        
        charBefore = (line[x - 1] if x > 0 else "").lower() # Character before match if it exists

        charAfter = (line[x + len(word)] if x + len(word) < len(line) else "").lower() # Character after match if it exists

        if charBefore not in alphabet and charAfter not in alphabet:
            print(line)
            break
        x += 1

This gives the output

I have a dog
I have a dog.
I have a dog, a cat, and a parakeet.
Doggy dog hotdog

Your question is answered for instance here How do I check for an EXACT word in a string in python. Re is your friend here as outlined in the answers given:

import re

s = 'doggy doggo'
word = 'dog'
if re.search(r'\b' + word + r'\b', s):
    print('{0} found'.format(word))
print('Now trying a string with dog in it')
s = 'a good dog'
if re.search(r'\b' + word + r'\b', s):
    print('{0} found'.format(word))

And then instead of s you simply enter the lines of text you want to check

for line in sys.stdin:
    if 'dog' in line:
        print(line)

That's another way to do it.

Related