<commit_before>class Neo4jClient
  def initialize(url)
    Neo4j::Session.open(:server_db, url)
  end

  def sentence_query_components(tokens, dependencies, sentence_index)
    nodes = tokens.map do |t|
      [t['word'], t['pos'], t['lemma'], t['index'].to_i-1]
    end
    relations = dependencies.uniq.map do |d|
      [d['dep'], d['governor'].to_i-1, d['dependent'].to_i-1]
    end
    relations.reject! { |l, g, d| g == d || l == 'ROOT'}

    return if relations.empty?

    nodes.map! do |word, pos, lemma, index|
      options = { word: word, part_of_speech: pos, lemma: lemma, index: index }
      Node.string_for_create(options, sentence_index)
    end
    query_string = "#{nodes.join(", ")}"

    relations.map! do |label, parent_index, child_index|
      Relation.string_for_create("n#{parent_index}", label, "n#{child_index}", sentence_index)
    end
    query_string += ", #{relations.join(", ")}"
  end

  def generate_create_query_for_sentences(sentences)
    query_string = "CREATE " + sentences.each_with_index.to_a.map do |data, index|
      sentence_query_components(*data, index)
    end.compact.join(", ")
  end

  def execute(query_string)
    Neo4j::Session.query(query_string)
  end

  def clear
    Node.delete_all
  end

  def verbs
    non_aux_verb_query = "MATCH (verb:Node) WHERE verb.part_of_speech =~ 'VB.?'
                          MATCH p=(verb)--(x)
                          WHERE NOT ANY (r in relationships(p) WHERE r.label =~ 'aux.*')
                          RETURN DISTINCT verb;"
    Neo4j::Session.query(non_aux_verb_query).map { |e| e.verb }
  end

  def permitted_descendants(node, copula)
    standard_query = %q{match (verb:Node {uuid: "NODE_UUID"})<commit_msg>Exclude expl verbs from verbs shortcut<commit_after>class Neo4jClient
  def initialize(url)
    Neo4j::Session.open(:server_db, url)
  end

  def sentence_query_components(tokens, dependencies, sentence_index)
    nodes = tokens.map do |t|
      [t['word'], t['pos'], t['lemma'], t['index'].to_i-1]
    end
    relations = dependencies.uniq.map do |d|
      [d['dep'], d['governor'].to_i-1, d['dependent'].to_i-1]
    end
    relations.reject! { |l, g, d| g == d || l == 'ROOT'}

    return if relations.empty?

    nodes.map! do |word, pos, lemma, index|
      options = { word: word, part_of_speech: pos, lemma: lemma, index: index }
      Node.string_for_create(options, sentence_index)
    end
    query_string = "#{nodes.join(", ")}"

    relations.map! do |label, parent_index, child_index|
      Relation.string_for_create("n#{parent_index}", label, "n#{child_index}", sentence_index)
    end
    query_string += ", #{relations.join(", ")}"
  end

  def generate_create_query_for_sentences(sentences)
    query_string = "CREATE " + sentences.each_with_index.to_a.map do |data, index|
      sentence_query_components(*data, index)
    end.compact.join(", ")
  end

  def execute(query_string)
    Neo4j::Session.query(query_string)
  end

  def clear
    Node.delete_all
  end

  def verbs
    expl_verb_query = %q{MATCH (expl_verb:Node) WHERE expl_verb.part_of_speech =~ 'VB.?'
                         MATCH (expl_verb)-[r:REL]-(expl:Node)
                         WHERE r.label = "expl"
                         RETURN DISTINCT expl_verb.uuid as uuid;}
    expl_verb_uuids = Neo4j::Session.query(expl_verb_query).map(&:uuid)

    non_aux_verb_query = %q{MATCH (verb:Node) WHERE verb.part_of_speech =~ 'VB.?'
                            MATCH p=(verb)--(x)
                            WHERE NOT ANY (r in relationships(p) WHERE r.label =~ 'aux.*')
                            RETURN DISTINCT verb;}
    Neo4j::Session.query(non_aux_verb_query)
      .map(&:verb)
      .reject { |v| expl_verb_uuids.include? v.uuid }
  end

  def permitted_descendants(node, copula)
    standard_query = %q{match (verb:Node {uuid: "NODE_UUID"})
{'fraction_non_alphanumeric': 0.13917793964620187, 'fraction_numerical': 0.003902185223725286, 'mean_word_length': 2.9762150982419855, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2465289', 'n_tokens_mistral': 1335, 'n_tokens_neox': 1290, 'n_words': 277}