Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Support for tabular data entry in Bulk Search #9709

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
<details open class="bulk-search-controls">
<summary>Input</summary>
<div>
<p v-if="showColumnHint">Please include a header row. Supported columns include: "Title", "Author".</p>
<textarea v-model="bulkSearchState.inputText"></textarea>
<br />
<label>Format: <select v-model="bulkSearchState._activeExtractorIndex">
<option v-for="extractor, index in bulkSearchState.extractors" :value = "index" :key="index">
{{ extractor["name"] }}
{{ extractor.label }}
</option>
</select></label>
<label v-if="this.showApiKey">OpenAI API Key:
Expand Down Expand Up @@ -74,7 +75,11 @@ export default {
matchBooksText(){
if (this.loadingMatchedBooks) return 'Loading...'
return 'Match Books'
}
},
showColumnHint(){
if (this.bulkSearchState.activeExtractor) return this.bulkSearchState.activeExtractor.name === 'table_extractor'
return false
benbdeitch marked this conversation as resolved.
Show resolved Hide resolved
},
},
methods: {
togglePasswordVisibility(){
Expand Down
72 changes: 62 additions & 10 deletions openlibrary/components/BulkSearch/utils/classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ export class ExtractedBook {
}

class AbstractExtractor {

/**
* @param {string} name
* @param {string} label
*/
constructor(name) {
constructor(label) {
/** @type {string} */
this.name = name
this.label = label
}
/**
* @param {ExtractionOptions} _extractOptions
Expand All @@ -30,13 +31,14 @@ class AbstractExtractor {

export class RegexExtractor extends AbstractExtractor {

name = 'regex_extractor'
/**
*
* @param {string} name
* @param {string} label
* @param {string} pattern
*/
constructor(name, pattern){
super(name)
constructor(label, pattern){
super(label)
/** @type {RegExp} */
this.pattern = new RegExp(pattern, 'gmu');
}
Expand All @@ -56,12 +58,13 @@ export class RegexExtractor extends AbstractExtractor {

export class AiExtractor extends AbstractExtractor{

name = 'ai_extractor'
/**
* @param {string} name
* @param {string} label
* @param {string} model
*/
constructor(name, model) {
super(name)
constructor(label, model) {
super(label)
/** @type {string} */
this.model = model
}
Expand Down Expand Up @@ -122,6 +125,54 @@ export class AiExtractor extends AbstractExtractor{
}
}

export class TableExtractor extends AbstractExtractor{

name = 'table_extractor'
benbdeitch marked this conversation as resolved.
Show resolved Hide resolved
/**
*
* @param {string} label
*/
constructor(label) {
super(label)
/** @type {string} */
this.authorColumn = 'author'
/** @type {string} */
this.titleColumn = 'title'
}

/**
* @param {ExtractionOptions} extractionOptions
* @param {string} text
* @return {Promise<BookMatch[]>}
*/
async run(extractionOptions, text){

/** @type {string[]} */
const lines = text.split('\n')
/** @type {string[][]} */
const cells = lines.map(line => line.split('\t'))
/** @type {{columns: String[], rows: {columnName: string}[]}} */
const tableData = {
columns: cells[0],
rows: []
}
for (let i=1; i< cells.length; i++){
const row = {}
for (let j = 0; j < tableData.columns.length; j++){
row[tableData.columns[j].trim().toLowerCase()] = cells[i][j]
}
// @ts-ignore
tableData.rows.push(row)
}
return tableData.rows.map(
row => new BookMatch(
new ExtractedBook(
row[this.titleColumn] || '', row[this.authorColumn] || ''),
{})
)
}
}

class ExtractionOptions {
constructor() {
/** @type {string} */
Expand Down Expand Up @@ -169,7 +220,8 @@ export class BulkSearchState{
new RegexExtractor('e.g. "The Wizard of Oz - L. Frank Baum"', '(^|>)(?<title>[A-Za-z][\\p{L}0-9\\- ,]{1,250})\\s+[,-\u2013\u2014\\t]\\s+(?<author>[\\p{L}][\\p{L}\\.\\- ]{3,70})( \\(.*)?($|<\\/)'),
new RegexExtractor('e.g. "The Wizard of Oz (L. Frank Baum)"', '^(?<title>[\\p{L}].{1,250})\\s\\(?<author>(.{3,70})\\)$$'),
new RegexExtractor('Wikipedia Citation (e.g. Baum, Frank L. (1994). The Wizard of Oz)', '^(?<author>[^.()]+).*?\\)\\. (?<title>[^.]+)'),
new AiExtractor('✨ AI Extraction', 'gpt-4o-mini')
new AiExtractor('✨ AI Extraction', 'gpt-4o-mini'),
new TableExtractor('Extract from a Table/Spreadsheet')
]
/** @type {Number} */
this._activeExtractorIndex = 0
Expand Down