【问题标题】:Term split by hashtag of multiple words按多个单词的标签拆分术语
【发布时间】:2013-12-29 05:55:18
【问题描述】:

我正在尝试拆分包含多个单词的标签的术语,例如“#I-am-great”或“#awesome-dayofmylife”
那么我正在寻找的输出是:

 I am great
 awesome day of my life

我能做到的只有:

 >>> import re
 >>> name = "big #awesome-dayofmylife because #iamgreat"
 >>> name =  re.sub(r'#([^\s]+)', r'\1', name)
 >>> print name
 big awesome-dayofmylife because iamgreat

如果有人问我是否有可能的单词列表,那么答案是“否”,所以如果我能在这方面获得指导,那就太好了。有 NLP 专家吗?

【问题讨论】:

  • 您会将#something 拆分为some thing 还是something
  • 你不能在不知道它们就是这样的情况下拆分连接的单词;字。你需要一本字典。
  • @devnull 没关系,但这是个好问题
  • @qstebom 你知道我可以解析和拆分单词的任何在线 api 或字典吗?
  • 我有一个想法。您可以存储常用词列表(例如www-personal.umich.edu/~jlawler/wordlist),然后进行查找。然后对列表进行最长匹​​配。

标签: python regex nltk


【解决方案1】:

当然,上面所有的评论都是正确的:在单词之间没有空格或其他清晰分隔符的标签(尤其是英文)通常是模棱两可的,不能在所有情况下都正确解析。

但是,单词列表的想法实现起来相当简单,并且可能会产生有用的(尽管有时是错误的)结果,所以我实现了一个快速版本:

wordList = '''awesome day of my life because i am great something some
thing things unclear sun clear'''.split()

wordOr = '|'.join(wordList)

def splitHashTag(hashTag):
  for wordSequence in re.findall('(?:' + wordOr + ')+', hashTag):
    print ':', wordSequence   
    for word in re.findall(wordOr, wordSequence):
      print word,
    print

for hashTag in '''awesome-dayofmylife iamgreat something
somethingsunclear'''.split():
  print '###', hashTag
  splitHashTag(hashTag)

打印出来:

### awesome-dayofmylife
: awesome
awesome
: dayofmylife
day of my life
### iamgreat
: iamgreat
i am great
### something
: something
something
### somethingsunclear
: somethingsunclear
something sun clear

如你所见,它落入了 qstebom 为其设置的陷阱;-)

编辑:

上面代码的一些解释:

变量wordOr 包含所有单词的字符串,用管道符号(|) 分隔。在正则表达式中表示“这些词之一”。

第一个findall 得到一个模式,意思是“一个或多个这些单词的序列”,因此它匹配“dayofmylife”之类的内容。 findall 找到所有这些序列,所以我遍历它们 (for wordSequence in …)。对于每个单词序列,然后我搜索序列中的每个单词(也使用findall)并打印该单词。

【讨论】:

  • @Alfe 你能解释一下你的代码吗?什么是word或wordSequence
  • 我在回答中添加了一些解释。
【解决方案2】:

问题可以分解为几个步骤:

  1. 用英语单词填充列表
  2. 将句子拆分为由空格分隔的术语。
  3. 将以“#”开头的术语视为主题标签
  4. 对于每个主题标签,通过检查它们是否存在于单词列表中,按最长匹配查找单词。

这是使用这种方法的一种解决方案:

# Returns a list of common english terms (words)
def initialize_words():
    content = None
    with open('C:\wordlist.txt') as f: # A file containing common english words
        content = f.readlines()
    return [word.rstrip('\n') for word in content]


def parse_sentence(sentence, wordlist):
    new_sentence = "" # output    
    terms = sentence.split(' ')    
    for term in terms:
        if term[0] == '#': # this is a hashtag, parse it
            new_sentence += parse_tag(term, wordlist)
        else: # Just append the word
            new_sentence += term
        new_sentence += " "

    return new_sentence 


def parse_tag(term, wordlist):
    words = []
    # Remove hashtag, split by dash
    tags = term[1:].split('-')
    for tag in tags:
        word = find_word(tag, wordlist)    
        while word != None and len(tag) > 0:
            words.append(word)            
            if len(tag) == len(word): # Special case for when eating rest of word
                break
            tag = tag[len(word):]
            word = find_word(tag, wordlist)
    return " ".join(words)


def find_word(token, wordlist):
    i = len(token) + 1
    while i > 1:
        i -= 1
        if token[:i] in wordlist:
            return token[:i]
    return None 


wordlist = initialize_words()
sentence = "big #awesome-dayofmylife because #iamgreat"
parse_sentence(sentence, wordlist)

打印出来:

'big awe some day of my life because i am great '

您必须删除尾随空格,但这很容易。 :)

我从http://www-personal.umich.edu/~jlawler/wordlist得到了单词表。

【讨论】:

  • 是的,总之会改进我的答案。
猜你喜欢
  • 2018-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-21
  • 1970-01-01
  • 2011-08-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多