2016-09-19 7 views
1

テキストファイルをリストに入れてから、そのファイルを繰り返し処理して、仕事を嫌う人を排除するための正確かつ部分的なコピーを見つけようとしています。まず自分のクラス名簿を使い、.txtを名前に付け加えて割り当てを探し、割り当てが完了したかどうかを確認します。私は読んで500以上の学生の論文を持っています。これまでに書いたコードでは、文字通り.txtファイル内の単語を単語ごとに繰り返しているので、私は多くの "騙された"バックを手に入れています。助けてください。複数のテキストファイルを反復処理して比較する

def Cheaters(): 
    file = open("roster.txt", "r") 
    L = [] 
    for i in file: 
     new = [i[:-1], ".txt"] 
     new2 = "".join(new) 
     if i not in L: 
      L.append(new2) 
    for j in L: 
     try: 
      file2 = open(j, "r") 
      for n in file2: 
       for m in file2: 
        if n == m: 
         print("Cheated") 
     except: 
      print("No work submitted") 
+0

第1のことは、不正行為を構成するものを定義することです。一致した単語または単語のペアを見つけることは、特定のトピック内でかなり共通する可能性があるため、貧弱な指標です。各文書を文で区切り、文書間でいくつの文が共有されているかを比較することができます。あなたが本当に巧みになりたいのであれば、ある文書の各センテンスから別の文書の各センテンスにどのように多くの置換を取るかを比較する編集距離を実装することができます。 – James

+0

ありがとうございます。私は部分的またはすべての盗作を確認したい。あなたは、単語ごとにではなく行ごとにどのように反復することができるか説明してください。また、正確には「距離を編集する」という意味ですか?編集可能な要素の範囲など? –

+0

私はあなたを助けるためにいくつかのコードに取り組んでいます。すべてのドキュメントファイル名がリストの中にありますか? – James

答えて

0

これを試してください。ファイル構造を変更する必要があるかもしれませんが、それは近いはずです。

import re 
from itertools import product 

def hash_sentences(document): 
    # remove all characters except those below, replace with a space 
    # split into a list 
    cleaned_text = re.sub(r'[^A-z0-9,;:\.\?! ]', ' ', document) 
    sentences = re.split(r'[\?.!\.]', cleaned_text) 

    # the less than 5 removes short sentences like "Dr." 
    # return a hash of the sentences for comparison 
    return [hash(s.strip().lower()) for s in sentences if len(s) > 5] 

def compare_documents(doc1, doc2): 
    hash1 = hash_sentences(doc1) 
    hash2 = hash_sentences(doc2) 
    # return the percentage of sentences of doc1 that are in doc2 
    return sum((h in hash2) for h in hash1)/float(len(hash1)) 

# get list of document file names 
with open('roster.txt', 'r') as fp: 
    doc_fnames = [d+'.txt' for d in fp.readlines()] 

# create dictionay of file names and content 
doc_dict = {} 
for fname in doc_fnames: 
    try: 
     with open(fname, 'r') as fp: 
      doc_dict[fname] = fp.read() 
    except: 
     print('No submission: %s' %fname) 

# iterate through the pairs of documents 
for doc_pair in product(doc_dict.keys(), doc_dict.keys()): 
    pct = compare_documents(doc_dict[doc_pair[0]], doc_dict[doc_pair[1]]) 
    print('Percentage of %s sentences in %s: %0.2f%%' %(doc_pair[0], doc_pair[1], 100*pct))