Repository Public
1// ORIX Docs2// File: Packages/ORIXDocsKit/Tests/DocAuditTests/ResearchAssistantTests.swift3// Description: Tests of the research assistant: search, acronyms in English and French, cited passages, captions, outline, action items.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import Foundation8import Testing9import DocModel10import DocAudit1112private typealias Search = ResearchAssistant.SearchOptions1314@Suite struct SearchTests {15 private func sample() -> DocumentModel {16 var noteRef = Paragraph("Son résumé est joint")17 noteRef.runs.append(Run(inline: .note(NoteRef(kind: .footnote, id: "n1"))))18 noteRef.runs.append(Run(". The art of the start in 2024 and 1987."))19 var doc = makeDocument([heading(1, "Profile"), heading(2, "Career"), .paragraph(noteRef), para("RESUME writing is an Art."),20 .table(Table(strings: [["Item", "Note"], ["Resumé", "sent"]], width: 300)), heading(1, "Annex"), para("Nothing to see, 🧑🏽🚀 résumé.")])21 doc.notes["n1"] = Note(id: "n1", kind: .footnote, paragraphs: [Paragraph("See the resume attached.")])22 doc.pageSetup.header.default = [Paragraph("Résumé — draft", style: StyleID.header)]23 return doc24 }2526 private func matched(_ hit: ResearchAssistant.Hit, in doc: DocumentModel) -> String {27 doc.blocks(in: hit.story).paragraph(at: hit.path)!.plainText.utf16Substring(hit.range)28 }2930 @Test func caseAndDiacriticInsensitiveByDefault() {31 let doc = sample()32 let hits = ResearchAssistant.search("resume", in: doc, documentName: "cv")33 #expect(hits.count == 6)34 #expect(hits.map { matched($0, in: doc) } == ["résumé", "RESUME", "Resumé", "résumé", "Résumé", "resume"])35 #expect(hits[0].story == .body && hits[0].path == ParaPath(block: 2) && hits[0].range == 4..<10)36 #expect(hits[2].path == ParaPath(block: 4, cell: CellPath(row: 1, cell: 0)))37 #expect(hits[4].story == .header(section: 0, variant: .default) && hits[5].story == .note("n1"))38 #expect(hits[0].headingTrail == ["Profile", "Career"] && hits[3].headingTrail == ["Annex"] && hits[5].headingTrail.isEmpty)39 #expect(hits[0].context.contains("Son résumé est joint") && hits.allSatisfy { $0.documentName == "cv" })40 #expect(Set(hits.map(\.id)).count == hits.count)41 // The match after an emoji sequence is still correctly located (UTF-16 offsets).42 #expect(matched(hits[3], in: doc) == "résumé")43 }4445 @Test func sensitiveOptions() {46 let doc = sample()47 #expect(ResearchAssistant.search("resume", in: doc, documentName: "cv", options: Search(diacriticSensitive: true)).map { matched($0, in: doc) } == ["RESUME", "resume"])48 #expect(ResearchAssistant.search("resume", in: doc, documentName: "cv", options: Search(caseSensitive: true, diacriticSensitive: true)).count == 1)49 #expect(ResearchAssistant.search("résumé", in: doc, documentName: "cv", options: Search(caseSensitive: true, diacriticSensitive: true)).count == 2)50 }5152 @Test func wholeWord() {53 let doc = sample()54 #expect(ResearchAssistant.search("art", in: doc, documentName: "cv").count == 3) // art, start, Art55 let whole = ResearchAssistant.search("art", in: doc, documentName: "cv", options: Search(wholeWord: true))56 #expect(whole.map { matched($0, in: doc) } == ["art", "Art"])57 }5859 @Test func regularExpressions() {60 let doc = sample()61 let years = ResearchAssistant.search("\\b(19|20)\\d{2}\\b", in: doc, documentName: "cv", options: Search(regex: true))62 #expect(years.map { matched($0, in: doc) } == ["2024", "1987"])63 // Case- and diacritic-insensitive regex, with whole-word wrapping.64 let r = ResearchAssistant.search("r[eé]sum[eé]s?", in: doc, documentName: "cv", options: Search(wholeWord: true, regex: true))65 #expect(r.count == 6)66 #expect(ResearchAssistant.search("resum.", in: doc, documentName: "cv", options: Search(regex: true)).count == 6)67 #expect(ResearchAssistant.search("resum.", in: doc, documentName: "cv", options: Search(caseSensitive: true, diacriticSensitive: true, regex: true)).count == 1)68 // An invalid pattern and an empty query give nothing, they do not crash.69 #expect(ResearchAssistant.search("(unclosed", in: doc, documentName: "cv", options: Search(regex: true)).isEmpty)70 #expect(ResearchAssistant.search("", in: doc, documentName: "cv").isEmpty)71 }7273 @Test func scopeOptionsAndLimit() {74 let doc = sample()75 #expect(ResearchAssistant.search("resume", in: doc, documentName: "cv", options: Search(includeTables: false)).count == 5)76 #expect(ResearchAssistant.search("resume", in: doc, documentName: "cv", options: Search(includeNotes: false, includeHeadersAndFooters: false)).count == 4)77 #expect(ResearchAssistant.search("resume", in: doc, documentName: "cv", options: Search(maxHits: 2)).count == 2)78 }7980 @Test func deletedTextIsNotSearched() {81 var p = Paragraph("Visible alpha")82 p.runs.append(Run(" hidden alpha", revision: Revision(kind: .deletion, author: "X")))83 p.runs.append(Run(" final alpha"))84 let doc = makeDocument([.paragraph(p)])85 let hits = ResearchAssistant.search("alpha", in: doc, documentName: "d")86 #expect(hits.map(\.range) == [8..<13, 33..<38])87 }8889 @Test func acrossDocuments() {90 let a = makeDocument([para("The yield curve steepened.")]), b = makeDocument([para("Nothing relevant.")]), c = makeDocument([para("Yield and more yield.")])91 let hits = ResearchAssistant.search("yield", across: [("a.orixdoc", a), ("b.orixdoc", b), ("c.orixdoc", c)], options: Search())92 #expect(hits.map(\.documentName) == ["a.orixdoc", "c.orixdoc", "c.orixdoc"])93 #expect(ResearchAssistant.search("yield", across: [("a.orixdoc", a), ("c.orixdoc", c)], options: Search(maxHits: 2)).count == 2)94 #expect(ResearchAssistant.search("yield", across: [], options: Search()).isEmpty)95 }96}9798@Suite struct AcronymTests {99 private func acronyms(_ paragraphs: [String], language: String = "en") -> [String] {100 ResearchAssistant.undefinedAcronyms(in: makeDocument(paragraphs.map { para($0) }, language: language)).map(\.acronym)101 }102103 @Test func englishDefinitions() {104 #expect(acronyms(["The GDP rose."]) == ["GDP"])105 #expect(acronyms(["Gross domestic product (GDP) rose.", "Later, GDP fell."]).isEmpty)106 #expect(acronyms(["GDP fell early.", "The gross domestic product (GDP) then rose."]).isEmpty) // defined after first use: still defined107 #expect(acronyms(["GDP (gross domestic product) rose."]).isEmpty)108 #expect(acronyms(["GDP stands for gross domestic product."]).isEmpty)109 #expect(acronyms(["We use deoxyribonucleic acid (the DNAX marker) here."]) == ["DNAX"])110 #expect(acronyms(["The European Central Bank (ECB) and the ECBs of the world."]).isEmpty)111 #expect(acronyms(["Carbon dioxide (CO2) emissions and CO2 taxes."]).isEmpty)112 #expect(acronyms(["The CO2 tax."]) == ["CO2"])113 // A parenthesis that is not a definition does not count.114 #expect(acronyms(["Output rose (GDP) sharply.", "GDP (see Table 2) rose."]) == ["GDP"])115 }116117 @Test func frenchDefinitions() {118 #expect(acronyms(["Le PIB a augmenté de deux pour cent."], language: "fr") == ["PIB"])119 #expect(acronyms(["Le produit intérieur brut (PIB) a augmenté.", "Le PIB réel aussi."], language: "fr").isEmpty)120 #expect(acronyms(["Le PIB (produit intérieur brut) a augmenté."], language: "fr").isEmpty)121 #expect(acronyms(["L'Organisation des Nations unies (ONUDI) siège ici."], language: "fr") == ["ONUDI"])122 #expect(acronyms(["L'École de technologie supérieure (ÉTS) forme des ingénieurs. L'ÉTS est à Montréal."], language: "fr").isEmpty)123 #expect(acronyms(["Le sigle REER signifie régime enregistré d'épargne-retraite."], language: "fr").isEmpty)124 #expect(acronyms(["La Société de transport de Montréal (ci-après « STM ») exploite le métro. La STM aussi."], language: "fr").isEmpty)125 #expect(acronyms(["La STM exploite le métro et le REM circule."], language: "fr") == ["STM", "REM"])126 }127128 @Test func thingsThatAreNotAcronyms() {129 #expect(acronyms(["Chapter IV follows chapter III and volume XII.", "The USA and the UK signed in 2020 at 5 PM.", "Send the PDF, OK?"]).isEmpty)130 #expect(acronyms(["ANNUAL REPORT OF THE BOARD OF DIRECTORS", "A normal sentence follows."]).isEmpty)131 #expect(acronyms(["Mr Smith met Dr Jones in Québec. NASA agreed. iPhone and macOS are products. A B C."]).isEmpty)132 }133134 @Test func evidenceIsTheFirstUse() {135 let doc = makeDocument([heading(1, "Macro"), para("Intro text."), para("Here the CPI is discussed, and the CPI again."), para("CPI once more.")])136 let found = ResearchAssistant.undefinedAcronyms(in: doc)137 #expect(found.count == 1)138 #expect(found[0].acronym == "CPI" && found[0].count == 3)139 #expect(found[0].firstUse.path == ParaPath(block: 2) && found[0].firstUse.range == 9..<12 && found[0].firstUse.headingTrail == ["Macro"])140 #expect(found[0].firstUse.context.contains("Here the CPI is discussed"))141 #expect(ResearchAssistant.undefinedAcronyms(in: doc, known: ["CPI"]).isEmpty)142 }143}144145@Suite struct ResearchToolTests {146 @Test func citedPassages() {147 var p = Paragraph("Volatility clusters over time ")148 p.runs.append(Run(inline: .citation(Citation("engle1982", "bollerslev1986"))))149 p.runs.append(Run(". This paragraph also has an uncited sentence. Leverage effects were documented early."))150 p.runs.append(Run(" "))151 p.runs.append(Run(inline: .citation(Citation("black1976"))))152 var q = Paragraph()153 q.runs = [Run(inline: .citation(Citation("nelson1991"))), Run(" proposed the exponential model.")]154 let doc = makeDocument([heading(1, "Literature"), .paragraph(p), para("No citation here."), .paragraph(q)])155 let passages = ResearchAssistant.citedPassages(in: doc)156 #expect(passages.count == 3)157 #expect(passages[0].sentence == "Volatility clusters over time [engle1982; bollerslev1986]." && passages[0].keys == ["engle1982", "bollerslev1986"])158 #expect(passages[1].keys == ["black1976"] && passages[1].sentence.hasPrefix("Leverage effects were documented early."))159 #expect(passages[2].sentence == "[nelson1991] proposed the exponential model." && passages[2].path == ParaPath(block: 3))160 #expect(passages[0].headingTrail == ["Literature"])161 // The range designates the sentence in the paragraph.162 #expect(p.plainText.utf16Substring(passages[0].range).hasPrefix("Volatility clusters"))163 #expect(ResearchAssistant.citedPassages(in: makeDocument([para("Nothing cited.")])).isEmpty)164 }165166 @Test func unreferencedCaptions() {167 let f1 = captionParagraph("Figure", "First"), f2 = captionParagraph("Figure", "Second"), t1 = captionParagraph("Table", "Numbers")168 var ref = Paragraph("See ")169 ref.runs.append(Run(inline: .crossRef(CrossRef(target: f2.id))))170 let doc = makeDocument([.paragraph(f1), .paragraph(f2), .paragraph(t1), .paragraph(ref)])171 let found = ResearchAssistant.unreferencedCaptions(in: doc)172 #expect(found.map(\.paragraphID) == [f1.id, t1.id])173 #expect(found[0].label == "Figure" && found[0].number == 1 && found[0].text == "Figure 1: First" && found[1].text == "Table 1: Numbers")174 }175176 @Test func outline() {177 let doc = makeDocument([heading(1, "One"), para("x"), heading(2, "One point one"), heading(1, "Two")])178 let o = ResearchAssistant.outline(of: doc)179 #expect(o.map(\.text) == ["One", "One point one", "Two"] && o.map(\.level) == [1, 2, 1] && o.map(\.blockIndex) == [0, 2, 3])180 }181182 @Test func actionItemsAreRuleBased() {183 let doc = makeDocument([heading(1, "Minutes"), para("The meeting started at nine."), para("TODO: update the forecast table."), para("Section à faire avant vendredi."),184 para("The supplier must deliver the parts by June."), para("Action: Marie sends the contract."), para("[ ] Book the room"), para("☐ Réserver la salle"),185 para("[x] Already done"), para("Le client doit confirmer la commande."), para("Prochaines étapes : valider le budget."), para("They discussed the mustard supplier.")])186 let hits = ResearchAssistant.actionItems(in: doc)187 #expect(hits.map(\.path.block) == [2, 3, 4, 5, 6, 7, 9, 10])188 #expect(hits[0].range == 0..<4 && hits[0].headingTrail == ["Minutes"] && hits[0].context.contains("update the forecast table"))189 #expect(doc.body[4].paragraph!.plainText.utf16Substring(hits[2].range) == "must")190 }191192 @Test func repeatedWordingDirect() {193 let text = "The committee approved the budget. The committee then reviewed the plan. Later the committee met again, and the committee voted. " +194 "Finally the committee adjourned while the committee chair spoke."195 let doc = makeDocument([heading(1, "Committee committee committee"), para(text)])196 let found = ResearchAssistant.repeatedWording(in: doc, language: "en")197 let word = found.first { $0.kind == .word }198 #expect(word?.text == "committee" && word?.count == 6 && word?.occurrences.count == 6)199 #expect(word?.occurrences.allSatisfy { $0.path == ParaPath(block: 1) } == true)200 #expect(text.utf16Substring(word!.occurrences[0].range) == "committee")201 // Stop words never count, however frequent.202 #expect(!found.contains { $0.text == "the" })203 var strict = ResearchAssistant.RepetitionOptions()204 strict.minCount = 7205 #expect(!ResearchAssistant.repeatedWording(in: doc, language: "en", options: strict).contains { $0.kind == .word })206 // French stop words.207 let fr = makeDocument([para("Nous avons vu que les taux montent, que les prix montent et que les salaires suivent, parce que les marchés anticipent que les banques agiront.")], language: "fr")208 #expect(!ResearchAssistant.repeatedWording(in: fr, language: nil).contains { $0.kind == .word })209 }210211 /// Depends on the lemma models that ship with macOS (present for English and French).212 @Test func inflectedFormsCountTogether() {213 let en = makeDocument([para("We estimate the first model. The second model was estimated twice. Each estimate differs. She estimates it again, " +214 "estimating slowly, and the estimates agree.")])215 var o = ResearchAssistant.RepetitionOptions()216 o.minCount = 5217 let word = ResearchAssistant.repeatedWording(in: en, language: "en", options: o).first { $0.kind == .word }218 #expect(word?.text == "estimate" && word?.count == 6)219 let fr = makeDocument([para("Les marchés montent. Un marché monte quand ce marché anticipe. Ces marchés-là, tout marché le sait, restent des marchés.")], language: "fr-CA")220 let mot = ResearchAssistant.repeatedWording(in: fr, language: nil, options: o).first { $0.kind == .word }221 #expect(mot?.text == "marché" && mot?.count == 6)222 }223}224