Skip to content

Instantly share code, notes, and snippets.

@ap-Codkelden
Created October 14, 2020 12:09
Show Gist options
  • Save ap-Codkelden/7c8fa80444cd8708de4b178e888b210d to your computer and use it in GitHub Desktop.
Save ap-Codkelden/7c8fa80444cd8708de4b178e888b210d to your computer and use it in GitHub Desktop.
Manually labeled record pairs
import tkinter as tk
from tkinter.filedialog import askopenfilename
from tkinter import messagebox
from tempfile import gettempdir
from tkinter import ttk
import json
import pickle
class AnnotationGenerator(object):
def __init__(self, path):
with open(path) as f:
self.annotation_data = json.load(f)
def annotation_generator(self):
for pair in self.annotation_data['pairs']:
records = [pair['identifiers'][ids]["record"] for ids in pair['identifiers']]
rows_container = []
for field in pair['fields']:
field_name = field["a"]['name']
field_designators = [k for k in field.keys() if k!="similarity"]
values = [val for val in [field[k]["value"] for k in field_designators]]
if len(set(values)) == 1:
continue
# TODO
# форматтер значень -- дата як дата, None як ""
rows_container.append([field_name] + values)
yield {"rows": rows_container, "keys": records}
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.master.resizable(width=False, height=False)
self.create_widgets()
self.json_path = None
self.pack(fill='both', expand=True)
self.current_key_pair = None
self.id_store = []
def open_file(self):
path = askopenfilename(initialdir = gettempdir(),
filetypes=(("JSON", "*.json"), ("All Files", "*.*")),
title="Choose a file.")
self.dataset.delete(0, tk.END)
self.dataset.insert(0, path)
if not path:
return
self.json_path = path
ag = AnnotationGenerator(self.json_path)
self.generator = ag.annotation_generator()
def create_widgets(self):
self.dataset = tk.Entry(self)
self.dataset.grid(row=0, column=0, columnspan=3)
self.terminal_scrollbar = tk.Scrollbar(root)
self.file_open = tk.Button(self, command=self.open_file,
text="...").grid(row=0, column=3)
self.process = tk.Button(self, text="Start",
command=self.open_compare).grid(row=2, column=2)
self.quit = tk.Button(
self, text="Quit",command=self.save_and_quit).grid(
row=2, column=1,pady=5, padx=5)
def save_exchange(self):
if self.id_store:
with open('codes_exchange.pickle', 'wb') as f:
pickle.dump(self.id_store, f)
def save_and_quit(self):
self.save_exchange()
self.master.destroy()
def open_compare(self):
self.compare = tk.Toplevel()
self.compare.geometry("650x350")
self.compare.grid_rowconfigure(0, weight=1)
self.compare.grid_columnconfigure(0, weight=1)
cols = ('Атрибут', 'Значення1', 'Значення2')
# Treeview as table
self.listBox = ttk.Treeview(self.compare, columns=cols, show='headings')
self.listBox.configure(yscrollcommand=self.terminal_scrollbar.set)
#self.listBox.columnconfigure(0, weight=1)
#self.listBox.rowconfigure(0, weight=1)
self.listBox.grid(row=0, column=0, columnspan=4,
pady=5, padx=2, sticky=tk.NSEW)
# column headings
for col in cols:
self.listBox.heading(col, text=col)
match = tk.Button(self.compare, text="MATCH", width=15, bg="green",
activebackground="green", command=self.click_match)
distinct = tk.Button(self.compare, text="DISTINCT", width=15, bg="red",
activebackground="red", command=self.click_distinct)
save_data = tk.Button(self.compare, text="SAVE", width=15,
command=self.save_exchange)
save_data.grid(row=4, column=0, pady=5, sticky=tk.W)
match.grid(row=4, column=1, pady=5, sticky=tk.W)
distinct.grid(row=4, column=2, pady=5)
self.show()
def _clear_treeview(self):
self.listBox.delete(*self.listBox.get_children())
def click_distinct(self):
# print(self.current_key_pair, "loss")
self.show()
def click_match(self):
# print(self.current_key_pair, "stored")
self.id_store.append(self.current_key_pair)
self.show()
def show(self):
try:
# {"rows": rows_container, "keys": records}
table_data = next(self.generator)
self.current_key_pair = table_data["keys"]
except StopIteration:
self.save_exchange()
self.compare.destroy()
while tk.messagebox.showinfo(
title="The END", message="Нарешті все") != "ok":
return True
self.master.destroy()
return
# childrens = self.listBox.get_children()
self._clear_treeview()
for (index, row) in enumerate(table_data["rows"], 1):
tag = 'even' if (index % 2 == 0) else 'odd'
self.listBox.insert("", "end", values=(row), tags=(tag,))
# return {'status': 0, "keys": table_data['keys']}
self.listBox.tag_configure('even', background='#E8E8E8')
root = tk.Tk()
app = Application(master=root)
app.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment