Search code examples
rubyxmlxpathnokogiristanford-nlp

How to see if one Nokogiri::XML::Node contains parts of another Nokogiri::XML::Node?


Using ruby Nokogiri I have a Nokogiri::XML::Node object (ConstituencyXMLNode inherits from Nokogiri::XML::Node) that is a tree object like such:

#(ConstituencyXMLNode:0xc3c8 {
  name = "PP",
  children = [
    #(ConstituencyXMLNode:0xc3dc { name = "IN", children = [ #(Text "out")] }),
    #(ConstituencyXMLNode:0xc3f0 {
      name = "PP",
      children = [
        #(ConstituencyXMLNode:0xc404 { name = "IN", children = [ #(Text "of")] }),
        #(ConstituencyXMLNode:0xc418 {
          name = "NP",
          children = [
            #(ConstituencyXMLNode:0xc42c { name = "JJ", children = [ #(Text "non-living")] }),
            #(ConstituencyXMLNode:0xc440 { name = "NNS", children = [ #(Text "resources")] })]
          })]
      })]
  })

We will call it pp_leaf

and I want to compare and see if portions of this node are included within this other partial Nokogiri::XML::Node:

#(ConstituencyXMLNode:0xc3f0 {
  name = "PP",
  children = [
    #(ConstituencyXMLNode:0xc404 { name = "IN", children = [ #(Text "of")] }),
    #(ConstituencyXMLNode:0xc418 {
      name = "NP",
      children = [
        #(ConstituencyXMLNode:0xc42c { name = "JJ", children = [ #(Text "non-living")] }),
        #(ConstituencyXMLNode:0xc440 { name = "NNS", children = [ #(Text "resources")] })]
      })]
  })

We will call this node current_leaf.

It seems that Nokogiri::XML::NodeSet has the comparison tools I am looking for, but I am unsure of how to convert a Nokogiri::XML::Node to a Nokogiri::XML::NodeSet. None of the operators on Nokogiri::XML::Node appear to have the comparison operators I need to match if one set contains the other.

Any ideas?


Solution

  • I found a way to do it with:

    pp_leaf.xpath('.//*').include?(current_leaf)` 
    

    thanks to this answer. It breaks the node tree down into an array which lets you check if the node is a member.