-
Notifications
You must be signed in to change notification settings - Fork 2
/
sanskrit_tools.py
228 lines (205 loc) · 9.61 KB
/
sanskrit_tools.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Import necessary libraries
import pandas as pd
import json
# Load data
orthography = json.load(open("./data/orthography.json", "r"))
sandhi_data = json.load(open("./data/sandhi_data.json", "r"))
a_stem_declensions = json.load(open("./data/a_stem_declensions.json", "r"))
i_stem_declensions = json.load(open("./data/i_stem_declensions.json", "r"))
u_stem_declensions = json.load(open("./data/u_stem_declensions.json", "r"))
r_stem_declensions = json.load(open("./data/r_stem_declensions.json", "r"))
agent_nouns = open("./data/agent_nouns.csv", "r")
agent_nouns = agent_nouns.read()
relationship_nouns = open("./data/relationship_nouns.csv", "r")
relationship_nouns = relationship_nouns.read()
pres_ten_end = json.load(open("./data/pres_ten_end.json", "r"))
verb_classes = open("./data/verbs_classes.csv", "r")
verb_classes = verb_classes.read()
# text database
D_vowel = orthography["D_vowel"]
R_vowel = orthography["R_vowel"]
dia_d = orthography["dia_d"]
dia_r = orthography["dia_r"]
D_cons = orthography["D_cons"]
R_cons = orthography["R_cons"]
R_cons_na = orthography["R_cons_na"]
# combine all devanagari characters and diacritics
all_d = D_cons + D_vowel + dia_d
# combine romanizations of all devanagari characters and diacritics
all_r = R_cons + R_vowel + dia_r
# combine romanizations of all devanagari characters and diacritics, where the consonant characters
# have the implied *a dropped
all_na = R_cons_na + R_vowel + dia_r
# pairs for sandhi stuff
pairs11 = sandhi_data["pairs11"]
# sandhi joinings
joins11 = sandhi_data["joins11"]
pairs21 = sandhi_data["pairs21"]
joins21 = sandhi_data["joins21"]
pairs22 = sandhi_data["pairs22"]
joins22 = sandhi_data["joins22"]
cases = ["Nom.", "Voc.", "Acc.", "Ins.", "Dat.", "Abl.",
"Gen.", "Loc."]
columns = ['Singular','Dual','Plural']
index = ['First','Second','Third']
persons = ["third", "second", "first"]
pres_ten_end = pres_ten_end["pres_ten_end"]
# combined declensions
ma_dec = [a_stem_declensions["singdec_ma"], a_stem_declensions["dualdec_ma"], a_stem_declensions["plurdec_ma"]]
na_dec = [a_stem_declensions["singdec_na"], a_stem_declensions["dualdec_na"], a_stem_declensions["plurdec_na"]]
mi_dec = [i_stem_declensions["singdec_mi"], i_stem_declensions["dualdec_mi"], i_stem_declensions["plurdec_mi"]]
ni_dec = [i_stem_declensions["singdec_ni"], i_stem_declensions["dualdec_ni"], i_stem_declensions["plurdec_ni"]]
fi_dec = [i_stem_declensions["singdec_fi"], i_stem_declensions["dualdec_fi"], i_stem_declensions["plurdec_fi"]]
mu_dec = [u_stem_declensions["singdec_mu"], u_stem_declensions["dualdec_mu"], u_stem_declensions["plurdec_mu"]]
nu_dec = [u_stem_declensions["singdec_nu"], u_stem_declensions["dualdec_nu"], u_stem_declensions["plurdec_nu"]]
fu_dec = [u_stem_declensions["singdec_fu"], u_stem_declensions["dualdec_fu"], u_stem_declensions["plurdec_fu"]]
agent_r_masc = [r_stem_declensions["agent_r_masc_s"], r_stem_declensions["agent_r_masc_d"], r_stem_declensions["agent_r_masc_p"]]
rel_r_masc = [r_stem_declensions["rela_r_masc_s"], r_stem_declensions["rela_r_masc_d"], r_stem_declensions["rela_r_masc_p"]]
agent_r_fem = [r_stem_declensions["agent_r_fem_s"], r_stem_declensions["agent_r_fem_d"], r_stem_declensions["agent_r_fem_p"]]
rel_r_fem = [r_stem_declensions["rel_r_fem_s"], r_stem_declensions["rel_r_fem_d"], r_stem_declensions["rel_r_fem_p"]]
nr_dec = [r_stem_declensions["singdec_nr"], r_stem_declensions["dualdec_nr"], r_stem_declensions["plurdec_nr"]]
# function to process and display declensions
def list_declen(without_stem, declist):
singular = []
dual = []
plural = []
for i in range(0, 8):
singular.append(without_stem + declist[0][i])
dual.append(without_stem + declist[1][i])
plural.append(without_stem + declist[2][i])
finished = pd.DataFrame({'case':cases, 'singular':singular,
'dual':dual, 'plural':plural}).reindex(
['case', 'singular', 'dual', 'plural'], axis=1)
return(finished)
# determine which declensions to use
def decline(noun, gender):
stem_cut = noun[:len(noun)-1]
stem = noun[len(noun)-1:]
if stem == "a":
if gender == "masc":
output = list_declen(stem_cut, ma_dec)
elif gender == "neut":
output = list_declen(stem_cut, na_dec)
elif stem == "i":
if gender == "masc":
output = list_declen(stem_cut, mi_dec)
elif gender == "neut":
output = list_declen(stem_cut, ni_dec)
elif gender == "fem":
output = list_declen(stem_cut, fi_dec)
elif stem == "u":
if gender == "masc":
output = list_declen(stem_cut, mu_dec)
elif gender == "neut":
output = list_declen(stem_cut, nu_dec)
elif gender == "fem":
output = list_declen(stem_cut, fu_dec)
elif stem == "ṛ":
if gender == "masc":
if noun in agent_nouns:
output = list_declen(stem_cut, agent_r_masc)
elif noun in relationship_nouns:
output = list_declen(stem_cut, rel_r_masc)
if gender == "fem":
if noun in agent_nouns:
output = list_declen(stem_cut, agent_r_fem)
elif noun in relationship_nouns:
output = list_declen(stem_cut, rel_r_fem)
if gender == "neut":
output = list_declen(stem_cut, nr_dec)
else:
output = "noun not recognized"
return(output)
def conjugate(verb, verb_class):
weak_vowels = ["a", "ī", "ā", "i", "u", "ū", "ṛ", "ṝ", "ḷ"]
guna_vowels = ["a", "e", "a", "e", "o", "o", "ar", "ar", "al"]
if verb_class == "1" or verb_class == "I":
first = ["āmi", "āvaḥ", "āmaḥ"]
second = ["asi", "athaḥ", "atha"]
third = ["ati", "ataḥ", "anti"]
splitted = list(verb)
for i in range(0, len(splitted)):
if splitted[i] in weak_vowels:
splitted[i] = guna_vowels[weak_vowels.index(splitted[i])]
if splitted[len(splitted)-1:][0] == "e":
splitted[len(splitted)-1:] = "ay"
elif splitted[len(splitted)-1:][0] == "o":
splitted[len(splitted)-1:] = "av"
elif splitted[len(splitted)-1:][0] == "ai":
splitted[len(splitted)-1:] = "āy"
together = ''.join(str(x) for x in splitted)
for i in range(0, len(first)):
first[i] = together + first[i]
second[i] = together + second[i]
third[i] = together + third[i]
return(pd.DataFrame([first, second, third], index=index, columns=columns))
if verb_class == "4" or verb_class == "IV":
first = ["mi", "vaḥ", "maḥ"]
second = ["si", "thaḥ", "tha"]
third = ["ti", "taḥ", "nti"]
splitted = list(verb)
together = ''.join(str(x) for x in splitted)
for i in range(0, len(first)):
first[i] = together + "ya" + first[i]
second[i] = together + "ya" + second[i]
third[i] = together + "ya" + third[i]
return(pd.DataFrame([first, second, third], index=index, columns=columns))
if verb_class == "6" or verb_class == "VI":
first = ["āmi", "āvaḥ", "āmaḥ"]
second = ["āsi", "āthaḥ", "ātha"]
third = ["āti", "ātaḥ", "ānti"]
chopped = verb[:len(verb)-3]
for i in range(0, len(first)):
first[i] = chopped + first[i]
second[i] = chopped + second[i]
third[i] = chopped + third[i]
return(pd.DataFrame([first, second, third], index=index, columns=columns))
# maybe include an option for clusters
def devanagari(kind):
if kind == "vowels":
finished_sing = pd.DataFrame({'D_vowel':D_vowel, 'R_vowel':R_vowel}).reindex(
['D_vowel', 'R_vowel'], axis=1)
return(finished_sing)
elif kind == "consonants":
finished_sing = pd.DataFrame({'D_cons':D_cons, 'R_cons':R_cons}).reindex(
['D_cons', 'R_cons'], axis=1)
return(finished_sing)
else:
return("error: invalid argument")
# code to switch out consonant syllables in form *a with just *, where the is a diacritic vowel
# also concatenates and nicely outputs the final text
# takes arguments for the two lists of characters as well as the length of the lists
def switch(split_characters, length_of):
for i in range(0, length_of):
# if a diacritic vowel comes next, replace current consonant with one not ending in a
if split_characters[0][i] in dia_d:
split_characters[1][i-1] = all_na[all_r.index(split_characters[1][i-1])]
# join all characters together as a continuous stream of text; first the devanagari, then the
# transliteration
return(''.join(str(x) for x in split_characters[0]) + ' → ' + ''.join(str(x) for x in split_characters[1]))
# code to parse through the text, split into individual characters and find corresponding locations in arrays
# between devanagari and roman characters
def parse(text):
# vectors for storing romanized and devanagari output
output_r = []
output_d = []
# store locations of diacritic characters
locations = []
for i in range(0, len(text)):
place = all_d.index(text[i])
#locations.append(place)
output_d.append(all_r[place])
output_r.append(text[i])
return([output_r, output_d])
# make parse and switch functions work together
def romanize(dev_text):
return(switch(parse(dev_text), len(dev_text)))
columns = ['Singular','Dual','Plural']
index = ['First','Second','Third']
# Some examples
romanize("सत्यमेव जयते")
print(decline("Śiva", "masc"))
print(conjugate("nṛt", "IV"))
print(verb_classes)