postmodern / raingrams

A flexible and general-purpose ngrams library written in Ruby. Raingrams supports ngram sizes greater than 1, text/non-text grams, multiple parsing styles and open/closed vocabulary models.

This URL has Read+Write access

raingrams / spec / model_examples.rb
100644 84 lines (67 sloc) 2.212 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
require 'spec_helper'
 
shared_examples_for "Model" do
  it "should have ngrams" do
    @model.ngrams.each do |ngram|
      @model.has_ngram?(ngram).should == true
    end
  end
 
  it "should be able to iterate through all ngrams" do
    @model.each_ngram do |ngram|
      @model.has_ngram?(ngram).should == true
    end
  end
 
  it "should be able to select ngrams with certain properties" do
    ngrams = @model.ngrams_with do |ngram|
      ngram.include?(:the)
    end
 
    ngrams.each do |ngram|
      ngram.include?(:the).should == true
    end
  end
 
  it "should be able to select ngrams starting with a specified gram" do
    @model.ngrams_starting_with(:filtering).each do |ngram|
      ngram.starts_with?(:filtering).should == true
    end
  end
 
  it "should be able to select ngrams ending with a specified gram" do
    @model.ngrams_ending_with(:sword).each do |ngram|
      ngram.ends_with?(:sword).should == true
    end
  end
 
  it "should be able to select ngrams including any of the specified grams" do
    @model.ngrams_including_any(:The, :Deliverator).each do |ngram|
      ngram.includes_any?(:The, :Deliverator).should == true
    end
  end
 
  it "should be able to select ngrams including all of the specified grams" do
    @model.ngrams_including_all(:activated, :charcoal).each do |ngram|
      ngram.includes_all?(:activated, :charcoal).should == true
    end
  end
 
  it "should have grams" do
    @model.grams.each do |gram|
      @model.has_gram?(gram).should == true
    end
  end
 
  it "should provide a random ngram" do
    @model.has_ngram?(@model.random_ngram).should == true
  end
 
  it "should generate a random sentence" do
    sentence = @model.random_sentence
 
    @model.ngrams_from_sentence(sentence).each do |ngram|
      @model.has_ngram?(ngram).should == true
    end
  end
 
  it "should generate a random paragraph" do
    paragraph = @model.random_paragraph
 
    @model.ngrams_from_paragraph(paragraph).each do |ngram|
      @model.has_ngram?(ngram).should == true
    end
  end
 
  it "should generate a random text" do
    text = @model.random_text
 
    @model.ngrams_from_text(text).each do |ngram|
      @model.has_ngram?(ngram).should == true
    end
  end
end