# How to find the position of a word in a jumbled sentence?

I have an initial sentence:

The quick brown fox jumps over the lazy dog.

I have a new sentence (which is always a jumble of the original sentence):

The lazy dog jumps over the quick brown fox.

In the original sentence, for each word, I want to superscript the word position as according to the jumbled sentence. Can someone guide me as to how I can achieve this?

Any novel approach (using new packages) is appreciated. Thanks in advance. In the following MWE, I am obviously not achieving what I actually want.

\documentclass[12pt]{memoir}
\usepackage{listofitems}
\usepackage{amsmath}

\newcommand{\wordsI}
{   1. The,
2. quick,
3. brown,
4. fox
+
5. jumps,
6. over,
7. the,
8. lazy,
9. dog
}

\newcommand{\wordsII}
{   The
lazy
dog
jumps
over
the
quick
brown
fox
}

% Tokenize the words in order to display them
\newcommand{\tokenize}[1]
{%
\setsepchar{+/,/./}
\foreachitem\groupoflines\in\textarray
{
\setsepchar{,}
\foreachitem\line\in\linearray
{
\setsepchar{.}
$\text{\wordarray[2]} ^ {\wordarray[1]}$
}%
\newline
}
}

\begin{document}
\noindent
Actual sentence:
\newline
% The splitting of the sentence in 2 lines is intentional
\tokenize{\wordsI}

\noindent
Jumbled sentence:
\textbf{\wordsII}
\end{document}


In this example, I will get the result I need if I have the following definition instead:

\newcommand{\wordsI}
{   1. The,
7. quick,
8. brown,
9. fox
+
4. jumps,
5. over,
6. the,
2. lazy,
3. dog
}


But, I don't want to make the change manually. I am looking for a way to make it 'dynamic' based on the jumbled sentence.

EDIT: I want to achieve this even in scenarios like this:

Initial sentence:

the quick brown fox jumps over the lazy dog.

Jumbled sentence:

the lazy dog jumps over the quick brown fox.

In this case, I need to have some kind of 'tags' for the words in the initial sentence in order for the jumbled sentence to be non-ambiguous.

\newcommand{\wordsI}
{   1. the,
2. quick,
3. brown,
4. fox
+
5. jumps,
6. over,
7. the,
8. lazy,
9. dog
}

\newcommand{\wordsII}
{   7. the
8. lazy
9. dog
5. jumps
6. over
1. the
2. quick
3. brown
4. fox
}


Desired output:

• Can you add an approximate representation of the desired output, please? Jul 2 '17 at 22:36
• @egreg, I just made an edit. Hope it's sufficient. Please let me know if it is not clear. Jul 2 '17 at 22:39
• What do you want to do in the case of duplicate words? Can you add your desired output for that too? Jul 3 '17 at 11:58
• @ShreevatsaR Done. Jul 3 '17 at 12:10
• Well sorry my actual question was: how do you determine the output? When "the" occurs at both positions 1 and 6, why 6 1 in the output rather than 1 6? Or are both equally acceptable? Jul 3 '17 at 12:13

IMO the most interesting thing about TeX is its typesetting and the worst thing is its programming facility, so it's best to do such programming outside of TeX (as far away as possible!), and use TeX solely for typesetting. Everything may be possible with TeX but it won't necessarily be the easiest / most maintainable solution.

