Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor the source file into multiple files #9

Closed
boratonAJ opened this issue Apr 28, 2019 · 1 comment
Closed

Refactor the source file into multiple files #9

boratonAJ opened this issue Apr 28, 2019 · 1 comment
Assignees
Labels
enhancement New feature or request good first issue Good for newcomers question Further information is requested

Comments

@boratonAJ
Copy link
Owner

boratonAJ commented Apr 28, 2019

Refactor the source file into multiple files so that each file either solves a specific problem or is a python module to be loaded as needed

# Reads in text files and writes out word frequency data
# This python function will display the content of the file whose name you pass in the first argument like that:
def frequency_of_words(argv):
for filename in argv[1:]:
freq = collections.defaultdict(int)
freq = collections.Counter()
for line in open(filename, encoding='utf-8'):
words = line.strip().lower().split(' ')
for word in words:
freq.update([''.join(char for char in word if char.isalnum())])
del freq['']
output_filename = 'Data/output/' + filename.split("/")[2][:-3] +'freq' # substring the filename (slicing)
with open(output_filename, 'w') as my_file:
for word_ct in freq.most_common():
my_file.write(word_ct[0] + ' ' + str(word_ct[1]) + '\n') # write (word_ct[0], word_ct[1]) to file
for word in sorted(freq.items(), key=operator.itemgetter(1), reverse=True): #Sort by dict value - descending
print("{0}:{1}".format(word[0], word[1]))

@boratonAJ boratonAJ created this issue from a note in Words Counter Program (To Do) Apr 28, 2019
@boratonAJ boratonAJ moved this from To Do to In Progress in Words Counter Program Apr 28, 2019
@boratonAJ boratonAJ self-assigned this Apr 28, 2019
@boratonAJ boratonAJ added enhancement New feature or request good first issue Good for newcomers labels Apr 28, 2019
@boratonAJ
Copy link
Owner Author

Each of the source files is now created and they are now a python module

1.

import collections
import operator
import sys
def words_frequency(argv):
for filename in argv[1:]:
freq = collections.defaultdict(int)
freq = collections.Counter()
for line in open(filename, encoding='utf-8'):
words = line.strip().lower().split(' ')
for word in words:
freq.update([''.join(char for char in word if char.isalnum())])
del freq['']
output_filename = 'Data/output/' + filename.split("/")[2][:-3] +'freq' # substring the filename (slicing)
with open(output_filename, 'w') as my_file:
for word_ct in freq.most_common():
my_file.write(word_ct[0] + ' ' + str(word_ct[1]) + '\n') # write (word_ct[0], word_ct[1]) to file
for word in sorted(freq.items(), key=operator.itemgetter(1), reverse=True): #Sort by dict value - descending
print("{0}:{1}".format(word[0], word[1]))

2.

def words_frequency(argv):
for filename in argv[1:]:
freq = collections.defaultdict(int)
freq = collections.Counter()
for line in open(filename, encoding='utf-8'):
words = line.strip().lower().split(' ')
for word in words:
freq.update([''.join(char for char in word if char.isalnum())])
del freq['']
output_filename = 'Data/output/' + filename.split("/")[2][:-3] +'freq' # substring the filename (slicing)
with open(output_filename, 'w') as my_file:
for word_ct in freq.most_common():
my_file.write(word_ct[0] + ' ' + str(word_ct[1]) + '\n') # write (word_ct[0], word_ct[1]) to file
for word in sorted(freq.items(), key=operator.itemgetter(1), reverse=True): #Sort by dict value - descending
print("{0}:{1}".format(word[0], word[1]))

Words Counter Program automation moved this from In Progress to Done Apr 28, 2019
@boratonAJ boratonAJ added the question Further information is requested label Apr 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers question Further information is requested
Projects
Development

No branches or pull requests

1 participant