在字符串中查找名称

Finding names inside a string

我正在尝试检查输入的字符串中的名称。我可用的数据是来自 Facebook 的每个名字和姓氏。

我想让我的程序做的是输入 "johnsmith123"(例如)和 return ['john'、'smith'、'123']。如果 'johns' 和 'mith' 是列表中的名称,我希望它是 return ['john', 'smith', '123', 'johns' , 'mith']。基本上:可以构成输入短语的列表中单词的所有可能组合。

我知道正则表达式尝试对于查找来说真的非常快。使用名为 RegexFormat 7 的工具,我将单词列表变成了 50mb 的正则表达式特里。

这是我现在尝试 运行 使用那个 trie 的代码:

import io
import re

with io.open('REGEXES.rx.txt', encoding='latin-1') as myfile:
        TempRegex = myfile.read()

regex = re.compile(TempRegex)

while True == True:
    Password = input("Enter a phrase to be split: ")

    Words = re.findall(regex, Password)

    print(Words)

程序永远不会到达输入部分。我假设编译这么大的 regex trie 需要很长时间。

我需要知道的是,如果有什么方法可以一次性完成这个编译过程,将正则表达式对象保存到我的磁盘,然后只需将要使用的预编译对象加载到模块中,而不必每次编译?

是编译占用了这么多时间。我知道搜索实际上会很快进行。如果我能完成一次编译过程,我就可以 运行 通宵编译 ...

如果这不可行,我还能做什么?我可用的数据是来自 Facebook 的每个名字和姓氏的 100mb 单词列表,以及从该单词列表派生的正则表达式特里

我怀疑单个大型正则表达式是否是这里的最佳解决方案。所有可能的名字的单个散列 table 可能会更快。

all_first_names = set(['dan', 'bob', 'danny'])

username = 'dannysmith123'

# Get "extra" parts of the username if they exist
m = re.match(r'^([a-zA-Z]+)(.*)$', username)
name, extra = m.group(1), m.group(2)

# Get a list of possible first/last name splits
# [('d', 'annysmith'), ('da', 'nnysmith'), ...]
name_splits = [(name[:i], name[i:]) for i in range(1, len(name)+1)]

# Check each one of these splits to see if the first name
# is present in the master first name list, if so, add it to
# the list of valid matches.
match_list = []
for ns in name_splits:
    if ns[0] in all_first_names:
        match_list.extend(ns)
        if extra:
            match_list.append(extra)
            extra = None