ebene 0.2.0

A unique search technology
Documentation
extern crate strata;
extern crate itertools;

use strata::*;
use itertools::Itertools;

use std::collections::HashMap;
use std::env;
use std::fs::File;
use std::io;
use std::io::prelude::*;

fn main() {
    for file in env::args().skip(1) {
        let mut f = File::open(file).unwrap();
        let content = {
            let mut s = String::new();
            f.read_to_string(&mut s).unwrap();
            s
        };

        let mut index = HashMap::new();

        let mut chars = content.char_indices();

        loop {
            let (first, last) = {
                let mut words = chars.by_ref().take_while_ref(|&(_, c)| ! c.is_whitespace());
                (words.next(), words.last())
            };

            let extent = match (first, last) {
                (Some(s), Some(e)) => (s.0 as u64, e.0 as u64),
                (Some(s), None)    => (s.0 as u64, s.0 as u64),
                (None, _)          => break,
            };

            let word = &content[(extent.0 as usize)..(extent.1 as usize + 1)];

            // TODO: allow zero
            index.entry(word).or_insert(vec![]).push((extent.0 + 1, extent.1 + 1));

            for _x in chars.by_ref().take_while_ref(|&(_, c)| c.is_whitespace()) {}
        }

        println!("Indexed: {:?}", index);

        let stdin = io::stdin();

        for line in stdin.lock().lines() {
            let l = line.unwrap();
            let parts: Vec<_> = l.split(" ").collect();
            let a = index.get(parts[0]).map(|x| &x[..]).unwrap_or(&[][..]);
            let b = index.get(parts[2]).map(|x| &x[..]).unwrap_or(&[][..]);

            let op: Box<Algebra> = match parts[1] {
                "<" => Box::new(ContainedIn::new(a, b)),
                ">" => Box::new(Containing::new(a, b)),
                "/<" => Box::new(NotContainedIn::new(a, b)),
                "/>" => Box::new(NotContaining::new(a, b)),
                "&" => Box::new(BothOf::new(a, b)),
                "|" => Box::new(OneOf::new(a, b)),
                "->" => Box::new(FollowedBy::new(a, b)),
                _ => unimplemented!(),
            };

            for extent in op.iter_tau() {
                // TODO: allow zero
                let ex = (extent.0 - 1, extent.1 - 1);
                println!("{:?}: {}", ex, &content[(ex.0 as usize)..(ex.1 as usize + 1)]);
            }
        }

        // let l = "hello";
        // let r = "there";
        // let op = BothOf::new(&index[l][..], &index[r][..]);

        // println!("{:?}, {:?}", &index[l][..], &index[r][..]);

    }
}