<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: xiu kuang</title>
    <description>The latest articles on DEV Community by xiu kuang (@xiu_kuang_f0402a0d68ab4e6).</description>
    <link>https://dev.to/xiu_kuang_f0402a0d68ab4e6</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4055844%2F8a56a357-8126-419f-b79f-cef34c19537f.png</url>
      <title>DEV Community: xiu kuang</title>
      <link>https://dev.to/xiu_kuang_f0402a0d68ab4e6</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/xiu_kuang_f0402a0d68ab4e6"/>
    <language>en</language>
    <item>
      <title>Building a Browser-Based Sentence Counter with Local-First Text Processing</title>
      <dc:creator>xiu kuang</dc:creator>
      <pubDate>Fri, 31 Jul 2026 02:14:15 +0000</pubDate>
      <link>https://dev.to/xiu_kuang_f0402a0d68ab4e6/building-a-browser-based-sentence-counter-with-local-first-text-processing-2b05</link>
      <guid>https://dev.to/xiu_kuang_f0402a0d68ab4e6/building-a-browser-based-sentence-counter-with-local-first-text-processing-2b05</guid>
      <description>&lt;p&gt;Many text utilities are simple enough to run entirely in the browser.&lt;/p&gt;

&lt;p&gt;A sentence counter is a good example. The browser already has everything it needs to accept text, analyze it, and display useful statistics. Sending the input to a remote server is often unnecessary.&lt;/p&gt;

&lt;p&gt;I built a small browser-based sentence counter as part of &lt;a href="https://www.textfixhub.com/" rel="noopener noreferrer"&gt;TextFixHub&lt;/a&gt;, a collection of free text utilities for everyday writing and editing.&lt;/p&gt;

&lt;p&gt;You can try it here:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.textfixhub.com/tools/sentence-counter" rel="noopener noreferrer"&gt;TextFixHub Sentence Counter&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why process text locally?
&lt;/h2&gt;

&lt;p&gt;Text input can be sensitive. It may contain:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Draft articles&lt;/li&gt;
&lt;li&gt;School assignments&lt;/li&gt;
&lt;li&gt;Internal notes&lt;/li&gt;
&lt;li&gt;Customer messages&lt;/li&gt;
&lt;li&gt;Code comments&lt;/li&gt;
&lt;li&gt;Unpublished content&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For a basic counting tool, uploading this text to a server does not provide much additional value. Local processing has several useful properties:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The input does not need to leave the user's device.&lt;/li&gt;
&lt;li&gt;There is no text-processing API to maintain.&lt;/li&gt;
&lt;li&gt;The tool can respond without waiting for a network request.&lt;/li&gt;
&lt;li&gt;The application can work without a user account.&lt;/li&gt;
&lt;li&gt;The server does not need to store or process the submitted text.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This does not make every browser application private by default. Analytics, third-party scripts, error reporting, and hosting configuration still matter. It simply means that the core text analysis can remain local.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the tool calculates
&lt;/h2&gt;

&lt;p&gt;The sentence counter displays more than just a sentence total. It also calculates:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sentence count&lt;/li&gt;
&lt;li&gt;Word count&lt;/li&gt;
&lt;li&gt;Character count with spaces&lt;/li&gt;
&lt;li&gt;Character count without spaces&lt;/li&gt;
&lt;li&gt;Paragraph count&lt;/li&gt;
&lt;li&gt;Line count&lt;/li&gt;
&lt;li&gt;Average sentence length&lt;/li&gt;
&lt;li&gt;Average word length&lt;/li&gt;
&lt;li&gt;Estimated reading time&lt;/li&gt;
&lt;li&gt;Estimated speaking time&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The additional statistics make the tool useful for editing and reviewing content, rather than only answering “How many sentences are there?”&lt;/p&gt;

&lt;h2&gt;
  
  
  A simple analysis pipeline
&lt;/h2&gt;

&lt;p&gt;The browser-side pipeline is intentionally straightforward:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Read the current value from the text input.&lt;/li&gt;
&lt;li&gt;Normalize the relevant whitespace.&lt;/li&gt;
&lt;li&gt;Detect sentence boundaries.&lt;/li&gt;
&lt;li&gt;Split the text into words, paragraphs, and lines.&lt;/li&gt;
&lt;li&gt;Calculate the derived statistics.&lt;/li&gt;
&lt;li&gt;Render the results immediately.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The important design choice is that the input stays in the browser throughout this process.&lt;/p&gt;

&lt;p&gt;A simplified version of the idea looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;TextStats&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;sentences&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;
  &lt;span class="na"&gt;words&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;
  &lt;span class="na"&gt;characters&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;
  &lt;span class="na"&gt;charactersWithoutSpaces&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;
  &lt;span class="na"&gt;paragraphs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;
  &lt;span class="na"&gt;lines&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;countWords&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;normalized&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;trim&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;normalized&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;normalized&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="se"&gt;\s&lt;/span&gt;&lt;span class="sr"&gt;+/&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;countCharactersWithoutSpaces&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="se"&gt;\s&lt;/span&gt;&lt;span class="sr"&gt;/g&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The actual implementation also needs to handle empty input, whitespace-only input, paragraph boundaries, and the distinction between visible characters and whitespace.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sentence boundaries are not completely trivial
