<commit_before>/*
 * This file is part of the Joshua Machine Translation System.
 * 
 * Joshua is free software; you can redistribute it and/or modify it under the terms of the GNU
 * Lesser General Public License as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License along with this library;
 * if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 * 02111-1307 USA
 */
package joshua.decoder.hypergraph;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;
import java.util.logging.Logger;

import joshua.decoder.ff.state_maintenance.DPState;

/**
 * this class implement (1) HyperGraph-related data structures (Item and Hyper-edges)
 * 
 * Note: to seed the kbest extraction, each deduction should have the best_cost properly set. We do
 * not require any list being sorted
 * 
 * @author <NAME>, <<EMAIL>>
 * @version $LastChangedDate$
 */
public class HyperGraph {

  // pointer to goal HGNode
  public HGNode goalNode = null;

  public int numNodes = -1;
  public int numEdges = -1;
  public int sentID = -1;
  public int sentLen = -1;

  static final Logger logger = Logger.getLogger(HyperGraph.class.getName());

  public HyperGraph(HGNode goalNode, int numNodes, int numEdges, int sentID, int sentLen) {
    this.goalNode = goalNode;
    this.numNodes = numNodes;
    this.numEdges = numEdges;
    this.sentID = sentID;
    this.sentLen = sentLen;
  }


  public double bestLogP() {
    return this.goalNode.bestHyperedge.bestDerivationLogP;
  }

  // === merge two hypergraphs at root node
  public static HyperGraph mergeTwoHyperGraphs(HyperGraph hg1, HyperGraph hg2) {
    List<HyperGraph> hgs = new ArrayList<HyperGraph>();
    hgs.add(hg1);
    hgs.add(hg2);
    return mergeHyperGraphs(hgs);
  }

  public static HyperGraph mergeHyperGraphs(List<HyperGraph> hgs) {
    // Use the first hg to get i, j, lhs, sentID, sentLen.
    HyperGraph hg1 = hgs.get(0);
    int sentID = hg1.sentID;
    int sentLen = hg1.sentLen;

    // Create new goal node.
    int goalI = hg1.goalNode.i;
    int goalJ = hg1.goalNode.j;
    int goalLHS = hg1.goalNode.lhs;
    TreeMap<Integer, DPState> goalDPStates = null;
    double goalEstTotalLogP = -1;
    HGNode newGoalNode = new HGNode(goalI, goalJ, goalLHS, goalDPStates, null, goalEstTotalLogP);;

    // Attach all edges under old goal nodes into the new goal node.
    int numNodes = 0;
    int numEdges = 0;
    for (HyperGraph hg : hgs) {
      // Sanity check if the hgs belongs to the same source input.
      if (hg.sentID != sentID || hg.sentLen != sentLen || hg.goalNode.i != goalI<commit_msg>fixed to use StateComputer as has key instead of separate id<commit_after>/*
 * This file is part of the Joshua Machine Translation System.
 * 
 * Joshua is free software; you can redistribute it and/or modify it under the terms of the GNU
 * Lesser General Public License as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License along with this library;
 * if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 * 02111-1307 USA
 */
package joshua.decoder.hypergraph;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;
import java.util.logging.Logger;

import joshua.decoder.ff.state_maintenance.DPState;
import joshua.decoder.ff.state_maintenance.StateComputer;

/**
 * this class implement (1) HyperGraph-related data structures (Item and Hyper-edges)
 * 
 * Note: to seed the kbest extraction, each deduction should have the best_cost properly set. We do
 * not require any list being sorted
 * 
 * @author <NAME>, <<EMAIL>>
 * @version $LastChangedDate$
 */
public class HyperGraph {

  // pointer to goal HGNode
  public HGNode goalNode = null;

  public int numNodes = -1;
  public int numEdges = -1;
  public int sentID = -1;
  public int sentLen = -1;

  static final Logger logger = Logger.getLogger(HyperGraph.class.getName());

  public HyperGraph(HGNode goalNode, int numNodes, int numEdges, int sentID, int sentLen) {
    this.goalNode = goalNode;
    this.numNodes = numNodes;
    this.numEdges = numEdges;
    this.sentID = sentID;
    this.sentLen = sentLen;
  }


  public double bestLogP() {
    return this.goalNode.bestHyperedge.bestDerivationLogP;
  }

  // === merge two hypergraphs at root node
  public static HyperGraph mergeTwoHyperGraphs(HyperGraph hg1, HyperGraph hg2) {
    List<HyperGraph> hgs = new ArrayList<HyperGraph>();
    hgs.add(hg1);
    hgs.add(hg2);
    return mergeHyperGraphs(hgs);
  }

  public static HyperGraph mergeHyperGraphs(List<HyperGraph> hgs) {
    // Use the first hg to get i, j, lhs, sentID, sentLen.
    HyperGraph hg1 = hgs.get(0);
    int sentID = hg1.sentID;
    int sentLen = hg1.sentLen;

    // Create new goal node.
    int goalI = hg1.goalNode.i;
    int goalJ = hg1.goalNode.j;
    int goalLHS = hg1.goalNode.lhs;
    TreeMap<StateComputer, DPState> goalDPStates = null;
    double goalEstTotalLogP = -1;
    HGNode newGoalNode = new HGNode(goalI, goalJ, goalLHS, goalDPStates, null, goalEstTotalLogP);;

    // Attach all edges under old goal nodes into the new goal node.
    int numNodes = 0;
    int numEdges = 0;
    for (HyperGraph hg : hgs) {
      // Sanity check if the hgs belongs to the same source input.
      if (hg.sentID != sentID || hg.sentLen != sentLen || hg.goalNode.i != goalI
{'fraction_non_alphanumeric': 0.08738812042310821, 'fraction_numerical': 0.011391375101708706, 'mean_word_length': 3.5025641025641026, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '1682058', 'n_tokens_mistral': 2011, 'n_tokens_neox': 1870, 'n_words': 707}