Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

NLP – Comparing Word Order

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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());
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading