diff --git a/doc/classes/Mirimiri.html b/doc/classes/Mirimiri.html new file mode 100644 index 0000000..6e74cd8 --- /dev/null +++ b/doc/classes/Mirimiri.html @@ -0,0 +1,150 @@ + + + + Module: Mirimiri [RDoc Documentation] + + + + + + + + + +
+ + + + + + + + + + + +
ModuleMirimiri
In: + + + + + lib/mirimiri/string.rb + + + + +
+ + + + + lib/mirimiri/document.rb + + + + +
+ +
+
+ + +
+ +
+ +
+

+General module +

+ +
+ +
+ + +
+ + + +
+ +
+

Classes and Modules

+ + Class Mirimiri::Document
+Class Mirimiri::WebDocument
+Class Mirimiri::WikipediaPage
+ +
+ +
+

Constants

+ +
+ + + + + + + + + + + + +
Stoplist=[ "a", "anything", "anyway", "anywhere", "apart", "are", "around", "as", "at", "av", "be", "became", "because", "become", "becomes", "becoming", "been", "before", "beforehand", "behind", "being", "below", "beside", "besides", "between", "beyond", "both", "but", "by", "can", "cannot", "canst", "certain", "cf", "choose", "contrariwise", "cos", "could", "cu", "day", "do", "does", "doesn't", "doing", "dost", "doth", "double", "down", "dual", "during", "each", "either", "else", "elsewhere", "enough", "et", "etc", "even", "ever", "every", "everybody", "everyone", "everything", "everywhere", "except", "excepted", "excepting", "exception", "exclude", "excluding", "exclusive", "far", "farther", "farthest", "few", "ff", "first", "for", "formerly", "forth", "forward", "from", "front", "further", "furthermore", "furthest", "get", "go", "had", "halves", "hardly", "has", "hast", "hath", "have", "he", "hence", "henceforth", "her", "here", "hereabouts", "hereafter", "hereby", "herein", "hereto", "hereupon", "hers", "herself", "him", "himself", "hindmost", "his", "hither", "hitherto", "how", "however", "howsoever", "i", "ie", "if", "in", "inasmuch", "inc", "include", "included", "including", "indeed", "indoors", "inside", "insomuch", "instead", "into", "inward", "inwards", "is", "it", "its", "itself", "just", "kind", "kg", "km", "last", "latter", "latterly", "less", "lest", "let", "like", "little", "ltd", "many", "may", "maybe", "me", "meantime", "meanwhile", "might", "moreover", "most", "mostly", "more", "mr", "mrs", "ms", "much", "must", "my", "myself", "namely", "need", "neither", "never", "nevertheless", "next", "no", "nobody", "none", "nonetheless", "noone", "nope", "nor", "not", "nothing", "notwithstanding", "now", "nowadays", "nowhere", "of", "off", "often", "ok", "on", "once", "one", "only", "onto", "or", "other", "others", "otherwise", "ought", "our", "ours", "ourselves", "out", "outside", "over", "own", "per", "perhaps", "plenty", "provide", "quite", "rather", "really", "round", "said", "sake", "same", "sang", "save", "saw", "see", "seeing", "seem", "seemed", "seeming", "seems", "seen", "seldom", "selves", "sent", "several", "shalt", "she", "should", "shown", "sideways", "since", "slept", "slew", "slung", "slunk", "smote", "so", "some", "somebody", "somehow", "someone", "something", "sometime", "sometimes", "somewhat", "somewhere", "spake", "spat", "spoke", "spoken", "sprang", "sprung", "stave", "staves", "still", "such", "supposing", "than", "that", "the", "thee", "their", "them", "themselves", "then", "thence", "thenceforth", "there", "thereabout", "thereabouts", "thereafter", "thereby", "therefore", "therein", "thereof", "thereon", "thereto", "thereupon", "these", "they", "this", "those", "thou", "though", "thrice", "through", "throughout", "thru", "thus", "thy", "thyself", "till", "to", "together", "too", "toward", "towards", "ugh", "unable", "under", "underneath", "unless", "unlike", "until", "up", "upon", "upward", "upwards", "us", "use", "used", "using", "very", "via", "vs", "want", "was", "we", "week", "well", "were", "what", "whatever", "whatsoever", "when", "whence", "whenever", "whensoever", "where", "whereabouts", "whereafter", "whereas", "whereat", "whereby", "wherefore", "wherefrom", "wherein", "whereinto", "whereof", "whereon", "wheresoever", "whereto", "whereunto", "whereupon", "wherever", "wherewith", "whether", "whew", "which", "whichever", "whichsoever", "while", "whilst", "whither", "who", "whoa", "whoever", "whole", "whom", "whomever", "whomsoever", "whose", "whosoever", "why", "will", "wilt", "with", "within", "without", "worse", "worst", "would", "wow", "ye", "yet", "year", "yippee", "you", "your", "yours", "yourself", "yourselves" ]  +These are the default stopwords provided by Lemur. + +
+
+
+ + + + + + + + + +
+ +
+

