Given a text, text. The task is to count the number of sentences and words in the text. 
Definition of a sentence :- Sentences are defined as contiguous space-separated strings of characters ending with a sentence punctuation (' . ',  ' ! ' or ' ? ') or the last contiguous set of space-separated strings, even if they don't end with a punctuation mark.
Definition of a word :- Words are defined as a string of alphabetic characters i.e. any upper or lower case characters a-z or A-Z.
Example 1:
Input: text = "Sentences"
Output: 1 1
Explaination: There is only one word and 
sentence in the given text.
Example 2:
Input: text = "many??? Sentences are"
Output: 2 3
Explaination: There are two sentences and 
three words.
Your Task:
You do not need to read input or print anything. Your task is to complete the function sentenceWord() which takes text as input parameters and returns a list containing number of sentences and words respectively.
Expected Time Complexity: O(|text|)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ |text| ≤ 250

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Starter code: #User function Template for python3

class Solution:
    def sentenceWord(self, text):
        # code here

Solutions:
class Solution:

	def sentenceWord(self, text):
		sent = 1
		word = 1
		for i in range(len(text) - 1):
			if text[i] == ' ':
				word += 1
			elif (text[i] == '?' or text[i] == '.' or text[i] == '!') and (text[i + 1] != '.' and text[i + 1] != '!' and (text[i + 1] != '?')):
				sent += 1
		return (sent, word)


class Solution:

	def sentenceWord(self, text):
		sentences = 0
		text = text.split()
		words = len(text)
		for i in text:
			if '.' in i or '!' in i or '?' in i:
				sentences += 1
		if '.' not in text[-1] and '!' not in text[-1] and ('?' not in text[-1]):
			sentences += 1
		return [sentences, words]


import re

class Solution:

	def sentenceWord(self, text):
		no_of_sentences = re.split('[.?!]+', text)
		no_of_words = text.strip().split(' ')
		return (len(list(filter(lambda x: len(x) != 0, no_of_sentences))), len(no_of_words))


import re

class Solution:

	def sentenceWord(self, text):
		sent_pattern = re.compile('\\.+|\\!+|\\?+')
		matched_sent = re.findall(sent_pattern, text)
		word_pattern = re.compile('\\w+')
		matched_words = re.findall(word_pattern, text)
		sentence = 0
		FLAG = False
		if len(matched_sent) == 0:
			sentence = 1
			FLAG = True
		else:
			sentence = len(matched_sent)
		if FLAG:
			pass
		elif text[-1] != '!':
			sentence += 1
		return (sentence, len(matched_words))