3535 * "1,000+ integrations" or a figure left from an older catalog spreads to third-party sites.
3636 * A count belongs to Sim when "Sim" comes before it in the same clause and no other product's
3737 * possessive directly precedes it, or when it sits in a table row whose label cell is Sim.
38- * Sentence ends and contrasts ("while", "unlike", "compared with") start a new clause.
38+ * A comparison ("while", "unlike", "compared with", or "like" after a comma) is its own clause
39+ * up to the next comma, and the sentence's main clause resumes after it.
3940 * A count whose owner is ambiguous within its clause is treated as Sim's; when it belongs to
4041 * another product, name that product as its owner ("Zapier's 9,000+") or give it a sentence.
4142 * Blog and changelog posts are dated, so their counts are true as of publication.
@@ -148,26 +149,20 @@ const LINE_PREFIX = /^\s*(?:(?:[-*+>]|\d+[.)]|#{1,6})\s+)*(?:\w+:\s*["'])?/
148149const SIM_NAME = / \b S i m \b /
149150/** Words that turn a sentence toward another product: "while Zapier lists ...", "unlike Zapier". */
150151const CONTRAST = String . raw `(?:while|whereas|but|unlike|compared (?:with|to)|versus|vs\.?)`
152+ const SENTENCE_END = / (?< = [ . ! ? ; ] ) \s + /
151153/**
152- * A comparison set off by commas, as in "Sim, unlike Zapier, has ..." or "Zapier, like Sim, has
153- * ...", removed so the sentence keeps its subject. The span stays inside its sentence, ends at the
154- * first comma that is not a thousands separator, and is kept when a relative clause follows
155- * ("unlike Zapier, which has").
154+ * Where a comparison clause starts: a contrast word, or "like" when it opens the sentence or
155+ * follows a comma ("Like Sim, ...", "Zapier, like Sim, ..."), but not "tools like Slack".
156156 */
157- const PARENTHETICAL_COMPARISON = new RegExp (
158- String . raw `,\s+(?:like| ${ CONTRAST } )\s+ (?:[^,.!?;]|[,.](?=\d))*?,(?!\d )(?!\s*(?:which|that|who) \b)` ,
159- 'gi '
157+ const COMPARISON_START = new RegExp (
158+ String . raw `(?:^| ,\s*|\s+)(?= ${ CONTRAST } \b)| (?:^|,\s* )(?=like \b)` ,
159+ 'i '
160160)
161- /** A sentence end, or a contrast that hands the rest of the sentence to another subject. */
162- const CLAUSE_BREAK = new RegExp ( String . raw `(?<=[.!?;])\s+|,?\s+${ CONTRAST } \s+` , 'i' )
163161/**
164- * An opening comparison ("Unlike Sim, ...") whose main clause is about another product. An opener
165- * that states a count of its own ("While Sim has 280 + integrations, ...") is kept .
162+ * Where a comparison clause ends: the first comma that is neither a thousands separator nor the
163+ * start of a relative clause continuing it ("unlike Zapier, which has 9,000 + integrations") .
166164 */
167- const LEADING_COMPARISON = new RegExp (
168- String . raw `^(?:like|${ CONTRAST } )\b(?:[^,.!?;]|[,.](?=\d))*?,(?!\d)\s*` ,
169- 'i'
170- )
165+ const COMPARISON_END = / , (? ! \d ) (? ! \s * (?: w h i c h | t h a t | w h o ) \b ) /
171166/** The product whose possessive directly precedes a count, as in "Zapier's 9,000+". */
172167const POSSESSIVE_OWNER = / ( [ \w . - ] + ) [ ' ’ ] s \s * $ /
173168/** A Sim count below this share of the catalog total is left from an older catalog. */
@@ -181,24 +176,34 @@ function simTableRowCounts(cells: string[]): RegExpMatchArray[] {
181176 : [ ]
182177}
183178
179+ /**
180+ * A sentence's clauses: its main clause, with each comparison taken out and read as a clause of
181+ * its own, so "Sim, unlike Zapier, has ..." reads as "Sim has ..." plus "unlike Zapier".
182+ */
183+ function sentenceClauses ( sentence : string ) : string [ ] {
184+ const main : string [ ] = [ ]
185+ const comparisons : string [ ] = [ ]
186+ let rest = sentence
187+ for ( let start = COMPARISON_START . exec ( rest ) ; start ; start = COMPARISON_START . exec ( rest ) ) {
188+ main . push ( rest . slice ( 0 , start . index ) )
189+ const comparison = rest . slice ( start . index + start [ 0 ] . length )
190+ const end = COMPARISON_END . exec ( comparison )
191+ comparisons . push ( end ? comparison . slice ( 0 , end . index ) : comparison )
192+ rest = end ? comparison . slice ( end . index + 1 ) : ''
193+ }
194+ return [ [ ...main , rest ] . join ( ' ' ) , ...comparisons ]
195+ }
196+
184197/** Each count whose clause names Sim before it, unless another product's possessive owns it. */
185198function simProseCounts ( text : string ) : RegExpMatchArray [ ] {
186- const sentences = text
187- . replace ( LINE_PREFIX , '' )
188- . replace ( PARENTHETICAL_COMPARISON , '' )
189- . split ( CLAUSE_BREAK )
190- return sentences . flatMap ( ( sentence ) => {
191- const opener = LEADING_COMPARISON . exec ( sentence ) ?. [ 0 ]
192- const clause =
193- opener !== undefined && opener . match ( COUNT_CLAIM ) === null
194- ? sentence . slice ( opener . length )
195- : sentence
196- return [ ...clause . matchAll ( COUNT_CLAIM ) ] . filter ( ( match ) => {
199+ const clauses = text . replace ( LINE_PREFIX , '' ) . split ( SENTENCE_END ) . flatMap ( sentenceClauses )
200+ return clauses . flatMap ( ( clause ) =>
201+ [ ...clause . matchAll ( COUNT_CLAIM ) ] . filter ( ( match ) => {
197202 const before = clause . slice ( 0 , match . index )
198203 const owner = POSSESSIVE_OWNER . exec ( before ) ?. [ 1 ]
199204 return SIM_NAME . test ( before ) && ( owner === undefined || owner === 'Sim' )
200205 } )
201- } )
206+ )
202207}
203208
204209/** The counts on one line of text that belong to Sim, with the claim as written. */
0 commit comments