ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Wordlist, ok ... I forgot my password, very bright so john is there.
Is there better script for creating permutations of each word in their wordlist?
I found this code for PHP
Code:
<?php
// Load the dictionary file
$dictionary = file_get_contents("d:\wn.txt");
// Extract into an array
$words = split("\r\n", $dictionary);
// Create an empty output string
$output = "";
// Loop through each word
foreach ($words as $word)
{
// Test that the word is A-Z only and 4 letters in length
if (eregi("^[a-z]{4}$", $word))
{
// Append to the output string
$output .= ($word.",");
}
}
// Write the output to file
file_put_contents("d:\wn_filtered.txt", $output);
?>
my wordlist:
Quote:
linux
is
super
hello
this
is
wordlist
Last edited by frenchn00b; 08-18-2009 at 03:23 PM.
so in progress, I need to cat wordlist.lst and pipe it into this code
Code:
/* You could put your own name in for abcd to
see if there are any valid word permutations
Download:
http://prdownloads.sourceforge.net/cpearls/simple_but_common_cpp.tar.gz?download
You can find a large dictionary of words at the following link:
http://prdownloads.sourceforge.net/souptonuts/linuxwords.1.tar.gz?download
*/
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int main(void)
{
char a[]="abcd";
int r=1;
size_t len = strlen(a);
cout << a << "\t";
while(next_permutation(a,a+len)){
cout << a;
(r++ % len+2 == len+1) ? cout << "\n": cout << "\t";
}
}
A while ago someone asked how to find all the dictionary words in all permutation of an input string. (I suspect, but don' know if this is so, that the problem was in the context of some game show.) In any case, I wrote the following SQLite code to take a string and create all the (permuted) sub-strings of the input string. For example,
First, here's the code:
Code:
/* Find the next permutation of a string */
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sqlite3.h>
#define debug 0
extern FILE * stdin;
extern FILE * stdout;
extern FILE * stderr;
/**********************************************/
/* */
/* SQLite error report helper function */
/* */
/**********************************************/
int check_code( // Return 0 is OK, 1 otherwise
sqlite3 * db, // Data base information pointer
sqlite3_stmt *stmt, // Statement being checked
const int rc, // Value to check
const char * text, // Text to print if check fails
const int target) // SQLite expected return code
{
if (rc != target) {
fprintf(stderr, "%s: %s\n", text, sqlite3_errmsg(db));
if (stmt) sqlite3_finalize(stmt);
return 1;
}
return 0;
}
/**********************************************/
/* */
/* Partition a string into all its substrings */
/* */
/**********************************************/
int partition( // Return 0 on success, 1 otherwise
const char * const string, // String to be partitioned
sqlite3 * db) // Pointer to the SQLite data base to use for storage
{
long int npart; // Number of partitions
long int bits; // Binary value holder
int n; // Length of input string
register int pos; // Character to be next printed
register int k; // Inner loop index
long int i, j; // Current partition number
char * str; // Word holder
sqlite3_stmt *stmt; // Insert statement holder
char * errmsg;
int rc; // SQLite return code holder
char insert[256];
n = strlen(string);
npart = ((long int) 1) << (n - 1);
str = (char *) malloc(n*sizeof(char));
if (!str) {
fprintf(stderr,"partition: Could not allocate %d characters for \"str.\"\n", n);
return 1;
}
// Find the (next) partition and insert it into the data base if it's longer than 1 character
j = 0;
for (i = npart - 1; i >= 0; --i) {
bits = i;
pos=0;
k = 0;
while (bits) {
str[k++] = string[pos++];
if (bits & 1) {
str[k] = (char) 0;
// Add the word to the data base if it's long enough
if (k > 1) {
sqlite3_snprintf(255, insert, "INSERT INTO WORDS (WORD) VALUES(%Q)", str);
rc = sqlite3_exec(db, insert, NULL, NULL, &errmsg);
if (check_code(db, stmt, rc, "partition: Problem inserting WORD", SQLITE_OK)) {
sqlite3_free(errmsg);
sqlite3_close(db);
exit (1);
}
}
// And compute the next word . . .
++j;
k = 0;
}
bits = bits / 2;
}
// Finished with the current value of j. Process any remaining characters.
while (pos < n) str[k++] = string[pos++];
str[k] = (char) 0;
pos = 0;
// Add the last string if it's long enough
if (k > 1) {
sqlite3_snprintf(255, insert, "INSERT INTO WORDS (WORD) VALUES(%Q)", str);
rc = sqlite3_exec(db, insert, NULL, NULL, &errmsg);
if (check_code(db, stmt, rc, "partition: Problem inserting WORD", SQLITE_OK)) {
sqlite3_free(errmsg);
sqlite3_close(db);
exit (1);
}
}
bits = --i;
k = 0;
}
return 0;
}
int comb( // -1 If no more permutations,
// length of string if returned string is unchanged,
// otherwise index of character exchanged with next one
char * string,// String whose characters are being permuted
int *new) // Flag. 1 if string is a new string to be permuted, 0 otherwise
{
static int n = 0,
i,j,
* d = 0,
* e = 0,
* a = 0;
char c;
int k;
// Are we starting a new string?
if (*new) {
n = strlen(string);
// Remove any terminating new line character
if (string[n-1]=='\n') string[--n]=0;
// Do we have anything left?
if (n < 2) {
fprintf(stderr,"\"%s\" is too short to permute.\n", string);
return -1;
}
d = malloc(n * sizeof(int));
e = malloc(n * sizeof(int));
a = malloc(n * sizeof(int));
if (!d || !e || !a ) {
fprintf(stderr,"comb: Insufficient dynamic memory.\n");
if (d) free(d);
if (e) free(e);
if (a) free(a);
return -1;
}
// Initialize the tracking arrays
for (i = 0; i < n; ++i) {
d[i]=0;
e[i]=1;
a[i]=i+1;
}
// Start the exchange with the last character
j = n - 1;
// Set the 'new string" flag off
*new=0;
return n;
}
// If here, not initializing. Find the next permutation, if any
while (1) {
a[j] = a[j] - e[j];
if ((a[j] == j + 1) || (a[j]== 0)) {
e[j]=-e[j];
d[j]=1 - d[j];
j = j - 1;
// Are we done?
if (j == 0) {
free(d);
free(e);
free(a);
return -1;
}
}
else break;
}
k = a[j];
for (i = j + 1; i < n; ++i) {
k += d[i];
}
if (string[k-1] != string[k]) {
c = string[k];
string[k] = string[k - 1];
string[k - 1] = c;
return k - 1;
}
// Recursive call to deal with exchange of duplicate characters
return comb(string, new);
}
/******************************************************/
/* */
/* Call-back function used by sqlite3_exec() */
/* */
/******************************************************/
static int process_sql( // Return 0 on success
sqlite3 *db, // Data base being processed
int argc, // Number of row values returned by query
char **argv, // Data values returned
char **azColName) // Name of the data columns returned
{
if (argc < 0) return 1;
if (partition(argv[0], db)) {
// If we get here we had a data base error. Abort.
sqlite3_close(db);
exit (1);
}
// Partition the root word
if (partition(argv[0], db)) {
// If we get here we had a data base error. Abort.
sqlite3_close(db);
exit (1);
}
return 0;
}
int main(int argc, char **argv)
{
char word[256]; // Current word holder
char msg[256]; // Potential error message holder
int i;
register int j,k,l; // Temporary integers (loop index, etc.)
// SQLite stuff
sqlite3 *db;
sqlite3_stmt *stmt = 0;
sqlite3_stmt *root = 0;
int rc;
char create[]= "CREATE TABLE WORDS (WORD TEXT PRIMARY KEY ON CONFLICT IGNORE)";
char select[]= "SELECT WORD FROM WORDS ORDER BY WORD";
char roots[]= "CREATE TABLE ROOTS (ROOT TEXT PRIMARY KEY ON CONFLICT IGNORE)";
char getroot[]="SELECT ROOT FROM ROOTS ORDER BY ROOT";
char command[256]; // Command string holder
char *errmsg = (char *) NULL;
// Create a temporary data base to hold the strings found
rc = sqlite3_open_v2(NULL, &db, 0, errmsg);
sprintf(msg, "%s: Could not open an ephemeral SQLite data base", argv[0]);
if (check_code(db, stmt, rc, msg, SQLITE_OK)) {
close(db);
exit(1);
}
// Create the table to store the root "words"
rc = sqlite3_exec(db, roots, NULL, NULL, &errmsg);
sprintf(msg, "%s: Could not create table ROOTS", argv[0]);
if (check_code(db, stmt, rc, msg, SQLITE_OK)) {
sqlite3_close(db);
exit(1);
}
// Create the table to store the "words"
rc = sqlite3_exec(db, create, NULL, NULL, &errmsg);
sprintf(msg, "%s: Could not create table WORDS", argv[0]);
if (check_code(db, stmt, rc, msg, SQLITE_OK)) {
sqlite3_close(db);
exit(1);
}
// End SQLite stuff
// For each input string . ' '
while (fgets(word, 256, stdin)) {
// Insert all permutations of "word' into the temporary data base
i = 1;
while (comb(word, &i) >= 0) {
sqlite3_snprintf(255, command, "INSERT INTO WORDS (WORD) VALUES(%Q)", word);
rc = sqlite3_exec(db, command, NULL, NULL, &errmsg);
sprintf(msg, "%s: Problem inserting WORD (\"%s\")", argv[0], word);
if (check_code(db, stmt, rc, msg, SQLITE_OK)) {
sqlite3_free(errmsg);
sqlite3_close(db);
exit (1);
}
}
}
// O.K., now all the unique, permuted strings from the input are ready to be partitioned. Do it.
//
// Note: The processing is done in the call-back routine, "process_sql"
//
rc = sqlite3_exec(db, select, (sqlite3_callback) process_sql, db, &errmsg);
sprintf(msg, "%s: Problem creating WORDS", argv[0]);
if (check_code(db, stmt, rc, msg, SQLITE_OK)) {
sqlite3_free(errmsg);
sqlite3_close(db);
exit(1);
}
// Write the "words" stored in the data base to the output file
rc = sqlite3_prepare_v2(db, select, -1, &stmt, 0);
if (rc != SQLITE_OK) {
fprintf(stderr,"%s: Error preparing %s: %s\n", argv[0], select, sqlite3_errmsg(db));
sqlite3_finalize(stmt);
exit(1);
}
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
strncpy(word, (char *) sqlite3_column_text(stmt, 0), 255);
fprintf(stdout,"%s\n", word);
}
sprintf(msg, "%s: Error retrieving data from WORDS", argv[0]);
check_code( db, stmt, rc, msg, SQLITE_DONE);
sqlite3_finalize(stmt);
sqlite3_close(db);
exit ((rc==SQLITE_DONE) ? 0 : 1);
}
And, if that was compiled to, say substrings, this command: $ echo averylongstring | ./substrings | hunspell -G
would produce this output (truncated for brevity):
Code:
$ echo averylongstring | ./substrings | hunspell -G
an
as
at
av
ave
aver
avers
avert
avg
ay
ea
en
er
erg
err
es
gave
gin
gist
go
gong
gongs
gr
grin
gs
gt
in
ion
is
it
. . .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.