Still, if using TeX, this sort of programming is easier to do with LuaTeX (at least for me, and I imagine for most people). Compile the following file with lualatex (I've let your “tags” be optional: you can tag every word like the(1) quick(2) ..., or tag only the duplicate words):

\documentclass[12pt]{memoir}
\usepackage{amsmath} % For \text

\newcommand{\printword}[2]{$\text{#1} ^ {#2}$\quad} % Or whatever formatting you like.
\newcommand{\linesep}{\newline}

\directlua{dofile('jumble.lua')}
\newcommand{\printjumble}[2]{
\directlua{get_sentence1_lines()}{#1}
\directlua{get_sentence2_words()}{#2}
%
\noindent
Actual sentence:
\newline
\directlua{print_sentence1_lines()}

\noindent
Jumbled sentence:
\textbf{\directlua{print_sentence2()}}
}

\begin{document}
\printjumble{
the(1) quick brown fox
+
jumps over the(7) lazy dog
}{
the(7) lazy dog jumps over the(1) quick brown fox
}
\end{document}


where jumble.lua (which could be inlined into the same .tex file, but I prefer to keep separate) is the following:

-- Expected from TeX: before calling print_sentence1_lines(),
--     call get_sentence1_lines() and get_sentence2_words()
--     define \printword and \linesep.
-- Globals: sentence2_words, position_for_word, sentence1_lines

function get_sentence1_lines()
sentence1_lines = token.scan_string()
end

function get_sentence2_words()
local sentence2 = token.scan_string()
sentence2_words = {}
position_for_word = {}
local i = 0
for word in string.gmatch(sentence2, "%S+") do
i = i + 1
assert(position_for_word[word] == nil, string.format('Duplicate word: %s', word))
sentence2_words[i] = without_tags(word)
position_for_word[word] = i
end
end

function print_sentence2()
for i, word in ipairs(sentence2_words) do
tex.print(word)
end
end

function print_sentence1_lines()
for line in string.gmatch(sentence1_lines, "[^+]+") do
for word in string.gmatch(line, "%S+") do
position = position_for_word[word]
assert(position_for_word[word] ~= nil, string.format('New word: %s', word))
tex.print(string.format([[\printword{%s}{%s}]], without_tags(word), position))
end
tex.print([[\linesep]])
end
end

function without_tags(word)
local new_word = string.gsub(word, "%(.*%)", "")
return new_word
end


This produces

as in the question.

Note that you can make this a bit shorter (e.g. see the first revision of this answer) by moving things around, but I find it cleanest to keep (as much as possible) the typesetting instructions in the .tex file and and the programming in the .lua file.

• Thanks for your insight. I could actually use another programming language to code this logic and somehow integrate it with my TeX code. I never gave it a thought because I assumed TeX is too powerful and something of this sort can be easily achieved. Jul 3 '17 at 8:45
• I was using XeTeX. Now, the rest of my code is breaking when I use LuaTeX to compile it. So, unfortunately I cannot use this approach. Thank you for the effort you put in the solution. I hope it will be useful to someone in the future. I think I will use some programming language (as you mentioned) to overwrite a file and finish with this situation. Jul 3 '17 at 17:34

Something like this?

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn

\seq_new:N \l_jsp_sentence_temp_seq
\seq_new:N \l_jsp_sentence_original_seq
\seq_new:N \l_jsp_sentence_jumbled_seq
\prop_new:N \l_jsp_sentence_original_ind_prop
\prop_new:N \l_jsp_sentence_jumbled_ind_prop
\int_new:N \l_jsp_sentence_word_int

\NewDocumentCommand{\parseoriginalsentence}{m}
{
\seq_set_split:Nnn \l_jsp_sentence_temp_seq { + } { #1 }
\seq_clear:N \l_jsp_sentence_original_seq
\prop_clear:N \l_jsp_sentence_original_ind_prop
\seq_map_inline:Nn \l_jsp_sentence_temp_seq
{
\int_zero:N \l_jsp_sentence_word_int
\clist_map_inline:nn { ##1 }
{
\int_incr:N \l_jsp_sentence_word_int
\seq_put_right:Nn \l_jsp_sentence_original_seq { ####1 }
\prop_put:Nnx \l_jsp_sentence_original_ind_prop
{ ####1 } { \int_to_arabic:n { \l_jsp_sentence_word_int } }
}
\seq_put_right:Nn \l_jsp_sentence_original_seq { + }
}
}
\NewDocumentCommand{\parsejumbledsentence}{m}
{
\prop_clear:N \l_jsp_sentence_jumbled_ind_prop
\seq_set_split:Nnn \l_jsp_sentence_jumbled_seq { , } { #1 }
\int_zero:N \l_jsp_sentence_word_int
\seq_map_inline:Nn \l_jsp_sentence_jumbled_seq
{
\int_incr:N \l_jsp_sentence_word_int
\prop_put:Nnx \l_jsp_sentence_jumbled_ind_prop
{ ##1 } { \int_to_arabic:n { \l_jsp_sentence_word_int } }
}
}

\NewDocumentCommand{\printoriginalsentence}{s}
{
\IfBooleanTF{#1}
{
\jsp_sentence_print_from_original:
}
{
\jsp_sentence_print_from_jumbled:
}
}

\cs_new_protected:Nn \jsp_sentence_print_from_original:
{
\seq_map_inline:Nn \l_jsp_sentence_original_seq
{
\tl_if_eq:nnTF { ##1 } { + }
{
\par
}
{
\prop_item:Nn \l_jsp_sentence_original_ind_prop { ##1 }.\nobreakspace ##1 ~
}
}
}

\cs_new_protected:Nn \jsp_sentence_print_from_jumbled:
{
\seq_map_inline:Nn \l_jsp_sentence_original_seq
{
\tl_if_eq:nnTF { ##1 } { + }
{
\par
}
{
\prop_item:Nn \l_jsp_sentence_jumbled_ind_prop { ##1 }.\nobreakspace ##1 ~
}
}
}
\ExplSyntaxOff

\begin{document}

\parseoriginalsentence{
The,
quick,
brown,
fox
+
jumps,
over,
the,
lazy,
dog
}
\parsejumbledsentence{
The,
lazy,
dog,
jumps,
over,
the,
quick,
brown,
fox
}

\printoriginalsentence*

\bigskip

\printoriginalsentence

\end{document}


• Thank you very much. Yes, this is what I need. But I just realized that it is difficult when the words of the original sentence are not unique. I made edits to the question. I am trying to solve that 'difficult' scenario with the code you provided. Jul 3 '17 at 8:41