首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从行中提取名称

从行中提取名称
EN

Stack Overflow用户
提问于 2013-06-28 04:13:57
回答 2查看 193关注 0票数 2

我有以下格式的数据:

代码语言:javascript
运行
复制
Bxxxx, Mxxxx F  Birmingham   AL (123) 555-2281  NCC Clinical Mental Health, Counselor Education, Sexual Abuse Recovery, Depression/Grief/Chronically or Terminally Ill, Mental Health/Agency Counseling English 99.52029    -99.8115
Axxxx, Axxxx Brown  Birmingham   AL (123) 555-2281  NCC Clinical Mental Health, Depression/Grief/Chronically or Terminally Ill, Mental Health/Agency Counseling English 99.52029    -99.8115
Axxxx, Bxxxx    Mobile   AL (123) 555-8011  NCC Childhood & Adolescence, Clinical Mental Health, Sexual Abuse Recovery, Disaster Counseling English 99.68639    -99.053238
Axxxx, Rxxxx Lunsford   Athens   AL (123) 555-8119  NCC, NCCC, NCSC Career Development, Childhood & Adolescence, School, Disaster Counseling, Supervision   English 99.804501   -99.971283
Axxxx, Mxxxx    Mobile   AL (123) 555-5963  NCC Clinical Mental Health, Counselor Education, Depression/Grief/Chronically or Terminally Ill, Mental Health/Agency Counseling, Supervision   English 99.68639    -99.053238
Axxxx, Txxxx    Mountain Brook   AL (123) 555-3099  NCC Addictions and Dependency, Career Development, Childhood & Adolescence, Corrections/Offenders, Sexual Abuse Recovery    English 99.50214    -99.75557
Axxxx, Lxxxx    Birmingham   AL (123) 555-4550  NCC Addictions and Dependency, Eating Disorders English 99.52029    -99.8115
Axxxx, Wxxxx    Birmingham   AL (123) 555-2328  NCC     English 99.52029    -99.8115
Axxxx, Rxxxx    Mobile   AL (123) 555-9411  NCC Addictions and Dependency, Childhood & Adolescence, Couples & Family, Sexual Abuse Recovery, Depression/Grief/Chronically or Terminally Ill English 99.68639    -99.053238

并且只需要提取人名。理想情况下,我可以使用humanName获得一组name对象,其中包含字段name.firstname.middlename.lastname.title……

我尝试迭代,直到遇到表示州的前两个大写字母,然后将前面的内容存储到一个列表中,然后调用humanName,但这是一场灾难。我不想继续尝试这种方法。

有没有办法感知单词的开头和结尾?这可能会有帮助..。

推荐?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-06-28 04:59:58

最好的办法是找到不同的数据源。我是认真的。这张是他妈的。

如果你不能做到这一点,那么我会像这样做一些工作:

  1. 将所有双空格替换为单空格。
  2. 将该行按空格拆分。
  3. 获取列表中的最后两项。这些是lat和lng
  4. 在列表中向后循环,在潜在语言列表中查找每一项。如果查找失败,则完成对剩余列表项的languages.
  5. Join操作,并在行中返回空格
  6. ,找到第一个打开的paren。读取中的大约13或14个字符,将所有标点符号替换为空字符串,并通过拆分的commas.
  7. Using将电话号码后面的其余行重新格式化为普通电话,循环遍历列表中的每一项。如果文本以1个以上的大写字母开头,请将其添加到认证中。否则,将它添加到practice.
  8. Going的区域中,返回到步骤#6中找到的索引中,直到此时为止。把它分成空格,然后取最后一项。这就是州政府。剩下的只有名字和城市了!
  9. 取了空格分割行中的前两项。到目前为止,这是你对名字最好的猜测。
  10. 看第三个项目。如果只有一个字母,则将其添加到名称中,并从此处的数据文件中删除:http://download.geonames.org/export/zip/US.zip
  11. In list.
  12. Download US US.zip,将其全部拆分到选项卡。以索引2和4处的数据为例,它们是城市名称和州缩写。循环遍历所有数据,并将连接为缩写+ ":“+城市名称(即AK:沙点)的每一行插入到一个新列表中。
  13. 将行中其余项的所有可能联接组合在一起,格式与第13步相同。因此,第2行应使用AL:布朗伯明翰和AL:伯明翰。
  14. 循环遍历每个组合,并在步骤13中创建的列表中搜索它。如果找到,将其从拆分列表中删除。
  15. 将字符串拆分列表中的所有剩余项添加到人员姓名中。
  16. 如果需要,请在逗号上拆分姓名。索引是姓氏,index1是所有剩余的名字。不要对中间名做任何假设。

