Created
April 16, 2024 14:18
-
-
Save UncleCJ/3bc319851dc2b02325730e5b400f81ee to your computer and use it in GitHub Desktop.
One of my first attempts at a somewhat advanced Gramps SuperTool script. Gramps is an open source genealogy software, and the purpose of this script is to iterate over a number of Citations (or anything which holds notes), matching, rewriting and summarising matches to a list of regular expressions. In this case, one regex match NAD citations an…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Gramps SuperTool script file] | |
version=1 | |
[title] | |
SuperTool-Note-Regex-Collator | |
[description] | |
[category] | |
Citations | |
[initial_statements] | |
from typing import List, Tuple | |
import re | |
from collections import Counter | |
NAD_pattern = re.compile(r"([-\w ]+) kyrkoarkiv.*SE/VALA/[^/]+/([^/]+/\S+) \((\d+)-(\d+)\).*sida (\d+)") | |
MH_pattern = re.compile(r"(?:Bok|Book)[:\s]*(?:[-_:\w\s]*(?:Bok|Book)[:\s]*)?([\w\s]+)[\s_](AI[^-,]+)[^,]*, (\d+) - (\d+).*(?:[pP]age|[sS]ida)[:\s]*(\d+)") | |
patterns = (NAD_pattern, MH_pattern) | |
c = Counter() | |
def groupsfilter(groups: List[str]) -> Tuple[str]: | |
if groups[0][-1] != 's': | |
groups[0] = groups[0] + 's' | |
groups[0] = re.sub('_', ' ', groups[0]) | |
groups[1] = re.sub(' |/', '', groups[1]) | |
return tuple(groups) | |
def tuples_item_list(tuple_list: List[Tuple], i: int) -> List: | |
return list(t[i] for t in tuple_list) | |
def counter_formatter(c: Counter) -> List: | |
return list(map(lambda x: ( | |
str(x[1]).zfill(3), | |
x[0] if type(x[0]) == str else ' '.join(x[0]) | |
), c)) | |
[statements] | |
for note in notes: | |
if not (read_note := db.get_note_from_gramps_id(note.gramps_id)): | |
continue | |
for pattern in patterns: | |
if not (firstmatch := pattern.search(read_note.get())): | |
continue | |
c.update([ | |
groupsfilter(list(firstmatch.groups())) | |
]) | |
[filter] | |
[expressions] | |
counter_formatter(c.most_common(999)) | |
[scope] | |
selected | |
[unwind_lists] | |
True | |
[commit_changes] | |
False | |
[summary_only] | |
True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment