Consecutive words match in a dataset
The data structure you want is called a trie.  It's often used with letters in the nodes, for doing word search (finding anagrams and so on); in your case the keys would be words and the values would be sub-tries and arrays of line numbers or other metadata.

Given this input:

cat dog pig
dog pig cow


You'd make a structure like:

{
    cat: {
      dog: {
        pig: { "#": [ 1 ] },
        "#": [ 1 ]
      },
      "#": [ 1 ]
    },
    dog: {
      pig: { 
        cow: { "#": [ 2 ] },
        "#": [ 1, 2 ]
      },
      "#": [ 1, 2 ]
    },
    pig: { 
      cow: { "#": [ 2 ] },
      "#": [ 1, 2 ]
    },
    cow: { "#": [ 2 ] },
}