Home > Blockchain >  NLP - Comparing Word Order
NLP - Comparing Word Order

Time:12-14

I am new to NLP. Is there any algorithm to compare the words and positions of sentence 2 and sentence 1, or sentence 3 and sentence 1, to see if the words that match are in order or out of order?

SENTENCE 1:                I went to the store to pick up apples
                           0  1   2   3   4    5   6   7    8


SENTENCE 2 (in Order):     store  pick  apples 
                             0     1      2


SENTENCE 3 (Out of Order): store  apples pick 
                             0      1     2

Thank you.

CodePudding user response:

A simple method to determine if the words in sentence2 are in the same order as they are in sentence1 is to sort them by their position in the sentence1 and see if the list has changed. Assuming the sentences are List<String>:

bool sameOrder = sentence2.equals(sentence2.stream()
    .sorted(Comparator.comparingInt(sentence1::indexOf))
    .toList());
  • Related