<commit_before>package com.github.vangj.jbayes.inf.exact.graph.util;

import com.github.vangj.jbayes.inf.exact.graph.Node;
import com.github.vangj.jbayes.inf.exact.graph.lpd.Potential;
import com.github.vangj.jbayes.inf.exact.graph.lpd.PotentialEntry;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;

/**
 * Potential util.
 */
public class PotentialUtil {
  private PotentialUtil() {

  }

  /**
   * Makes a string from the map.
   * @param entries Map.
   * @return String.
   */
  public static String asString(Map<String, String> entries) {
    return entries.entrySet().stream()
        .map(entry -> (new StringBuilder())
            .append(entry.getKey())
            .append('=')
            .append(entry.getValue())
            .toString())
        .collect(Collectors.joining(",", "[" , "]"));
  }

  /**
   * Creates a new map sorted by the id of the specified map.
   * @param map Map.
   * @return Map sorted by id.
   */
  public static Map<String, String> sortByKeys(Map<String, String> map) {
    Map<String, String> out = new TreeMap<>(map);
    return out;
  }

  /**
   * Creates a potential from the node and its parents.
   * @param node Node.
   * @param parents Parent nodes.
   * @return Potential.
   */
  public static Potential getPotential(Node node, List<Node> parents) {
    List<List<String>> valueLists = new ArrayList<>();
    parents.forEach(parent -> {
      valueLists.add(new ArrayList<String>(parent.getValues()));
    });
    valueLists.add(new ArrayList<>(node.getValues()));

    List<List<String>> cartesian = cartesian(valueLists);
    final int size = parents.size();
    Potential potential = new Potential();
    cartesian.forEach(values -> {
      PotentialEntry entry = new PotentialEntry();
      for(int i=0; i < size; i++) {
        String value = values.get(i);
        String id = parents.get(i).getId();
        entry.add(id, value);
      }
      entry.add(node.getId(), values.get(size));
      potential.addEntry(entry);
    });

    final int total = Math.min(node.probs().size(), potential.entries().size());
    for(int i=0; i < total; i++) {
      Double prob = node.probs().get(i);
      potential.entries().get(i).value(prob);
    }
<commit_msg>refactored get potential method (one for node + parents, and one for just list of nodes, probabily for cliques later on)<commit_after>package com.github.vangj.jbayes.inf.exact.graph.util;

import com.github.vangj.jbayes.inf.exact.graph.Node;
import com.github.vangj.jbayes.inf.exact.graph.lpd.Potential;
import com.github.vangj.jbayes.inf.exact.graph.lpd.PotentialEntry;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;

/**
 * Potential util.
 */
public class PotentialUtil {
  private PotentialUtil() {

  }

  /**
   * Makes a string from the map.
   * @param entries Map.
   * @return String.
   */
  public static String asString(Map<String, String> entries) {
    return entries.entrySet().stream()
        .map(entry -> (new StringBuilder())
            .append(entry.getKey())
            .append('=')
            .append(entry.getValue())
            .toString())
        .collect(Collectors.joining(",", "[" , "]"));
  }

  /**
   * Creates a new map sorted by the id of the specified map.
   * @param map Map.
   * @return Map sorted by id.
   */
  public static Map<String, String> sortByKeys(Map<String, String> map) {
    Map<String, String> out = new TreeMap<>(map);
    return out;
  }

  private static List<Node> merge(Node node, List<Node> parents) {
    List<Node> list = new ArrayList<>(parents);
    list.add(node);
    return list;
  }

  /**
   * Creates a potential from the node and its parents. If
   * the specified node has any conditional probabilities, the potential
   * entries will be set to them.
   * @param node Node.
   * @param parents Parent nodes.
   * @return Potential.
   */
  public static Potential getPotential(Node node, List<Node> parents) {
    Potential potential = getPotential(merge(node, parents));

    final int total = Math.min(node.probs().size(), potential.entries().size());
    for(int i=0; i < total; i++) {
      Double prob = node.probs().get(i);
      potential.entries().get(i).value(prob);
    }

    return potential;
  }

  /**
   * Creates a potential from the specified list of nodes. All probabilities
   * are initialized to 1.
   * @param nodes List of nodes.
   * @return Potential.
   */
  public static Potential getPotential(List<Node> nodes) {
    List<List<String>> valueLists = new ArrayList<>();
    nodes.forEach(node -> {
      valueLists.add(new ArrayList<>(node.getValues()));
    });

    List<List<String>> cartesian = cartesian(valueLists);
    final int size = nodes.size();
    Potential potential = new Potential();
    cartesian.forEach(values -> {
      PotentialEntry entry = new PotentialEntry();
      for(int i=0; i < size; i++) {
        String value = values.get(i);
        String id = nodes.get(i).getId();
        entry.add(id, value);
      }
      potential.addEntry(entry);
    });

{'fraction_non_alphanumeric': 0.14340935446463085, 'fraction_numerical': 0.0009663703131039815, 'mean_word_length': 2.9533995416348358, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 39, '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': '2407586', 'n_tokens_mistral': 1648, 'n_tokens_neox': 1590, 'n_words': 410}