为了让大家发笑,我实现了这个。好好享受吧。

代码语言:javascript
运行
复制
import itertools

# this list of languages could be longer and should read from a file
languages = ["English", "Spanish", "Italian", "Japanese", "French",
             "Standard Chinese", "Chinese", "Hindi", "Standard Arabic", "Russian"]

languages = [language.lower() for language in languages]

# Loop through US.txt and format it. Download from geonames.org.
cities = []
with open('US.txt', 'r') as us_data:
    for line in us_data:
        line_split = line.split("\t")
        cities.append("{}:{}".format(line_split[4], line_split[2]))

# This is the dataset
with open('state-teachers.txt', 'r') as teachers:
    next(teachers)  # skip header

    for line in teachers:
        # Replace all double spaces with single spaces
        while line.find("  ") != -1:
            line = line.replace("  ", " ")

        line_split = line.split(" ")

        # Lat/Lon are the last 2 items
        longitude = line_split.pop().strip()
        latitude = line_split.pop().strip()

        # Search for potential languages and trim off the line as we find them
        teacher_languages = []

        while True:
            language_check = line_split[-1]
            if language_check.lower().replace(",", "").strip() in languages:
                teacher_languages.append(language_check)
                del line_split[-1]
            else:
                break

        # Rejoin everything and then use phone number as the special key to split on
        line = " ".join(line_split)

        phone_start = line.find("(")
        phone = line[phone_start:phone_start+14].strip()

        after_phone = line[phone_start+15:]

        # Certifications can be recognized as acronyms
        # Anything else is assumed to be an area of practice
        certifications = []
        areas_of_practice = []

        specialties = after_phone.split(",")
        for specialty in specialties:
            specialty = specialty.strip()
            if specialty[0:2].upper() == specialty[0:2]:
                certifications.append(specialty)
            else:
                areas_of_practice.append(specialty)

        before_phone = line[0:phone_start-1]
        line_split = before_phone.split(" ")

        # State is the last column before phone
        state = line_split.pop()

        # Name should be the first 2 columns, at least. This is a basic guess.
        name = line_split[0] + " " + line_split[1]

        line_split = line_split[2:]

        # Add initials
        if len(line_split[0].strip()) == 1:
            name += " " + line_split[0].strip()
            line_split = line_split[1:]

        # Combo of all potential word combinations to see if we're dealing with a city or a name
        combos = [" ".join(combo) for combo in set(itertools.permutations(line_split))] + line_split

        line = " ".join(line_split)
        city = ""

        # See if the state:city combo is valid. If so, set it and let everything else be the name
        for combo in combos:
            if "{}:{}".format(state, combo) in cities:
                city = combo
                line = line.replace(combo, "")
                break

        # Remaining data must be a name
        if line.strip() != "":
            name += " " + line

        # Clean up names
        last_name, first_name = [piece.strip() for piece in name.split(",")]

        print first_name, last_name
票数 1
EN

Stack Overflow用户

发布于 2013-06-28 04:36:58

不是一个代码答案,但看起来你可以从http://www.abec.alabama.gov/rostersearch2.asp?search=%25&submit1=Search的许可委员会获得你想要的大部分/所有数据。名字很容易在那里找到。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17352358

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档