&lt;/h2&gt;

&lt;p&gt;Counting sentences looks easy until real text appears.&lt;/p&gt;

&lt;p&gt;A simple approach can count punctuation such as &lt;code&gt;.&lt;/code&gt;, &lt;code&gt;!&lt;/code&gt;, and &lt;code&gt;?&lt;/code&gt;. However, periods can also appear in:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Abbreviations&lt;/li&gt;
&lt;li&gt;Decimal numbers&lt;/li&gt;
&lt;li&gt;Domain names&lt;/li&gt;
&lt;li&gt;Email addresses&lt;/li&gt;
&lt;li&gt;Version numbers&lt;/li&gt;
&lt;li&gt;Initials&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This means that a sentence counter should not imply perfect linguistic understanding. It is a practical text utility with defined heuristics.&lt;/p&gt;

&lt;p&gt;For a lightweight browser tool, a transparent heuristic is often more useful than a large natural-language-processing dependency. The tool should also explain what it counts and avoid presenting an approximate result as a scientific linguistic measurement.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reading time and speaking time
&lt;/h2&gt;

&lt;p&gt;Reading and speaking time are estimates based on word count.&lt;/p&gt;

&lt;p&gt;For example, the application can use separate words-per-minute assumptions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;READING_WORDS_PER_MINUTE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;SPEAKING_WORDS_PER_MINUTE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;130&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;readingMinutes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
  &lt;span class="nx"&gt;wordCount&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;READING_WORDS_PER_MINUTE&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;speakingMinutes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
  &lt;span class="nx"&gt;wordCount&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;SPEAKING_WORDS_PER_MINUTE&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These numbers are not universal facts about every reader or speaker. They are simple reference estimates that help users understand the approximate size of their text.&lt;/p&gt;

&lt;p&gt;The interface should avoid suggesting false precision. Displaying “about 2 minutes” is generally more useful than displaying a value with several decimal places.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why I used a static deployment
&lt;/h2&gt;

&lt;p&gt;The application is built with Next.js and TypeScript and deployed as a static website.&lt;/p&gt;

&lt;p&gt;The tool does not need a database or an application server to analyze text. This keeps the architecture small:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The UI runs in the browser.&lt;/li&gt;
&lt;li&gt;The text-processing functions are regular TypeScript modules.&lt;/li&gt;
&lt;li&gt;The page can be statically generated.&lt;/li&gt;
&lt;li&gt;The deployment surface is smaller than a server-based implementation.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A smaller architecture also makes the privacy explanation easier to verify: the main text operation is visible in the client-side application rather than hidden behind an API.&lt;/p&gt;

&lt;h2&gt;
  
  
  Testing the text-processing functions
&lt;/h2&gt;

&lt;p&gt;The UI is only one part of the tool. The text-processing functions should be tested independently with cases such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Empty input&lt;/li&gt;
&lt;li&gt;Whitespace-only input&lt;/li&gt;
&lt;li&gt;One sentence&lt;/li&gt;
&lt;li&gt;Multiple paragraphs&lt;/li&gt;
&lt;li&gt;Multiple line breaks&lt;/li&gt;
&lt;li&gt;Punctuation at the end of a sentence&lt;/li&gt;
&lt;li&gt;Text containing numbers&lt;/li&gt;
&lt;li&gt;Text containing Unicode characters&lt;/li&gt;
&lt;li&gt;Text with repeated spaces&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Separating the counting logic from the UI makes these cases easier to test and reduces the chance that a visual change breaks the underlying calculations.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I learned
&lt;/h2&gt;

&lt;p&gt;The main lesson was that small tools still need clear boundaries.&lt;/p&gt;

&lt;p&gt;A sentence counter does not need an account system, a database, or a text-processing API. It does need:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A clearly defined counting method&lt;/li&gt;
&lt;li&gt;Useful output beyond one number&lt;/li&gt;
&lt;li&gt;Reasonable handling of empty and unusual input&lt;/li&gt;
&lt;li&gt;Tests for the core functions&lt;/li&gt;
&lt;li&gt;A clear explanation of what happens to user text&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Local-first processing is not a complete privacy guarantee for an entire website. It is a practical architectural decision for the core task: the text entered into the tool does not need to be uploaded for the tool to work.&lt;/p&gt;

&lt;p&gt;The finished tool is available here:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.textfixhub.com/tools/sentence-counter" rel="noopener noreferrer"&gt;Try the TextFixHub Sentence Counter&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The other TextFixHub tools are available at &lt;a href="https://www.textfixhub.com/" rel="noopener noreferrer"&gt;https://www.textfixhub.com/&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>frontend</category>
      <category>javascript</category>
      <category>tools</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
