Skip to content

Instantly share code, notes, and snippets.

@thomasjslone
Created April 14, 2017 10:58
Show Gist options
  • Save thomasjslone/176c5b592da45dda44f5b7dbd6e2c921 to your computer and use it in GitHub Desktop.
Save thomasjslone/176c5b592da45dda44f5b7dbd6e2c921 to your computer and use it in GitHub Desktop.
## Name: Dictionary
## Version: 1.1.1
## Release: 2017-4-9
## Manufacturer: Thomas Tech
class Dictionary
def initialize
@dex = []
@data = [] # there can be arrays stored here containing descriptions, part of speech and any aditional entry info, add save/load support later
if File.exist?(Dir.getwd.to_s + "/dictionary.dex") ; load_dex ; end
end
def add(string)
if string.to_s.length > 0
new_words = 0
string = string.tr("~`!@#$%^&*()_+-={[}]|:;\"',<.>/?"," ").to_s ; string = string.split("\n").join(" ") ; string = string.to_s.split("\\").join(" ")
string.to_s.split(" ").each { |word| ; unless @dex.include?(word.downcase) ; if word.ascii_only? ; @dex << word.downcase.to_s ; new_words += 1 ; end ; end }
if new_words > 0 ; @dex = @dex.sort ; save_dex end
new_words
else
0
end
end
def get(arg)
if arg.is_a? Range or arg.is_a? Integer
@dex[arg]
elsif arg.to_s == "true"
@dex
elsif arg.is_a? String and arg.split(",").length > 1
rt = []
arg.split(",").each do |i|
rt << @dex[i.to_i]
end
if rt.length > 0
rt
else
false
end
else
false
end
end
def del(arg)
if arg.is_a? Range or arg.is_a? Integer
ol = @dex.length
@dex.delete_at(arg)
ol - @dex.length
elsif arg.is_a? String and @dex.include?(arg.downcase)
@dex.delete(arg.downcase) ; 1
elsif arg.is_a? String and arg.include?(",") and arg.split(",").length > 1
dc = 0
arg.split(",").each do |w|
if @dex.include?(w.downcase)
@dex.delete(w.downcase) ; dc += 1
end
end
dc
else
false
end
end
def view(arg)
v = get(arg)
if v.is_a? Array
l = [] ; sv = arg.to_s.split("..")[0].to_i
v.each do |w|
l << @dex.index(w).to_s + " : " + w.to_s
end
puts l.join("\n").to_s
elsif v.is_a? String
puts arg.to_s + " : " + v.to_s
elsif v == false
"You entered an invalid value, enter an integer or range."
end
end
def search(term)
found = []
if @dex.length > 0
@dex.each do |w|
if w.include?(term.to_s.downcase)
if w.length > term.to_s.length
nw = w.split(term).join(term.upcase).to_s
end
found << @dex.index(w).to_s + " : " + nw.to_s
end
end
if found.length > 0
found.sort.join("\n").to_s
else
"No instances of term found."
end
else
"Cannot search because the dictionary is empty."
end
end
def digest_file(path)
if File.exist?(path)
begin
fi = File.open(path.to_s,"r") ; res = add(fi.read.to_s) ; fi.close ; res
rescue
"Unable to acess file."
end
else
"No such filepath: " + path.to_s
end
end
def digest_url(url)
begin
if url.include?("http://") == false ; url = "http://" + url.to_s ; end
require 'open-uri'
fi = open(url) ; res = add(fi.read.to_s) ; fi.close ; res
rescue
"No such website or network location."
end
end
def digest_directory(dir)
if File.directory?(dir)
m = map_directory(dir)[1]
gt = 0
diff = @dex.length.to_i
m.each do |d|
begin
ml = m.length.to_s
fi = File.open(d.to_s,"r") ; add(fi.read.to_s).to_i ; fi.close ; gt += 1
p = gt.to_f / ml.to_f * 100
puts "Progress: " + gt.to_s + "/" + ml.to_s + " %" + p.to_s[0..8].to_s
rescue
end
diff = @dex.length.to_i - diff.to_i
"Process completed, " + diff.to_s + " words added to dex."
end
else
"Directory does not exist: " + dir.to_s
end
end
def save_dex
begin
fi = File.open(Dir.getwd.to_s + "/dictionary.dex","w") ; fi.write(@dex.to_s) ; fi.close ; true
rescue
false
end
end
def load_dex
begin
fi = File.open(Dir.getwd.to_s + "/dictionary.dex","r") ; dex = fi.read.to_s ; fi.close
begin
eval("@dex = " + dex.to_s) ; true
rescue
false
end
rescue
false
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment