2016-11-25 5 views
0

私の次のコードは、現在、それは私の辞書ファイルから文章中の単語を見つけることができれば、それはものを見つけない場合、それは、その後に次の行を検索し、参照するために、テキストファイルをチェックしている文にリストから特定の出力の単語に色を追加しますこれらの条件の両方が1つの行で満たされていれば、この行が印刷されているかどうかを調べることができます。のpython3は

私がしようとしているのは、CategoryGAと呼ばれるセカンダリリストにある単語に対してレキシコンの単語の色をたとえば赤色の&に設定することです。これは私の目的で、発見された言葉が由来しています。

import re 
import collections 
from collections import defaultdict 
from collections import Counter 
import sys 

from Categories.GainingAccess import GA 

Chatpath = "########/Chat1.txt" 
Chatfile = Chatpath 

lpath = 'Lexicons/######.txt' 
lfile = lpath 
CategoryGA = GA 
Hits = [] 

""" 
text_file = open(path, "r") 

lines = text_file.read().split() 

c = Counter(lines) 

for i, j in c.most_common(50): 
    print(i, j) 

""" 


# class LanguageModelling: 

def readfile(): 
    Word_Hit = None 
    with open(Chatfile) as file_read: 
     content = file_read.readlines() 
     for line_num, line in enumerate(content): 
      if any(word in line for word in CategoryGA): 
       Word_Hit = False 
       for word in CategoryGA: 
        if line.find(word) != -1: 
         Word_Hit = True 
         Hits.append(word) 
         Cleanse = re.sub('<.*?>', '', line) 

         print('%s appeared on Line %d : %s' % (word, line_num, Cleanse)) 

     file_read.close() 

    count = Counter(Hits) 
    count.keys() 
    for key, value in count.items(): 
     print(key, ':', value) 


def readlex(): 
    with open(lfile) as l_read: 
     l_content = l_read.readlines() 
     for line in l_content: 
      r = re.compile(r'^\d+\s+\d+\.\d+%\s*') 
      l_Cleanse = r.sub('', line) 
      print(l_Cleanse) 

    l_read.close() 


def LanguageDetect(): 
    with open(Chatfile) as c_read, open(lfile) as l_read: 
     c_content = c_read.readlines() 

     lex_content = l_read.readlines() 
     for line in c_content: 
      Cleanse = re.sub('<.*?>', '', line) 
      if any(lex_word in line for lex_word in lex_content) \ 
        and \ 
        any(cat_word in line for cat_word in CategoryGA): 
       lex_word = '\033[1;31m{}\033[1;m'.format(lex_word) 

       cat_word = '\033[1;44m{}\033[1;m'.format(cat_word) 
       print(Cleanse) 
       # print(cat_word) 

    c_read.close() 
    l_read.close() 

#readfile() 
LanguageDetect() 
# readlex() 

これは私の完全なコードですが、問題はlex_word & cat_word変数を割り当てることでしようとする私の現在の方法は働いていない「LanguageDetect」方式で発生していると率直に言って、私がしようとすべきことの困惑次。

レキシコン:私が使用readlexメソッド内で、その後

31547 4.7072% i 
25109 3.7466% u 
20275 3.0253% you 
10992 1.6401% me 
9490 1.4160% do 
7681 1.1461% like 
6293 0.9390% want 
6225 0.9288% my 
5459 0.8145% have 
5141 0.7671% your 
5103 0.7614% lol 
4857 0.7247% can 

r = re.compile(r'^\d+\s+\d+\.\d+%\s*') 
      l_Cleanse = r.sub('', line) 

私は、これは、なぜ私ができる」までのように主な問題かもしれ信じている単語/文字の前にすべてのものを削除しますレキシコンの単語に色を付けるが、これを修正する方法は不明である。

+0

色の解釈は、あなたが実行している端末に依存します。あなたの端末は色を扱うことができますか? –

+0

私はちょうどそれが色を処理することができる、私は現在の問題は、端末よりもコードで多くのある –

+1

'lex_word =「」\ 033 [1; 31メートル「信じるべき現在のでpycharmを経由して出力しています033 \ + lex_word +」 [1; m''は私のために働いているのですが、なぜ私は尋ねました –

答えて

0

私はあなたの問題を考えるには、あなたがラインデータを処理する方法から来ているが、たぶん私は明確にあなたの質問を理解していませんでした。トリックを行う必要があります

lex_content = ['aaa', 'xxx'] 
CategoryGA = ['ccc', 'ddd'] 
line = 'abc aaa bbb ccc' 

for lex_word in lex_content: 
    for cat_word in CategoryGA: 
    if lex_word in line and cat_word in line: 
     print(lex_word, cat_word) 
     line = line.replace(lex_word, '\033[1;31m' + lex_word + '\033[1;m') 
     line = line.replace(cat_word, '\033[1;44m' + cat_word + '\033[1;m') 
     print(line) 

は、出力を提供します:

output

+0

の回答を参照してください。これでcat_wordは動作しますが、lex_wordは動作しない理由の1つですどのように辞書が見えますが、私はreadlexメソッドで正規表現文でデータを整理します。正規表現コードの前と後に辞書が表示される方法で私の質問を更新します –