0

この時点で2つのことを行う必要がありますが、あなたの助けが必要です。

  1. データをクリーンアップするためのベストプラクティス-余分なタグと「>>>>>>>」、およびその他の意味のない通信フローサムとジェットサムをプログラムで削除します
  2. クリーンアップしたら、djangoとsqliteでうまく機能するようにパックするにはどうすればよいですか?
    • 日付、人物、件名、単語に基づいてcsvに変換し、データベース内のデータクラスに入力しますか?

さて、データベースに入る前に、並べ替えを並べ替えてデータをきれいに表示できるようにしたいと思います。データベースにデータを入れた経験はほとんどありません。最も近いのは、XML、csv、JSONからの作業です。

ランキングごとにngramを取得する必要があります。たとえば、特定の単語が1人の一連の電子メールに何回表示されるかなどです。私は、人々が主題などについて私に話している方法の流れを知ることに近づこうとしています。ジョン・クレインバーグの彼自身の電子メールを分析する作品の非常に初歩的なバージョンです。

優しく、ラフに、しかし助けてください:)!

>私の出力は現在次のようになっています::1、'each':1、'Me':1、'IN!\ r \ n \ r \ n2012/1/31':1、'calculator。\r \ n> >>>>> \ r \ n >>>>>>':1、'人':1、' = 97MB \ r \ n> \ r \ n>':1、'私たち':2、'書いた:\ r \ n >> >> >> \ r \ n >> >> >>':1、' = \ r \ n書き込み:\ r \ n >> >>> \ r \ n >> >>> >':1、' 2012/1/31':2、' are':1、'31、':5、' = 97MB \ r \ n >> >> \ r \ n >> >>':1 、'1:45':1、'be \ r \ n >> >>>':1、'送信済み':

  import getpass, imaplib, email

# NGramCounter builds a dictionary relating ngrams (as tuples) to the number
# of times that ngram occurs in a text (as integers)
class NGramCounter(object):

  # parameter n is the 'order' (length) of the desired n-gram
  def __init__(self, text):
    self.text = text
    self.ngrams = dict()

    # feed method calls tokenize to break the given string up into units
  def tokenize(self):
    return self.text.split(" ")

  # feed method takes text, tokenizes it, and visits every group of n tokens
  # in turn, adding the group to self.ngrams or incrementing count in same
  def parse(self):

    tokens = self.tokenize()
    #Moves through every individual word in the text, increments counter if already found
    #else sets count to 1
    for word in tokens:
        if word in self.ngrams:
            self.ngrams[word] += 1
        else:
            self.ngrams[word] = 1

  def get_ngrams(self):
    return self.ngrams

#loading profile for login
M = imaplib.IMAP4_SSL('imap.gmail.com')
M.login("EMAIL", "PASS")
M.select()
new = open('liamartinez.txt', 'w')
typ, data = M.search(None, 'FROM', 'SEARCHGOES_HERE') #Gets ALL messages

def get_first_text_part(msg): #where should this be nested? 
    maintype = msg.get_content_maintype()
    if maintype == 'multipart':
        for part in msg.get_payload():
            if part.get_content_maintype() == 'text':
                return part.get_payload()
    elif maintype == 'text':
        return msg.get_payload()

for num in data[0].split(): #Loops through all messages
    typ, data = M.fetch(num, '(RFC822)') #Pulls Message
    msg = email.message_from_string(data[0][2]) #Puts message into easy to use python objects
    _from =  msg['from'] #pull from
    _to = msg['to'] #pull to
    _subject = msg['subject'] #pull subject
    _body = get_first_text_part(msg) #pull body
    if _body:
        ngrams = NGramCounter(_body)
        ngrams.parse()
        _feed = ngrams.get_ngrams()
        # print "\n".join("\t".join(str(_feed) for col in row) for row in tab)
        print _feed
    # print 'Content-Type:',msg.get_content_type()
    #     print _from
    #     print _to
    #     print _subject
    #     print _body
    #    

    new.write(_from)

    print '---------------------------------'

M.close()
M.logout()
4

2 に答える 2

1

メインループに問題はありません。ただし、外部サーバーからすべてのメールを取得する必要があるため、プロセスはやや遅くなります. 私が提案したいのは、クライアント上のすべてのメッセージを一度ダウンロードすることです。次に、それらをデータベース(sqlite、zodb、mongodbなど、好みのもの)に保存し、その後、dbオブジェクトに対して必要なすべての分析を実行します。2 つのプロセス (ダウンロードと分析) は、相互に部分的に保持する方が適切です。そうしないと、それらの調整が複雑になり、コードの複雑さが増します。

于 2012-04-12T07:20:48.050 に答える
0

交換

if _body:
    ngrams = NGramCounter(_body)
    ngrams.parse()
    _feed = ngrams.get_ngrams()
    # print "\n".join("\t".join(str(_feed) for col in row) for row in tab)
    print _feed

if _body:
    ngrams = NGramCounter(" ".join(_body.strip(">").split()))
    ngrams.parse()
    _feed = ngrams.get_ngrams()
    print _feed
于 2012-04-14T18:35:02.683 に答える