Ready to begin
No records have been processed. Start with Next step.
![For illustrative noun positions [1,3,8,14,19] and pronoun17, the nearest earlier noun is position14.](/images/computational-thinking/week-7/nearest-noun.png)
Follow the dictionary as its keys and values change. Predict each lookup or update before revealing the next step.
Ready
No records have been processed. Start with Next step.
Predict the next key, whether it already exists, and the value that will be stored.
matchD = {}
foreach p in partOfSpeech["Pronoun"] {
matched = -1
foreach n in partOfSpeech["Noun"] {
if (n < p) {
matched = n
}
else {
exitloop
}
}
matchD[p] = matched
}For each pronoun p, reset matched to −1. Walk the sorted noun positions. Every n<p is a candidate; updating matched as n increases leaves the largest position below p. At the first n≥p, exit only the INNER loop and store matchD[p]. Then continue with the next pronoun.
| Pronoun position and word | Chosen noun position and word |
|---|---|
| 0 It | −1: no preceding noun |
| 9 his | 4 Swaminathan |
| 11 He | 10 eyes. |
| 27 it | 26 Sunday, |
| 40 He | 39 discipline. |
| 55 his | 54 Vedanayagam, |
| 61 his | 59 headmaster |
This is the lecture’s deliberately simple positional heuristic. It can choose a grammatically wrong antecedent: “He” at 11 is mapped to “eyes.” at 10. It demonstrates dictionary/list processing, not a complete English pronoun resolver. The −1 result is a sentinel, never a valid word lookup.
Why must matched reset for every pronoun?
Otherwise a pronoun with no preceding noun could inherit an unrelated earlier result.
Why can the loop stop at n≥p?
All later noun positions are at least as large because the list is sorted.
What does the source rule return for the paragraph’s first word “It” at 0?
−1, because no noun position is earlier than 0.