-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdas_jäschke_dic_search.py
135 lines (115 loc) · 4.13 KB
/
das_jäschke_dic_search.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import tk
import os
import re
import sys
import traceback
import easygui
from pathlib import Path
from os import listdir
import time
import pyewts
from tibetan_sort.tibetan_sort import TibetanSort
from collections import namedtuple
import random
import applescript
def abort(message):
exc = traceback.format_exc()
print(exc + message)
sys.exit()
def Skim_go_to_page(dic_name, dic_page, offset=0):
scpt = f'''
tell application "Skim"
set p to documents
repeat with i from 1 to count p
if the name of p's item i begins with "{dic_name}" then set doc_counter to i
end repeat
tell document doc_counter
go to page {dic_page + offset}
end tell
end tell'''
r = applescript.run(scpt)
def find_closest_header_page(query, dic_name):
if dic_name == "Jäschke":
headers = jaschke_headers
elif dic_name == "Das":
headers = das_headers
if not query.endswith("་"):
query += "་"
if query in [x.word for x in headers]:
offset = 0
else:
offset = -1
index = sorter.sort_list(
[x.word for x in headers] + [query]).index(query) + offset
return int(headers[index].page)
def sorter_test(ordered_headers_list):
jumbled_headers_list = ordered_headers_list[:]
random.shuffle(jumbled_headers_list)
jumbled_headers_list = sorter.sort_list(
x.word for x in jumbled_headers_list)
range_list_length = range(len(ordered_headers_list))
for count, x, y in zip(
range_list_length, ordered_headers_list, jumbled_headers_list):
message = ""
if x.word != y:
message = "failure"
print(count, x, "|||", y, "|||", message)
def wordlist_test():
test_words_J = [tibWord("ཁ་ག་པོ་", 36),
tibWord("གོང་མ་", 72),
tibWord("མགུར་ལྷ་", 90),
tibWord("བགྲོང་བ་", 90),
tibWord("ཚ་འཁྲུ་", 442),
tibWord("ཚང་ངུ", 444)]
test_words_D = [tibWord("བསམ་བྱ་", 1317),
tibWord("ལ་པ་ཤ་", 1201),
tibWord("དད་འདུན་", 617),
tibWord("པོ་ཏ་ལ་", 785),
tibWord("མང་བ་", 952),
tibWord("སྐྱེར་སྐྱ་", 110),
tibWord("རྒྱབ་པ་", 309),
tibWord("ལྕོག་", 401)]
for test_word in test_words_D:
closest_header_page = find_closest_header_page(
test_word.word, dic_name="Das")
print(test_word.page, closest_header_page)
def remove_wasur(tib_string):
return tib_string.replace("ྭ","")
def build_headers(file):
headers = []
try:
with open(file, "r") as file:
lines = file.readlines()
for line in lines:
if "\t" not in line:
query = re.search(r"(\d+)\.\s([^(]+)", line.rstrip())
page, tibText = query.group(
1), remove_wasur(query.group(2))
word = tibWord(tibText, page)
headers.append(word)
except BaseException:
abort(f"{file}|{line}")
return headers
sorter = TibetanSort()
home = str(Path.home())
tibWord = namedtuple("word", "word page")
jaschke_headers_file = os.path.join(
home,
"Documents",
"Code",
"tib_dic_utilities",
"jäschke_headers.md")
das_headers_file = os.path.join(
home,
"Documents",
"Code",
"tib_dic_utilities",
"das_headers.md")
# query = easygui.enterbox("Tibetan word to be found.")
query = remove_wasur(os.environ['KMVAR_local_query'])
das_headers = build_headers(file=das_headers_file)
jaschke_headers = build_headers(file=jaschke_headers_file)
page_J = find_closest_header_page(query, dic_name="Jäschke")
page_D = find_closest_header_page(query, dic_name="Das")
Skim_go_to_page(dic_name="Jäschke", dic_page=page_J, offset=26)
Skim_go_to_page(dic_name="Das_Tibetan", dic_page=page_D, offset=40)