#!/usr/bin/env python3

# Copyright (c) 2026 Bible Code Lab
# You may share and modify this code only for the purpose of improvement.
# Modifications intended to alter, bias, or manipulate computational results,
# or to introduce malicious behavior, are strictly prohibited.
# This software is provided "as is", without warranty of any kind.
# 
# SCRIPT NAME:  kjv-words-to-dict.py
#
# DESCRIPTION:  Generates a Mispar ha-Akhor gematria dictionary of the KJV Bible words
#
# AUTHOR:       Bible Code Lab
#
# DATE:         2026-02-12
#
# VERSION:      1.0
#
# TESTED ON:    Python 3.13.5
#
# USAGE:        $ python3 kjv-words-to-dict.py

import re
import json
import urllib.request

url = 'https://kjvcode.com/wp-content/uploads/2024/05/KJV-Concord-1769-Words-Per-Line-1.txt'
filename = 'KJV-Concord-1769-Words-Per-Line-1.txt' 

def weighted_gematria(text: str) -> int:
    total = 0
    cont = 1
    for ch in text.upper():
        if 'A' <= ch <= 'Z':
            total += (ord(ch) - ord('A') + 1) * cont
            cont = cont + 1
    return total

urllib.request.urlretrieve(url, filename)

with open(filename, 'r') as file:
    kjv_wg_dict = {}
    for line in file:
        word = line.strip().split()[1]
        wordf = re.sub(r"[^a-zA-Z]+", "", word)
        wordf = wordf.lower()
        k = weighted_gematria(wordf)
        if (k in kjv_wg_dict):
            list = kjv_wg_dict[k]
            if wordf not in list:
                list.append(wordf)
        else:
            list = []
            list.append(wordf)
            kjv_wg_dict.update({k:list})

    file = open('kjv-wg-word-dict.txt', 'w')
    print(json.dumps(kjv_wg_dict, sort_keys=True, indent=4), file = file)
    file.close()

