2017-11-19 5 views
0

行列を使用せずに共起問題を解決しようとしています。だから、私はテキストファイルを取り込み、すべての文字を小文字にし、句読点を削除し、リストに分割します。次に、1行目のすべての単語について、その単語がキーであり、行番号が値である辞書にその単語が必要です。次に、2行目については、単語がキーになり、2行目が値などになります。行番号と同じ値になるように値を取得する方法がわかりません。私はforループを実行するのに失敗しました。私はどのように正確に私はx <、>などを比較することができますか分からないとして、私はループのアキュムレータを作る方法を見つけることができません。 len(ofdict)は動作しません。len(list)は動作しません。私は何が必要なのか分からない。私は7時間くらい止まってしまった。行列を使用しない共起

import re 
import string 
string.punctuation 


def open_file(f): 
    with open(input("Enter a file name: "),'r') as inf: 

    # 
    while 1: 
     f = inf.readline() 
     f = f.lower() 
     f = re.sub(r'\b\w{1,2}\b', '', f) 
     f = "".join(i for i in f if i not in string.punctuation) 
     print_list([f]) 

     if not f: 
      break 
    # 

def print_list(f): 
    f = list(filter(None, f)) 
    for k in range(0, len(f)): 
    l = (f[k]) 
    l = l.strip(' ') 
    l = l.strip() 
    read_data(l) 
# 

def read_data(l): 
    l = [l] 
    l = [x for x in l if x != ''] 
    d = list(filter(None, l)) 
    print_list2(d) 

def print_list2(f): 
    l = 0 
for k in range(0, len(f)): 

    my_dictionary = f[k] 
    word = my_dictionary.split() 

    this_dic = {word[k] : {l} for k in range(0, len(word))} 
    print(this_dic) 












r = open_file(input("Press Any Key To Begin: You Will Enter A File Name 
During\The Next Prompt ")) 

答えて

0

ここに私が見つけた解決策があります。基本的には、私がしなければならないときにループしている間にループしようとしていました。アイデアはwhileループで、readlineメソッドを利用します。辞書の値を設定する必要があるまで、結果をパラメータとして後の関数に渡します。

import re 
import string 
string.punctuation 


def open_file(f): 
    with open(input("Enter a file name: "),'r') as inf: 


    # 
    lines = 0 
    while 1: 
     lines += 1 
     f = inf.readline() 
     f = f.lower() 
     f = re.sub(r'\b\w{1,2}\b', '', f) 
     f = "".join(i for i in f if i not in string.punctuation) 

     print_list([f], lines) 

     if not f: 
      break 
    # 

def print_list(f, lines): 
    f = list(filter(None, f)) 
    for k in range(0, len(f)): 
     l = (f[k]) 
     l = l.strip(' ') 
     l = l.strip() 
     read_data(l, lines) 
# 

def read_data(l, lines): 
    l = [l] 
    l = [x for x in l if x != ''] 
    d = list(filter(None, l)) 
    print_list2(d, lines) 

def print_list2(f, lines): 

    for k in range(0, len(f)): 

     my_dictionary = f[k] 
     word = my_dictionary.split() 

     this_dic = {word[k] : {lines} for k in range(0, len(word))} 
     print(this_dic) 












r = open_file(input("Press Any Key To Begin: You Will Enter A File Name During\ 
       The Next Prompt ")) 
関連する問題