2005-02-07 21:15:16 +00:00
|
|
|
#!/usr/local/bin/python
|
|
|
|
#
|
|
|
|
# $FreeBSD$
|
|
|
|
#
|
|
|
|
# an aggressive little script for trimming duplicate cookies
|
2015-11-19 00:01:52 +00:00
|
|
|
from __future__ import print_function
|
2015-04-02 22:42:23 +00:00
|
|
|
import argparse
|
|
|
|
import re
|
2005-02-07 21:15:16 +00:00
|
|
|
|
|
|
|
wordlist = [
|
|
|
|
'hadnot',
|
|
|
|
'donot', 'hadnt',
|
|
|
|
'dont', 'have', 'more', 'will', 'your',
|
|
|
|
'and', 'are', 'had', 'the', 'you',
|
|
|
|
'am', 'an', 'is', 'll', 've', 'we',
|
|
|
|
'a', 'd', 'i', 'm', 's',
|
|
|
|
]
|
|
|
|
|
2015-04-02 22:42:23 +00:00
|
|
|
|
2005-02-07 21:15:16 +00:00
|
|
|
def hash(fortune):
|
|
|
|
f = fortune
|
|
|
|
f = f.lower()
|
|
|
|
f = re.sub('[\W_]', '', f)
|
|
|
|
for word in wordlist:
|
|
|
|
f = re.sub(word, '', f)
|
|
|
|
# f = re.sub('[aeiouy]', '', f)
|
|
|
|
# f = re.sub('[^aeiouy]', '', f)
|
|
|
|
f = f[:30]
|
|
|
|
# f = f[-30:]
|
|
|
|
return f
|
|
|
|
|
2015-04-02 22:42:23 +00:00
|
|
|
|
2005-02-07 21:15:16 +00:00
|
|
|
def edit(datfile):
|
|
|
|
dups = {}
|
|
|
|
fortunes = []
|
|
|
|
fortune = ""
|
2015-04-02 22:42:23 +00:00
|
|
|
with open(datfile, "r") as datfiledf:
|
|
|
|
for line in datfiledf:
|
|
|
|
if line == "%\n":
|
|
|
|
key = hash(fortune)
|
|
|
|
if key not in dups:
|
|
|
|
dups[key] = []
|
|
|
|
dups[key].append(fortune)
|
|
|
|
fortunes.append(fortune)
|
|
|
|
fortune = ""
|
|
|
|
else:
|
|
|
|
fortune += line
|
2012-10-22 02:29:56 +00:00
|
|
|
for key in list(dups.keys()):
|
2005-02-07 21:15:16 +00:00
|
|
|
if len(dups[key]) == 1:
|
|
|
|
del dups[key]
|
2015-04-02 22:42:23 +00:00
|
|
|
with open(datfile + "~", "w") as o:
|
|
|
|
for fortune in fortunes:
|
|
|
|
key = hash(fortune)
|
|
|
|
if key in dups:
|
|
|
|
print('\n' * 50)
|
|
|
|
for f in dups[key]:
|
|
|
|
if f != fortune:
|
|
|
|
print(f, '%')
|
|
|
|
print(fortune, '%')
|
|
|
|
if input("Remove last fortune? ") == 'y':
|
|
|
|
del dups[key]
|
|
|
|
continue
|
|
|
|
o.write(fortune + "%\n")
|
2005-02-07 21:15:16 +00:00
|
|
|
|
2015-04-02 22:42:23 +00:00
|
|
|
parser = argparse.ArgumentParser(description="trimming duplicate cookies")
|
|
|
|
parser.add_argument("filename", type=str, nargs=1)
|
|
|
|
args = parser.parse_args()
|
|
|
|
edit(args.filename[0])
|