コードのパフォーマンスを改善するためのアドバイスが必要です。
2 つのファイル ( Keyword.txt 、 description.txt ) があります。Keyword.txt はキーワードのリスト (具体的には 11,000 以上) で構成され、descriptions.txt は非常に大きなテキストの説明 (9,000 以上) で構成されています。
keyword.txt からキーワードを 1 つずつ読み取って、説明にキーワードが存在するかどうかを確認しようとしています。キーワードが存在する場合は、新しいファイルに書き込みます。つまり、これは多対多の関係 (11,000 * 9,000) のようなものです。
キーワードの例:
Xerox
VMWARE CLOUD
サンプルの説明 (それは巨大です):
Planning and implementing entire IT Infrastructure. Cyberoam firewall implementation and administration in head office and branch office. Report generation and analysis. Including band width conception, internet traffic and application performance. Windows 2003/2008 Server Domain controller implementation and managing. VERITAS Backup for Clients backup, Daily backup of applications and database. Verify the backed up database for data integrity. Send backup tapes to remote location for safe storage Installing and configuring various network devices; Routers, Modems, Access Points, Wireless ADSL+ modems / Routers Monitoring, managing & optimizing Network. Maintaining Network Infrastructure for various clients. Creating Users and maintaining the Linux Proxy servers for clients. Trouble shooting, diagnosing, isolating & resolving Windows / Network Problems. Configuring CCTV camera, Biometrics attendance machine, Access Control System Kaspersky Internet Security / ESET NOD32
以下は私が書いたコードです:
import csv
import nltk
import re
wr = open(OUTPUTFILENAME,'w')
def match():
c = 0
ft = open('DESCRIPTION.TXT','r')
ky2 = open('KEYWORD.TXT','r')
reader = csv.reader(ft)
keywords = []
keyword_reader2 = csv.reader(ky2)
for x in keyword_reader2: # Storing all the keywords to a list
keywords.append(x[1].lower())
string = ' '
c = 0
for row in reader:
sentence = row[1].lower()
id = row[0]
for word in keywords:
if re.search(r'\b{}\b'.format(re.escape(word.lower())),sentence):
string = string + id+'$'+word.lower()+'$'+sentence+ '\n'
c = c + 1
if c > 5000: # I am writing 5000 lines at a time.
print("Batch printed")
c = 0
wr.write(string)
string = ' '
wr.write(string)
ky2.close()
ft.close()
wr.close()
match()
現在、このコードが完了するまでに約 120 分かかります。速度を改善するためにいくつかの方法を試しました。
- 最初は一度に 1 行ずつ書いていましたが、小さなファイルですべてをメモリに入れる余裕があるため、一度に 5000 行に変更しました。あまり改善が見られませんでした。
- すべてを標準出力にプッシュし、コンソールからパイプを使用してすべてをファイルに追加しました。これはさらに遅かった。
コードで何か間違ったことをした可能性があるため、これを行うためのより良い方法があるかどうかを知りたいです。
私の PC の仕様: RAM: 15 GB プロセッサ: i7 第 4 世代