Skip to content

Commit a9ef012

Browse files
committed
refactor: extract simple search CQP building
1 parent 89b6a01 commit a9ef012

File tree

2 files changed

+32
-21
lines changed

2 files changed

+32
-21
lines changed

app/scripts/components/search/simple-search.ts

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import "./global-filters"
1414
import { Condition, CqpQuery } from "@/cqp_parser/cqp.types"
1515
import { StoreService } from "@/services/store"
1616
import { savedSearches } from "@/search/saved-searches"
17+
import { buildSimpleLemgramCqp, buildSimpleWordCqp } from "@/search/simple-search"
1718

1819
type SimpleSearchController = IController & {
1920
input: string
@@ -185,29 +186,12 @@ angular.module("korpApp").component("simpleSearch", {
185186
}
186187

187188
ctrl.getCQP = function () {
188-
const query: CqpQuery = []
189+
let query: CqpQuery = []
189190
const currentText = (ctrl.currentText || "").trim()
190191

191-
if (currentText) {
192-
currentText.split(/\s+/).forEach((word) => {
193-
let value = regescape(word)
194-
if ($scope.prefix) value = `${value}.*`
195-
if ($scope.suffix) value = `.*${value}`
196-
const condition = createCondition(value)
197-
if (ctrl.isCaseInsensitive) condition.flags = { c: true }
198-
query.push({ and_block: [[condition]] })
199-
})
200-
} else if (ctrl.lemgram) {
201-
const conditions: Condition[] = [{ type: "lex", op: "contains", val: ctrl.lemgram }]
202-
// The complemgram attribute is a set of strings like: <part1>+<part2>+<...>:<probability>
203-
if ($scope.prefix) {
204-
conditions.push({ type: "complemgram", op: "contains", val: `${ctrl.lemgram}\\+.*` })
205-
}
206-
if ($scope.suffix) {
207-
conditions.push({ type: "complemgram", op: "contains", val: `.*\\+${ctrl.lemgram}:.*` })
208-
}
209-
query.push({ and_block: [conditions] })
210-
}
192+
if (currentText)
193+
query = buildSimpleWordCqp(currentText, $scope.prefix, $scope.suffix, ctrl.isCaseInsensitive)
194+
else if (ctrl.lemgram) query = buildSimpleLemgramCqp(ctrl.lemgram, $scope.prefix, $scope.suffix)
211195

212196
if (store.globalFilter) mergeCqpExprs(query, store.globalFilter)
213197
return stringify(query)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { createCondition } from "@/cqp_parser/cqp"
2+
import { Condition, CqpQuery } from "@/cqp_parser/cqp.types"
3+
import { regescape } from "../util"
4+
5+
/** Create query from word input string. */
6+
export function buildSimpleWordCqp(input: string, prefix = false, suffix = false, ignoreCase = false): CqpQuery {
7+
return input.split(/\s+/).map((word) => {
8+
let value = regescape(word)
9+
if (prefix) value = `${value}.*`
10+
if (suffix) value = `.*${value}`
11+
const condition = createCondition(value)
12+
if (ignoreCase) condition.flags = { c: true }
13+
return { and_block: [[condition]] }
14+
})
15+
}
16+
17+
export function buildSimpleLemgramCqp(input: string, prefix = false, suffix = false): CqpQuery {
18+
const conditions: Condition[] = [{ type: "lex", op: "contains", val: input }]
19+
// The complemgram attribute is a set of strings like: <part1>+<part2>+<...>:<probability>
20+
if (prefix) {
21+
conditions.push({ type: "complemgram", op: "contains", val: `${input}\\+.*` })
22+
}
23+
if (suffix) {
24+
conditions.push({ type: "complemgram", op: "contains", val: `.*\\+${input}:.*` })
25+
}
26+
return [{ and_block: [conditions] }]
27+
}

0 commit comments

Comments
 (0)