#106 - Basic, English-only OCR

This commit is contained in:
Simon Cambier
2022-11-06 17:04:46 +01:00
parent 40f9df6a47
commit f0b2de4316
8 changed files with 95 additions and 48 deletions

View File

@@ -124,15 +124,12 @@ See [styles.css](./assets/styles.css) for more information.
**Omnisearch makes Obsidian sluggish at startup.** **Omnisearch makes Obsidian sluggish at startup.**
- You may have _big_ documents. Huge notes (like novels) can freeze the interface for a short time when being indexed. While Omnisearch uses a cache between sessions, it's still rebuilt at startup to keep it up-to-date. - While Omnisearch does its best to work smoothly in the background, bigger vaults can cause some hiccups at startup because of the search index size.
**I have thousands of notes, and at startup I have to wait a few seconds before Omnisearch gives me the context of a result.** **Omnisearch gives inconsistent/invalid results, there are errors in the developer console**
- Omnisearch refreshes its index at startup. During this time, you can still find notes, but Omnisearch is not able to show you the excerpts. - Restart Obsidian to force a reindex of Omnisearch.
- The cache can be corrupted; you can clear it at the bottom of the settings page, then restart Obsidian.
**Omnisearch gives inconsistent/invalid results, or there are errors in the developer console.**
- Restart Obsidian to force a reindex of Omnisearch
**A query should return a result that does not appear.** **A query should return a result that does not appear.**
@@ -153,6 +150,8 @@ You can write your issue [here](https://github.com/scambier/obsidian-omnisearch/
Omnisearch is licensed under [GPL-3](https://tldrlegal.com/license/gnu-general-public-license-v3-(gpl-3)). Omnisearch is licensed under [GPL-3](https://tldrlegal.com/license/gnu-general-public-license-v3-(gpl-3)).
## Sponsors ## Thanks
To all people who donate through [Ko-Fi](https://ko-fi.com/scambier) or [Github Sponsors](https://github.com/sponsors/scambier) ❤
![JetBrains Logo (Main) logo](https://resources.jetbrains.com/storage/products/company/brand/logos/jb_beam.svg) ![JetBrains Logo (Main) logo](https://resources.jetbrains.com/storage/products/company/brand/logos/jb_beam.svg)

View File

@@ -44,7 +44,7 @@
"dexie": "^3.2.2", "dexie": "^3.2.2",
"lodash-es": "4.17.21", "lodash-es": "4.17.21",
"minisearch": "github:scambier/minisearch#callback_desync", "minisearch": "github:scambier/minisearch#callback_desync",
"obsidian-text-extract": "1.0.1", "obsidian-text-extract": "1.0.2",
"pure-md5": "^0.1.14" "pure-md5": "^0.1.14"
}, },
"pnpm": { "pnpm": {

8
pnpm-lock.yaml generated
View File

@@ -23,7 +23,7 @@ specifiers:
lodash-es: 4.17.21 lodash-es: 4.17.21
minisearch: github:scambier/minisearch#callback_desync minisearch: github:scambier/minisearch#callback_desync
obsidian: latest obsidian: latest
obsidian-text-extract: 1.0.1 obsidian-text-extract: 1.0.2
prettier: ^2.7.1 prettier: ^2.7.1
prettier-plugin-svelte: ^2.8.0 prettier-plugin-svelte: ^2.8.0
pure-md5: ^0.1.14 pure-md5: ^0.1.14
@@ -40,7 +40,7 @@ dependencies:
dexie: 3.2.2 dexie: 3.2.2
lodash-es: 4.17.21 lodash-es: 4.17.21
minisearch: github.com/scambier/minisearch/adf11cab46d851220a41c9ad95ed986b630f0f3c minisearch: github.com/scambier/minisearch/adf11cab46d851220a41c9ad95ed986b630f0f3c
obsidian-text-extract: 1.0.1 obsidian-text-extract: 1.0.2
pure-md5: 0.1.14 pure-md5: 0.1.14
devDependencies: devDependencies:
@@ -4326,8 +4326,8 @@ packages:
object-keys: 1.1.1 object-keys: 1.1.1
dev: true dev: true
/obsidian-text-extract/1.0.1: /obsidian-text-extract/1.0.2:
resolution: {integrity: sha512-IJlxbZi/WxzWKwnyruFP/7KUTed6MZW+74OGI5ovZVfikkWx7lfBe1rn23La2kq/BrwETIAugO5Ke4ppvp53gA==} resolution: {integrity: sha512-OOnV1B0kTED46vxPRLOHdHeev6CqcXs6A39DE3IJaEV9PNZKPF3/f6d3t7/zFLgOVMNiVD9Uj+YziAhXeKt4lw==}
dependencies: dependencies:
dexie: 3.2.2 dexie: 3.2.2
p-limit: 4.0.0 p-limit: 4.0.0

View File

@@ -18,7 +18,6 @@
$: { $: {
imagePath = null imagePath = null
if (isFileImage(note.path)) { if (isFileImage(note.path)) {
console.log(note.path)
// @ts-ignore // @ts-ignore
const file = app.vault.getFiles().find(f => f.path === note.path) const file = app.vault.getFiles().find(f => f.path === note.path)
if (file) { if (file) {

View File

@@ -3,6 +3,7 @@ import {
extractHeadingsFromCache, extractHeadingsFromCache,
getAliasesFromMetadata, getAliasesFromMetadata,
getTagsFromMetadata, getTagsFromMetadata,
isFileImage,
isFilePlaintext, isFilePlaintext,
removeDiacritics, removeDiacritics,
} from './tools/utils' } from './tools/utils'
@@ -10,7 +11,7 @@ import * as NotesIndex from './notes-index'
import type { TFile } from 'obsidian' import type { TFile } from 'obsidian'
import type { IndexedDocument } from './globals' import type { IndexedDocument } from './globals'
import { getNonExistingNotes } from './tools/notes' import { getNonExistingNotes } from './tools/notes'
import { getPdfText } from 'obsidian-text-extract' import { getPdfText, getImageText } from 'obsidian-text-extract'
/** /**
* Return all plaintext files as IndexedDocuments * Return all plaintext files as IndexedDocuments
@@ -27,20 +28,33 @@ export async function getPlainTextFiles(): Promise<IndexedDocument[]> {
} }
/** /**
* Return all PDF files as IndexedDocuments. * Return all PDFs as IndexedDocuments.
* If a PDF isn't cached, it will be read from the disk and added to the IndexedDB
*/ */
export async function getPDFFiles(): Promise<IndexedDocument[]> { export async function getPDFAsDocuments(): Promise<IndexedDocument[]> {
const files = app.vault.getFiles().filter(f => f.path.endsWith('.pdf')) const files = app.vault.getFiles().filter(f => f.path.endsWith('.pdf'))
return await getBinaryFiles(files)
}
/**
* Return all imageas as IndexedDocuments.
*/
export async function getImagesAsDocuments(): Promise<IndexedDocument[]> {
const files = app.vault.getFiles().filter(f => isFileImage(f.path))
return await getBinaryFiles(files)
}
async function getBinaryFiles(files: TFile[]): Promise<IndexedDocument[]> {
const data: IndexedDocument[] = [] const data: IndexedDocument[] = []
const input = [] const input = []
for (const file of files) { for (const file of files) {
input.push(new Promise(async (resolve, reject) => { input.push(
new Promise(async (resolve, reject) => {
const doc = await fileToIndexedDocument(file) const doc = await fileToIndexedDocument(file)
await cacheManager.updateLiveDocument(file.path, doc) await cacheManager.updateLiveDocument(file.path, doc)
data.push(doc) data.push(doc)
return resolve(null) return resolve(null)
})) })
)
} }
await Promise.all(input) await Promise.all(input)
return data return data
@@ -51,13 +65,15 @@ export async function getPDFFiles(): Promise<IndexedDocument[]> {
* Will use the cache if possible. * Will use the cache if possible.
*/ */
export async function fileToIndexedDocument( export async function fileToIndexedDocument(
file: TFile, file: TFile
): Promise<IndexedDocument> { ): Promise<IndexedDocument> {
let content: string let content: string
if (isFilePlaintext(file.path)) { if (isFilePlaintext(file.path)) {
content = await app.vault.cachedRead(file) content = await app.vault.cachedRead(file)
} else if (file.path.endsWith('.pdf')) { } else if (file.path.endsWith('.pdf')) {
content = await getPdfText(file) content = await getPdfText(file)
} else if (isFileImage(file.path)) {
content = await getImageText(file)
} else { } else {
throw new Error('Invalid file: ' + file.path) throw new Error('Invalid file: ' + file.path)
} }

View File

@@ -84,9 +84,7 @@ export default class OmnisearchPlugin extends Plugin {
showWelcomeNotice(this) showWelcomeNotice(this)
} }
onunload(): void { onunload(): void {}
}
addRibbonButton(): void { addRibbonButton(): void {
this.ribbonButton = this.addRibbonIcon('search', 'Omnisearch', _evt => { this.ribbonButton = this.addRibbonIcon('search', 'Omnisearch', _evt => {
@@ -110,13 +108,13 @@ async function populateIndex(): Promise<void> {
// Initialize minisearch // Initialize minisearch
let engine = SearchEngine.getEngine() let engine = SearchEngine.getEngine()
// No cache for iOS // if not iOS, load data from cache
if (!Platform.isIosApp) { if (!Platform.isIosApp) {
engine = await SearchEngine.initFromCache() engine = await SearchEngine.initFromCache()
} }
// Load plaintext files // Load plaintext files
console.log('Omnisearch - Fetching notes') console.log('Omnisearch - Reading notes')
const plainTextFiles = await FileLoader.getPlainTextFiles() const plainTextFiles = await FileLoader.getPlainTextFiles()
let allFiles = [...plainTextFiles] let allFiles = [...plainTextFiles]
// iOS: since there's no cache, directly index the documents // iOS: since there's no cache, directly index the documents
@@ -127,15 +125,28 @@ async function populateIndex(): Promise<void> {
// Load PDFs // Load PDFs
if (settings.PDFIndexing) { if (settings.PDFIndexing) {
console.log('Omnisearch - Fetching PDFs') console.log('Omnisearch - Reading PDFs')
const pdfs = await FileLoader.getPDFFiles() const pdfDocuments = await FileLoader.getPDFAsDocuments()
// iOS: since there's no cache, just index the documents // iOS: since there's no cache, just index the documents
if (Platform.isIosApp) { if (Platform.isIosApp) {
await wait(1000) await wait(1000)
await engine.addAllToMinisearch(pdfs) await engine.addAllToMinisearch(pdfDocuments)
} }
// Add PDFs to the files list // Add PDFs to the files list
allFiles = [...allFiles, ...pdfs] allFiles = [...allFiles, ...pdfDocuments]
}
// Load Images
if (settings.imagesIndexing) {
console.log('Omnisearch - Reading Images')
const imagesDocuments = await FileLoader.getImagesAsDocuments()
// iOS: since there's no cache, just index the documents
if (Platform.isIosApp) {
await wait(1000)
await engine.addAllToMinisearch(imagesDocuments)
}
// Add Images to the files list
allFiles = [...allFiles, ...imagesDocuments]
} }
console.log('Omnisearch - Total number of files: ' + allFiles.length) console.log('Omnisearch - Total number of files: ' + allFiles.length)
@@ -146,6 +157,7 @@ async function populateIndex(): Promise<void> {
console.log('Omnisearch - Checking index cache diff...') console.log('Omnisearch - Checking index cache diff...')
// Check which documents need to be removed/added/updated // Check which documents need to be removed/added/updated
const diffDocs = await cacheManager.getDiffDocuments(allFiles) const diffDocs = await cacheManager.getDiffDocuments(allFiles)
console.log(`Omnisearch - Files to add/remove/update: ${diffDocs.toAdd.length}/${diffDocs.toDelete.length}/${diffDocs.toUpdate.length}`)
needToUpdateCache = !!( needToUpdateCache = !!(
diffDocs.toAdd.length || diffDocs.toAdd.length ||
diffDocs.toDelete.length || diffDocs.toDelete.length ||
@@ -154,20 +166,16 @@ async function populateIndex(): Promise<void> {
// Add // Add
await engine.addAllToMinisearch(diffDocs.toAdd) await engine.addAllToMinisearch(diffDocs.toAdd)
console.log(`Omnisearch - ${diffDocs.toAdd.length} files to add`)
diffDocs.toAdd.forEach(doc => diffDocs.toAdd.forEach(doc =>
cacheManager.updateLiveDocument(doc.path, doc) cacheManager.updateLiveDocument(doc.path, doc)
) )
// Delete // Delete
console.log(`Omnisearch - ${diffDocs.toDelete.length} files to remove`)
diffDocs.toDelete.forEach(d => engine.removeFromMinisearch(d)) diffDocs.toDelete.forEach(d => engine.removeFromMinisearch(d))
diffDocs.toDelete.forEach(doc => cacheManager.deleteLiveDocument(doc.path)) diffDocs.toDelete.forEach(doc => cacheManager.deleteLiveDocument(doc.path))
// Update (delete + add) // Update (delete + add)
console.log(`Omnisearch - ${diffDocs.toUpdate.length} files to update`) diffDocs.toUpdate.forEach(({ oldDoc, newDoc }) => {
diffDocs.toUpdate
.forEach(({ oldDoc, newDoc }) => {
engine.removeFromMinisearch(oldDoc) engine.removeFromMinisearch(oldDoc)
cacheManager.updateLiveDocument(oldDoc.path, newDoc) cacheManager.updateLiveDocument(oldDoc.path, newDoc)
}) })
@@ -205,13 +213,13 @@ async function cleanOldCacheFiles() {
} }
function showWelcomeNotice(plugin: Plugin) { function showWelcomeNotice(plugin: Plugin) {
const code = '1.7.6' const code = '1.8.0-beta.3'
if (settings.welcomeMessage !== code) { if (settings.welcomeMessage !== code) {
const welcome = new DocumentFragment() const welcome = new DocumentFragment()
welcome.createSpan({}, span => { welcome.createSpan({}, span => {
span.innerHTML = `<strong>Omnisearch has been updated</strong> span.innerHTML = `<strong>Omnisearch BETA has been updated</strong>
New beta feature: PDF search 🔎📄 You can now enable "Images Indexing" to use Optical Character Recognition on your scanned documents
<small>Toggle "<i>BETA - Index PDFs</i>" in Omnisearch settings page.</small>` 🔎🖼`
}) })
new Notice(welcome, 30000) new Notice(welcome, 30000)
} }

View File

@@ -25,6 +25,8 @@ export interface OmnisearchSettings extends WeightingSettings {
indexedFileTypes: string[] indexedFileTypes: string[]
/** Enable PDF indexing */ /** Enable PDF indexing */
PDFIndexing: boolean PDFIndexing: boolean
/** Enable PDF indexing */
imagesIndexing: boolean
/** Display Omnisearch popup notices over Obsidian */ /** Display Omnisearch popup notices over Obsidian */
showIndexingNotices: boolean showIndexingNotices: boolean
/** Activate the small 🔍 button on Obsidian's ribbon */ /** Activate the small 🔍 button on Obsidian's ribbon */
@@ -147,7 +149,7 @@ export class SettingsTab extends PluginSettingTab {
indexPDFsDesc.createSpan({}, span => { indexPDFsDesc.createSpan({}, span => {
span.innerHTML = `Omnisearch will include PDFs in search results. span.innerHTML = `Omnisearch will include PDFs in search results.
<ul> <ul>
<li>⚠️ Depending on their size, PDFs can take anywhere from a few seconds to 2 minutes to be processed.</li> <li>⚠️ Each PDF can take anywhere from a few seconds to 2 minutes to be processed.</li>
<li>⚠️ Texts extracted from PDFs may contain errors such as missing spaces, or spaces in the middle of words.</li> <li>⚠️ Texts extracted from PDFs may contain errors such as missing spaces, or spaces in the middle of words.</li>
<li>⚠️ Some PDFs can't be processed correctly and will return an empty text.</li> <li>⚠️ Some PDFs can't be processed correctly and will return an empty text.</li>
<li>This feature is currently a work-in-progress, please report issues that you might experience.</li> <li>This feature is currently a work-in-progress, please report issues that you might experience.</li>
@@ -164,6 +166,26 @@ export class SettingsTab extends PluginSettingTab {
}) })
) )
// PDF Indexing
const indexImagesDesc = new DocumentFragment()
indexImagesDesc.createSpan({}, span => {
span.innerHTML = `Omnisearch will use <a href="https://en.wikipedia.org/wiki/Tesseract_(software)">Tesseract</a> to index images from their text.
<ul>
<li>Only English is supported at the moment.</li>
<li>Not all images can be correctly read by the OCR, this feature works best with scanned documents.</li>
</ul>
<strong style="color: var(--text-accent)">Needs a restart to fully take effect.</strong>`
})
new Setting(containerEl)
.setName('BETA - Images Indexing')
.setDesc(indexImagesDesc)
.addToggle(toggle =>
toggle.setValue(settings.imagesIndexing).onChange(async v => {
settings.imagesIndexing = v
await saveSettings(this.plugin)
})
)
// #endregion Behavior // #endregion Behavior
// #region User Interface // #region User Interface
@@ -315,6 +337,7 @@ export const DEFAULT_SETTINGS: OmnisearchSettings = {
ignoreDiacritics: true, ignoreDiacritics: true,
indexedFileTypes: [] as string[], indexedFileTypes: [] as string[],
PDFIndexing: false, PDFIndexing: false,
imagesIndexing: false,
showIndexingNotices: false, showIndexingNotices: false,
showShortName: false, showShortName: false,

View File

@@ -184,7 +184,9 @@ export function getCtrlKeyLabel(): 'ctrl' | '⌘' {
export function isFileIndexable(path: string): boolean { export function isFileIndexable(path: string): boolean {
return ( return (
(settings.PDFIndexing && path.endsWith('.pdf')) || isFilePlaintext(path) (settings.PDFIndexing && path.endsWith('.pdf')) ||
isFilePlaintext(path) ||
(settings.imagesIndexing && isFileImage(path))
) )
} }