def replace_words(self, filename, initial_word, replacement_word): with open(filename, 'r') as input: with open('../texts/new_%s' % self.filename, 'w') as output: for line in input: line = line.rstrip() newline = line.replace(initial_word, replacement_word) output.write(newline) if __name__ == '__main__': # Read command line inputs old_filename = sys.argv[1] base = os.path.basename(old_filename) old_word = sys.argv[2] new_word = sys.argv[3] Replace(old_filename, old_word, new_word) # Check wordcount old_file_count = WC(sys.argv[1]) new_filename = '../texts/new_%s' % base new_file_count = WC(new_filename) # Output stats - optional print("Old Wordcount", old_file_count.word_count()) print("WC __name__:", WC.__name__) print("New Wordcount", new_file_count.word_count()) print("replacefile __name__:", __name__)
def colman_liau(filename): file_operator = WC(filename) index = 0.0588 * get_letters(file_operator) - 0.296 * get_sentences( file_operator) - 15.8 return ("Reading Grade Level of Text:", round(index))
def get_sentences(filename): S = WC.sentence_count(filename)[1] / WC.word_count(filename)[1] * 100 return S
def get_letters(filename): L = WC.character_count(filename)[1] / WC.word_count(filename)[1] * 100 return L
"""Tests textedit functionality of the textedit module on the pool_of_tears.txt file""" from review import readability #import everything in the file named readability from review.wordcount import WC #import classes specifically from edit.spacing import Spacing from edit.replace import Replace test_file = '../texts/pool_of_tears.txt' # Count words print("Wordcount.py") alice = WC(test_file) WC.counts(alice) print("\n") # Readability Index print("readability.py") print(readability.colman_liau(test_file)) print("\n") # Change Spacing print("spacing.py") sp = Spacing(test_file) Spacing.spacing_check(sp) print("Spaces replaced") print("\n") # Replace Words print("replace.py") Replace(test_file, "Alice", "Dora the Explorer")
#!/usr/bin/env python import unittest import sys import os from review.wordcount import WC # import wordcount from its relative path WC_test = WC('../texts/alice.txt') class Test(unittest.TestCase): wc = ('Words:', 274) sc = ('Sentences:', 7) cc = ('Letters:', 1120) def test_wc(self): self.assertCountEqual(WC_test.word_count(), self.wc) def test_sentences(self): self.assertEqual(WC_test.sentence_count(), self.sc) def test_characters(self): self.assertEqual(WC_test.letter_count(), self.cc) if __name__ == '__main__': unittest.main()