<ulclass="crumbs"><li><ahref="../index.html">Home</a></li><li><ahref="../units.html">Unit Test Tools</a></li><li><ahref="index.html">linguistics-test</a></li><li><ahref="index.html#1">Chapter 1: Scaffolding</a></li><li><b>Diagramming</b></li></ul></div>
<pclass="purpose">Turning a file of natural language into a syntax tree.</p>
<pclass="commentary firstcommentary"><aid="SP1"></a><b>§1. </b>Everything is now set up to read in a text file, break it into sentences,
and then hand each one to <ahref="../linguistics-module/index.html"class="internal">linguistics</a> in turn to construct syntax diagrams.
</p>
<pclass="commentary">We need to tell <ahref="../linguistics-module/index.html"class="internal">linguistics</a> which parts of a verb we will allow in these
sentences — the answer being, all of them. (Inform is more restrictive.)
<spanclass="reserved-syntax">parse_node_tree</span><spanclass="plain-syntax"> *</span><spanclass="function-syntax">Diagramming::test_diagrams</span><buttonclass="popup"onclick="togglePopup('usagePopup1')"><spanclass="comment-syntax">?</span><spanclass="popuptext"id="usagePopup1">Usage of <spanclass="code-font"><spanclass="function-syntax">Diagramming::test_diagrams</span></span>:<br/>Program Control - <ahref="1-pc.html#SP1">§1</a></span></button><spanclass="plain-syntax">(</span><spanclass="reserved-syntax">text_stream</span><spanclass="plain-syntax"> *</span><spanclass="identifier-syntax">arg</span><spanclass="plain-syntax">, </span><spanclass="reserved-syntax">int</span><spanclass="plain-syntax"></span><spanclass="identifier-syntax">raw</span><spanclass="plain-syntax">) {</span>
<spanclass="plain-syntax"></span><spanclass="named-paragraph-container code-font"><ahref="1-dgr2.html#SP2_1"class="named-paragraph-link"><spanclass="named-paragraph">Turn the file into a syntax tree</span><spanclass="named-paragraph-number">2.1</span></a></span><spanclass="plain-syntax">;</span>
<spanclass="plain-syntax"></span><spanclass="named-paragraph-container code-font"><ahref="1-dgr2.html#SP2_2"class="named-paragraph-link"><spanclass="named-paragraph">Use the linguistics module on each sentence</span><spanclass="named-paragraph-number">2.2</span></a></span><spanclass="plain-syntax">;</span>
<pclass="commentary firstcommentary"><aid="SP2_1"></a><b>§2.1. </b><spanclass="named-paragraph-container code-font"><spanclass="named-paragraph-defn">Turn the file into a syntax tree</span><spanclass="named-paragraph-number">2.1</span></span><spanclass="comment-syntax"> =</span>
<ulclass="endnotetexts"><li>This code is used in <ahref="1-dgr2.html#SP2">§2</a>.</li></ul>
<pclass="commentary firstcommentary"><aid="SP2_2"></a><b>§2.2. </b><spanclass="named-paragraph-container code-font"><spanclass="named-paragraph-defn">Use the linguistics module on each sentence</span><spanclass="named-paragraph-number">2.2</span></span><spanclass="comment-syntax"> =</span>
<ulclass="endnotetexts"><li>This code is used in <ahref="1-dgr2.html#SP2">§2</a>.</li></ul>
<pclass="commentary firstcommentary"><aid="SP3"></a><b>§3. </b>The work of the <ahref="../words-module/index.html"class="internal">words</a> and <ahref="../syntax-module/index.html"class="internal">syntax</a> modules means that we now have a
rudimentary syntax tree, in which each sentence is just a single <spanclass="extract"><spanclass="extract-syntax">SENTENCE_NT</span></span>
node without children. We look for these, and apply <sentence>, the most
powerful nonterminal from the <ahref="../linguistics-module/index.html"class="internal">linguistics</a> module, to them. All being well
(i.e., if any sentence structure can be found), this returns a subtree of
further nodes, which we graft below the <spanclass="extract"><spanclass="extract-syntax">SENTENCE_NT</span></span>.
<spanclass="reserved-syntax">void</span><spanclass="plain-syntax"></span><spanclass="function-syntax">Diagramming::diagram</span><buttonclass="popup"onclick="togglePopup('usagePopup2')"><spanclass="comment-syntax">?</span><spanclass="popuptext"id="usagePopup2">Usage of <spanclass="code-font"><spanclass="function-syntax">Diagramming::diagram</span></span>:<br/><ahref="1-dgr2.html#SP2_2">§2.2</a></span></button><spanclass="plain-syntax">(</span><spanclass="reserved-syntax">parse_node</span><spanclass="plain-syntax"> *</span><spanclass="identifier-syntax">p</span><spanclass="plain-syntax">) {</span>
<spanclass="plain-syntax"></span><ahref="../../../inweb/docs/foundation-module/3-em.html#SP5"class="function-link"><spanclass="function-syntax">Errors::nowhere</span></a><spanclass="plain-syntax">(</span><spanclass="string-syntax">"sentence has two certainties"</span><spanclass="plain-syntax">);</span>
<spanclass="plain-syntax"></span><ahref="../../../inweb/docs/foundation-module/3-em.html#SP5"class="function-link"><spanclass="function-syntax">Errors::nowhere</span></a><spanclass="plain-syntax">(</span><spanclass="string-syntax">"sentence failed to parse"</span><spanclass="plain-syntax">);</span>
<spanclass="plain-syntax"> }</span>
<spanclass="plain-syntax"> }</span>
<spanclass="plain-syntax">}</span>
</pre>
<pclass="commentary firstcommentary"><aid="SP4"></a><b>§4. </b>That sorts out the verbs and prepositions, but the noun phrases are not
by default parsed: they are simply left as <spanclass="extract"><spanclass="extract-syntax">UNPARSED_NOUN_NT</span></span> nodes.
<spanclass="reserved-syntax">void</span><spanclass="plain-syntax"></span><spanclass="function-syntax">Diagramming::parse_noun_phrases</span><buttonclass="popup"onclick="togglePopup('usagePopup3')"><spanclass="comment-syntax">?</span><spanclass="popuptext"id="usagePopup3">Usage of <spanclass="code-font"><spanclass="function-syntax">Diagramming::parse_noun_phrases</span></span>:<br/><ahref="1-dgr2.html#SP2_2">§2.2</a></span></button><spanclass="plain-syntax">(</span><spanclass="reserved-syntax">parse_node</span><spanclass="plain-syntax"> *</span><spanclass="identifier-syntax">p</span><spanclass="plain-syntax">) {</span>