[Validate]

+
+ + + diff --git a/doc/classes/Mirimiri/Document.html b/doc/classes/Mirimiri/Document.html new file mode 100644 index 0000000..b8dbd34 --- /dev/null +++ b/doc/classes/Mirimiri/Document.html @@ -0,0 +1,342 @@ + + + + Class: Mirimiri::Document [RDoc Documentation] + + + + + + + + + +
+ + + + + + + + + + + + + + + + +
ClassMirimiri::Document
In: + + + + + lib/mirimiri/document.rb + + + + +
+ +
Parent: + + Object + +
+
+ + +
+ +
+ +
+

+A Document is a bag of words and is constructed +from a string. +

+ +
+ +
+ + +
+

Methods

+ +
+ + count_words   + + entropy   + + format_words   + + new   + + ngrams   + + tf   + +
+
+ +
+ + + +
+ + + +
+

Attributes

+ +
+ + + + + + + + + + + + + + + + + + +
doc_content [R] 
words [R] 
+
+
+ + + + +
+ +

Public Class methods

+ + +
+ + + + +
+ +
+
+ + +

Public Instance methods

+ + +
+ + + + +
+ +

+Returns a Hash containing the words and their associated counts in the +current Document. +

+
+  count_words #=> { "guitar"=>1, "bass"=>3, "album"=>20, ... }
+
+ +
+
+ + +
+ + + + +
+ +

+Computes the entropy of a given string s inside the document. +

+

+If the string parameter is composed of many words (i.e. tokens separated by +whitespace(s)), it is considered as an ngram. +

+
+  entropy("guitar") #=> 0.00432114812727959
+  entropy("dillinger escape plan") #=> 0.265862076325102
+
+ +
+
+ + +
+ + + + +
+ +

+Returns an Array containing the n-grams (words) from the current +Document. +

+
+  ngrams(2) #=> ["the free", "free encyclopedia", "encyclopedia var", "var skin", ...]
+
+ +
+
+ + +
+ + + + +
+ +

+Computes the term frequency of a given word s. +

+
+  tf("guitar") #=> 0.000380372765310004
+
+ +
+
+ + +

Protected Instance methods

+ + +
+ + + + +
+ +

+Any non-word characters are removed from the words (see perldoc.perl.org/perlre.html +and the W special escape). +

+

+Protected function, only meant to by called at the initialization. +

+ +
+
+ + + +
+ + + + +
+ +
+

[Validate]

