Giter VIP home page Giter VIP logo

xwords-rs's Introduction

xwords

xwords is a fast library that fills crossword puzzles. This repo also contains a lightweight CLI for invoking the library.

Caveat Emptor

This is foremost a hobbyist project for me to learn a bit about profiling and optimizing rust. I am more than happy to accept contributions or to consider feature requests, but please be aware that the future of this project is somewhat uncertain.

CLI

This command fills a grid that is stored in a local file using a default wordlist.

$ xwords --input grids/20201005_empty.txt

CFS*ANGELI*ORDU
AIA*DEEPAS*SEIN
SCLAVONIAN*MFAS
IKANTLETGO*ALLE
OLDY**ROE*ANOSE
*YOOHOOMRSBLOOM
***FUGUE*IRIDAL
FAA*LIS*ECO*SPY
IMPROV*ACOOK***
BILLIEJEANKING*
SEUSS*IAD**CEIL
**STTIC**ALKALI
CITI*CACOMISTLE
CORN*OMOLON*LIS
CLEE*NATURA*YST

This command runs in about 2 seconds on my machine.

Library

use xwords::{crossword::Crossword, fill_crossword_with_default_wordlist};

fn main() -> Result<(), String> {
    let empty_crossword = Crossword::new(String::from(
        "
    *    *     
    *    *     
         *     
   *   *   *   
**    *        
      *     ***
     *    *    
   *       *   
    *    *     
***     *      
        *    **
   *   *   *   
     *         
     *    *    
     *    *    
",
    ))?;
    let filled_crossword = fill_crossword_with_default_wordlist(&empty_crossword)?;
    println!("{}", filled_crossword);
    Ok(())
}

/*
ZETA*TWIT*VOWEL
ETAT*IANA*EVOKE
RINTINTIN*REVIE
OCT*TIE*TUI*ENR
**ATHA*TASTINGS
TOLEAN*ILIES***
ISIAC*TEAN*STEM
ZAT*ACHATES*HRA
AYES*SETE*TYEES
***TUTSI*URALIC
VENERATE*SEWA**
ORA*TRO*UES*TOA
WISHI*NETASSETS
ETHIC*EVIL*USTO
RUEDA*SWAL*OTSU
*/

On my machine, the above snippet runs in about 3 seconds.

Behind the scenes, this snippet loads an indexed wordlist, and iteratively fills the input with valid words.

xwords-rs's People

Contributors

szunami avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

super7ramp

xwords-rs's Issues

Streaming support for find_fills

In the early phases, find_fills returns many results. Currently, other threads are starved while this big list of results is processed. If we stream these fills, other threads can begin working sooner.

WASM compatibility

cargo build --examples --target wasm32-unknown-unknown currently fails b/c of pprof

Add a cli

  • support for different fillers
  • support loading a file
  • interactive finding of grids / file editing

Experiment with shingling

Intuitively, we want to avoid spending resources on partially filled candidates which have rare or nonexistent n-shingles (https://en.wikipedia.org/wiki/N-gram). We can assign n-grams frequency scores based on the wordlist, and can score candidates based on the frequency scores (accounting for " " and * somehow).

Filler prints output when solving takes some time

Issue

Filler prints some statistics when filling takes some time.

It's not really an issue when called from command-line but it's not desirable when integrating it as a library. (I've been working on integrating it in my own small tool. For now I've just patched it to remove the calls to println! from filler.)

Steps to reproduce

  1. Create the following grid in grids/empty_12x10.txt:
            
    *       
          * 
    *   *   
    *       
       *    
      *   * 
  *  *   *  
   *    *   
            
  1. Calls filler from command line: ./target/debug/xwords --width 12 --height 10 --input grids/empty_12x10.txt

Expected result

No progress output, unless specified otherwise (e.g. with an option from command-line). Ideally API would provide some kind of progress notification mechanism.

Actual result

After a while, some outputs starts to be printed periodically:

    C       
    *       
    C     *C
    *   *   
    *       
       *    
      *   *C
  *  *   *  
  P* O  *   
PHILOSOPHIST

(...)

Tune which word we fill

today we use

return 2 * word.contents.len() as i32 - empty_squares;

This seems directionally right, but a bit loose

Could be sped up dramatically with better state-space search

There are a number of algorithmic tricks to make the state-space search dramatically faster, for example variable ordering, value-ordering, forward-pruning, backjumping, LDS/DDS. Here's a paper by my PhD advisor Matt Ginsberg describing Dr Fill, which is probably currently the world's fastest crossword-filler. A Rust implementation of the solver described here would be a really interesting project.

Filler panics when given grid is already filled

Issue

Filler panics when given grid is already filled. That's a very minor corner case but still :)

Steps to reproduce

  1. Create the following file in grids/filled_4x4.txt
CFOS
BIRI
CCCV
SASA
  1. Call the filler using command-line interface: ./target/debug/xwords --input grids/filled_4x4.txt

Expected result

The grid is returned as is since it is valid.

Actual result

> ./target/debug/xwords --input grids/filled_4x4.txt 
thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', src/fill/filler.rs:76:18
stack backtrace:
   0: rust_begin_unwind
             at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/std/src/panicking.rs:575:5
   1: core::panicking::panic_fmt
             at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/core/src/panicking.rs:65:14
   2: core::panicking::panic
             at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/core/src/panicking.rs:115:5
   3: core::option::Option<T>::unwrap
             at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/core/src/option.rs:778:21
   4: <xwords::fill::filler::Filler as xwords::fill::Fill>::fill
             at ./src/fill/filler.rs:65:27
   5: xwords::main
             at ./src/bin/xwords.rs:66:18
   6: core::ops::function::FnOnce::call_once
             at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/core/src/ops/function.rs:251:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
>

Environment

stable-x86_64-unknown-linux-gnu (default)
rustc 1.66.1 (90743e729 2023-01-10)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.