def create_log_entry(): logger = jlogger.Logger() logger.create_entry('', 1) logger.save() filename = os.path.join(DATA_PATH, FILENAME) subprocess.run(['vim', '+normal G$', filename])
def edit_log_entry(id): logger = jlogger.Logger() entry = logger.get_entry_by_id(id) filename = os.path.join(DATA_PATH, FILENAME) if not os.path.isfile(filename): raise ValueError(filename + ' does not exist') subprocess.run(['vim', '+normal %dgg$' % entry.line_num, filename])
def stats(): logger = jlogger.Logger() token_counts = [] for e in logger.entries: num_tokens = len(e.get_tokens()) token_counts.append(num_tokens) mean = sum(token_counts) / len(token_counts) dev = [(c - mean)**2 for c in token_counts] stddev = math.sqrt(sum(dev) / len(token_counts)) print('Average tokens:', mean) print('STD DEV tokens:', stddev) print('Min tokens:', min(token_counts)) print('Max tokens:', max(token_counts))
def vocab(): words = [] kps = load_knowledge() for key in kps: words += jlogger.tokenize(key) words += jlogger.tokenize(kps[key]['text']) logger = jlogger.Logger() for e in logger.entries: words += e.get_tokens() words = [w for w in words] with open(os.path.join(data_path, 'vocab.txt'), 'w') as f: counter = Counter(words) for w in counter.most_common(): f.write(w[0] + ' ' + str(w[1]) + '\n')
def overview(tag_name=None): logger = jlogger.Logger() tags = [] if tag_name: tags = logger.tags[tag_name].get_child_tags() else: tags = logger.get_tags() for t in tags[:5]: t.print_summary() print(bcolors.HEADER + '==============================' + bcolors.ENDC) print(bcolors.HEADER + 'Remaining Tags' + bcolors.ENDC) print(bcolors.HEADER + '==============================' + bcolors.ENDC) tag_names = {t.name for t in tags} for t in tags[5:]: dt = datetime.datetime.strftime(t.modified_at, "%Y-%m-%d %H:%M:%S") print('%s: %d (%s)' % (t.name, len(t.entries), dt))
def get_titles(): titles = {} logger = jlogger.Logger() for e in logger.entries: titles[e.title] = e return titles
def process_query(args): query = ' '.join(args.command) if query == 'sync': return sync(dry_run=bool(args.dry_run), verbose=bool(args.verbose)) if args.command[0] == 'replace': if len(args.command) < 2: return q = args.command[1] logger = jlogger.Logger() e = logger.get_log_entry_by_id(int(q)) if e is None: print('Entry does not exist') else: logger.replace_log_entry(e) return if args.command[0] == 'edit': if len(args.command) < 2: return q = args.command[1] edit_log_entry(int(q)) return if args.command[0] == 'autoformat': if len(args.command) < 2: return logger = jlogger.Logger() return logger.autoformat(args.command[1]) if args.command[0] == 'chrono': if len(args.command) < 3: return logger = jlogger.Logger() return logger.add_chrono(args.command[1], args.command[2]) if query == 'log': return create_log_entry() if query == 'view': return view_log(args.n) if query == 'titles': return view_titles() if query == 'chronos': return view_chronos() if query == 'vocab': return vocab() if query == 'tags': return tags() if query == 'bak': return backup() if args.command[0] == 'overview': if len(args.command) > 1: return overview(args.command[1]) return overview() if query == 'archive': logger = jlogger.Logger() return logger.archive() if query == 'stats': return stats() if query == 'hier': logger = jlogger.Logger() return print_tag_hierarchy(logger.main_tag) if query == 'write': logger = jlogger.Logger() return logger.write_full() if query == 'load': logger = jlogger.Logger() return logger.load('jmfveneroso.txt') if query == 'checkpoint': titles = get_titles() if 'Checkpoint' in titles: entry = titles['Checkpoint'] text = ' '.join(entry['text']).strip() checkpoint_date = datetime.datetime.strptime(text, '%Y-%m-%d') days_to_checkpoint = abs(checkpoint_date - datetime.datetime.now()).days print('%d days remaining to the next checkpoint' % days_to_checkpoint) return if not process_knowledge_piece(query): search(query, bool(args.all))
def tags(): logger = jlogger.Logger() tags = logger.get_tags() for t in tags: dt = datetime.datetime.strftime(t.modified_at, "%Y-%m-%d %H:%M:%S") print("%s (%d): %s" % (t.name, len(t.entries), dt))
def search(q, show_all=False): logger = jlogger.Logger() v = jlogger.load_vocab() tkns = jlogger.tokenize(q) if len(tkns) == 1: if try_exact_match(logger, tkns[0].lower()): return if tkns[0] in logger.tags: entries = logger.tags[tkns[0]].get_entries() tkns = tkns[1:] else: entries = logger.entries tkns = [get_closest_word(t, v) for t in tkns] tkn_set = {t for t in tkns} scored_entries = [] for e in reversed(entries): words = e.get_tokens() score = 0 norm = 0 for w in words: if w in v: if w in tkn_set: score += (1.0 / v[w])**2 norm += (1.0 / v[w])**2 if norm > 0.0: score /= math.sqrt(norm) scored_entries.append([score, e]) scored_entries = [e for e in scored_entries if e[0] > 0.0] for i in range(len(scored_entries)): e = scored_entries[i][1] scored_entries = sorted(scored_entries, key=lambda e: e[0], reverse=True) if len(scored_entries) == 0: print("No results found") return if show_all: for e in scored_entries: e.print_summarized() return global orig_settings cursor = 0 pressed_key = None while pressed_key != chr(27): subprocess.call('clear') print('Query:', bcolors.HEADER + q + bcolors.ENDC + ' ' + ' '.join(tkns)) print('Showing result %d of %d' % (cursor + 1, len(scored_entries))) print() entry = scored_entries[cursor] entry[1].print_detailed() tty.setcbreak(sys.stdin) pressed_key = sys.stdin.read(1)[0] termios.tcsetattr(sys.stdin, termios.TCSADRAIN, orig_settings) if pressed_key == 'j' or pressed_key == 13: cursor = cursor + 1 if cursor < len(scored_entries) - 1 else cursor elif pressed_key == 'k': cursor = cursor - 1 if cursor > 0 else cursor elif pressed_key == chr(10): return elif pressed_key == 'q': return
def view_log(n): logger = jlogger.Logger() print_entries(logger.get_entries(), n)