【问题标题】:Python3 add colour to specific outputted words from lists in a sentencePython3为句子中列表中的特定输出单词添加颜色
【发布时间】:2016-11-25 13:22:28
【问题描述】:

我下面的代码目前正在检查一个文本文件,看看它是否可以从我的词典文件中找到一个句子中的单词,如果它确实找到了,那么它会搜索这一行,看看它是否可以从二级列表中找到一个单词在一行中同时满足这两个条件,然后打印这一行。

我要做的是将词典单词颜色设置为例如红色和蓝色,用于在名为 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()

这是我的完整代码,但问题出现在“LanguageDetect”方法中,我目前通过分配 lex_word 和 cat_word 变量进行尝试的方法没有奏效,坦率地说,我不知道接下来要尝试什么。

词典:

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

然后在我使用的 readlex 方法中:

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

要删除单词/字符之前的所有内容,我认为这可能是我无法为词典单词着色但不确定如何解决此问题的主要问题。

【问题讨论】:

  • 颜色解释取决于您运行的终端。你确定你的终端可以处理颜色吗?
  • 我目前只是通过 pycharm 输出,所以应该能够处理颜色,我认为目前的问题更多在于代码而不是终端
  • lex_word = ''\033[1;31m' + lex_word + '\033[1;m' 为我工作,所以我问了
  • 你在代码的什么地方使用了这个?
  • 我提取了它,由于缺少库,无法测试您的代码,请参阅我的答案了解更多详情

标签: python list python-3.x colors


【解决方案1】:

我认为您的问题来自您处理线路数据的方式,但也许我没有清楚地理解您的问题。

这应该可以解决问题:

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) 

给出输出:

【讨论】:

  • 所以我添加了这个并且 cat_word 工作但 lex_word 不我认为它不工作的一个原因是词典的外观但我确实使用正则表达式语句清理数据在 readlex 方法中,我将用正则表达式代码之前和之后如何显示我的词典来更新我的问题
猜你喜欢
  • 2013-01-23
  • 1970-01-01
  • 2016-09-19
  • 1970-01-01
  • 1970-01-01
  • 2015-04-24
  • 1970-01-01
  • 2020-07-04
相关资源
最近更新 更多