Python - 从文本中提取主题标签;以标点符号结束

时间:2016-12-03 23:23:20

标签: python twitter

对于我的编程类,我必须根据以下描述创建一个函数:

  

参数是推文。此函数应按照它们在推文中出现的顺序返回包含推文中所有主题标签的列表。返回列表中的每个主题标签都应删除初始哈希符号,并且主题标签应该是唯一的。 (如果推文使用相同的主题标签两次,它只包含在列表中一次。主题标签的顺序应该与推文中每个标签第一次出现的顺序相匹配。)

我不确定如何制作它以便在遇到标点符号时标签结束(请参阅第二个doctest示例)。我当前的代码没有输出任何内容:

def extract(start, tweet):
    """ (str, str) -> list of str

    Return a list of strings containing all words that start with a specified character.

    >>> extract('@', "Make America Great Again, vote @RealDonaldTrump")
    ['RealDonaldTrump']
    >>> extract('#', "Vote Hillary! #ImWithHer #TrumpsNotMyPresident")
    ['ImWithHer', 'TrumpsNotMyPresident']
    """

    words = tweet.split()
    return [word[1:] for word in words if word[0] == start]

def strip_punctuation(s):
    """ (str) -> str

    Return a string, stripped of its punctuation.

    >>> strip_punctuation("Trump's in the lead... damn!")
    'Trumps in the lead damn'
    """
    return ''.join(c for c in s if c not in '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~')

def extract_hashtags(tweet):
    """ (str) -> list of str

    Return a list of strings containing all unique hashtags in a tweet.
    Outputted in order of appearance.

    >>> extract_hashtags("I stand with Trump! #MakeAmericaGreatAgain #MAGA #TrumpTrain")
    ['MakeAmericaGreatAgain', 'MAGA', 'TrumpTrain']
    >>> extract_hashtags('NEVER TRUMP. I'm with HER. Does #this! work?')
    ['this']
    """

    hashtags = extract('#', tweet)

    no_duplicates = []

    for item in hashtags:
        if item not in no_duplicates and item.isalnum():
            no_duplicates.append(item)

    result = []
    for hash in no_duplicates:
        for char in hash:
            if char.isalnum() == False and char != '#':
                hash == hash[:char.index()]
                result.append()
    return result

我此时已经迷失了;任何帮助,将不胜感激。先感谢您。

注意:我们允许使用正则表达式或导入任何模块。

1 个答案:

答案 0 :(得分:0)

你看起来有点迷失。解决这些类型问题的关键是将问题分成更小的部分,解决这些问题,然后将结果结合起来。你已经得到了所需的每一件......:

def extract_hashtags(tweet):
    # strip the punctuation on the tags you've extracted (directly)
    hashtags = [strip_punctuation(tag) for tag in extract('#', tweet)]
    # hashtags is now a list of hash-tags without any punctuation, but possibly with duplicates

    result = []
    for tag in hashtags:
        if tag not in result:  # check that we haven't seen the tag already (we know it doesn't contain punctuation at this point)
            result.append(tag)
    return result

ps:这是一个非常适合正则表达式解决方案的问题,但如果你想要快速strip_punctuation,你可以使用:

def strip_punctuation(s):
    return s.translate(None, '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~')