<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Tanguy Firinga on Medium]]></title>
        <description><![CDATA[Stories by Tanguy Firinga on Medium]]></description>
        <link>https://medium.com/@tanguy.firinga_87550?source=rss-2d6ae1007658------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*aiNh63P0ne2O5ucF</url>
            <title>Stories by Tanguy Firinga on Medium</title>
            <link>https://medium.com/@tanguy.firinga_87550?source=rss-2d6ae1007658------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 18:50:56 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@tanguy.firinga_87550/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to keep track of clause in Word add-in to integrate AI-generated reformulation]]></title>
            <link>https://medium.com/doctrine/how-to-keep-track-of-clause-in-word-add-in-to-integrate-ai-generated-reformulation-420b5216ccaa?source=rss-2d6ae1007658------2</link>
            <guid isPermaLink="false">https://medium.com/p/420b5216ccaa</guid>
            <category><![CDATA[office-365]]></category>
            <category><![CDATA[legaltech]]></category>
            <category><![CDATA[bag-of-words]]></category>
            <category><![CDATA[engineering]]></category>
            <dc:creator><![CDATA[Tanguy Firinga]]></dc:creator>
            <pubDate>Tue, 06 Jan 2026 07:59:38 GMT</pubDate>
            <atom:updated>2026-01-06T07:59:38.746Z</atom:updated>
            <content:encoded><![CDATA[<h3>How to keep track of a clause in Word add-in to integrate AI-generated reformulation</h3><h3>Context</h3><p><strong>Flow Counsel</strong> brings AI-powered contract analysis to Doctrine, helping legal teams review and refine clauses with clarity and precision.</p><p>Through our Word add-in, legal professionals can analyze their contracts and receive AI-suggested reformulations for individual clauses.</p><p>However, integrating these suggestions back into the document isn’t straightforward — the Word add-in API offers limited support for interacting with existing contract content.</p><p>In this article, we’ll walk through how we manage to keep track of existing clauses inside a Word document and how we enable users to browse clauses seamlessly before inserting reformulations.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EAdSGostRQCLSLOh47G-MQ.png" /><figcaption>Doctrine Word Add-in</figcaption></figure><h3>What is a Word add-in, and what does the contract analysis workflow look like?</h3><p>A Word add-in is essentially <strong>a web application running inside a dedicated panel within Microsoft Word</strong>. This architecture allows us to reuse the entire stack already built for the <a href="https://doctrine.fr/">doctrine.fr</a> web app — from React components to contract-analysis backend endpoints.</p><p>When the user launches a new analysis, the add-in uploads the entire .docx file. The contract analysis pipeline then:</p><ul><li>identifies clauses,</li><li>retrieves relevant legal content,</li><li>and detects risks within each clause.</li></ul><p>The outcome is a list of legal risks by clause, and for every clause flagged as risky, the system suggests an AI-generated reformulation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*h1lm_8vLy8F1WdJBhjeT0g.png" /><figcaption>Clause analysis in Doctrine Word add-in</figcaption></figure><p>When the user clicks on a risky clause, our goal is to scroll to that exact clause in the Word document, display its analysis, and offer to apply the proposed rewrite.</p><p>The main challenge: <strong>finding the clause again inside the original Word document.</strong></p><h3>Find the clause in the entire document</h3><p>In theory, the best solution would be to track clauses throughout the entire analysis workflow. In practice, this turns out to be difficult for several reasons:</p><ul><li>Word paragraphs don’t have stable IDs across sessions.</li><li>Although the Word add-in API allows assigning persistent IDs, extracting these IDs inside our backend analysis pipeline is complex.</li><li>We wanted to avoid developing a Word-specific analysis workflow at this stage.</li></ul><p>In short, the only reliable way to locate a clause within the document is by matching its content.</p><h3>Why the search API is not enough?</h3><p>In order to find a clause in the document, our first instinct was to rely on the built-in search API provided by Office.js:</p><pre>Word.run(async (context) =&gt; {<br>  const rangeCollection = context.document.search(&#39;my clause ...&#39;)<br>  rangeCollection.load(&#39;items&#39;);<br>  await context.sync();<br><br>  const ranges = rangeCollection.items;<br>  const firstRange = ranges[0];<br>	<br>  firstRange.select(); // selection triggers scrolling<br>});</pre><p>At first glance, the API seems straightforward: provide a string, get back all matching ranges. However, in practice, it quickly reaches its limits.</p><p>Clauses are often more complex than plain text — they can include bullet points, numbering, subtitles or mixed formatting that the Add-in search API doesn’t handle well. As a result, many clauses can’t be matched reliably through a simple text search.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1012/1*YCgmG-md89ueJd7cTqZuXg.png" /><figcaption>A complex clause</figcaption></figure><h3>Solution: implement a bag-of-words search</h3><p>Since the built-in search API can’t reliably match clauses that include lists, numbering, or formatting, we needed a more flexible approach.</p><p>Instead of matching exact strings, we decided to compare <strong>the meaning</strong> of the text itself — using a simple <strong>bag-of-words</strong> algorithm.</p><p>The idea is to represent both the searched clause and each paragraph of the document as collections of words (bags), ignoring layout or punctuation.</p><p>Then, we slide through the document’s paragraphs and look for sequences whose word composition is close enough to the searched clause.</p><p>Here’s the core of our implementation:</p><pre>function findParagraphs(paragraphs: string[], searchedText: string) {<br>  const searchTextBag = new BagOfWords(searchedText);<br><br>  let currentTextBag: BagOfWords | null = null;<br>  let resultParagraphs: string[] = [];<br><br>  for (const paragraph of paragraphs) {<br>    const paragraphBag = new BagOfWords(paragraph);<br>    const hasResult = resultParagraphs.length !== 0;<br><br>    if (!hasResult &amp;&amp; paragraphBag.bagSize === 0) continue;<br><br>    const inclusion = searchTextBag.getFuzzyInclude(paragraphBag);<br><br>    if (inclusion &gt;= INCLUSION_THRESHOLD) {<br>      // include current paragraph to result and update current bag<br>      resultParagraphs.push(paragraph);<br>      currentTextBag = hasResult &amp;&amp; currentTextBag<br>        ? currentTextBag.subtract(paragraphBag)<br>        : searchTextBag.subtract(paragraphBag);<br>    } else {<br>      const remaining = currentTextBag<br>        ? currentTextBag.bagSize / searchTextBag.bagSize<br>        : 1;<br><br>      // return found paragraphs when there are few remaining words <br>      if (currentTextBag &amp;&amp; remaining &lt;= 1 - INCLUSION_THRESHOLD) {<br>        return resultParagraphs;<br>      }<br><br>      // reset result if else<br>      resultParagraphs = [];<br>      currentTextBag = null;<br>    }<br>  }<br><br>  const remaining = currentTextBag<br>    ? currentTextBag.bagSize / searchTextBag.bagSize<br>    : 1;<br><br>  return remaining &gt; 1 - INCLUSION_THRESHOLD ? [] : resultParagraphs;<br>}</pre><p>This algorithm iterates through each paragraph, computing how much of the searched clause it contains.</p><p>If the inclusion score stays above a defined threshold, the paragraph is added to the result.</p><p>Otherwise, the search resets — ensuring only the most relevant, continuous sequence of paragraphs is returned.</p><p>With this approach, clause detection becomes <strong>robust to formatting</strong>, <strong>punctuation</strong>, and <strong>minor edits</strong>, making it far more reliable than a plain text search.</p><h3>Limitations and next steps</h3><p>The main drawback of this approach is that it requires <strong>loading the entire document into memory</strong>.</p><pre>Word.run(async (context) =&gt; {<br> const paragraphs = context.document.body.paragraphs<br> paragraphs.load(&#39;items&#39;);<br> await context.sync();<br> <br> const paragraphTexts = paragraphs.items.map(i =&gt; i.text);<br> const resultParahraphTexts = findParagraphs(paragraphTexts, &#39;my clause...&#39;);<br> <br> const resultRange = computeRangeFromParagraphs(paragraphs.items, resultParahraphTexts);<br> <br> resultRange.select();<br>});</pre><p>For small contracts, this isn’t an issue — but for large documents with hundreds of clauses, it can become memory-intensive and slightly impact performance.</p><p>At Doctrine, we follow the <a href="https://medium.com/doctrine/doctrine-engineering-principles-and-practices-7faab666fac9"><strong>RERO</strong></a> principle — <em>Release Early, Release Often</em>.</p><p><strong>Our goal was to validate the approach quickly, ensure it worked reliably in real-world cases, and iterate based on feedback.</strong></p><p>This first version does exactly that: it provides a robust, practical foundation for clause matching in order to validate feature value while leaving room for future optimization.</p><p>Now that we can reliably find and select the exact range of a clause through the Word add-in API, we finally have all the pieces needed to integrate AI-generated reformulations directly into the document. This opens the door to a smoother review workflow: <strong>users can jump from a detected risk to the corresponding clause, inspect the proposed rewrite, and apply it with a single action — all without leaving Word</strong>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=420b5216ccaa" width="1" height="1" alt=""><hr><p><a href="https://medium.com/doctrine/how-to-keep-track-of-clause-in-word-add-in-to-integrate-ai-generated-reformulation-420b5216ccaa">How to keep track of clause in Word add-in to integrate AI-generated reformulation</a> was originally published in <a href="https://medium.com/doctrine">Inside Doctrine</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>