+ 1

Write a python program to find string,charecters,words and sentence in a text file contains 5000 lines of sentence.

5th Sep 2017, 7:14 PM
luckk lucky
luckk lucky - avatar
26 Réponses
+ 6
Regex will manage it nicely. You can check the code now, I updated it. You just need to assign re.findall to a variable. This will be a list of sentences, so you can check its length to see the number of sentences found.
8th Sep 2017, 3:30 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 8
@RUDRESH Here's how. Either by str.split() or by using regex module. Notice the difference in precision. https://code.sololearn.com/cbmdTeEar96f/?ref=app
7th Sep 2017, 5:26 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 7
You will need regex for this. Check out 're' library and its documentation, it's really powerful: https://docs.python.org/3/library/re.html
5th Sep 2017, 7:37 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 6
Depends what you define as 'sentence'. Any character plus whitespaces string between two dots? Will that be enough for a definition?
5th Sep 2017, 8:16 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 5
Check a few replies back or see here: https://code.sololearn.com/cbmdTeEar96f/?ref=app
8th Sep 2017, 3:34 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 5
a is just a variable to which I assigned a docstring. You can open() a file and .read() it, in order to look for sentences in it.
8th Sep 2017, 4:33 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 5
@yuri Yes, it might work better for matching, indeed. Won't dispose of the line breaks before the beginnings of the sentences though :)
8th Sep 2017, 7:53 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 3
@Kuba Siekierzynski Works fine. ☺
7th Sep 2017, 5:38 PM
Haris
Haris - avatar
+ 1
fname = input("Enter file name") num_words = 0 chars = 0 with open(fname,'r')as f: for line in f: words = lines split() num_words +=len(words) chars = chars+len(line) print (num_words) print (chars)
5th Sep 2017, 7:19 PM
luckk lucky
luckk lucky - avatar
+ 1
This d answer for only words & characters.i dnt knw the code for sentence & string..if anyone know thn plz suggest me..
5th Sep 2017, 7:21 PM
luckk lucky
luckk lucky - avatar
+ 1
there are several typos, indentations, break operator missing. import os.path text = """Hi , Fisrt line fdsgd fgfdkg sdg sdf sdfg -> voidvoid <- sdf kkds dfsdf sdf sdf df. fsdfsd sfd sgojhoptrye -> void <- y ytryrthdfdfs re re ewr gfdsfg r rtert . F rtret ret dsfgedr """ my_file = 'text2.txt' my_string ='void' infile = open(my_file,"w") infile.write (text) infile.close() infile = open(my_file,"r") numlines =0 found =0 for line in infile: print (line) numlines+=1 while 1: str_found_at = line.find(my_string) if str_found_at == -1: break else: found+=1 line = line[str_found_at+len(my_string):] infile.close() print ("%s was found %i times in %i lines" %(my_string,found,numlines))
26th Sep 2017, 2:59 PM
yuri
+ 1
i know, but I need something to test your code at SL.
26th Sep 2017, 3:08 PM
yuri
+ 1
my does
26th Sep 2017, 3:09 PM
yuri
0
it works @ kuba??
5th Sep 2017, 7:53 PM
luckk lucky
luckk lucky - avatar
0
can som1 give me d code??..
5th Sep 2017, 7:59 PM
luckk lucky
luckk lucky - avatar
0
Google regex. No program necessary.
5th Sep 2017, 8:07 PM
Haris
Haris - avatar
0
how to get sentence separated
7th Sep 2017, 11:42 AM
RUDRESH SUNAGAD
RUDRESH SUNAGAD - avatar
0
it is a big text file how can i split it bt commas(,). so how to count that how many no of sentence in the text file?? this my questions..
8th Sep 2017, 9:46 AM
luckk lucky
luckk lucky - avatar
0
where u update
8th Sep 2017, 3:33 PM
luckk lucky
luckk lucky - avatar
0
how can i use some some files & wht z role of a here?
8th Sep 2017, 4:22 PM
luckk lucky
luckk lucky - avatar