2017-12-10 13 views
0

나는 Enron 전자 메일의 모든 본문을 하나의 파일에 추가하려고하므로 Stop 단어를 제거하고 NLTK로 문장으로 분할하여이 전자 메일의 텍스트를 처리 할 수 ​​있습니다. 전달 된 메시지와 회신 한 메시지에 문제가 있습니다. 어떻게 치료할 지 모르겠습니다. Well, with the photographer and the band, I would say we've pretty much outdone our budget! Here's the information on the photographer. I have a feeling for some of the major packages we could negotiate at least a couple of hours at the rehearsal dinner. I have no idea how much this normally costs, but he isn't cheap!---------------------- Forwarded by Elizabeth Lay/HOU/AZURIX on 09/13/99 07:34 PM [email protected] on 09/13/99 05:37:37 PMPlease respond to [email protected] To: Elizabeth Lay/HOU/[email protected]: Subject: Denis Reggie Wedding PhotographyHello Elizabeth:Congratulations on your upcoming marriage! I am Ashley Collins, Mr.Reggie's Coordinator. Linda Kessler forwarded your e.mail address to me sothat I may provide you with information on photography coverage for Mr.Reggie's wedding photography. 그래서 결과는 전혀 순수 텍스트되지 않습니다Enron 전자 메일 본문에서 "전달 된 메시지"제목 및 원하지 않는 내용을 지우는 방법은 무엇입니까?

import os, email, sys, re,nltk, pprint 
    from email.parser import Parser 

    rootdir = '/Users/art/Desktop/maildir/lay-k/elizabeth' 
    #function that appends all the body parts of Emails 
    def email_analyse(inputfile, email_body): 
     with open(inputfile, "r") as f: 
     data = f.read() 

     email = Parser().parsestr(data) 

     email_body.append(email.get_payload()) 
    #end of function 
    #defining a list that will contain bodies 
    email_body = [] 
    #call the function email_analyse for every function in directory 
    for directory, subdirectory, filenames in os.walk(rootdir): 
     for filename in filenames: 
      email_analyse(os.path.join(directory, filename), email_body) 
    #the stage where I clean the emails 

    with open("email_body.txt", "w") as f: 
     for val in email_body: 
      if(val): 
       val = val.replace("\n", "") 
       val = val.replace("=01", "") 
       #for some reason I had many of ==20 and =01 in my text 
       val = val.replace("==20", "") 
       f.write(val) 
       f.write("\n") 

이 부분의 출력은 다음과 같습니다 이 지금까지 내 코드입니다. 그것을 올바르게하는 방법에 대한 아이디어?

답변

1

전달 된 텍스트와 회신 텍스트를 구문 분석하려면 형식이 코퍼스 전체에서 일관성을 유지해야하기 때문에 정규식을보고 싶을 수 있습니다.

-{4,}(.*)(\d{2}:\d{2}:\d{2})\s*(PM|AM) 

형식 XX에 네 개 이상의 하이픈과 시간 사이의 모든 내용과 일치합니다 : XX : XX PM을

는 전달 된 텍스트를 삭제하려면, 당신은이 같은 정규 표현식을 사용할 수 있습니다. 3 개의 대시를 함께 사용하면 아마 잘 작동합니다. 이메일 본문에 하이픈과 em 대시를 일치시키지 않기를 바랍니다. 이 정규식 놀러와이 링크에에 일치 및 제목 헤더에 대한 자신을 작성할 수 있습니다 https://regex101.com/r/VGG4bu/1/

또한 파이썬에서 정규 표현식에 대해 이야기 NLTK 책의 3.4 절에서 볼 수

: http://www.nltk.org/book/ch03.html

행운을 빈다! 이것은 흥미로운 프로젝트처럼 들립니다.