-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindpairs.py
46 lines (35 loc) · 1.35 KB
/
findpairs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import sys
import csv
if len(sys.argv) < 3:
print("use syntax: findpairs.py [character-list.txt] [text-to-search.txt] [optional-output-file.csv]")
exit()
character_file = ""
source_file = ""
output_file = ""
if len(sys.argv) >= 3:
character_file = sys.argv[1]
source_file = sys.argv[2]
if len(sys.argv) >= 4:
output_file = sys.argv[3]
characters = []
source = ""
with open(character_file) as character_list:
characters = character_list.read().splitlines()
with open(source_file) as source_text:
source = source_text.read()
pairList = []
for i in range(len(characters)):
for j in range(len(characters)):
if j > i:
count = source.count(characters[i] + " and " + characters[j]) + source.count(characters[j] + " and " + characters[i])
if count > 0:
pairList.append({"Character 1": characters[i], "Character 2": characters[j], "Count": count})
pairList = sorted(pairList, key=lambda pair: pair["Count"], reverse=True)
for pair in pairList:
print(pair["Character 1"] + "," + pair["Character 2"] + "," + str(pair["Count"]))
if output_file != "":
with open(output_file, "w") as pairs_csv:
pair_writer = csv.DictWriter(pairs_csv, fieldnames=["Character 1", "Character 2", "Count"])
pair_writer.writeheader()
for pair in pairList:
pair_writer.writerow(pair)