Sub GeneratePermutations(s As String) As List
Dim res As List
res.Initialize
If s.Length = 1 Then
res.Add(s)
Return res
End If
For i = 0 To s.Length - 1
Dim ch As String = s.CharAt(i)
Dim rem As String = s.SubString2(0, i) & s.SubString(i + 1)
Dim perms As List = GeneratePermutations(rem)
For Each perm As String In perms
res.Add(ch & perm)
Next
Next
Return res
End Sub
CREATE TEMP TABLE IF NOT EXISTS temp_permutations (word TEXT);
DELETE FROM temp_permutations;
Dim permutations As List = GeneratePermutations("yourstring")
For Each perm As String In permutations
DB.ExecNonQuery2("INSERT INTO temp_permutations (word) VALUES (?)", Array As Object(perm))
Next
SELECT lang.word
FROM language_table lang
JOIN temp_permutations temp ON lang.word = temp.word;
Sub GeneratePermutations(s As String) As List
Dim res As List
res.Initialize
If s.Length = 1 Then
res.Add(s)
Return res
End If
For i = 0 To s.Length - 1
Dim ch As String = s.CharAt(i)
Dim rem As String = s.SubString2(0, i) & s.SubString(i + 1)
Dim perms As List = GeneratePermutations(rem)
For Each perm As String In perms
res.Add(ch & perm)
Next
Next
Return res
End Sub
Sub QueryPermutations(db As SQL, input As String) As List
' Create temporary table
db.ExecNonQuery("CREATE TEMP TABLE IF NOT EXISTS temp_permutations (word TEXT);")
db.ExecNonQuery("DELETE FROM temp_permutations;")
' Generate permutations
Dim permutations As List = GeneratePermutations(input)
For Each perm As String In permutations
db.ExecNonQuery2("INSERT INTO temp_permutations (word) VALUES (?)", Array As Object(perm))
Next
' Query matching words
Dim result As List
result.Initialize
Dim rs As ResultSet = db.ExecQuery("SELECT lang.word FROM language_table lang JOIN temp_permutations temp ON lang.word = temp.word;")
Do While rs.NextRow
result.Add(rs.GetString("word"))
Loop
rs.Close
Return result
End Sub
I'm going to be aggressive and say: Sure, it's possible! Not that I know how to do it, obviously. It's just that it seems SQL can do anything.Any experiences to share?
Here is a brainteaser for any B4X insommiacs out there.....
I have a language database in SQLite with around 30k words. Given a string comprising 4 to 12 characters, is it possible to devise an SQL query that extracts all matching words for every possible permutation of those exact characters.
I started writing a query in SQLlite Expert Pro but it appears to be an awful lot more complicated than you might imagine.
Any experiences to share?
Lo condividerò sono solo oberato dal lavoro ma prometto che in pochi giorni metterò qui la libreria che ho creato proprio per questo ed altri problemi legati alle parole@LordZenzo Congratulazioni per aver trovato una soluzione. È qualcosa che desideri condividere o ti stai semplicemente vantando?
Congratulations on finding a solution. Is this something you wish to share or are you just bragging?
I will share it I am just overworked but I promise that in a few days I will put the library I created for this and other word problems hereEnglish please.
Sub GeneratePermutations(s As String) As List
Dim Possibilities As List
Possibilities.Initialize
If s.Length = 1 Then
Possibilities.Add(s)
Return Possibilities
End If
For i = 0 To s.Length - 1
Dim ch As String = s.CharAt(i)
Dim rem As String = s.SubString2(0, i) & s.SubString(i + 1)
Dim perms As List = GeneratePermutations(rem)
For Each perm As String In perms
Possibilities.Add(ch & perm)
Next
Next
Return Possibilities
End Sub
Sub Button1_Click
Dim Input As String = Dialog.TextInputDialog("Anagrams","Enter Letter Jumble","","")
If Input.Length = 0 Then Return
Dim Permutations, Results As List
Permutations.Initialize
Results.Initialize
Permutations = GeneratePermutations(Input)
MainForm.Title = Input & " " & Permutations.Size
For Each Word As String In Permutations
Dim found As String
found = SQL.ExecQuerySingleResult2("SELECT Word FROM English WHERE Word = ? " ,Array As String(Word))
If found <> Null Then
Results.Add(found)
End If
Next
End Sub
There is a trick to obtain anagrams very quickly, instantly.Any other suggestions?
usa la mia libreria, la puoi scaricare dal link e puoi fare una donazione, oltre a gli anagrammi ha altre funzioni interessanti come la distanza di levensthain e il suggerrimento di correzzioni, devi solo avere un csv con il dizionario, io ne propongo per 3 lingue, ma puoi creare il tuo personale al primo utilizzo impiega qualche secondo (o piu) per caricare le parole e generare una tabella sqlite, ma poi è velocissimoAltri suggerimenti?
There is a trick to obtain anagrams very quickly, instantly.
I can't reveal it;
I wrote an anagram program many years ago and use the following method. I did use lists but a rewrite would move to SQL.
If one "Normalizes" each word into a string with each letter and a count as shown below, one can easily find anagrams by searching for the identical normailzed strings.
[A1C1E1L1N1R1U1]=NUCLEAR would also give UNCLEAR, etc.
Easy search in SQL with no extra non-words. A 300,000 word database I have is basically instantaneous.
Dim str As String = SortLetters(Letters)
Query = $"SELECT Word FROM Words WHERE KeyField = ?"$
Dim RS As ResultSet
RS = DB.ExecQuery2(Query, Array As String(str))
Private Sub SortLetters(Letters As String) As String
Dim Result As String
Dim lstLetters As List
lstLetters.Initialize
Dim bytChars() As Byte = Letters.GetBytes("UTF8")
For i = 0 To bytChars.Length - 1
lstLetters.Add(bytChars(i))
Next
lstLetters.Sort(True)
For i = 0 To lstLetters.Size - 1
Result = Result & Chr(lstLetters.Get(i))
Next
Return Result
End Sub
It is not needed. If you search for the 10-letter anagrams provided, only words 10 long will be extracted that way.Further to @LucaMs example, by adding a word length column you can use a where clause to ignore words of the wrong length
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?