mirror of
https://gitlab.com/walljm/dynamicbible.git
synced 2025-07-29 02:29:50 -04:00
339 lines
15 KiB
C#
339 lines
15 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading;
|
|
using System.Windows.Forms;
|
|
using System.Xml;
|
|
using System.Xml.Linq;
|
|
|
|
namespace DynamicBibleUtility
|
|
{
|
|
public partial class frmMain : Form
|
|
{
|
|
List<string> _words = new List<string>();
|
|
Index _idx = new Index();
|
|
public frmMain()
|
|
{
|
|
InitializeComponent();
|
|
}
|
|
private List<string> _exclusions = new List<string>() { "us", "these", "her", "saith", "shalt", "let", "do", "your", "we", "no", "go", "if", "at", "an", "so", "before", "also", "on", "had", "you", "there", "then", "up", "by", "upon", "were", "are", "this", "when", "thee", "their", "ye", "will", "as", "thy", "my", "me", "have", "from", "was", "but", "which", "thou", "all", "it", "with", "them", "him", "they", "is", "be", "not", "his", "i", "shall", "a", "for", "unto", "he", "in", "to", "that", "of", "and", "the" };
|
|
private char[] _trims = new char[] {'\'', ',', ':', ';', '"', '?', '.', '[', ']', '{', '}', '<', '>', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '=', '+' };
|
|
|
|
public delegate void UpdateStatusDelegate(string status);
|
|
private void UpdateStatus(string statusMsg)
|
|
{
|
|
if (this.txtStatus.InvokeRequired)
|
|
{
|
|
txtStatus.Invoke(new UpdateStatusDelegate(UpdateStatus), statusMsg);
|
|
}
|
|
else
|
|
{
|
|
txtStatus.AppendText(statusMsg);
|
|
txtStatus.ScrollToCaret();
|
|
}
|
|
}
|
|
|
|
#region Index
|
|
private void AddWordToIndex(string s, string bk, string ch, string vs)
|
|
{
|
|
s = s.Trim().ToLower().TrimEnd(_trims).TrimStart(_trims).Replace("'", "");
|
|
if (s != "" && !_exclusions.Contains(s))
|
|
{
|
|
// add the word to the index
|
|
if (!_words.Contains(s))
|
|
{
|
|
_words.Add(s);
|
|
IndexItem i = new IndexItem();
|
|
i.word = s;
|
|
i.refs.Add(bk + ":" + ch + ":" + vs);
|
|
_idx.Add(i);
|
|
}
|
|
else
|
|
{
|
|
IndexItem i = _idx.GetItem(s);
|
|
if (!i.refs.Contains(bk + ":" + ch + ":" + vs))
|
|
{
|
|
i.refs.Add(bk + ":" + ch + ":" + vs);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void CreateIndex_Click(object sender, EventArgs e)
|
|
{
|
|
_idx.Clear();
|
|
_words.Clear();
|
|
txtStatus.Text = "";
|
|
|
|
System.Threading.Thread _thread = new System.Threading.Thread(CreateIndex);
|
|
_thread.SetApartmentState(ApartmentState.STA);
|
|
_thread.IsBackground = true;
|
|
_thread.Start();
|
|
}
|
|
private void CreateIndex()
|
|
{
|
|
|
|
OpenFileDialog ofd = new OpenFileDialog();
|
|
|
|
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
|
|
{
|
|
DynamicBible.Schemas.XMLBIBLE b = (DynamicBible.Schemas.XMLBIBLE)VAE.Data.XML.GetData(typeof(DynamicBible.Schemas.XMLBIBLE), ofd.FileName);
|
|
|
|
// to index, you need to iterate through every word in the bible.
|
|
|
|
foreach (DynamicBible.Schemas.XMLBIBLE_BOOK bk in b.BIBLEBOOKS)
|
|
{
|
|
foreach (DynamicBible.Schemas.XMLBIBLE_CHAPTER ch in bk.CHAPTERS)
|
|
{
|
|
foreach (DynamicBible.Schemas.XMLBIBLE_VERSES vs in ch.VERSES)
|
|
{
|
|
if (vs.Items != null)
|
|
{
|
|
foreach (object w in vs.Items)
|
|
{
|
|
// for each word, add an entry.
|
|
if (w.GetType() == typeof(DynamicBible.Schemas.XMLBIBLE_GR))
|
|
{
|
|
DynamicBible.Schemas.XMLBIBLE_GR gr = ((DynamicBible.Schemas.XMLBIBLE_GR)w);
|
|
if (gr.Text != null)
|
|
{
|
|
foreach (string t in gr.Text)
|
|
{
|
|
foreach (string s in t.Split(' '))
|
|
{
|
|
AddWordToIndex(s, bk.bnumber.ToString(), ch.cnumber.ToString(), vs.vnumber.ToString());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else if (w.GetType() == typeof(DynamicBible.Schemas.XMLBIBLE_STYLE_ITEM))
|
|
{
|
|
DynamicBible.Schemas.XMLBIBLE_STYLE_ITEM o = ((DynamicBible.Schemas.XMLBIBLE_STYLE_ITEM)w);
|
|
if (o.Text != null)
|
|
{
|
|
foreach (string t in o.Text)
|
|
{
|
|
foreach (string s in t.Split(' '))
|
|
{
|
|
AddWordToIndex(s, bk.bnumber.ToString(), ch.cnumber.ToString(), vs.vnumber.ToString());
|
|
}
|
|
}
|
|
}
|
|
if (o.gr != null)
|
|
{
|
|
DynamicBible.Schemas.XMLBIBLE_STYLE_GR gr = o.gr;
|
|
if (gr.Text != null)
|
|
{
|
|
foreach (string t in gr.Text)
|
|
{
|
|
foreach (string s in t.Split(' '))
|
|
{
|
|
AddWordToIndex(s, bk.bnumber.ToString(), ch.cnumber.ToString(), vs.vnumber.ToString());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if (o.STYLE != null)
|
|
{
|
|
foreach (DynamicBible.Schemas.XMLBIBLE_STYLE_STYLE so in o.STYLE)
|
|
{
|
|
if (so.Text != null)
|
|
{
|
|
foreach (string t in so.Text)
|
|
{
|
|
foreach (string s in t.Split(' '))
|
|
{
|
|
AddWordToIndex(s, bk.bnumber.ToString(), ch.cnumber.ToString(), vs.vnumber.ToString());
|
|
}
|
|
}
|
|
}
|
|
if (so.gr != null)
|
|
{
|
|
DynamicBible.Schemas.XMLBIBLE_STYLE_STYLE_GR gr = so.gr;
|
|
if (gr.Value != null)
|
|
{
|
|
foreach (string s in gr.Value.Split(' '))
|
|
{
|
|
AddWordToIndex(s, bk.bnumber.ToString(), ch.cnumber.ToString(), vs.vnumber.ToString());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if (vs.Text != null)
|
|
{
|
|
foreach (string w in vs.Text)
|
|
{
|
|
foreach (string s in w.Split(' '))
|
|
{
|
|
if (s != null && s.Trim() != "")
|
|
{
|
|
AddWordToIndex(s, bk.bnumber.ToString(), ch.cnumber.ToString(), vs.vnumber.ToString());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
UpdateStatus("Book: " + bk.bnumber + ", Word Count: " + _words.Count + "\r\n");
|
|
}
|
|
|
|
//_idx.Sort(CompareIndexCount);
|
|
//foreach (IndexItem i in _idx)
|
|
//{
|
|
// if (i.word.Split(' ').Count() > 1)
|
|
// {
|
|
// this.txtStatus.Text += ("Oops, ");
|
|
// this.txtStatus.Text += (i.word + ": " + i.refs.Count+"\r\n");
|
|
// }
|
|
// else if (i.refs.Count > 2000)
|
|
// {
|
|
// this.txtStatus.Text += ("\"" + i.word + "\",");
|
|
// }
|
|
|
|
//}
|
|
FolderBrowserDialog fbd = new FolderBrowserDialog();
|
|
if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
|
|
{
|
|
_idx.Sort(CompareIndexByWord);
|
|
Index tmp = new Index();
|
|
for (int i = 0; i < _idx.Count; i++)
|
|
{
|
|
if (i % 50 == 49 || i == _idx.Count - 1)
|
|
{
|
|
tmp.Add(_idx[i]);
|
|
UpdateStatus("words.unshift('" + _idx[i].word + "');\r\n");
|
|
System.IO.File.WriteAllText(fbd.SelectedPath + @"\" + _idx[i].word + "idx.json", Serialize(tmp));
|
|
tmp.Clear();
|
|
}
|
|
else
|
|
{
|
|
tmp.Add(_idx[i]);
|
|
}
|
|
}
|
|
MessageBox.Show("Complete.");
|
|
}
|
|
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region Sorters
|
|
private static int CompareIndexByWord(IndexItem x, IndexItem y)
|
|
{
|
|
return x.word.CompareTo(y.word);
|
|
}
|
|
private static int CompareIndexByCount(IndexItem x, IndexItem y)
|
|
{
|
|
return x.refs.Count.CompareTo(y.refs.Count);
|
|
}
|
|
#endregion
|
|
|
|
#region JSON Serialization Functions
|
|
private object Deserialize(string s, System.Type t)
|
|
{
|
|
return AjaxPro.JavaScriptDeserializer.DeserializeFromJson(s, t);
|
|
}
|
|
private string Serialize(object o)
|
|
{
|
|
System.Text.StringBuilder sb = new System.Text.StringBuilder();
|
|
AjaxPro.JavaScriptSerializer.Serialize(o, sb);
|
|
return sb.ToString();
|
|
}
|
|
#endregion
|
|
|
|
private void btnCreateText_Click(object sender, EventArgs e)
|
|
{
|
|
txtStatus.Text = "";
|
|
|
|
System.Threading.Thread _thread = new System.Threading.Thread(CreateText);
|
|
_thread.SetApartmentState(ApartmentState.STA);
|
|
_thread.IsBackground = true;
|
|
_thread.Start();
|
|
}
|
|
private void CreateText()
|
|
{
|
|
// iterate through text, output json format.
|
|
OpenFileDialog ofd = new OpenFileDialog();
|
|
List<Book> bbl = new System.Collections.Generic.List<Book>();
|
|
|
|
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
|
|
{
|
|
var doc = XDocument.Load(ofd.FileName);
|
|
|
|
foreach (XNode n in doc.Root.Nodes())
|
|
{
|
|
if (n.NodeType == XmlNodeType.Element)
|
|
{
|
|
XElement el = (XElement)n;
|
|
if (el.Name == "BIBLEBOOK")
|
|
{
|
|
Book bk = new Book();
|
|
bk.bk = Convert.ToInt32(el.FirstAttribute.Value.ToString());
|
|
foreach (XElement chn in el.Nodes())
|
|
{
|
|
Chapter ch = new Chapter();
|
|
ch.ch = Convert.ToInt32(chn.FirstAttribute.Value);
|
|
foreach (XElement vs in chn.Nodes())
|
|
{
|
|
Verse v = new Verse();
|
|
v.v = Convert.ToInt32(vs.FirstAttribute.Value);
|
|
foreach (object o in vs.Nodes())
|
|
{
|
|
v.w.AddRange(ProcessText(o));
|
|
}
|
|
ch.vss.Add(v);
|
|
}
|
|
bk.chs.Add(ch);
|
|
|
|
System.IO.File.WriteAllText(bk.bk.ToString() + "-" + ch.ch + ".json", Serialize(ch).Replace(",\"s\":\"\"", ""));
|
|
}
|
|
bbl.Add(bk);
|
|
|
|
UpdateStatus("Book: " + bk.bk + "\r\n");
|
|
}
|
|
}
|
|
}
|
|
// finished.
|
|
|
|
}
|
|
}
|
|
|
|
private List<Text> ProcessText(object o)
|
|
{
|
|
List<Text> Ts = new List<DynamicBibleUtility.Text>();
|
|
if (o.GetType() == typeof(XElement))
|
|
{
|
|
if (((XElement)o).Name == "gr")
|
|
{
|
|
Ts.Add(new Text(((XElement)o).Value, ((XElement)o).FirstAttribute.Value));
|
|
}
|
|
else if ((((XElement)o).Name.ToString().ToLower() == "style"))
|
|
{
|
|
foreach (object n in ((XElement)o).Nodes())
|
|
{
|
|
Ts.AddRange(ProcessText(n));
|
|
}
|
|
}
|
|
else
|
|
{
|
|
throw new Exception("Unknown Element");
|
|
}
|
|
}
|
|
else if (o.GetType() == typeof(XText))
|
|
{
|
|
Ts.Add(new Text(((XText)o).Value));
|
|
}
|
|
else
|
|
{
|
|
throw new Exception("Unknown Element");
|
|
}
|
|
return Ts;
|
|
}
|
|
}
|
|
}
|