Reputation: 97
For example, I have this sentence:
hello. my name is Jess. what is your name?
and I want to change it into:
Hello. My name is Jess. What is your name?
I came up with this code, but I have one problem with connecting everything back together
def main():
name = input('Enter your sentence: ')
name = name.split('. ')
for item in name:
print (item[0].upper() + item[1:], end='. ')
When I put in the sentence, it will return:
Hello. My name is Jess. What is your name?.
How can I stop the punctuation from appearing at the end of the sentence? Also, what if I have a question in the middle, for example:
hi. what is your name? my name is Jess.
Upvotes: 4
Views: 7435
Reputation: 1663
I have recently come across this string.capsword and found it useful.
import string
sentence = 'hello. my name is Jess. what is your name?'
string.capsword(sentence, sep='. ') # sep should be a dot and space
Upvotes: 0
Reputation: 302
This one is better solution
x = "hello. my name is Jess. what is your name?"
print( '. '.join(map(lambda s: s.strip().capitalize(), x.split('.'))))
output:
Hello. My name is jess. What is your name?
Upvotes: 8
Reputation: 1987
I have written a solution to similar question, you can read how the following codes works here: https://stackoverflow.com/a/48688182/5265414
original_data = raw_input("Enter text: ")
list = original_data.split(".")
if original_data.endswith('.'):
list.remove('')
for w in list:
stripper= w.strip().capitalize() +"."
print stripper,
Upvotes: 0
Reputation: 1254
You can construct your string first, following the same procedure, and the showing the string, except the last two values (you also need to remove the last space):
def main():
result = ""
name = input('Enter your sentence: ')
name = name.split('. ')
for item in name:
result += item[0].upper() + item[1:] + '. '
print result[:-2]
Upvotes: 2
Reputation: 473873
As an alternative (and may be as an over complication also), you can use nltk
's Sentence Segmentation. Relying on @J.F. Sebastian's answer:
>>> import nltk.data
>>>
>>> sent_tokenizer = nltk.data.load('tokenizers/punkt/english.pickle')
>>>
>>> text = "hello. my name is Jess. what is your name?"
>>>
>>> sentences = sent_tokenizer.tokenize(text)
>>> sentences = [sent.capitalize() for sent in sentences]
>>> print(' '.join(sentences))
Hello. My name is jess. What is your name?
Why it is not as simple as splitting a string by .
? Generally speaking, the problem with .
is that it is not only serving as a sentence delimiter. It also can be a part of an acronym or mark abbreviations inside a sentence (think also about all of the Mr.
, Mrs.
, Dr.
etc):
Sentence segmentation is difficult because period is used to mark abbreviations, and some periods simultaneously mark an abbreviation and terminate a sentence, as often happens with acronyms like U.S.A.
Upvotes: 2
Reputation: 16309
Am I overlooking the obvious or can you just right strip?
Ie before you output your string:
finalstring = string.rstrip('.')
Upvotes: 0