372 lines
34 KiB
HTML
372 lines
34 KiB
HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
<html xmlns="http://www.w3.org/1999/xhtml">
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
|
|
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
|
|
<meta name="generator" content="Doxygen 1.8.6"/>
|
|
<title>Boost.Locale: Boundary analysis</title>
|
|
<link href="tabs.css" rel="stylesheet" type="text/css"/>
|
|
<script type="text/javascript" src="jquery.js"></script>
|
|
<script type="text/javascript" src="dynsections.js"></script>
|
|
<link href="navtree.css" rel="stylesheet" type="text/css"/>
|
|
<script type="text/javascript" src="resize.js"></script>
|
|
<script type="text/javascript" src="navtree.js"></script>
|
|
<script type="text/javascript">
|
|
$(document).ready(initResizable);
|
|
$(window).load(resizeHeight);
|
|
</script>
|
|
<link href="doxygen.css" rel="stylesheet" type="text/css" />
|
|
</head>
|
|
<body>
|
|
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
|
|
<div id="titlearea">
|
|
<table cellspacing="0" cellpadding="0">
|
|
<tbody>
|
|
<tr style="height: 56px;">
|
|
<td id="projectlogo"><img alt="Logo" src="boost-small.png"/></td>
|
|
<td style="padding-left: 0.5em;">
|
|
<div id="projectname">Boost.Locale
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<!-- end header part -->
|
|
<!-- Generated by Doxygen 1.8.6 -->
|
|
<div id="navrow1" class="tabs">
|
|
<ul class="tablist">
|
|
<li><a href="index.html"><span>Main Page</span></a></li>
|
|
<li class="current"><a href="pages.html"><span>Related Pages</span></a></li>
|
|
<li><a href="modules.html"><span>Modules</span></a></li>
|
|
<li><a href="namespaces.html"><span>Namespaces</span></a></li>
|
|
<li><a href="annotated.html"><span>Classes</span></a></li>
|
|
<li><a href="files.html"><span>Files</span></a></li>
|
|
<li><a href="examples.html"><span>Examples</span></a></li>
|
|
</ul>
|
|
</div>
|
|
</div><!-- top -->
|
|
<div id="side-nav" class="ui-resizable side-nav-resizable">
|
|
<div id="nav-tree">
|
|
<div id="nav-tree-contents">
|
|
<div id="nav-sync" class="sync"></div>
|
|
</div>
|
|
</div>
|
|
<div id="splitbar" style="-moz-user-select:none;"
|
|
class="ui-resizable-handle">
|
|
</div>
|
|
</div>
|
|
<script type="text/javascript">
|
|
$(document).ready(function(){initNavTree('boundary_analysys.html','');});
|
|
</script>
|
|
<div id="doc-content">
|
|
<div class="header">
|
|
<div class="headertitle">
|
|
<div class="title">Boundary analysis </div> </div>
|
|
</div><!--header-->
|
|
<div class="contents">
|
|
<div class="textblock"><ul>
|
|
<li><a class="el" href="boundary_analysys.html#boundary_analysys_basics">Basics</a></li>
|
|
<li><a class="el" href="boundary_analysys.html#boundary_analysys_segments">Iterating Over Segments</a><ul>
|
|
<li><a class="el" href="boundary_analysys.html#boundary_analysys_segments_basics">Basic Iteration</a></li>
|
|
<li><a class="el" href="boundary_analysys.html#boundary_analysys_segments_rules">Using Rules</a></li>
|
|
<li><a class="el" href="boundary_analysys.html#boundary_analysys_segments_search">Locating Segments</a></li>
|
|
</ul>
|
|
</li>
|
|
<li><a class="el" href="boundary_analysys.html#boundary_analysys_break">Iterating Over Boundary Points</a><ul>
|
|
<li><a class="el" href="boundary_analysys.html#boundary_analysys_break_basics">Basic Iteration</a></li>
|
|
<li><a class="el" href="boundary_analysys.html#boundary_analysys_break_rules">Using Rules</a></li>
|
|
<li><a class="el" href="boundary_analysys.html#boundary_analysys_break_search">Locating Boundary Points</a></li>
|
|
</ul>
|
|
</li>
|
|
</ul>
|
|
<h1><a class="anchor" id="boundary_analysys_basics"></a>
|
|
Basics</h1>
|
|
<p>Boost.Locale provides a boundary analysis tool, allowing you to split text into characters, words, or sentences, and find appropriate places for line breaks.</p>
|
|
<dl class="section note"><dt>Note</dt><dd>This task is not a trivial task. </dd></dl>
|
|
<dl class="section user"><dt></dt><dd>A Unicode code point and a character are not equivalent, for example: Hebrew word Shalom - "שָלוֹם" that consists of 4 characters and 6 code points (4 base letters and 2 diacritical marks) </dd></dl>
|
|
<dl class="section user"><dt></dt><dd>Words may not be separated by space characters in some languages like in Japanese or Chinese.</dd></dl>
|
|
<p>Boost.Locale provides 2 major classes for boundary analysis:</p>
|
|
<ul>
|
|
<li><a class="el" href="classboost_1_1locale_1_1boundary_1_1segment__index.html">boost::locale::boundary::segment_index</a> - an object that holds an index of segments in the text (like words, characters, sentences). It provides an access to <a class="el" href="classboost_1_1locale_1_1boundary_1_1segment.html">segment</a> objects via iterators.</li>
|
|
<li><a class="el" href="classboost_1_1locale_1_1boundary_1_1boundary__point__index.html">boost::locale::boundary::boundary_point_index</a> - an object that holds an index of boundary points in the text. It allows to iterate over the <a class="el" href="classboost_1_1locale_1_1boundary_1_1boundary__point.html">boundary_point</a> objects.</li>
|
|
</ul>
|
|
<p>Each of the classes above use an iterator type as template parameter. Both of these classes accept in their constructor:</p>
|
|
<ul>
|
|
<li>A flag that defines boundary analysis <a class="el" href="group__boundary.html#ga15de9963ce9bb6037c8525901dfbf641">boundary_type</a>.</li>
|
|
<li>The pair of iterators that define the text range that should be analysed</li>
|
|
<li>A locale parameter (if not given the global one is used)</li>
|
|
</ul>
|
|
<p>For example: </p>
|
|
<div class="fragment"><div class="line"><span class="keyword">namespace </span>ba=boost::locale::boundary;</div>
|
|
<div class="line">std::string text= ... ;</div>
|
|
<div class="line">std::locale loc = ... ;</div>
|
|
<div class="line">ba::segment_index<std::string::const_iterator> map(<a class="code" href="group__boundary.html#gga15de9963ce9bb6037c8525901dfbf641a99aad8b8a5e25baa9f695abe5e574bb6">ba::word</a>,text.begin(),text.end(),loc);</div>
|
|
</div><!-- fragment --><p>Each of them provide a members <code>begin()</code>, <code>end()</code> and <code>find()</code> that allow to iterate over the selected segments or boundaries in the text or find a location of a segment or boundary for given iterator.</p>
|
|
<p>Convenience a typedefs like <a class="el" href="group__boundary.html#gad4785439a3f03ee455c93830b8f1366c">ssegment_index</a> or <a class="el" href="group__boundary.html#gabcb5bcc788909afeb8c68d857284cb59">wcboundary_point_index</a> provided as well, where "w", "u16" and "u32" prefixes define a character type <code>wchar_t</code>, <code>char16_t</code> and <code>char32_t</code> and "c" and "s" prefixes define whether <code>std::basic_string<CharType>::const_iterator</code> or <code>CharType const *</code> are used.</p>
|
|
<h1><a class="anchor" id="boundary_analysys_segments"></a>
|
|
Iterating Over Segments</h1>
|
|
<h1><a class="anchor" id="boundary_analysys_segments_basics"></a>
|
|
Basic Iteration</h1>
|
|
<p>The text segments analysis is done using <a class="el" href="classboost_1_1locale_1_1boundary_1_1segment__index.html">segment_index</a> class.</p>
|
|
<p>It provides a bidirectional iterator that returns <a class="el" href="classboost_1_1locale_1_1boundary_1_1segment.html">segment</a> object. The segment object represents a pair of iterators that define this segment and a rule according to which it was selected. It can be automatically converted to <code>std::basic_string</code> object.</p>
|
|
<p>To perform boundary analysis, we first create an index object and then iterate over it:</p>
|
|
<p>For example:</p>
|
|
<div class="fragment"><div class="line"><span class="keyword">using namespace </span>boost::locale::boundary;</div>
|
|
<div class="line"><a class="code" href="classboost_1_1locale_1_1generator.html">boost::locale::generator</a> gen;</div>
|
|
<div class="line">std::string text=<span class="stringliteral">"To be or not to be, that is the question."</span></div>
|
|
<div class="line"><span class="comment">// Create mapping of text for token iterator using global locale.</span></div>
|
|
<div class="line"><a class="code" href="classboost_1_1locale_1_1boundary_1_1segment__index.html">ssegment_index</a> map(<a class="code" href="group__boundary.html#gga15de9963ce9bb6037c8525901dfbf641a99aad8b8a5e25baa9f695abe5e574bb6">word</a>,text.begin(),text.end(),gen(<span class="stringliteral">"en_US.UTF-8"</span>)); </div>
|
|
<div class="line"><span class="comment">// Print all "words" -- chunks of word boundary</span></div>
|
|
<div class="line"><span class="keywordflow">for</span>(<a class="code" href="group__boundary.html#gaf7a775e77dbbca3495e11d646df96fd2">ssegment_index::iterator</a> it=map.begin(),e=map.end();it!=e;++it)</div>
|
|
<div class="line"> std::cout <<<span class="stringliteral">"\""</span><< * it << <span class="stringliteral">"\", "</span>;</div>
|
|
<div class="line">std::cout << std::endl;</div>
|
|
</div><!-- fragment --><p>Would print:</p>
|
|
<pre class="fragment">"To", " ", "be", " ", "or", " ", "not", " ", "to", " ", "be", ",", " ", "that", " ", "is", " ", "the", " ", "question", ".",
|
|
</pre><p>This sentence "生きるか死ぬか、それが問題だ。" (<a href="http://tatoeba.org/eng/sentences/show/868189">from Tatoeba database</a>) would be split into following segments in <code>ja_JP.UTF-8</code> (Japanese) locale:</p>
|
|
<pre class="fragment">"生", "きるか", "死", "ぬか", "、", "それが", "問題", "だ", "。",
|
|
</pre><p>The boundary analysis that is done by Boost.Locale is much more complicated then just splitting the text according to white space characters, even thou it is not perfect.</p>
|
|
<h1><a class="anchor" id="boundary_analysys_segments_rules"></a>
|
|
Using Rules</h1>
|
|
<p>The segments selection can be customized using <a class="el" href="group__boundary.html#gad19735180401edb15acbdbbeb21e5a73">rule()</a> and <a class="el" href="group__boundary.html#ga205fd51daa439a18527675e663a0802f">full_select()</a> member functions.</p>
|
|
<p>By default segment_index's iterator return each text segment defined by two boundary points regardless the way they were selected. Thus in the example above we could see text segments like "." or " " that were selected as words.</p>
|
|
<p>Using a <code>rule()</code> member function we can specify a binary mask of rules we want to use for selection of the boundary points using <a class="el" href="group__boundary.html#bl_boundary_word_rules">word</a>, <a class="el" href="group__boundary.html#bl_boundary_line_rules">line</a> and <a class="el" href="group__boundary.html#bl_boundary_sentence_rules">sentence</a> boundary rules.</p>
|
|
<p>For example, by calling</p>
|
|
<div class="fragment"><div class="line">map.rule(<a class="code" href="group__boundary.html#ga3ab98808dbb1cc4a346dcc2554c9d8dc">word_any</a>);</div>
|
|
</div><!-- fragment --><p>Before starting the iteration process, specify a selection mask that fetches: numbers, letter, Kana letters and ideographic characters ignoring all non-word related characters like white space or punctuation marks.</p>
|
|
<p>So the code:</p>
|
|
<div class="fragment"><div class="line"><span class="keyword">using namespace </span>boost::locale::boundary;</div>
|
|
<div class="line">std::string text=<span class="stringliteral">"To be or not to be, that is the question."</span></div>
|
|
<div class="line"><span class="comment">// Create mapping of text for token iterator using global locale.</span></div>
|
|
<div class="line"><a class="code" href="classboost_1_1locale_1_1boundary_1_1segment__index.html">ssegment_index</a> map(<a class="code" href="group__boundary.html#gga15de9963ce9bb6037c8525901dfbf641a99aad8b8a5e25baa9f695abe5e574bb6">word</a>,text.begin(),text.end()); </div>
|
|
<div class="line"><span class="comment">// Define a rule</span></div>
|
|
<div class="line">map.<a class="code" href="group__boundary.html#ga72b4ceb5bacec0eded2601c43a4d671a">rule</a>(<a class="code" href="group__boundary.html#ga3ab98808dbb1cc4a346dcc2554c9d8dc">word_any</a>);</div>
|
|
<div class="line"><span class="comment">// Print all "words" -- chunks of word boundary</span></div>
|
|
<div class="line"><span class="keywordflow">for</span>(<a class="code" href="group__boundary.html#gaf7a775e77dbbca3495e11d646df96fd2">ssegment_index::iterator</a> it=map.begin(),e=map.end();it!=e;++it)</div>
|
|
<div class="line"> std::cout <<<span class="stringliteral">"\""</span><< * it << <span class="stringliteral">"\", "</span>;</div>
|
|
<div class="line">std::cout << std::endl;</div>
|
|
</div><!-- fragment --><p>Would print:</p>
|
|
<pre class="fragment">"To", "be", "or", "not", "to", "be", "that", "is", "the", "question",
|
|
</pre><p>And the for given text="生きるか死ぬか、それが問題だ。" and rule(<a class="el" href="group__boundary.html#ga705ab96f9e62810c8ed977c90d404ef8">word_ideo</a>), the example above would print.</p>
|
|
<pre class="fragment">"生", "死", "問題",
|
|
</pre><p>You can access specific rules the segments where selected it using <a class="el" href="classboost_1_1locale_1_1boundary_1_1segment.html#a5b36a522d7013306617dbcccc9919343">segment::rule()</a> member function. Using a bit-mask of rules.</p>
|
|
<p>For example:</p>
|
|
<div class="fragment"><div class="line"><a class="code" href="classboost_1_1locale_1_1generator.html">boost::locale::generator</a> gen;</div>
|
|
<div class="line"><span class="keyword">using namespace </span>boost::locale::boundary;</div>
|
|
<div class="line">std::string text=<span class="stringliteral">"生きるか死ぬか、それが問題だ。"</span>;</div>
|
|
<div class="line"><a class="code" href="classboost_1_1locale_1_1boundary_1_1segment__index.html">ssegment_index</a> map(<a class="code" href="group__boundary.html#gga15de9963ce9bb6037c8525901dfbf641a99aad8b8a5e25baa9f695abe5e574bb6">word</a>,text.begin(),text.end(),gen(<span class="stringliteral">"ja_JP.UTF-8"</span>)); </div>
|
|
<div class="line"><span class="keywordflow">for</span>(<a class="code" href="group__boundary.html#gaf7a775e77dbbca3495e11d646df96fd2">ssegment_index::iterator</a> it=map.begin(),e=map.end();it!=e;++it) {</div>
|
|
<div class="line"> std::cout << <span class="stringliteral">"Segment "</span> << *it << <span class="stringliteral">" contains: "</span>;</div>
|
|
<div class="line"> <span class="keywordflow">if</span>(it->rule() & <a class="code" href="group__boundary.html#ga90cf4f01b95658f659685377226677e7">word_none</a>)</div>
|
|
<div class="line"> std::cout << <span class="stringliteral">"white space or punctuation marks "</span>;</div>
|
|
<div class="line"> <span class="keywordflow">if</span>(it->rule() & <a class="code" href="group__boundary.html#ga52d8c63e1f3f8c898c645352206a78ef">word_kana</a>)</div>
|
|
<div class="line"> std::cout << <span class="stringliteral">"kana characters "</span>;</div>
|
|
<div class="line"> <span class="keywordflow">if</span>(it->rule() & <a class="code" href="group__boundary.html#ga705ab96f9e62810c8ed977c90d404ef8">word_ideo</a>)</div>
|
|
<div class="line"> std::cout << <span class="stringliteral">"ideographic characters"</span>;</div>
|
|
<div class="line"> std::cout<< std::endl;</div>
|
|
<div class="line">}</div>
|
|
</div><!-- fragment --><p>Would print</p>
|
|
<pre class="fragment">Segment 生 contains: ideographic characters
|
|
Segment きるか contains: kana characters
|
|
Segment 死 contains: ideographic characters
|
|
Segment ぬか contains: kana characters
|
|
Segment 、 contains: white space or punctuation marks
|
|
Segment それが contains: kana characters
|
|
Segment 問題 contains: ideographic characters
|
|
Segment だ contains: kana characters
|
|
Segment 。 contains: white space or punctuation marks
|
|
</pre><p>One important things that should be noted that each segment is defined by a pair of boundaries and the rule of its ending point defines if it is selected or not.</p>
|
|
<p>In some cases it may be not what we actually look like.</p>
|
|
<p>For example we have a text:</p>
|
|
<pre class="fragment">Hello! How
|
|
are you?
|
|
</pre><p>And we want to fetch all sentences from the text.</p>
|
|
<p>The <a class="el" href="group__boundary.html#bl_boundary_sentence_rules">sentence rules</a> have two options:</p>
|
|
<ul>
|
|
<li>Split the text on the point where sentence terminator like ".!?" detected: <a class="el" href="group__boundary.html#ga3befefe67f79691c117bf5588741355b">sentence_term</a></li>
|
|
<li>Split the text on the point where sentence separator like "line feed" detected: <a class="el" href="group__boundary.html#gaf67883341dd3d8f786e7281d40790000">sentence_sep</a></li>
|
|
</ul>
|
|
<p>Naturally to ignore sentence separators we would call <a class="el" href="group__boundary.html#gad19735180401edb15acbdbbeb21e5a73">segment_index::rule(rule_type v)</a> with sentence_term parameter and then run the iterator.</p>
|
|
<div class="fragment"><div class="line"><a class="code" href="classboost_1_1locale_1_1generator.html">boost::locale::generator</a> gen;</div>
|
|
<div class="line"><span class="keyword">using namespace </span>boost::locale::boundary;</div>
|
|
<div class="line">std::string text= <span class="stringliteral">"Hello! How\n"</span></div>
|
|
<div class="line"> <span class="stringliteral">"are you?\n"</span>;</div>
|
|
<div class="line"><a class="code" href="classboost_1_1locale_1_1boundary_1_1segment__index.html">ssegment_index</a> map(<a class="code" href="group__boundary.html#gga15de9963ce9bb6037c8525901dfbf641a88aa1509eace7589f5df87d4694871e9">sentence</a>,text.begin(),text.end(),gen(<span class="stringliteral">"en_US.UTF-8"</span>)); </div>
|
|
<div class="line">map.<a class="code" href="group__boundary.html#ga72b4ceb5bacec0eded2601c43a4d671a">rule</a>(<a class="code" href="group__boundary.html#ga3befefe67f79691c117bf5588741355b">sentence_term</a>);</div>
|
|
<div class="line"><span class="keywordflow">for</span>(<a class="code" href="group__boundary.html#gaf7a775e77dbbca3495e11d646df96fd2">ssegment_index::iterator</a> it=map.begin(),e=map.end();it!=e;++it) </div>
|
|
<div class="line"> std::cout << <span class="stringliteral">"Sentence ["</span> << *it << <span class="stringliteral">"]"</span> << std::endl;</div>
|
|
</div><!-- fragment --><p>However we would get the expected segments: </p>
|
|
<pre class="fragment">Sentence [Hello! ]
|
|
Sentence [are you?
|
|
]
|
|
</pre><p>The reason is that "How\n" is still considered a sentence but selected by different rule.</p>
|
|
<p>This behavior can be changed by setting <a class="el" href="group__boundary.html#ga205fd51daa439a18527675e663a0802f">segment_index::full_select(bool)</a> to <code>true</code>. It would force iterator to join the current segment with all previous segments that may not fit the required rule.</p>
|
|
<p>So we add this line:</p>
|
|
<div class="fragment"><div class="line">map.full_select(<span class="keyword">true</span>);</div>
|
|
</div><!-- fragment --><p>Right after "map.rule(sentence_term);" and get expected output:</p>
|
|
<pre class="fragment">Sentence [Hello! ]
|
|
Sentence [How
|
|
are you?
|
|
]
|
|
</pre><h2><a class="anchor" id="boundary_analysys_segments_search"></a>
|
|
Locating Segments</h2>
|
|
<p>Sometimes it is useful to find a segment that some specific iterator is pointing on.</p>
|
|
<p>For example a user had clicked at specific point, we want to select a word on this location.</p>
|
|
<p><a class="el" href="classboost_1_1locale_1_1boundary_1_1segment__index.html">segment_index</a> provides <a class="el" href="group__boundary.html#ga2480236106971797460187777f2a4411">find(base_iterator p)</a> member function for this purpose.</p>
|
|
<p>This function returns the iterator to the segmet such that <em>p</em> points to.</p>
|
|
<p>For example:</p>
|
|
<div class="fragment"><div class="line">text=<span class="stringliteral">"to be or "</span>;</div>
|
|
<div class="line"><a class="code" href="group__boundary.html#gad4785439a3f03ee455c93830b8f1366c">ssegment_index</a> map(<a class="code" href="group__boundary.html#gga15de9963ce9bb6037c8525901dfbf641a99aad8b8a5e25baa9f695abe5e574bb6">word</a>,text.begin(),text.end(),gen(<span class="stringliteral">"en_US.UTF-8"</span>));</div>
|
|
<div class="line">ssegment_index::iterator p = map.find(text.begin() + 4);</div>
|
|
<div class="line"><span class="keywordflow">if</span>(p!=map.end())</div>
|
|
<div class="line"> std::cout << *p << std::endl;</div>
|
|
</div><!-- fragment --><p>Would print:</p>
|
|
<pre class="fragment">be
|
|
</pre><dl class="section note"><dt>Note</dt><dd></dd></dl>
|
|
<p>if the iterator lays inside the segment this segment returned. If the segment does not fit the selection rules, then the segment following requested position is returned.</p>
|
|
<p>For example: For <a class="el" href="group__boundary.html#gga15de9963ce9bb6037c8525901dfbf641a99aad8b8a5e25baa9f695abe5e574bb6">word</a> boundary analysis with <a class="el" href="group__boundary.html#ga3ab98808dbb1cc4a346dcc2554c9d8dc">word_any</a> rule:</p>
|
|
<ul>
|
|
<li>"t|o be or ", would point to "to" - the iterator in the middle of segment "to".</li>
|
|
<li>"to |be or ", would point to "be" - the iterator at the beginning of the segment "be"</li>
|
|
<li>"to| be or ", would point to "be" - the iterator does is not point to segment with required rule so next valid segment is selected "be".</li>
|
|
<li>"to be or| ", would point to end as not valid segment found.</li>
|
|
</ul>
|
|
<h1><a class="anchor" id="boundary_analysys_break"></a>
|
|
Iterating Over Boundary Points</h1>
|
|
<h1><a class="anchor" id="boundary_analysys_break_basics"></a>
|
|
Basic Iteration</h1>
|
|
<p>The <a class="el" href="classboost_1_1locale_1_1boundary_1_1boundary__point__index.html">boundary_point_index</a> is similar to <a class="el" href="classboost_1_1locale_1_1boundary_1_1segment__index.html">segment_index</a> in its interface but as a different role. Instead of returning text chunks (<a class="el" href="classboost_1_1locale_1_1boundary_1_1segment.html">segment</a>s, it returns <a class="el" href="classboost_1_1locale_1_1boundary_1_1boundary__point.html">boundary_point</a> object that represents a position in text - a base iterator used that is used for iteration of the source text C++ characters. The <a class="el" href="classboost_1_1locale_1_1boundary_1_1boundary__point.html">boundary_point</a> object also provides a <a class="el" href="classboost_1_1locale_1_1boundary_1_1boundary__point.html#a757b28e11c66f9871d3f51fe93a51bdb">rule()</a> member function that defines a rule this boundary was selected according to.</p>
|
|
<dl class="section note"><dt>Note</dt><dd>The beginning and the ending of the text are considered boundary points, so even an empty text consists of at least one boundary point.</dd></dl>
|
|
<p>Lets see an example of selecting first two sentences from a text:</p>
|
|
<div class="fragment"><div class="line"><span class="keyword">using namespace </span>boost::locale::boundary;</div>
|
|
<div class="line"><a class="code" href="classboost_1_1locale_1_1generator.html">boost::locale::generator</a> gen;</div>
|
|
<div class="line"></div>
|
|
<div class="line"><span class="comment">// our text sample</span></div>
|
|
<div class="line">std::string <span class="keyword">const</span> text=<span class="stringliteral">"First sentence. Second sentence! Third one?"</span>;</div>
|
|
<div class="line"><span class="comment">// Create an index </span></div>
|
|
<div class="line"><a class="code" href="classboost_1_1locale_1_1boundary_1_1boundary__point__index.html">sboundary_point_index</a> map(<a class="code" href="group__boundary.html#gga15de9963ce9bb6037c8525901dfbf641a88aa1509eace7589f5df87d4694871e9">sentence</a>,text.begin(),text.end(),gen(<span class="stringliteral">"en_US.UTF-8"</span>));</div>
|
|
<div class="line"></div>
|
|
<div class="line"><span class="comment">// Count two boundary points</span></div>
|
|
<div class="line"><a class="code" href="group__boundary.html#ga1af6e72b3c384edcebc0cf319fe97efe">sboundary_point_index::iterator</a> p = map.<a class="code" href="group__boundary.html#ga56f42a32f0378b6e157671f9e17bd66f">begin</a>(),e=map.end();</div>
|
|
<div class="line"><span class="keywordtype">int</span> count = 0;</div>
|
|
<div class="line"><span class="keywordflow">while</span>(p!=e && count < 2) {</div>
|
|
<div class="line"> ++count;</div>
|
|
<div class="line"> ++p;</div>
|
|
<div class="line">}</div>
|
|
<div class="line"></div>
|
|
<div class="line"><span class="keywordflow">if</span>(p!=e) {</div>
|
|
<div class="line"> std::cout << <span class="stringliteral">"First two sentences are: "</span> </div>
|
|
<div class="line"> << std::string(text.begin(),p->iterator()) </div>
|
|
<div class="line"> << std::endl;</div>
|
|
<div class="line">}</div>
|
|
<div class="line"><span class="keywordflow">else</span> {</div>
|
|
<div class="line"> std::cout <<<span class="stringliteral">"There are less then two sentences in this "</span></div>
|
|
<div class="line"> <<<span class="stringliteral">"text: "</span> << text << std::endl;</div>
|
|
<div class="line">}</div>
|
|
</div><!-- fragment --><p>Would print:</p>
|
|
<pre class="fragment">First two sentences are: First sentence. Second sentence!
|
|
</pre><h1><a class="anchor" id="boundary_analysys_break_rules"></a>
|
|
Using Rules</h1>
|
|
<p>Similarly to the <a class="el" href="classboost_1_1locale_1_1boundary_1_1segment__index.html">segment_index</a> the <a class="el" href="classboost_1_1locale_1_1boundary_1_1boundary__point__index.html">boundary_point_index</a> provides a <a class="el" href="group__boundary.html#ga56e63913f51109e05a24a7136472a975">rule(rule_type mask)</a> member function to filter boundary points that interest us.</p>
|
|
<p>It allows to set <a class="el" href="group__boundary.html#bl_boundary_word_rules">word</a>, <a class="el" href="group__boundary.html#bl_boundary_line_rules">line</a> and <a class="el" href="group__boundary.html#bl_boundary_sentence_rules">sentence</a> rules for filtering boundary points.</p>
|
|
<p>Lets change an example above a little:</p>
|
|
<div class="fragment"><div class="line"><span class="comment">// our text sample</span></div>
|
|
<div class="line">std::string <span class="keyword">const</span> text= <span class="stringliteral">"First sentence. Second\n"</span></div>
|
|
<div class="line"> <span class="stringliteral">"sentence! Third one?"</span>;</div>
|
|
</div><!-- fragment --><p>If we run our program as is on the sample above we would get: </p>
|
|
<pre class="fragment">First two sentences are: First sentence. Second
|
|
</pre><p>Which is not something that we really expected. As the "Second\n" is considered an independent sentence that was separated by a line separator "Line Feed".</p>
|
|
<p>However, we can set set a rule <a class="el" href="group__boundary.html#ga3befefe67f79691c117bf5588741355b">sentence_term</a> and the iterator would use only boundary points that are created by a sentence terminators like ".!?".</p>
|
|
<p>So by adding: </p>
|
|
<div class="fragment"><div class="line">map.rule(<a class="code" href="group__boundary.html#ga3befefe67f79691c117bf5588741355b">sentence_term</a>);</div>
|
|
</div><!-- fragment --><p>Right after the generation of the index we would get the desired output:</p>
|
|
<pre class="fragment">First two sentences are: First sentence. Second
|
|
sentence!
|
|
</pre><p>You can also use <a class="el" href="classboost_1_1locale_1_1boundary_1_1boundary__point.html#a757b28e11c66f9871d3f51fe93a51bdb">boundary_point::rule()</a> member function to learn about the reason this boundary point was created by comparing it with an appropriate mask.</p>
|
|
<p>For example:</p>
|
|
<div class="fragment"><div class="line"><span class="keyword">using namespace </span>boost::locale::boundary;</div>
|
|
<div class="line"><a class="code" href="classboost_1_1locale_1_1generator.html">boost::locale::generator</a> gen;</div>
|
|
<div class="line"><span class="comment">// our text sample</span></div>
|
|
<div class="line">std::string <span class="keyword">const</span> text= <span class="stringliteral">"First sentence. Second\n"</span></div>
|
|
<div class="line"> <span class="stringliteral">"sentence! Third one?"</span>;</div>
|
|
<div class="line"><a class="code" href="classboost_1_1locale_1_1boundary_1_1boundary__point__index.html">sboundary_point_index</a> map(<a class="code" href="group__boundary.html#gga15de9963ce9bb6037c8525901dfbf641a88aa1509eace7589f5df87d4694871e9">sentence</a>,text.begin(),text.end(),gen(<span class="stringliteral">"en_US.UTF-8"</span>));</div>
|
|
<div class="line"></div>
|
|
<div class="line"><span class="keywordflow">for</span>(<a class="code" href="group__boundary.html#ga1af6e72b3c384edcebc0cf319fe97efe">sboundary_point_index::iterator</a> p = map.begin(),e=map.end();p!=e;++p) {</div>
|
|
<div class="line"> <span class="keywordflow">if</span>(p->rule() & <a class="code" href="group__boundary.html#ga3befefe67f79691c117bf5588741355b">sentence_term</a>)</div>
|
|
<div class="line"> std::cout << <span class="stringliteral">"There is a sentence terminator: "</span>;</div>
|
|
<div class="line"> <span class="keywordflow">else</span> <span class="keywordflow">if</span>(p->rule() & <a class="code" href="group__boundary.html#gaf67883341dd3d8f786e7281d40790000">sentence_sep</a>)</div>
|
|
<div class="line"> std::cout << <span class="stringliteral">"There is a sentence separator: "</span>;</div>
|
|
<div class="line"> <span class="keywordflow">if</span>(p->rule()!=0) <span class="comment">// print if some rule exists</span></div>
|
|
<div class="line"> std::cout << <span class="stringliteral">"["</span> << std::string(text.begin(),p->iterator()) </div>
|
|
<div class="line"> << <span class="stringliteral">"|"</span> << std::string(p->iterator(),text.end()) </div>
|
|
<div class="line"> << <span class="stringliteral">"]\n"</span>;</div>
|
|
<div class="line">}</div>
|
|
</div><!-- fragment --><p>Would give the following output: </p>
|
|
<pre class="fragment">There is a sentence terminator: [First sentence. |Second
|
|
sentence! Third one?]
|
|
There is a sentence separator: [First sentence. Second
|
|
|sentence! Third one?]
|
|
There is a sentence terminator: [First sentence. Second
|
|
sentence! |Third one?]
|
|
There is a sentence terminator: [First sentence. Second
|
|
sentence! Third one?|]
|
|
</pre><h2><a class="anchor" id="boundary_analysys_break_search"></a>
|
|
Locating Boundary Points</h2>
|
|
<p>Sometimes it is useful to find a specific boundary point according to given iterator.</p>
|
|
<p><a class="el" href="classboost_1_1locale_1_1boundary_1_1boundary__point__index.html">boundary_point_index</a> provides a <a class="el" href="group__boundary.html#ga0bb71a287afca990e85b17246568492d">iterator find(base_iterator p)</a> member function.</p>
|
|
<p>It would return an iterator to a boundary point on <em>p's</em> location or at the location following it if <em>p</em> does not point to appropriate position.</p>
|
|
<p>For example, for word boundary analysis:</p>
|
|
<ul>
|
|
<li>If a base iterator points to "to |be", then the returned boundary point would be "to |be" (same position)</li>
|
|
<li>If a base iterator points to "t|o be", then the returned boundary point would be "to| be" (next valid position)</li>
|
|
</ul>
|
|
<p>For example if we want to select 6 words around specific boundary point we can use following code:</p>
|
|
<div class="fragment"><div class="line"><span class="keyword">using namespace </span>boost::locale::boundary;</div>
|
|
<div class="line"><a class="code" href="classboost_1_1locale_1_1generator.html">boost::locale::generator</a> gen;</div>
|
|
<div class="line"><span class="comment">// our text sample</span></div>
|
|
<div class="line">std::string <span class="keyword">const</span> text= <span class="stringliteral">"To be or not to be, that is the question."</span>;</div>
|
|
<div class="line"></div>
|
|
<div class="line"><span class="comment">// Create a mapping</span></div>
|
|
<div class="line"><a class="code" href="classboost_1_1locale_1_1boundary_1_1boundary__point__index.html">sboundary_point_index</a> map(<a class="code" href="group__boundary.html#gga15de9963ce9bb6037c8525901dfbf641a99aad8b8a5e25baa9f695abe5e574bb6">word</a>,text.begin(),text.end(),gen(<span class="stringliteral">"en_US.UTF-8"</span>));</div>
|
|
<div class="line"><span class="comment">// Ignore wite space</span></div>
|
|
<div class="line">map.<a class="code" href="group__boundary.html#ga1d214029f1a780b7bf6e3f23a3004c03">rule</a>(<a class="code" href="group__boundary.html#ga3ab98808dbb1cc4a346dcc2554c9d8dc">word_any</a>);</div>
|
|
<div class="line"></div>
|
|
<div class="line"><span class="comment">// define our arbitraty point</span></div>
|
|
<div class="line">std::string::const_iterator pos = text.begin() + 12; <span class="comment">// "no|t";</span></div>
|
|
<div class="line"></div>
|
|
<div class="line"><span class="comment">// Get the search range</span></div>
|
|
<div class="line"><a class="code" href="group__boundary.html#ga1af6e72b3c384edcebc0cf319fe97efe">sboundary_point_index::iterator</a> </div>
|
|
<div class="line"> begin =map.begin(),</div>
|
|
<div class="line"> end = map.end(),</div>
|
|
<div class="line"> it = map.find(pos); <span class="comment">// find a boundary</span></div>
|
|
<div class="line"></div>
|
|
<div class="line"><span class="comment">// go 3 words backward</span></div>
|
|
<div class="line"><span class="keywordflow">for</span>(<span class="keywordtype">int</span> count = 0;count <3 && it!=begin; count ++) </div>
|
|
<div class="line"> --it;</div>
|
|
<div class="line"></div>
|
|
<div class="line"><span class="comment">// Save the start</span></div>
|
|
<div class="line">std::string::const_iterator start = *it;</div>
|
|
<div class="line"></div>
|
|
<div class="line"><span class="comment">// go 6 words forward</span></div>
|
|
<div class="line"><span class="keywordflow">for</span>(<span class="keywordtype">int</span> count = 0;count < 6 && it!=end; count ++)</div>
|
|
<div class="line"> ++it;</div>
|
|
<div class="line"></div>
|
|
<div class="line"><span class="comment">// make sure we at valid position</span></div>
|
|
<div class="line"><span class="keywordflow">if</span>(it==end)</div>
|
|
<div class="line"> --it;</div>
|
|
<div class="line"></div>
|
|
<div class="line"><span class="comment">// print the text</span></div>
|
|
<div class="line">std::cout << std::string(start,it->iterator()) << std::endl;</div>
|
|
</div><!-- fragment --><p>That would print:</p>
|
|
<pre class="fragment"> be or not to be, that
|
|
</pre> </div></div><!-- contents -->
|
|
</div><!-- doc-content -->
|
|
|
|
<li class="footer">
|
|
© Copyright 2009-2012 Artyom Beilis, Distributed under the <a href="http://www.boost.org/LICENSE_1_0.txt">Boost Software License</a>, Version 1.0.
|
|
</li>
|
|
</ul>
|
|
</div>
|
|
</body>
|
|
</html>
|