2014-11-20 2 views
0

프로젝트 중간에 나는 비교적 큰 텍스트 파일, 즉 5 백만 라인의 단어를 걸러 내야합니다. Ruby 2.0.0을 사용하여 Windows 환경에서 정상적으로 작동하는 빠른 루비 스크립트를 작성했습니다. 이 프로그램은 텍스트 파일을 받아 블랙리스트 단어를 필터링하는 다른 텍스트 파일을 출력합니다. AWS에서 unix 인스턴스로 이식 할 때 스크립트는 출력 파일에 텍스트를 쓰지 않았습니다. Ruby/Unix 환경에서 Ruby/Windows 설정에서 할 필요가없는 파일 IO가 필요한 특별한 것이 있습니까?유닉스 인스턴스의 Ruby 파일 출력

내 코드는 다음과 같습니다 :

# Ruby Script to remove stop words make sure all included 
# words are in the frequency list 
# Date 11/19/14 (PTC) 

# Open linux.words and place in an array 
wordList = [] 
lineList = [] 
stopWords = [] 
goodLineList = [] 

File.open('freqList.txt') do |f| 
    f.lines.each do |line| 
    wordList << line.downcase.gsub(/\n/, "") 
    # puts line.downcase 
    end 
end 

File.open('stop_words.txt') do |f| 
    f.lines.each do |line| 
    stopWords << line.downcase 
    # puts line.downcase 
    end 
end 

# read in sample to object 
File.open('temp.txt') do |f| 
    f.lines.each do |line| 
    lineList << line.downcase 
    end 
end 


# for each line, split then if only save actual words to new object 

lineList.each do |line| 
    s = "" 
    splitLine = line.split(" ") 
    splitLine.each do |token| 
    if wordList.include? token and !stopWords.include? token 
     s = s + token + " " 
    end 
    end 

    goodLineList << s 

end # line in list 

# Write object to new file 
cleanedFile = File.open("cleanData.txt", "w") 
count = 1 
goodLineList.each do |line| 
    cleanedFile.puts line 
    puts "Line: " + count.to_s + line 
    count = count + 1`enter code here` 
end 
cleanedFile.close 

puts "finished" 
+0

Windows (DOS)의 줄 끝이 UNIX에서와 다르므로 문제가 될 수 있습니다. – fedorqui

+0

답장을 보내 주셔서 감사합니다. "cleanFile.puts line"을 대체하기위한 제안 사항. 파일에 밀리는 것은 모두 하나의 NULL 바이트입니다. – ptcesq

+0

Ruby에 익숙하지 않아서 (비록 내 소원 목록에 있지만) 모르겠다. 필자가 제안하는 것은 또한'-f'와 함께'grep'을 검사하는 것입니다. 이것은 파일에 패턴이 있고 그 파일을 grep하려고하는 간단한 경우에 사용할 수 있습니다. 예를 들어 [파일에서 그렙 패턴 읽기] (http://unix.stackexchange.com/questions/83260/reading-grep-patterns-from-a-file)를 참조하십시오. – fedorqui

답변

0

사용 chomp은 운영 시스템에서 휴대용 :

f.lines.each do |line| 
    wordList << line.chomp.downcase 
end 

또는 :

wordList = f.lines.map {|line| line.chomp.downcase} 

http://ruby-doc.org/core-2.0/String.html

귀하의 실제 문제는 여기에 있습니다

count = count + 1`enter code here` 

명령의``원인 실행 "여기에 코드를 입력"및 enter은 내가 아는 한, 유효한 UNIX 명령이 아닙니다.