+
+ + + diff --git a/doc/classes/Mirimiri/Document.src/M000022.html b/doc/classes/Mirimiri/Document.src/M000022.html new file mode 100644 index 0000000..fdcae28 --- /dev/null +++ b/doc/classes/Mirimiri/Document.src/M000022.html @@ -0,0 +1,23 @@ + + + + format_words (Mirimiri::Document) + + + + +
# File lib/mirimiri/document.rb, line 34
+    def format_words
+      wo = []
+
+      @doc_content.split.each do |w|
+        w.split(/\W/).each do |sw| 
+          wo.push(sw.downcase) if sw =~ /[a-zA-Z]/ 
+        end
+      end
+      
+      wo
+    end
+ + diff --git a/doc/classes/Mirimiri/Document.src/M000023.html b/doc/classes/Mirimiri/Document.src/M000023.html new file mode 100644 index 0000000..136a93e --- /dev/null +++ b/doc/classes/Mirimiri/Document.src/M000023.html @@ -0,0 +1,26 @@ + + + + ngrams (Mirimiri::Document) + + + + +
# File lib/mirimiri/document.rb, line 49
+    def ngrams(n)
+      window       = []
+      ngrams_array = []
+
+      @words.each do |w|
+        window.push(w)
+        if window.size == n
+          ngrams_array.push window.join(" ")
+          window.delete_at(0)
+        end
+      end
+
+      ngrams_array.uniq
+    end
+ + diff --git a/doc/classes/Mirimiri/Document.src/M000024.html b/doc/classes/Mirimiri/Document.src/M000024.html new file mode 100644 index 0000000..df19d9f --- /dev/null +++ b/doc/classes/Mirimiri/Document.src/M000024.html @@ -0,0 +1,18 @@ + + + + count_words (Mirimiri::Document) + + + + +
# File lib/mirimiri/document.rb, line 67
+    def count_words
+      counts = Hash.new { |h,k| h[k] = 0 }
+      @words.each { |w| counts[w] += 1 }
+
+      counts
+    end
+ + diff --git a/doc/classes/Mirimiri/Document.src/M000025.html b/doc/classes/Mirimiri/Document.src/M000025.html new file mode 100644 index 0000000..b745028 --- /dev/null +++ b/doc/classes/Mirimiri/Document.src/M000025.html @@ -0,0 +1,24 @@ + + + + entropy (Mirimiri::Document) + + + + +
# File lib/mirimiri/document.rb, line 81
+    def entropy(s)
+      en = 0.0
+      counts = self.count_words
+
+      s.split.each do |w|
+        p_wi = counts[w].to_f/@words.count.to_f
+        en += p_wi*Math.log2(p_wi)
+      end
+
+      en *= -1
+      en
+    end
+ + diff --git a/doc/classes/Mirimiri/Document.src/M000026.html b/doc/classes/Mirimiri/Document.src/M000026.html new file mode 100644 index 0000000..ab8eab7 --- /dev/null +++ b/doc/classes/Mirimiri/Document.src/M000026.html @@ -0,0 +1,15 @@ + + + + tf (Mirimiri::Document) + + + + +
# File lib/mirimiri/document.rb, line 97
+    def tf(s)
+      self.count_words[s].to_f/@words.size.to_f
+    end
+ + diff --git a/doc/classes/Mirimiri/Document.src/M000027.html b/doc/classes/Mirimiri/Document.src/M000027.html new file mode 100644 index 0000000..a7a5576 --- /dev/null +++ b/doc/classes/Mirimiri/Document.src/M000027.html @@ -0,0 +1,16 @@ + + + + new (Mirimiri::Document) + + + + +
# File lib/mirimiri/document.rb, line 102
+    def initialize(content)
+      @doc_content = content
+      @words = format_words
+    end
+ + diff --git a/doc/classes/Mirimiri/WebDocument.html b/doc/classes/Mirimiri/WebDocument.html new file mode 100644 index 0000000..810a4c9 --- /dev/null +++ b/doc/classes/Mirimiri/WebDocument.html @@ -0,0 +1,209 @@ + + + + Class: Mirimiri::WebDocument [RDoc Documentation] + + + + + + + + + +
+ + + + + + + + + + + + + + + + +
ClassMirimiri::WebDocument
In: + + + + + lib/mirimiri/document.rb + + + + +
+ +
Parent: + + + + Mirimiri::Document + + + +
+
+ + +
+ +
+ +
+

+A WebDocument is a Document with a url. +

+ +
+ +
+ + +
+

Methods

+ +
+ + get_content   + + new   + +
+
+ +
+ + + +
+ + + +
+

Attributes

+ +
+ + + + + + + + + + +
url [R] 
+
+
+ + + + +
+ +

Public Class methods

+ + +
+ + + + +
+ +

+Returns the HTML text from the page of a given url. +

+ +
+
+ + +
+ + + + +
+ +

+WebDocument constructor, the content of the +Document is the HTML page without the tags. +

+ +
+
+ + + +
+ + + + +
+ +
+

[Validate]

+
+ + + diff --git a/doc/classes/Mirimiri/WebDocument.src/M000028.html b/doc/classes/Mirimiri/WebDocument.src/M000028.html new file mode 100644 index 0000000..b2ee9c4 --- /dev/null +++ b/doc/classes/Mirimiri/WebDocument.src/M000028.html @@ -0,0 +1,16 @@ + + + + get_content (Mirimiri::WebDocument) + + + + +
# File lib/mirimiri/document.rb, line 115
+    def self.get_content(url)
+      require 'net/http'
+      Net::HTTP.get(URI.parse(url))
+    end
+ + diff --git a/doc/classes/Mirimiri/WebDocument.src/M000029.html b/doc/classes/Mirimiri/WebDocument.src/M000029.html new file mode 100644 index 0000000..c98eb93 --- /dev/null +++ b/doc/classes/Mirimiri/WebDocument.src/M000029.html @@ -0,0 +1,16 @@ + + + + new (Mirimiri::WebDocument) + + + + +
# File lib/mirimiri/document.rb, line 122
+    def initialize(url)
+      @url = url
+      super WebDocument.get_content(url).strip_javascripts.strip_stylesheets.strip_xml_tags
+    end
+ + diff --git a/doc/classes/Mirimiri/WikipediaPage.html b/doc/classes/Mirimiri/WikipediaPage.html new file mode 100644 index 0000000..83f945a --- /dev/null +++ b/doc/classes/Mirimiri/WikipediaPage.html @@ -0,0 +1,204 @@ + + + + Class: Mirimiri::WikipediaPage [RDoc Documentation] + + + + + + + + + +
+ + + + + + + + + + + + + + + + +
ClassMirimiri::WikipediaPage
In: + + + + + lib/mirimiri/document.rb + + + + +
+ +
Parent: + + + + Mirimiri::WebDocument + + + +
+
+ + +
+ +
+ +
+

