【问题标题】:How to identify colors in a string with nltk in python?python - 如何在python中使用nltk识别字符串中的颜色?
【发布时间】:2016-10-27 03:22:37
【问题描述】:

这个问题确实不言自明,但我的问题是我希望能够使用 nltk 识别字符串中的颜色,而我所能找到的只是如何对词性进行分类。我知道我可以列出我想要支持的所有颜色,但是因为我想支持 css 中可用的所有颜色,所以这将是一个很长的列表(其中一些会变得很奇怪,比如蓝绿色和海蓝宝石)。如果有比将它们全部写出来更简单的方法,将不胜感激。谢谢!

编辑:

当我第一次问我的问题时,我似乎忘了提到我需要像自然语言一样间隔开的颜色名称,而不是一起运行,因为它用于语音识别。因此,我选择了“Tadhg McDonald-Jensen”的答案作为最佳答案,因为它很好地回答了我最初的问题。但是,我也发布了我自己的答案,其中提供了带有空格的颜色名称。希望这会有所帮助!

【问题讨论】:

    标签: python python-3.x colors nltk


    【解决方案1】:

    您可以使用the webcolors package 获取它识别的所有css 颜色名称,只需检查webcolors.CSS3_NAMES_TO_HEX 的成员身份:

    >>> import webcolors
    >>> "green" in webcolors.CSS3_NAMES_TO_HEX
    True
    >>> "deepskyblue" in webcolors.CSS3_NAMES_TO_HEX
    True
    >>> "aquamarine" in webcolors.CSS3_NAMES_TO_HEX
    True
    >>> len(webcolors.CSS3_NAMES_TO_HEX)
    147
    

    这意味着webcolors.CSS3_NAMES_TO_HEX.keys() 会给你一个python2 列表或python3 中设置的所有css3 颜色名称的dictkeys。

    【讨论】:

      【解决方案2】:

      解决方案(无论如何对我来说):

      注意:如果您只需要没有空格的颜色(“deepskyblue”而不是“deep sky blue”),之前的任何答案都可以使用。 但是,由于我将它与语音识别结合使用,所以我 需要用空格分隔的颜色,就像在自然语言中一样 使用以下代码(在 python 3 中)实现,我认为它更完整:

      import urllib.request
      from bs4 import BeautifulSoup
      
      def getColors():
          html = urllib.request.urlopen('http://www.w3schools.com/colors/colors_names.asp').read()
          soup = BeautifulSoup(html, 'html.parser')
          children = [item.findChildren() for item in soup.find_all('tr')]
          colors = [''.join( ' '+x if 'A' <= x <= 'Z' else x for x in item[0].text.replace(u'\xa0', '')).strip().lower() for item in children]
          return colors[1:]
      

      如果你跑了

      print(getColors())
      

      你得到:

       ['alice blue', 'antique white', 'aqua', 'aquamarine', 'azure', 'beige', 'bisque', 'black', 'blanched almond', 'blue', 'blue violet', 'brown', 'burly wood', 'cadet blue', 'chartreuse', 'chocolate', 'coral', 'cornflower blue', 'cornsilk', 'crimson', 'cyan', 'dark blue', 'dark cyan', 'dark golden rod', 'dark gray', 'dark grey', 'dark green', 'dark khaki', 'dark magenta', 'dark olive green', 'dark orange', 'dark orchid', 'dark red', 'dark salmon', 'dark sea green', 'dark slate blue', 'dark slate gray', 'dark slate grey', 'dark turquoise', 'dark violet', 'deep pink', 'deep sky blue', 'dim gray', 'dim grey', 'dodger blue', 'fire brick', 'floral white', 'forest green', 'fuchsia', 'gainsboro', 'ghost white', 'gold', 'golden rod', 'gray', 'grey', 'green', 'green yellow', 'honey dew', 'hot pink', 'indian red', 'indigo', 'ivory', 'khaki', 'lavender', 'lavender blush', 'lawn green', 'lemon chiffon', 'light blue', 'light coral', 'light cyan', 'light golden rod yellow', 'light gray', 'light grey', 'light green', 'light pink', 'light salmon', 'light sea green', 'light sky blue', 'light slate gray', 'light slate grey', 'light steel blue', 'light yellow', 'lime', 'lime green', 'linen', 'magenta', 'maroon', 'medium aqua marine', 'medium blue', 'medium orchid', 'medium purple', 'medium sea green', 'medium slate blue', 'medium spring green', 'medium turquoise', 'medium violet red', 'midnight blue', 'mint cream', 'misty rose', 'moccasin', 'navajo white', 'navy', 'old lace', 'olive', 'olive drab', 'orange', 'orange red', 'orchid', 'pale golden rod', 'pale green', 'pale turquoise', 'pale violet red', 'papaya whip', 'peach puff', 'peru', 'pink', 'plum', 'powder blue', 'purple', 'rebecca purple', 'red', 'rosy brown', 'royal blue', 'saddle brown', 'salmon', 'sandy brown', 'sea green', 'sea shell', 'sienna', 'silver', 'sky blue', 'slate blue', 'slate gray', 'slate grey', 'snow', 'spring green', 'steel blue', 'tan', 'teal', 'thistle', 'tomato', 'turquoise', 'violet', 'wheat', 'white', 'white smoke', 'yellow', 'yellow green']
      

      希望这会有所帮助!

      【讨论】:

      • 如果不是您使用的解决方案,您为什么要接受我的回答?我很欣赏这个代表,但如果它没有做你想要的,我认为说“这是我接受的答案”是没有意义的。
      • 我接受了它,因为它回答了我原来的问题,其中我遗漏了我想将它与语音识别一起使用的细节。同样沿着相同的思路,因为它回答了大多数可能只想要简单颜色的其他人,他们在 css 中的方式(没有空格)想知道的问题。所以,主要是因为它对其他人更有帮助,那些对我做类似事情的人可以看看我的帖子,也因为我在这里认识,我真的不知道该怎么做。
      • P.S.显然你在这里做的比我多得多,所以如果你认为我应该改变它,我很乐意。谢谢!
      • 嗯...实际上没有问题编辑中的解释(我在发布该评论之前错过了)在解释它方面做得很好,并且从未来观众的角度考虑网站是正是它的设计目的,所以保持这种态度! :D
      • 感谢您的帮助!
      【解决方案3】:

      我不会使用 nltk,而是使用正则表达式。

      1. 获取所有 css 颜色的列表 (here)
      2. 提取颜色名称并建立一个列表(使用 beautifulsoup)
      3. 构建正则表达式模式
      4. 使用这个正则表达式模式来匹配你想要的字符串

      这项工作适合我
      (如果需要,您只需要更改最后 2 行和代理设置)

      from bs4 import BeautifulSoup
      
      color_url = 'http://colours.neilorangepeel.com/'
      proxies = {'http': 'http://proxy.foobar.fr:3128'}#if needed
      
      #GET THE HTML FILE
      import urllib.request
      authinfo = urllib.request.HTTPBasicAuthHandler()# set up authentication info
      proxy_support = urllib.request.ProxyHandler(proxies)
      opener = urllib.request.build_opener(proxy_support, authinfo,
                                           urllib.request.CacheFTPHandler)# build a new opener that adds authentication and caching FTP handlers
      urllib.request.install_opener(opener)# install the opener
      colorfile = urllib.request.urlopen(color_url)
      
      soup = BeautifulSoup(colorfile, 'html.parser')
      
      #BUILD THE REGEX PATERN
      colors = soup.find_all('h1')
      colorsnames = [color.string for color in colors]
      colorspattern = '|'.join(colorsnames)
      colorregex = re.compile(colorspattern)
      
      #MATCH WHAT YOU NEED
      if colorregex.search(yourstring):
          do what you want
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-12-08
        • 2014-05-08
        • 2020-03-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-02-05
        相关资源
        最近更新 更多