from itertools import combinations

class TextNodes(object):
  """docstring for TextNodes"""
  def __init__(self, frequency_dictionary):
    self.frequency_dictionary = frequency_dictionary
    self._ontology_keys = None
    self._graph_keys = None

  @property
  def ontology_keys(self):
    """! Return the set of the keys of the ontology."""
    if not self._ontology_keys:
      self._ontology_keys = set([key for key, _ in self.frequency_dictionary])
    return self._ontology_keys

  @property
  def graph_keys(self):
    """! Return the set of the nodes' identifiers."""
    if not self._graph_keys:
      self._graph_keys = set([node for _, node in self.frequency_dictionary])
    return self._graph_keys

  def get_combinations(self, nodes = None):
    """!
    Return combinations of the keys of the ontology.

    Keys that belong to the same graph's node are not combined together.
    A combination is a pair of two keys (a 2-element tuple).
    If the nodes are given then the method returns only combinations for
    these graph's keys.

    @param nodes the set of nodes' identifiers from a text.
    @type nodes: set
    
    @return a generator of combinations.
    @rtype: generator
    """
    if not nodes:
      generator = ((key1, key2) for (key1, node1), (key2, node2) \
        in combinations(self.frequency_dictionary.iterkeys(), 2))
    else:
      generator = ((key1, key2) for (key1, node1), (key2, node2) \
        in combinations(self.frequency_dictionary.iterkeys(), 2) \
        if node1 in nodes and node2 in nodes)
    return generator

  def __len__(self):
    return len(self.frequency_dictionary)

