用Python编写标记器

2024-05-12 00:12:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在Python中设计一个自定义的标记赋予器模块,让用户指定要用于输入的标记赋予器。例如,考虑以下输入:

Q: What is a good way to achieve this? A: I am not so sure. I think I will use Python.

我希望能够提供NLTK's sentence tokenizationsent_tokenize()作为一个选项,因为它在很多情况下都能很好地工作,我不想重新发明轮子。除此之外,我还想提供一个更细粒度的标记化构建器(类似于规则引擎)。让我解释一下:

假设我提供了两个标记器:

SENTENCE # Tokenizes the given input by using sent_tokenize()
WORD # Tokenizes the given input by using word_tokenize()
QA # Tokenizes using a custom regular expression. E.g., Q: (.*?) A: (.*?)

我想支持以下规则:

  1. QA->;句子:首先应用QA标记器,然后应用句子标记器
  2. QA:只应用QA标记器

因此,预期产出如下:

1。QA->;句子

[
  ('QUESTION', 
             ('SENTENCE', 'What is a good way to achieve this?'), 
  ),
  ('ANSWER', 
             ('SENTENCE', 'I am not so sure', 'I think I will use Python')
  )
]

2。质量保证

[
  ('QUESTION', 'What is a good way to achieve this?'),
  ('ANSWER', 'I am not so sure. I think I will use Python')
]

什么样的设计才能有效地实现这一点?


Tags: to标记soisnotthisamqa
2条回答

由于在Python中标记化很容易,我想知道您的模块计划提供什么。 我的意思是,当开始一个软件时,一个好的设计来自于考虑使用场景,而不是首先考虑数据结构。

您的预期输出示例有点混乱。 我假设您希望标记器在左侧返回名称,在右侧返回标记列表。 我玩了一点以获得类似的结果,但使用列表更容易处理:

import re

# some tokenizers
def tokzr_WORD(txt): return ('WORD', re.findall(r'(?ms)\W*(\w+)', txt))  # split words
def tokzr_SENT(txt): return ('SENTENCE', re.findall(r'(?ms)\s*(.*?(?:\.|\?|!))', txt))  # split sentences
def tokzr_QA(txt):
    l_qa = []
    for m in re.finditer(r'(?ms)^[\s#\-\*]*(?:Q|Question)\s*:\s*(?P<QUESTION>\S.*?\?)[\s#\-\*]+(?:A|Answer)\s*:\s*(?P<ANSWER>\S.*?)$', txt):  # split (Q, A) sequences
        for k in ['QUESTION', 'ANSWER']:
            l_qa.append(m.groupdict()[k])
    return ('QA', l_qa)
def tokzr_QA_non_canonical(txt):  # Note: not supported by tokenize_recursively() as not canonical.
    l_qa = []
    for m in re.finditer(r'(?ms)^[\s#\-\*]*(?:Q|Question)\s*:\s*(?P<QUESTION>\S.*?\?)[\s#\-\*]+(?:A|Answer)\s*:\s*(?P<ANSWER>\S.*?)$', txt):  # split (Q, A) sequences
        for k in ['QUESTION', 'ANSWER']:
            l_qa.append((k, m.groupdict()[k]))
    return l_qa

dict_tokzr = {  # control string: tokenizer function
    'WORD'    : tokzr_WORD,
    'SENTENCE': tokzr_SENT,
    'QA'      : tokzr_QA,
}

# the core function
def tokenize_recursively(l_tokzr, work_on, lev=0):
    if isinstance(work_on, basestring):
        ctrl, work_on = dict_tokzr[l_tokzr[0]](work_on)  # tokenize
    else:
        ctrl, work_on = work_on[0], work_on[1:]  # get right part
    ret = [ctrl]
    if len(l_tokzr) == 1:
        ret.append(work_on)  # add right part
    else:
        for wo in work_on:  # dive into tree
            t = tokenize_recursively(l_tokzr[1:], wo, lev + 1)
            ret.append(t)
    return ret

# just for printing
def nestedListLines(aList, ind='    ', d=0):
    """ Returns multi-line string representation of \param aList.  Use \param ind to indent per level. """
    sRet = '\n' + d * ind + '['
    nested = 0
    for i, e in enumerate(aList):
        if i:
            sRet += ', '
        if type(e) == type(aList):
            sRet += nestedListLines(e, ind, d + 1)
            nested = 1
        else:
            sRet += '\n' + (d + 1) * ind + repr(e) if nested else repr(e)
    sRet += '\n' + d * ind + ']' if nested else ']'
    return sRet

# main()
inp1 = """
    * Question: I want try something.  Should I?
    * Answer  : I'd assume so.  Give it a try.
"""
inp2 = inp1 + 'Q: What is a good way to achieve this?  A: I am not so sure. I think I will use Python.'
print repr(tokzr_WORD(inp1))
print repr(tokzr_SENT(inp1))
print repr(tokzr_QA(inp1))
print repr(tokzr_QA_non_canonical(inp1))  # Really this way?
print

for ctrl, inp in [  # example control sequences
    ('SENTENCE-WORD', inp1),
    ('QA-SENTENCE', inp2)
]:
    res = tokenize_recursively(ctrl.split('-'), inp)
    print nestedListLines(res)

顺便说一下,Python/Lib/tokenize.py(对于Python代码本身)可能值得一看如何处理事情。

如果我正确地理解了这个问题,那么我认为你应该重新发明轮子。我将为您想要的不同类型的标记化实现状态机,并使用python字典保存标记。

http://en.wikipedia.org/wiki/Finite-state_machine

示例状态机,它将获取一个带有空格的句子并打印出单词,当然您可以用更简单的方法来完成这个特定的示例!但一般来说,使用状态机,您可以获得线性时间性能,并且可以轻松地对其进行优化!

while 1:
    if state == "start":
        if i == len(text):
            state = "end"
        elif text[i] == " ":
            state = "new word"
            i = i - 1
        else:
            word.append(text[i])
    elif state == "new word":
        print(''.join(word))
        del word[:]
        state = "start"
    elif state == "end":
        print(''.join(word))
        break
    i = i + 1

http://docs.python.org/2/library/collections.html#collections.Counter

然后您可以使用这个python数据结构来保存令牌。我觉得非常适合你的需要!

希望这能有所帮助。

相关问题 更多 >