Skip to content

Instantly share code, notes, and snippets.

@huseyin
Last active April 28, 2016 22:19
Show Gist options
  • Save huseyin/104cfc7664fd21169168842d69738e52 to your computer and use it in GitHub Desktop.
Save huseyin/104cfc7664fd21169168842d69738e52 to your computer and use it in GitHub Desktop.
NYP Ödev-6
#!/usr/bin/env ruby
# encoding: utf-8
require 'stemmify'
require 'forwardable'
require 'command_line_reporter'
STP_FILE = File.join File.dirname(__FILE__), 'stp.txt'
TEXT_FILE = File.join File.dirname(__FILE__), 'metin.txt'
class Süzgeç
include CommandLineReporter
extend Forwardable
attr_accessor :file, :words
def_delegators :@words, :uniq, :size, :<<
def initialize(file)
@file = file
end
def to_a
contents = File.read file
# Okunan dosya içeriğini '\n' karakterine göre parçala ve kelimelerine
# ayırır. Oluşturulan her bir kelimenin yalın karşılığı varsa alır
# yoksa kelimeyi döner.
self.words = contents.split(/\\n/).collect { |f| f.split ' ' }.flatten
.map { |w| nosuffix w.downcase.stem || w }
end
# Argüman verilen dosyadaki satır sıralı kelimeleri hali hazırdaki
# listeden atar.
def invert(to)
wordlist = File.readlines(to).map { |w| w.chomp }
self.words = to_a - wordlist
end
def report(limit=nil)
wc = {}
# Her bir kelimeyi geçtiği toplam kelime sayısı ile eşleştir.
uniq.each { |w| wc[w] = words.count w }
# Eğer limit değeri girilmemişse tüm raporu ekrana bas.
wc.sort_by { |k, v| v }.each do |word, count|
if limit
tableize(word, count) if limit >= count
else
tableize word, count
end
end
end
private
def nosuffix(word)
word.gsub /[[:punct:]]+$/, ''
end
def tableize(*cols)
table :border => true do
row do
cols.each { |c| column c, :align => 'center' }
end
end
end
end
def main
süzgeç = Süzgeç.new TEXT_FILE
süzgeç.invert STP_FILE
süzgeç.report
# Ufak bir ayrı demo.
puts "Toplam kelime sayısı: #{süzgeç.size}"
puts '"liked" kelimesi eklendi'
süzgeç << 'liked'
puts "Toplam kelime sayısı: #{süzgeç.size}"
süzgeç.report 3
end
main if $PROGRAM_NAME == $0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment