Skip to content

Instantly share code, notes, and snippets.

@gajewsk2
Created December 3, 2018 03:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gajewsk2/48e35f7be220a87a8efe77cfc16e43cb to your computer and use it in GitHub Desktop.
Save gajewsk2/48e35f7be220a87a8efe77cfc16e43cb to your computer and use it in GitHub Desktop.
anki matching for decks as text. For those times when you need to find the overlap of two sets of cards
MODIFIED_TAG = 'mita_scale'
def match(a, field_index_a, b, field_index_b):
matches = []
matched = {row[field_index_a]: True for row in a}
for row_b in b:
if row_b[field_index_b] in matched:
print(row_b[field_index_b])
matches.append(row_b)
return matches
def open_file(file_name):
with open(file_name + '.txt') as f:
file = f.readlines()
content = [[field for field in row.split('\t')] for row in file]
return content
def write_file(content, file_name):
with open(file_name + '.txt', 'w') as file:
for row in content:
file.write('\t'.join(row))
def modifier(content):
# hard-coding tag index as it could change otherwise should use list[-1]
modifier_index = len(content[0]) - 1
for row_idx, row in enumerate(content):
for entry_idx, entry in enumerate(row):
if entry_idx == modifier_index:
content[row_idx][modifier_index] = MODIFIED_TAG + ' ' + content[row_idx][modifier_index]
return content
def write_matched_fields(file_path_a, match_index_a, file_path_b, match_index_b, output_file):
a = open_file(file_path_a)
b = open_file(file_path_b)
content = modifier(match(a, match_index_a, b, match_index_b))
write_file(content, output_file)
if __name__ == '__main__':
write_matched_fields('base', 1, 'subset', 1, 'output')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment