1

割り当ての最初のタスクを完了しようとしています。

通常のメール 5 通と前払い詐欺メール (別名スパム) 5 通を受け取ります。それらをすべてテキスト ファイルに変換し、それぞれを単語の配列に変換します (ここでは分割が役立つ場合があります)。次に、一連の正規表現を使用して単語の配列を検索し、キーワードを探して、どのファイルがスパムかどうかを分類します。より凝ったものにしたい場合は、各アレイに 10 点満点のスパム スコアを与えることができます。

  1. HTML ページを開いてファイルを読み込みます。
  2. ファイルからスクリプト、リンクなどを取り除きます。
  3. ボディ・パラを単体で持つ。
  4. テキストファイル(file2)を開き、それに書き込みます(UTF-8)。
  5. HTML ドキュメント (ファイル 1) からコンテンツを渡します。
  6. 次に、テキスト ファイル (file2) の単語を配列に入れ、後で分割します。
  7. スパムと見なされる単語を見つける配列を調べ、電子メールがスパムかどうかを示すメッセージを画面に出力します。

これが私のコードです:

require 'nokogiri'
file = File.open("EMAILS/REG/Membership.htm", "r")
doc = Nokogiri::HTML(file)
#What ever is passed from elements to the newFile is being put into the new array however the euro sign doesn't appear correctly
elements = doc.xpath("/html/body//p").text
#puts elements

newFile = File.open("test1.txt", "w")
newFile.write(elements)
newFile.close()


#I want to open the file again and print the lines to the screen
#
array_of_words = {}
puts "\n\tRetrieving test1.txt...\n\n"
File.open("test1.txt", "r:UTF-8").each_line do |line|
    words = line.split(' ')
    words.each do |word|
        puts "#{word}"
        #array_of_words[word] = gets.chomp.split(' ')
    end
end

編集済み:ここでファイルを編集しましたが、配列内のユーロ記号の UTF-8 エンコーディングを取得できません (画像を参照)。

require 'nokogiri'

doc = Nokogiri::HTML(File.open("EMAILS/REG/Membership.htm", "r:UTF-8"))

#What ever is passed from elements to the newFile is being put into the new 
#array however the euro sign doesn't appear correctly
elements = doc.xpath("//p").text
#puts elements

File.write("test1.txt", elements)

puts "\n\tRetrieving test1.txt...\n\n"

#I want to open the file again and print the lines to the screen
#
word_array = Array.new
File.read("test1.txt").each_line do |line|
    line.split(' ').each do |word|
        puts "#{word}"
        word_array << word
    end
end
4

2 に答える 2