+A WikipediaPage is a WebDocument. +

+ +
+ +
+ + +
+

Methods

+ +
+ + get_url   + + search_homepage   + + search_wikipedia_titles   + +
+
+ +
+ + + +
+ + + + + + +
+ +

Public Class methods

+ + +
+ + + + +
+ +
+
+ + +
+ + + + +
+ +
+
+ + + + + + +
+ + + + +
+ +
+

[Validate]

+
+ + + diff --git a/doc/classes/Mirimiri/WikipediaPage.src/M000030.html b/doc/classes/Mirimiri/WikipediaPage.src/M000030.html new file mode 100644 index 0000000..9ac3fdc --- /dev/null +++ b/doc/classes/Mirimiri/WikipediaPage.src/M000030.html @@ -0,0 +1,19 @@ + + + + search_wikipedia_titles (Mirimiri::WikipediaPage) + + + + +
# File lib/mirimiri/document.rb, line 135
+    def self.search_wikipedia_titles(name)
+      raise ArgumentError, "Bad encoding", name unless name.isutf8
+
+      res = REXML::Document.new(Net::HTTP.get( URI.parse "http://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=#{URI.escape name}&format=xml" ).toutf8).elements['api/query/search']
+
+      res.collect { |e| e.attributes['title'] } unless res.nil?
+    end
+ + diff --git a/doc/classes/Mirimiri/WikipediaPage.src/M000031.html b/doc/classes/Mirimiri/WikipediaPage.src/M000031.html new file mode 100644 index 0000000..b5078fb --- /dev/null +++ b/doc/classes/Mirimiri/WikipediaPage.src/M000031.html @@ -0,0 +1,19 @@ + + + + get_url (Mirimiri::WikipediaPage) + + + + +
# File lib/mirimiri/document.rb, line 143
+    def self.get_url(name)
+      raise ArgumentError, "Bad encoding", name unless name.isutf8
+
+      atts = REXML::Document.new(Net::HTTP.get( URI.parse "http://en.wikipedia.org/w/api.php?action=query&titles=#{URI.escape name}&inprop=url&prop=info&format=xml" ).toutf8).elements['api/query/pages/page'].attributes
+
+      atts['fullurl'] if atts['missing'].nil?
+    end
+ + diff --git a/doc/classes/Mirimiri/WikipediaPage.src/M000032.html b/doc/classes/Mirimiri/WikipediaPage.src/M000032.html new file mode 100644 index 0000000..f369de0 --- /dev/null +++ b/doc/classes/Mirimiri/WikipediaPage.src/M000032.html @@ -0,0 +1,17 @@ + + + + search_homepage (Mirimiri::WikipediaPage) + + + + +
# File lib/mirimiri/document.rb, line 151
+    def self.search_homepage(name)
+      title = WikipediaPage.search_wikipedia_titles name
+
+      WikipediaPage.new(WikipediaPage.get_url title[0]) unless title.nil? || title.empty?
+    end
+ + diff --git a/doc/files/lib/mirimiri/corpus_rb.html b/doc/files/lib/mirimiri/corpus_rb.html new file mode 100644 index 0000000..6558302 --- /dev/null +++ b/doc/files/lib/mirimiri/corpus_rb.html @@ -0,0 +1,90 @@ + + + + File: corpus.rb [RDoc Documentation] + + + + + + + + + +
+

corpus.rb

+ + + + + + + + + +
Path:lib/mirimiri/corpus.rb + +
Last Update:2010-12-20 10:35:26 +0100
+
+ + +
+ +
+ +
+ + +
+ + + +
+ + + + + + + + + +
+ +
+

[Validate]

