SearchQueryParser
1.0.0
SearchQueryParser
adalah parser (dan penanda) kueri mesin pencari sederhana seperti Google untuk Swift. Dibutuhkan string pencarian dan membangun pohon sintaksis abstraknya. Paket ini berlaku untuk digunakan dengan Kit Pencarian Apple untuk menyorot hasil yang ditemukan.
Bentuk Backus–Naur (BNF):
Query = Sweeping-Mute-And-Term, ⧚’?, EOF ;
Sweeping-Mute-And-Term = Mute-And-Term, { ⧚’?, Mute-And-Term }* ;
Mute-And-Term = Or-Term, Or-Term* ;
Or-Term = And-Term, {⧚?, (‘|’ | “OR”), And-Term }* ;
And-Term = Not-Term, {⧚?, (‘&’ | “AND”), Not-Term }* ;
Not-Term = ⧚?, (’!’ | “NOT”), Not-Term | Primary-Term ;
Primary-Term = ⧚?, ’(‘ ⧚?, Mute-And-Term, ⧚? ‘)’ |
⧚?, “‘“ ⧚?, Phrase, ⧚? “‘“ |
⧚?, Prefix-Wildcard-Search-Term |
⧚?, Suffix-Wildcard-Search-Term |
⧚?, Search-Term ;
Phrase = Search-Term, { ⧚, Search-Term }* ;
Prefix-Wildcard-Search-Term = ‘*’, Search-Term ;
Suffix-Wildcard-Search-Term = Search-Term, ‘*’ ;
Search-Term = Letter | Letter, Search-Term ;
Letter = Alpha | Digit | ‘_’ ;
⧚’ = ⧚-(“*” | “!” | “&” | “(“ | “)” | “””)
⧚ = { ? any-character ?-Letter }+ ;
Alpha = ? alpha-character ? ;
Digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" ;
import SearchQueryParser
func printQueryAST ( _ query : String ) {
guard let parser = SearchQueryParser ( query ) , let ast = parser . astRoot
else {
print ( " Couldn't parse " )
return
}
print ( " AST: ( ast ) " )
}
printQueryAST ( " foo bar " )
import SearchQueryParser
func highlightSearchQueryMatches ( query : String , in text : NSMutableAttributedString ) {
guard let queryTerms = SearchQueryParser ( query )
else {
print ( " No matches found " )
return
}
let tokensProvider = SearchTextScanner ( text . string )
guard let applicator = SearchQueryApplicator ( searchQueryTerms : queryTerms ,
textTokens : tokensProvider )
else {
print ( " Nothing was marked " )
return
}
applicator . markedRanges . forEach { range in
text . addAttribute ( . backgroundColor , value : NSColor . yellow , range : range )
}
}
let text = NSMutableAttributedString ( string : " Lorem ipsum ... " )
highlightSearchQueryMatches ( query : " foo & bar " , in : text )
https://github.com/SerhiyButz/SearchQueryParser.git
Klik "Selanjutnya"
Pastikan bidang "Aturan" disetel seperti ini: "Versi: Hingga Jurusan Berikutnya: 1.3.0"
Klik "Berikutnya" untuk menyelesaikan
Untuk info lebih lanjut, lihat di sini.
Tambahkan yang berikut ke Podfile Anda:
platform :osx , '10.12'
target 'YOUR-TARGET' do
use_frameworks!
pod 'SearchQueryParser' , :git => 'https://github.com/SerhiyButz/SearchQueryParser.git'
end
Kemudian jalankan pod install
.
Proyek ini dilisensikan di bawah lisensi MIT.