Home > front end >  How to extract words from a sentence and check if a word is not there in it
How to extract words from a sentence and check if a word is not there in it

Time:11-12

this is my code below. I need to take an input from the user (a sentence) and check whether there are any numbers in it from 0 to 10 in it or not. I know there are many ways to approach it, e.g. split(), isalnum(), etc. but I just need help in putting it all together. Please find my code below:

sentence1 = input("Enter any sentence (it may include numbers): ")
numbers = ["1","2","3","4","5","6","7","8","9","10","0"]

ss1 = sentence1.split()
print(ss1)
if numbers in ss1:
  print("There are numbers between 0 to 10 in the sentece")
else:
  print("There are no numbers in the sentence”)

Thanks :)

Edit: Expected Output should be like:

Enter any sentence (it may include numbers): I am 10 years old
There are numbers between 0 and 10 in this sentence

CodePudding user response:

I would use a regex:

def contains_nums(s):
    import re
    m = re.search('\d ', s)
    if m:
        return f'"{s}" contains {m.group()}'
    else:
        return f'"{s}" contains no numbers'

Examples:

>>> contains_nums('abc 10')
'"abc 10" contains 10'

>>> contains_nums('abc def')
'"abc def" contains no numbers'

NB. this is only checking the first number, use re.findall if you need all. Also this is finding numbers within words, if you want separate numbers only, use a regex with word boundaries (\b\d \b), finally, if you want to restrict to 0-10 numbers, use (?<!\d)1?\d(?!\d) (or \b(?<!\d)1?\d(?!\d)\b for independent numbers)

more complete solution
def contains_nums(s, standalone_number=False, zero_to_ten=False):
    import re
    
    regex = r'(?<!\d)1?\d(?!\d)' if zero_to_ten else '\d '
    
    if standalone_number:
        regex = r'\b%s\b' % regex
    
    m = re.search(regex, s)
    if m:
        return f'"{s}" contains {m.group()}'
    else:
        a = "standalone " if standalone_number else ""
        b = "0-10 " if zero_to_ten else ""
        return f'"{s}" contains no {a}{b}numbers'
>>> contains_nums('abc100', standalone_number=False, zero_to_ten=False)
'"abc100" contains 100'

>>> contains_nums('abc100', standalone_number=True, zero_to_ten=False)
'"abc100" contains no standalone numbers'

>>> contains_nums('abc 100', standalone_number=True, zero_to_ten=True)
'"abc 100" contains no standalone 0-10 numbers'

CodePudding user response:

Try this solution:

sentence1 = input("Enter any sentence (it may include numbers): ")
numbers = ["1","2","3","4","5","6","7","8","9","10","0"]
verif = False
for n in numbers:
    if n in sentence1:
        verif = True
        break
if verif:
    print("There are numbers between 0 to 10 in the sentence")
else:
    print('There are no numbers in the sentence')

output:

Enter any sentence (it may include numbers): gdrsgrg8
There are numbers between 0 to 10 in the sentence

CodePudding user response:

To make your program work with minimal changes, you could use sets:

sentence1 = input("Enter any sentence (it may include numbers): ")
numbers = {"1","2","3","4","5","6","7","8","9","10","0"}

ss1 = sentence1.split()
print(ss1)
if numbers.intersection(set(ss1)):
  print("There are numbers between 0 to 10 in the sentece")
else:
  print("There are no numbers in the sentence")

Output (with number):

Enter any sentence (it may include numbers): There is 1 number
['There', 'is', '1', 'number']
There are numbers between 0 to 10 in the sentece

Output (without number):

Enter any sentence (it may include numbers): There are no numbers
['There', 'are', 'no', 'numbers']
There are no numbers in the sentence

However this will only work if the numbers to be detected are space separated. But then again, this is almost identical to your solution except it uses set.

CodePudding user response:

import re

sentence = input("Enter a sentence: ")

def check_numbers(sentence):
    if re.search(r'\d', sentence):
        return True
    else:
        return False
                 
check_numbers(sentence)

Input: Hello World ... Output: False

Input: Hello 1 World ... Output: True

CodePudding user response:

One of the way is to check the intersection of the set of numbers between and the set of words in the phrase. If it is not empty, there are numbers:

sentence1 = input("Enter any sentence (it may include numbers): ")
numbers = {"1","2","3","4","5","6","7","8","9","10","0"}

ss1 = set(sentence1.split())
print(ss1)
if ss1.intersection(numbers):
    print("There are numbers between 0 to 10 in the sentece")
else:
    print("There are no numbers in the sentence")

CodePudding user response:

You can use a pattern \b(?:10|\d)\b which will match either 10 or a digit 0-9 between word boundaries \b to prevent a partial word match.

Note that the message in your question print("There are no numbers in the sentence”) is not entirely correct. If there is only 19 in the string, there is a number but is will not match as it is not in the range of 0-10.

See a regex demo for the matches.

import re

sentence1 = input("Enter any sentence (it may include numbers): ")

if re.search(r"\b(?:10|\d)\b", sentence1):
  print("There are numbers between 0 to 10 in the sentece")
else:
  print("There are no numbers between 0 to 10 in the sentence")

CodePudding user response:

sentence1 = input("Enter any sentence (it may include numbers): ")
numbers = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "0"]

ss1 = sentence1.split()
for x in numbers:
    if x in ss1:
        print(x, " belongs to the sentence")
    else:
        print(x, "does not belong to a sentence")
  • Related