<commit_before>module Anemon
  class Scrapper
    require 'anemone'
    require 'nokogiri'
    require 'open-uri'

    def scrap(url)

      filename = url.gsub("http://", "").split(".").join("_").strip + ".txt"

      # only scrap pages you havent scrapped yet
      if File.file?(filename)
        puts "This page has already been scrapped"
      else
        puts "scrapping #{url} for data..."
        data = []
        crawl(url).each do |urli|
          html_data = open(urli).read
          nokogiri_object = Nokogiri::HTML(html_data)
          elements = nokogiri_object.xpath("//p")

          elements.each do |element|
            data = element.text
          end
        end
        write_to_file(url, data)
        store_visited_link(url)
        # in ruby the last statement in a method is always the returned value
        # of the particular method
        {:url => url, :content => data }
      end
    end

    def compare(filename ,file_name)
      scrapdata = File.readlines(filename)
      uploaded_data = File.readlines(file_name)

      unless (scrapdata && uploaded_data).empty? 
        Puts "Plagiarism detected"
      else
         scrap(url)
      end
    end

    private

    def crawl(url)
      puts "crawling the web..."<commit_msg>Fix code error and add a few comments<commit_after>module Anemon
  class Scrapper
    require 'anemone'
    require 'nokogiri'
    require 'open-uri'

    def scrap(url)

      filename = url.gsub("http://", "").split(".").join("_").strip + ".txt"

      # only scrap pages you havent scrapped yet
      if File.file?(filename)
        puts "This page has already been scrapped"
      else
        puts "scrapping #{url} for data..."
        data = []
        crawl(url).each do |urli|
          html_data = open(urli).read
          nokogiri_object = Nokogiri::HTML(html_data)
          elements = nokogiri_object.xpath("//p")

          elements.each do |element|
            data = element.text
          end
        end
        write_to_file(url, data)
        store_visited_link(url)
        # in ruby the last statement in a method is always the returned value
        # of the particular method
        {:url => url, :content => data }
      end
    end

    def compare(filename, file_name)
      # user = User.first
      # doc = User.docs.first
      # path = doc.attachment.path
      # Plag.compare('tribiantech_net.txt', path)
      scrapdata = File.readlines(filename)
      uploaded_data = File.readlines(file_name)

      unless (scrapdata && uploaded_data).empty?
        puts "Plagiarism detected"
      else
         scrap(url)
      end
    end

    private

    def crawl(url)
      puts "crawling the web..."
{'fraction_non_alphanumeric': 0.10274480712166172, 'fraction_numerical': 0.0, 'mean_word_length': 2.026936026936027, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1299114', 'n_tokens_mistral': 829, 'n_tokens_neox': 818, 'n_words': 224}