+
+ + + diff --git a/doc/files/lib/mirimiri/document_rb.html b/doc/files/lib/mirimiri/document_rb.html new file mode 100644 index 0000000..20bf00b --- /dev/null +++ b/doc/files/lib/mirimiri/document_rb.html @@ -0,0 +1,106 @@ + + + + File: document.rb [RDoc Documentation] + + + + + + + + + +
+

document.rb

+ + + + + + + + + +
Path:lib/mirimiri/document.rb + +
Last Update:2010-12-20 10:36:07 +0100
+
+ + +
+ +
+ +
+

Required files

+ +
+ + net/http   + + rexml/document   + + net/http   + + kconv   + +
+
+ +
+ + +
+ + + +
+ + + + + + + + + +
+ +
+

[Validate]

+
+ + + diff --git a/doc/files/lib/mirimiri/query_rb.html b/doc/files/lib/mirimiri/query_rb.html new file mode 100644 index 0000000..be9b0d0 --- /dev/null +++ b/doc/files/lib/mirimiri/query_rb.html @@ -0,0 +1,90 @@ + + + + File: query.rb [RDoc Documentation] + + + + + + + + + +
+

query.rb

+ + + + + + + + + +
Path:lib/mirimiri/query.rb + +
Last Update:2010-12-20 10:36:27 +0100
+
+ + +
+ +
+ +
+ + +
+ + + +
+ + + + + + + + + +
+ +
+

[Validate]

+
+ + + diff --git a/doc/files/lib/mirimiri/regexp_rb.html b/doc/files/lib/mirimiri/regexp_rb.html new file mode 100644 index 0000000..69ca171 --- /dev/null +++ b/doc/files/lib/mirimiri/regexp_rb.html @@ -0,0 +1,90 @@ + + + + File: regexp.rb [RDoc Documentation] + + + + + + + + + +
+

regexp.rb

+ + + + + + + + + +
Path:lib/mirimiri/regexp.rb + +
Last Update:2010-12-20 10:36:42 +0100
+
+ + +
+ +
+ +
+ + +
+ + + +
+ + + + + + + + + +
+ +
+

[Validate]

+
+ + + diff --git a/doc/files/lib/mirimiri/string_rb.html b/doc/files/lib/mirimiri/string_rb.html new file mode 100644 index 0000000..22fcae3 --- /dev/null +++ b/doc/files/lib/mirimiri/string_rb.html @@ -0,0 +1,109 @@ + + + + File: string.rb [RDoc Documentation] + + + + + + + + + +
+

string.rb

+ + + + + + + + + +
Path:lib/mirimiri/string.rb + +
Last Update:2010-12-20 10:37:16 +0100
+
+ + +
+ +
+ +
+

+General module +

+ +
+ +
+

Required files

+ +
+ + cgi   + + kconv   + +
+
+ +
+ + +
+ + + +
+ + + + + + + + + +
+ +
+

[Validate]

+
+ + + diff --git a/doc/files/lib/mirimiri/ttagger_rb.html b/doc/files/lib/mirimiri/ttagger_rb.html new file mode 100644 index 0000000..a7788d2 --- /dev/null +++ b/doc/files/lib/mirimiri/ttagger_rb.html @@ -0,0 +1,90 @@ + + + + File: ttagger.rb [RDoc Documentation] + + + + + + + + + +
+

ttagger.rb

+ + + + + + + + + +
Path:lib/mirimiri/ttagger.rb + +
Last Update:2010-12-20 10:37:32 +0100
+
+ + +
+ +
+ +
+ + +
+ + + +
+ + + + + + + + + +
+ +
+

[Validate]

+
+ + + diff --git a/doc/files/lib/mirimiri_rb.html b/doc/files/lib/mirimiri_rb.html new file mode 100644 index 0000000..5fc39e2 --- /dev/null +++ b/doc/files/lib/mirimiri_rb.html @@ -0,0 +1,110 @@ + + + + File: mirimiri.rb [RDoc Documentation] + + + + + + + + + +
+

mirimiri.rb

+ + + + + + + + + +
Path:lib/mirimiri.rb + +
Last Update:2010-12-20 10:33:51 +0100
+
+ + +
+ +
+ +
+

Required files

+ +
+ + rir/document   + + rir/string   + + rir/query   + + rir/corpus   + + rir/regexp   + + rir/ttagger   + +
+
+ +
+ + +
+ + + +
+ + + + + + + + + +
+ +
+

[Validate]

