Hi everyone, my name is Ryan Han and I am a senior at the College pursuing a Data Science major and a Math minor.
This script asks for a .txt/.md file and word of your choice and figures out how many times the specific word starts a sentence in the file.
# first_word_counter.py
# By: Ryan Han
# VERSION 1.0
# Last Edit: 2020-02-10
# This script takes .txt or .md files and a word of your choice and returns
# each sentence/sentences where the specified word is the first word. The code
# also prints out the number of times the 'word' appears as the first word.
####################
# Required Module #
####################
import argparse
####################
# Function #
####################
def first_word_counter():
# Asking user for file name and word
= input("Enter file name: ")
file_name = input("Enter a word you would like to count in the file: ")
word
# Allows me to press enter to access default sample .txt file
if len(file_name) < 1 : file_name = "mbox-short.txt"
# Opening specified file
= open(file_name) # Opening said file name
open_file
# Establishing "count" at 0
= 0
count # Starting loop to examine every line in file
for line in open_file:
# striping the lines of white space
= line.rstrip()
line # Once line with word is found
if line.startswith(word):
# each line split into each characters, the characters make up the variable words.
= line.split()
words # print whole sentence
print(line)
# at the end of loop add 1 to count
= count + 1
count
# Proper grammar
if count == 1:
print("There is " + str(count) + " line in the file with " + "'" + word + "'" + " as the first word.")
else:
print("There are " + str(count) + " lines in the file with " + "'" + word + "'" + " as the first word.")
###################
# Main #
###################
if __name__ == '__main__':
# --help command line description
= argparse.ArgumentParser(
parser = "This script will ask you for a .txt/.md file and word of your choice. Then it will figure out how many times your word starts a sentence in the file.")
description = parser.parse_args()
args
first_word_counter()