Saturday, November 7, 2026
HomeiOS Developmentios - Environment friendly Approaches to Phrase Search Sport DB Search Wanted

ios – Environment friendly Approaches to Phrase Search Sport DB Search Wanted

[ad_1]

I’ve a recreation that makes use of a library of ordinary phrases, and a pool of as much as 9 letters that the consumer has to play with. There may be additionally a board, that accommodates 0-11 letters which have been positioned by the participant.

The sport permits the participant to solely play one tile at a time, and solely connected at both one finish or the opposite of the letters on the board. When there are 4 or extra letters on the board, they’ll additionally exchange a letter already positioned.

enter image description here

The important thing factor is that solely letters mixtures that both type a phrase, or a fraction of a phrase within the dictionary are permitted. So, some letters that the participant has could also be legitimate, as a result of they can be utilized to type a phrase or fragment, composed of the letters on the board plus the letter. Others is not going to have a match, and be invalid decisions to play.

In apply (the sport is practical and on the App Retailer https://apps.apple.com/us/app/wordsby2/id1553717309 ), which means that I’ve a method for looking for authorized mixtures of letters. The shape this takes is to type all of the permutations of letters attainable given the principles. This is able to all the time be fashioned of the present board letters, and one letter from the consumer’s rack of letters; by default, every letter within the participant’s pool is permitted by means of the bottom phrase on the board.

If Pink is about to play, I generate all of the mixtures that may be fashioned with the letters on the board [J O I N], and all of the distinctive letters within the rack of letters that Pink has [R U E P O]. If it was Blue, Id substitute these for [M I O X A Z R].

The less complicated drawback is Pink, as a result of there are fewer letters. I’d generate a thesaurus like

RJOIN
JRIN
JORN
JOIR
JOINR
UJOIN
UOIN
JUIN
JOUN
JOIU
JOINU

, and many others.

An additional caveat is, that I am solely concerned with figuring out the board positions that may be performed to. So, if I discovered that there was a phrase, or a phrase with a fraction “JORN”, I’d not must do any subsequent searches for any letter that concerned the third place within the phrase JOIN -> JO!N can be saved internally when that phrase or phrase fragment was discovered.

Complicating that is that if I do not discover a full phrase, then I’ve to make certain that the phrase the mixture is a fraction of would slot in the 11 areas on the board, not requiring any areas off the board, so the search can be one thing like

**RJOIN*****
***JRIN*****
***JORN*****
***JOIR*****
***JOINR****
**UJOIN*****
***UOIN*****
***JUIN*****
***JOUN*****
***JOIU*****
***JOINU****

The present deployment is written in Swift, and makes use of CoreData, so principally SOLite. That is surprisingly fast, however there are searches that trigger a momentary pause whereas the data is returned.

However we’re considering a Internet model, so we’ll have the choice to port the DB to Postgres (our default alternative). As a result of our codebase is in Swift, any capabilities that need to carried out on the again finish shall be lifted instantly from the present recreation, and run in Vapor.

This job is one that may positively not be a browser facet job, however I fear about what the compute prices and efficiency shall be. So, clearly, one of the best factor to do is to optimize the DB searches now.

The present mannequin works; the DB is a single-table so I do not suppose there are any magic indexes that may enhance it.

Is there an ordinary method to take care of this type of drawback? I’ve thought-about dividing all phrases over e.g. 6 letters into two sections, the primary 3 letters, and the remaining ones, after which utilizing a two-stage search that may permit the ensuing letter mixtures to outline the viability of any subsequent searches. This sort of strategy intuitively looks as if it may be a good suggestion, however maybe it is what an index does anyway?

I do know this can be a basic query, nevertheless it boils right down to, does this adhere to an ordinary resolution that I may implement, and what’s it, or what are they?

Here is the routine that does the bottom type of the search:

public func wordMatcher (pad: Int, phrase: Array<String>, substitutes : Set<String> ) -> Set<Int> {
//        scans db for any phrases at the moment attainable utilizing present phrase + 1 of the present rack lotters
        openBoard(empty: true)
        if phrase.rely == 0 {
            openBoard()
            return lFreq
        }
        if substitutes.rely == 0 {
            return lFreq
        }
        let context = CoreDataManager.shared.persistentContainer.viewContext
        var pad_ = pad
        var question: Array<String>
        var predMatch: String
//        var foundPositions : Set<Int> = []
        let rq: NSFetchRequest<Phrase> = Phrase.fetchRequest()
        rq.fetchLimit = 1
        let subs = "[(substitutes.joined())]"
//        if phrase.rely >= 4 { // as a result of these places shall be blocked off anyway in any other case
        let begin = pad > 0 ? -1 : 0
        let end = 11 - (pad + phrase.rely) > 0 ? phrase.rely + 1 : phrase.rely
        for i in begin..<end {
            question = phrase
            var _pad = 11 - (pad + phrase.rely)
            if i == -1 {
                question = Array(arrayLiteral: subs) + question
                pad_ -= 1
            } else if i >= phrase.rely {
                question.append(subs)
                _pad -= 1
            } else { // MARK: THIS IS PROBABLY NOT TESTING ALL INTERNAL POSITIONS
                pad_ = pad
                question[i] = subs
            }
            let endPad = _pad > 0 ? "[a-z]{0,(_pad)}" : ""
            let prePad = pad_ > 0 ? "[a-z]{0,(pad_)}" : ""
            predMatch = "^(prePad)(question.joined())(endPad)"
//            print(predMatch)
            rq.predicate = NSPredicate(format: "phrase MATCHES[cd] %@", predMatch)
            do {
                if attempt context.fetch(rq).rely != 0 {
                    print("VALID: (predMatch)")
                    lFreq.insert(i + pad)
                }
            } catch {
                print("error looking out")
            }
//            }
        }
        print("lFreq is (lFreq)")
        if phrase.rely < 4 {
            clearWord(begin: pad, size: phrase.rely)
        }
        return lFreq
    }

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments