Skip to content
This repository has been archived by the owner on Jun 15, 2024. It is now read-only.

Commit

Permalink
dev: add notebook analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
doomspec committed Oct 9, 2023
1 parent f05b481 commit 0dff970
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 12 deletions.
2 changes: 1 addition & 1 deletion evonote/indexing/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ def process_note_without_content(cls, notes: List[Note], indexing: Indexing,
new_weights = []

for note in notes:
keywords_on_path = note.get_note_path()
keywords_on_path = note.note_path()
# keep last 1/3 of the keywords
n_keywords = min(max(math.ceil(len(keywords_on_path) / 3), 3),
len(keywords_on_path))
Expand Down
40 changes: 40 additions & 0 deletions evonote/notebook/analysis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from evonote.notebook.notebook import Notebook


def analyze_notebook_sparsity(notebook: Notebook):
"""
Return the average number of children per note.
"""
notes = notebook.get_note_list()
max_children = -1
total_children = 0
n_non_leaf_notes = 0
for note in notes:
n_children = len(note.children())
if n_children > 0:
n_non_leaf_notes += 1
total_children += n_children
max_children = max(max_children, n_children)
average_children = total_children / n_non_leaf_notes
heavy_notes = get_children_heavy_notes(notebook)
print("Total_notes:", len(notes))
print("Average children per non-leaf note:", average_children)
print("Max children per note:", max_children)
# print heavy notes
for note in heavy_notes:
print("Note:", note.note_path())
print("Children:", len(note.children()))
print("Content:", note.content)
print("")

def get_children_heavy_notes(notebook: Notebook, min_children=10):
"""
Return a list of notes with at least min_children children.
"""
notes = notebook.get_note_list()
heavy_notes = []
for note in notes:
n_children = len(note.children())
if n_children >= min_children:
heavy_notes.append(note)
return heavy_notes
20 changes: 13 additions & 7 deletions evonote/notebook/note.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

from copy import copy
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Dict

if TYPE_CHECKING:
from evonote.notebook.notebook import Notebook
Expand Down Expand Up @@ -38,15 +38,21 @@ def copy_to(self, notebook: Notebook):
## Functions for getting the relation of notes
"""

def get_note_path(self):
def note_path(self):
return self.notebook.get_note_path(self)

def get_parent(self):
def parent(self):
return self.notebook.get_parent(self)

def get_children(self):
def children(self) -> Dict[str, Note]:
return self.notebook.get_children_dict(self)

def title(self) -> str:
note_path = self.note_path()
if len(note_path) == 0:
return ""
return note_path[-1]

def has_child(self, key: str):
return self.notebook.has_child(self, key)

Expand Down Expand Up @@ -100,12 +106,12 @@ def be(self, content: str) -> Note:

def __str__(self):
if len(self.content) == 0:
return "Path" + str(self.get_note_path())
return "Path" + str(self.note_path())
return self.content

def __repr__(self):
return f"<{self.__class__.__name__}> {str(self.get_note_path())}"
return f"<{self.__class__.__name__}> {str(self.note_path())}"


class NoteResource:
def __init__(self):
Expand Down
7 changes: 3 additions & 4 deletions evonote/test/notebook/test_notebook_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ def test_adding_and_parent():
note1 = notebook.get_new_note_by_path(["a", "b", "c"]).be("test1")
note2 = notebook.add_note_by_path(["a", "b", "d"], "test2")
note3 = notebook.get_note_by_path(["a", "b"]).be("test3")
a = note2.get_parent()
assert note1.get_parent().content == "test3" == note3.content == note2.get_parent().content
assert note1.parent().content == "test3" == note3.content == note2.parent().content


def test_adding_twice():
notebook = Notebook("test")
note1 = notebook.get_new_note_by_path(["a", "b", "c"]).be("test1")
note2 = notebook.add_note_by_path(["a", "b", "c"], "test2")
notebook.get_new_note_by_path(["a", "b", "c"]).be("test1")
notebook.add_note_by_path(["a", "b", "c"], "test2")
assert notebook.get_note_by_path(["a", "b", "c"]).content == "test2"


Expand Down
6 changes: 6 additions & 0 deletions playground/analyze_project.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from evonote.notebook.analysis import analyze_notebook_sparsity
import evonote
from evonote.transform.module_to_notebook import get_notebook_for_module

notebook = get_notebook_for_module(evonote)
analyze_notebook_sparsity(notebook)

0 comments on commit 0dff970

Please sign in to comment.