<commit_before>/******************************************************************************
* GenealoJ
* The main parsing and container class
* 
* Author:       <NAME> <<EMAIL>>
* License:      MIT License: http://creativecommons.org/licenses/MIT/
******************************************************************************/

package com.msbmsb.genealoj;

import com.msbmsb.genealoj.GedcomNode;
import com.msbmsb.genealoj.IndividualNode;
import com.msbmsb.genealoj.Utils;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.FileNotFoundException;

import java.io.File;
import java.util.List;
import java.util.ArrayList;

public class GenealoJ {
  /**
   * m_parseRoot is the GedcomNode for all level=0 nodes
   */
  private GedcomNode m_parseRoot = new GedcomNode(-1, "ROOT");

  /**
   * File source for current gedcom parse
   */
  private File m_gedcomFile = null;

  /**
   * Check for valid initialization of input file
   */
  private boolean m_initialized = false;

  /**
   * Constructor. Given a file name, parses the file and builds a hierarchy
   * of GEDCOM lines represented by the GedcomNode class.
   * @param file String filename of input GEDCOM file to parse
   */
  public GenealoJ(String file) {
    m_gedcomFile = new File(file);
    m_initialized = init();
  }

  /**
   * Constructor. Given a File object, parses the file and builds a hierarchy
   * of GEDCOM lines represented by the GedcomNode class.
   * @param file File of input GEDCOM file to parse
   */
  public GenealoJ(File file) {
    m_gedcomFile = file;
    m_initialized = init();
  }

  /**
   * Private initialization method for loading input GEDCOM file
   * Private to enforce a one-parser-per-file contract
   * and setting any necessary parameters
   * @return boolean of initialized status
   */
  private boolean init() {
    boolean initialized = true;
    initialized &= load(m_gedcomFile);

    return initialized;
  }

  /**
   * Private method for loading the input GEDCOM file
   * @param file File to load
   * @return boolean of successful load
   */
  private boolean load(File file) {
    BufferedReader br;
    try {
      br = new BufferedReader(new FileReader(file));
      // begin parsing at the m_parseRoot level=-1 node
      parseGedcom(br, m_parseRoot);
      br.close();
      // now link individuals in the genealogy graph through their families
      linkIndividuals(m_parseRoot);
      return true;
    } catch(FileNotFoundException fne) {
      System.err.println("Input file: " + file + " not found!");
      return false;
    } catch(IOException ioe) {
      System.err.println("IOException parsing file: " + file);
      ioe.printStackTrace();
      return false;
    }
  }

  /**
   * Get level=0 nodes of the given tag type
   * @param tag the tag of nodes to return
   * @return List<GedcomNode> list of nodes matching;
   *         null if none found with given tag
   */
  public List<GedcomNode> getNodes(String tag) {
    return m_parseRoot.getChildrenWithTag(tag);
  }

  public List<GedcomNode> getDescendants(String tag) {
    return m_parseRoot.getDescendantsWithTag(tag);
  }
  
  /**<commit_msg>add method to get root node of parse<commit_after>/******************************************************************************
* GenealoJ
* The main parsing and container class
* 
* Author:       <NAME> <<EMAIL>>
* License:      MIT License: http://creativecommons.org/licenses/MIT/
******************************************************************************/

package com.msbmsb.genealoj;

import com.msbmsb.genealoj.GedcomNode;
import com.msbmsb.genealoj.IndividualNode;
import com.msbmsb.genealoj.Utils;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.FileNotFoundException;

import java.io.File;
import java.util.List;
import java.util.ArrayList;

public class GenealoJ {
  /**
   * m_parseRoot is the GedcomNode for all level=0 nodes
   */
  private GedcomNode m_parseRoot = new GedcomNode(-1, "ROOT");

  /**
   * File source for current gedcom parse
   */
  private File m_gedcomFile = null;

  /**
   * Check for valid initialization of input file
   */
  private boolean m_initialized = false;

  /**
   * Constructor. Given a file name, parses the file and builds a hierarchy
   * of GEDCOM lines represented by the GedcomNode class.
   * @param file String filename of input GEDCOM file to parse
   */
  public GenealoJ(String file) {
    m_gedcomFile = new File(file);
    m_initialized = init();
  }

  /**
   * Constructor. Given a File object, parses the file and builds a hierarchy
   * of GEDCOM lines represented by the GedcomNode class.
   * @param file File of input GEDCOM file to parse
   */
  public GenealoJ(File file) {
    m_gedcomFile = file;
    m_initialized = init();
  }

  /**
   * Private initialization method for loading input GEDCOM file
   * Private to enforce a one-parser-per-file contract
   * and setting any necessary parameters
   * @return boolean of initialized status
   */
  private boolean init() {
    boolean initialized = true;
    initialized &= load(m_gedcomFile);

    return initialized;
  }

  /**
   * Private method for loading the input GEDCOM file
   * @param file File to load
   * @return boolean of successful load
   */
  private boolean load(File file) {
    BufferedReader br;
    try {
      br = new BufferedReader(new FileReader(file));
      // begin parsing at the m_parseRoot level=-1 node
      parseGedcom(br, m_parseRoot);
      br.close();
      // now link individuals in the genealogy graph through their families
      linkIndividuals(m_parseRoot);
      return true;
    } catch(FileNotFoundException fne) {
      System.err.println("Input file: " + file + " not found!");
      return false;
    } catch(IOException ioe) {
      System.err.println("IOException parsing file: " + file);
      ioe.printStackTrace();
      return false;
    }
  }

  /**
   * Get level=0 nodes of the given tag type
   * @param tag the tag of nodes to return
   * @return List<GedcomNode> list of nodes matching;
   *         null if none found with given tag
   */
  public List<GedcomNode> getNodes(String tag) {
    return m_parseRoot.getChildrenWithTag(tag);
  }

  public List<GedcomNode> getDescendants(String tag) {
    return m_parseRoot.getDescendantsWithTag(tag);
  }

  public GedcomNode getRoot() {
    return m_parseRoot;
  }
  
  /**
{'fraction_non_alphanumeric': 0.13588472265261853, 'fraction_numerical': 0.0012395413696932136, 'mean_word_length': 3.2861885790172645, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2487064', 'n_tokens_mistral': 1927, 'n_tokens_neox': 1879, 'n_words': 600}