+
+ + + diff --git a/lib/mirimiri.rb b/lib/mirimiri.rb new file mode 100644 index 0000000..05141df --- /dev/null +++ b/lib/mirimiri.rb @@ -0,0 +1,8 @@ +#!/usr/bin/env ruby + +require 'mirimiri/document' +require 'mirimiri/string' +require 'mirimiri/query' +require 'mirimiri/corpus' +require 'mirimiri/regexp' +require 'mirimiri/ttagger' diff --git a/lib/mirimiri/corpus.rb b/lib/mirimiri/corpus.rb new file mode 100644 index 0000000..632f7fc --- /dev/null +++ b/lib/mirimiri/corpus.rb @@ -0,0 +1,38 @@ +#!/usr/bin/env ruby + +#-- +# This file is a part of the mirimiri library +# +# Copyright (C) 2010-2011 Romain Deveaud +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +#++ + +class Corpus + attr_accessor :path + + def initialize(path) + @path = path.chomp "/" + end + + # Recursively outputs all files in +self.path+. + # WARNING ! This function may take a lot of time if many + # files are in subdirectories. + # + # c = Corpus.new "my/path" + # c.files # => ["README.txt", "lib/code.rb"] + def files + Dir["#{@path}/**/*.*"] + end +end diff --git a/lib/mirimiri/document.rb b/lib/mirimiri/document.rb new file mode 100644 index 0000000..20752c3 --- /dev/null +++ b/lib/mirimiri/document.rb @@ -0,0 +1,163 @@ +#!/usr/bin/env ruby + +#-- +# This file is a part of the mirimiri library +# +# Copyright (C) 2010-2011 Romain Deveaud +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +#++ + + +# General module +module Mirimiri + + # A Document is a bag of words and is constructed from a string. + class Document + attr_reader :words, :doc_content + + # Any non-word characters are removed from the words (see http://perldoc.perl.org/perlre.html + # and the \\W special escape). + # + # Protected function, only meant to by called at the initialization. + def format_words + wo = [] + + @doc_content.split.each do |w| + w.split(/\W/).each do |sw| + wo.push(sw.downcase) if sw =~ /[a-zA-Z]/ + end + end + + wo + end + + # Returns an Array containing the +n+-grams (words) from the current Document. + # + # ngrams(2) #=> ["the free", "free encyclopedia", "encyclopedia var", "var skin", ...] + def ngrams(n) + window = [] + ngrams_array = [] + + @words.each do |w| + window.push(w) + if window.size == n + ngrams_array.push window.join(" ") + window.delete_at(0) + end + end + + ngrams_array.uniq + end + + # Returns a Hash containing the words and their associated counts in the current Document. + # + # count_words #=> { "guitar"=>1, "bass"=>3, "album"=>20, ... } + def count_words + counts = Hash.new { |h,k| h[k] = 0 } + @words.each { |w| counts[w] += 1 } + + counts + end + + # Computes the entropy of a given string +s+ inside the document. + # + # If the string parameter is composed of many words (i.e. tokens separated + # by whitespace(s)), it is considered as an ngram. + # + # entropy("guitar") #=> 0.00432114812727959 + # entropy("dillinger escape plan") #=> 0.265862076325102 + def entropy(s) + en = 0.0 + counts = self.count_words + + s.split.each do |w| + p_wi = counts[w].to_f/@words.count.to_f + en += p_wi*Math.log2(p_wi) + end + + en *= -1 + en + end + + # Computes the term frequency of a given *word* +s+. + # + # tf("guitar") #=> 0.000380372765310004 + def tf(s) + self.count_words[s].to_f/@words.size.to_f + end + + + def initialize(content) + @doc_content = content + @words = format_words + end + + protected :format_words + end + + # A WebDocument is a Document with a +url+. + class WebDocument < Document + attr_reader :url + + # Returns the HTML text from the page of a given +url+. + def self.get_content(url) + require 'net/http' + Net::HTTP.get(URI.parse(url)) + end + + # WebDocument constructor, the content of the Document is the HTML page + # without the tags. + def initialize(url) + @url = url + super WebDocument.get_content(url).strip_javascripts.strip_stylesheets.strip_xml_tags + end + end + + # A WikipediaPage is a WebDocument. + class WikipediaPage < WebDocument + require 'rexml/document' + require 'net/http' + require 'kconv' + + + def self.search_wikipedia_titles(name) + raise ArgumentError, "Bad encoding", name unless name.isutf8 + + res = REXML::Document.new(Net::HTTP.get( URI.parse "http://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=#{URI.escape name}&format=xml" ).toutf8).elements['api/query/search'] + + res.collect { |e| e.attributes['title'] } unless res.nil? + end + + def self.get_url(name) + raise ArgumentError, "Bad encoding", name unless name.isutf8 + + atts = REXML::Document.new(Net::HTTP.get( URI.parse "http://en.wikipedia.org/w/api.php?action=query&titles=#{URI.escape name}&inprop=url&prop=info&format=xml" ).toutf8).elements['api/query/pages/page'].attributes + + atts['fullurl'] if atts['missing'].nil? + end + + def self.search_homepage(name) + title = WikipediaPage.search_wikipedia_titles name + + WikipediaPage.new(WikipediaPage.get_url title[0]) unless title.nil? || title.empty? + end + +# def initialize(name) +# title = WikipediaPage.search_wikipedia_titles name +# raise ArgumentError, "No page found" if title.empty? +# super WikipediaPage.get_url title[0] +# end + end +end diff --git a/lib/mirimiri/query.rb b/lib/mirimiri/query.rb new file mode 100644 index 0000000..28d6653 --- /dev/null +++ b/lib/mirimiri/query.rb @@ -0,0 +1,85 @@ +#!/usr/bin/env ruby + +#-- +# This file is a part of the mirimiri library +# +# Copyright (C) 2010-2011 Romain Deveaud +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +#++ + +class Query +end + +module Indri + + class Parameters + attr_accessor :index_path, :memory, :count, :offset, :run_id, :print_query, :print_docs, :rule, :baseline + + def initialize(corpus,mem="1g",count="1000",offset="1",run_id="default",print_query=false,print_docs=false) + @index_path = corpus + @memory = mem + @count = count + @offset = offset + @run_id = run_id + @print_query = print_query ? "true" : "false" + @print_docs = print_docs ? "true" : "false" + end + + def to_s + h = "\n" + h += "#{@memory}\n" + h += "#{@index_path}\n" + h += "#{@count}\n" + unless @baseline.nil? + h += "#{@baseline}\n" + else + h += "#{@rule}\n" + end + h += "#{@offset}\n" + h += "#{@run_id}\n" + h += "#{@print_query}\n" + h += "#{@print_docs}\n" + + h + end + end + + class IndriQuery < Query + attr_accessor :id, :query, :params, :rule + + def initialize(id,query,params) + @params = params + # Here we set the default retrieval model as Language Modeling + # with a Dirichlet smoothing at 2500. + # TODO: maybe a Rule class... + @params.rule = 'method:dirichlet,mu:2500' if @params.rule.nil? + + @id = id + @query = query + end + + def to_s + h = @params.to_s + h += "\n" + h += "#{@id}\n" + h += "#{@query}\n" + h += "\n" + h += "" + + h + end + end + +end diff --git a/lib/mirimiri/regexp.rb b/lib/mirimiri/regexp.rb new file mode 100644 index 0000000..e2d7851 --- /dev/null +++ b/lib/mirimiri/regexp.rb @@ -0,0 +1,28 @@ +#!/usr/bin/env ruby + +#-- +# This file is a part of the mirimiri library +# +# Copyright (C) 2010-2011 Romain Deveaud +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +#++ + +class Regexp + + def negated + /^((?!#{self}).)*$/ + end + +end diff --git a/lib/mirimiri/string.rb b/lib/mirimiri/string.rb new file mode 100644 index 0000000..11d73d9 --- /dev/null +++ b/lib/mirimiri/string.rb @@ -0,0 +1,174 @@ +#!/usr/bin/env ruby + +#-- +# This file is a part of the mirimiri library +# +# Copyright (C) 2010-2011 Romain Deveaud +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +#++ + +module Mirimiri + + # These are the default stopwords provided by Lemur. + Stoplist = [ + "a", "anything", "anyway", "anywhere", "apart", "are", "around", "as", "at", "av", + "be", "became", "because", "become", "becomes", "becoming", "been", "before", "beforehand", + "behind", "being", "below", "beside", "besides", "between", "beyond", "both", "but", "by", + "can", "cannot", "canst", "certain", "cf", "choose", "contrariwise", "cos", "could", "cu", + "day", "do", "does", "doesn't", "doing", "dost", "doth", "double", "down", "dual", "during", + "each", "either", "else", "elsewhere", "enough", "et", "etc", "even", "ever", "every", + "everybody", "everyone", "everything", "everywhere", "except", "excepted", "excepting", + "exception", "exclude", "excluding", "exclusive", "far", "farther", "farthest", "few", "ff", + "first", "for", "formerly", "forth", "forward", "from", "front", "further", "furthermore", + "furthest", "get", "go", "had", "halves", "hardly", "has", "hast", "hath", "have", "he", + "hence", "henceforth", "her", "here", "hereabouts", "hereafter", "hereby", "herein", "hereto", + "hereupon", "hers", "herself", "him", "himself", "hindmost", "his", "hither", "hitherto", + "how", "however", "howsoever", "i", "ie", "if", "in", "inasmuch", "inc", "include", + "included", "including", "indeed", "indoors", "inside", "insomuch", "instead", "into", + "inward", "inwards", "is", "it", "its", "itself", "just", "kind", "kg", "km", "last", + "latter", "latterly", "less", "lest", "let", "like", "little", "ltd", "many", "may", "maybe", + "me", "meantime", "meanwhile", "might", "moreover", "most", "mostly", "more", "mr", "mrs", + "ms", "much", "must", "my", "myself", "namely", "need", "neither", "never", "nevertheless", + "next", "no", "nobody", "none", "nonetheless", "noone", "nope", "nor", "not", "nothing", + "notwithstanding", "now", "nowadays", "nowhere", "of", "off", "often", "ok", "on", "once", + "one", "only", "onto", "or", "other", "others", "otherwise", "ought", "our", "ours", + "ourselves", "out", "outside", "over", "own", "per", "perhaps", "plenty", "provide", "quite", + "rather", "really", "round", "said", "sake", "same", "sang", "save", "saw", "see", "seeing", + "seem", "seemed", "seeming", "seems", "seen", "seldom", "selves", "sent", "several", "shalt", + "she", "should", "shown", "sideways", "since", "slept", "slew", "slung", "slunk", "smote", + "so", "some", "somebody", "somehow", "someone", "something", "sometime", "sometimes", + "somewhat", "somewhere", "spake", "spat", "spoke", "spoken", "sprang", "sprung", "stave", + "staves", "still", "such", "supposing", "than", "that", "the", "thee", "their", "them", + "themselves", "then", "thence", "thenceforth", "there", "thereabout", "thereabouts", + "thereafter", "thereby", "therefore", "therein", "thereof", "thereon", "thereto", "thereupon", + "these", "they", "this", "those", "thou", "though", "thrice", "through", "throughout", "thru", + "thus", "thy", "thyself", "till", "to", "together", "too", "toward", "towards", "ugh", + "unable", "under", "underneath", "unless", "unlike", "until", "up", "upon", "upward", + "upwards", "us", "use", "used", "using", "very", "via", "vs", "want", "was", "we", "week", + "well", "were", "what", "whatever", "whatsoever", "when", "whence", "whenever", "whensoever", + "where", "whereabouts", "whereafter", "whereas", "whereat", "whereby", "wherefore", + "wherefrom", "wherein", "whereinto", "whereof", "whereon", "wheresoever", "whereto", + "whereunto", "whereupon", "wherever", "wherewith", "whether", "whew", "which", "whichever", + "whichsoever", "while", "whilst", "whither", "who", "whoa", "whoever", "whole", "whom", + "whomever", "whomsoever", "whose", "whosoever", "why", "will", "wilt", "with", "within", + "without", "worse", "worst", "would", "wow", "ye", "yet", "year", "yippee", "you", "your", + "yours", "yourself", "yourselves" + ] + + +end + +# Extention of the standard class String with useful function. +class String + include Mirimiri + + # Returns +true+ if +self+ belongs to Rir::Stoplist, +false+ otherwise. + def is_stopword? + Stoplist.include?(self.downcase) + end + + # Do not use. + # TODO: rewamp. find why this function is here. + def remove_special_characters + self.split.collect { |w| w.gsub(/\W/,' ').split.collect { |w| w.gsub(/\W/,' ').strip.sub(/\A.\z/, '')}.join(' ').strip.sub(/\A.\z/, '')}.join(' ') + end + + # Removes all XML-like tags from +self+. + # + # s = "test" + # s.strip_xml_tags! + # s #=> "test" + def strip_xml_tags! + replace strip_with_pattern /<\/?[^>]*>/ + end + + # Removes all XML-like tags from +self+. + # + # s = "test" + # s.strip_xml_tags #=> "test" + # s #=> "test" + def strip_xml_tags + dup.strip_xml_tags! + end + + # Removes all Javascript sources from +self+. + # + # s = " + # + # test" + # s.strip_javascripts! + # s #=> "test" + def strip_javascripts! + replace strip_with_pattern / + # + # test" + # s.strip_javascripts #=> "test" + def strip_javascripts + dup.strip_javascripts! + end + + def strip_stylesheets! + # TODO: rewamp. dunno what is it. + replace strip_with_pattern /