So, in an exercise in futility, I decided to write a script that will take either a file or a string and find patterns in the words, and display the results for a nice friendly human use.
Right now, it simply searches forwards and backwards(ish), but Im wondering if there is a cleaner way to iterate the middle part of the words without manually slicing them.
Any help is much appreciated:
import sys
try:
document = open(sys.argv[1], 'r')
except:
selection = input('Would you like to: \n1)Input a file name relative to path.\n2)Input a string\nSelection: ')
if selection == str(1):
doc = input('File: ')
try:
document = open(doc, 'r')
except Exception as e:
print (e + "File Not Found...")
elif selection == str(2):
document = input("String: ")
else:
print("There are only 2 choices here...")
sys.exit()
#define characters to ignore
extra_chars = "!@#$%^&*()-_=+?><,.;:'\"[]{}|\/"
patterns = dict()
try:
words = document.read().split()
except:
words = document.split()
for word in words:
for char in extra_chars:
word = word.replace(char, "")
for i in range(len(word)+1):
if i == 0:
pass
else:
p = word[0:i]
if p not in patterns:
patterns[p] = 1
else:
patterns[p] += 1
for i in range(len(word)+1):
if i==0:
pass
else:
p = word[-i:]
if p not in patterns:
patterns[p] = 1
else:
patterns[p] += 1
for k, v in sorted(patterns.items()):
print(k +": "+str(v))
sys.exit()