# python 如何識別字符串中的人名 ,如何識別一串拼音字符串以及韻母的提取 ## 一、識別字符串中的人名或特定名詞 ### 1.安裝Python SDK ~~~markdown 安裝方法:pip install baidu-aip ~~~ ### 2.獲取APP ID ~~~markdown 為了使用這個接口,我們還需要獲取到百度智能雲提供的賬號(如下圖中的APPID、 API KEY、 SECRET KEY)。 登錄官網后,我們需要在百度智能雲的管理中心創建一個應用, 這樣我們就能通過這個ID使用接口了,如下圖。 網址 https://console.bce.baidu.com/ai/#/ai/nlp/overview/index ~~~  ### 3.代碼調用 ~~~python def get_chinese_name(text): """ :param text: 中文字符串 :return: 人名 """ """識別人名""" # 上一步獲取到的ID AK SK APP_ID = '你的ID' API_KEY = '你的AK' SECRET_KEY = '你的SK' client = AipNlp(APP_ID, API_KEY, SECRET_KEY) text = str(text.encode('gbk', 'ignore'), encoding='gbk') # ignore忽略無法編碼的字符,如果不加這個會報錯。 # 設置請求間隔,免費版的QPS限制為2,有能力的可以購買。 time.sleep(1) # 調用詞法分析的返回結果 print(client.lexer(text)) """ 調用詞法分析 """ for i in client.lexer(text)['items']: # 若字符串中有人名就返回人名 if i['ne'] == 'PER': return i['item'] return '' ~~~ 我們測試一段字符串 ~~~python text = "這是一段測試文本,我的中文名是媛媛" print(get_chinese_name(text)) ~~~ 返回結果 ~~~python {'log_id': 375282685928253176, 'text': '這是一段測試文本,我的中文名是媛媛', 'items': [{'loc_details': [], 'byte_offset': 0, 'uri': '', 'pos': 'r', 'ne': '', 'item': '這', 'basic_words': ['這'], 'byte_length': 2, 'formal': ''}, {'loc_details': [], 'byte_offset': 2, 'uri': '', 'pos': 'v', 'ne': '', 'item': '是', 'basic_words': ['是'], 'byte_length': 2, 'formal': ''}, {'loc_details': [], 'byte_offset': 4, 'uri': '', 'pos': 'm', 'ne': '', 'item': '一段', 'basic_words': ['一', '段'], 'byte_length': 4, 'formal': ''}, {'loc_details': [], 'byte_offset': 8, 'uri': '', 'pos': 'vn', 'ne': '', 'item': '測試', 'basic_words': ['測試'], 'byte_length': 4, 'formal': ''}, {'loc_details': [], 'byte_offset': 12, 'uri': '', 'pos': 'n', 'ne': '', 'item': '文本', 'basic_words': ['文本'], 'byte_length': 4, 'formal': ''}, {'loc_details': [], 'byte_offset': 16, 'uri': '', 'pos': 'w', 'ne': '', 'item': ',', 'basic_words': [','], 'byte_length': 1, 'formal': ''}, {'loc_details': [], 'byte_offset': 17, 'uri': '', 'pos': 'r', 'ne': '', 'item': '我', 'basic_words': ['我'], 'byte_length': 2, 'formal': ''}, {'loc_details': [], 'byte_offset': 19, 'uri': '', 'pos': 'u', 'ne': '', 'item': '的', 'basic_words': ['的'], 'byte_length': 2, 'formal': ''}, {'loc_details': [], 'byte_offset': 21, 'uri': '', 'pos': 'n', 'ne': '', 'item': '中文名', 'basic_words': ['中文', '名'], 'byte_length': 6, 'formal': ''}, {'loc_details': [], 'byte_offset': 27, 'uri': '', 'pos': 'v', 'ne': '', 'item': '是', 'basic_words': ['是'], 'byte_length': 2, 'formal': ''}, {'loc_details': [], 'byte_offset': 29, 'uri': '', 'pos': '', 'ne': 'PER', 'item': '媛媛', 'basic_words': ['媛媛'], 'byte_length': 4, 'formal': ''}]} 媛媛 ~~~ ### 4.參數說明 ~~~markdown 在這里還需要說明一下接口返回參數 。為了方便,我們先把上一步得到的數據格式化如下,其中 pos : 詞性,詞性標注算法使用。 ~~~ #### **詞性縮略說明** ***ne :*** 命名實體類型。如下面例子中的“媛媛”的ne關鍵字為PER ,表示人名。  #### **專名識別縮略詞含義**  ~~~python { 'log_id': 375282685928253176, 'text': '這是一段測試文本,我的中文名是媛媛', 'items': [{ 'loc_details': [], 'byte_offset': 0, 'uri': '', 'pos': 'r', 'ne': '', 'item': '這', 'basic_words': ['這'], 'byte_length': 2, 'formal': '' }, { 'loc_details': [], 'byte_offset': 2, 'uri': '', 'pos': 'v', 'ne': '', 'item': '是', 'basic_words': ['是'], 'byte_length': 2, 'formal': '' }, { 'loc_details': [], 'byte_offset': 4, 'uri': '', 'pos': 'm', 'ne': '', 'item': '一段', 'basic_words': ['一', '段'], 'byte_length': 4, 'formal': '' }, { 'loc_details': [], 'byte_offset': 8, 'uri': '', 'pos': 'vn', 'ne': '', 'item': '測試', 'basic_words': ['測試'], 'byte_length': 4, 'formal': '' }, { 'loc_details': [], 'byte_offset': 12, 'uri': '', 'pos': 'n', 'ne': '', 'item': '文本', 'basic_words': ['文本'], 'byte_length': 4, 'formal': '' }, { 'loc_details': [], 'byte_offset': 16, 'uri': '', 'pos': 'w', 'ne': '', 'item': ',', 'basic_words': [','], 'byte_length': 1, 'formal': '' }, { 'loc_details': [], 'byte_offset': 17, 'uri': '', 'pos': 'r', 'ne': '', 'item': '我', 'basic_words': ['我'], 'byte_length': 2, 'formal': '' }, { 'loc_details': [], 'byte_offset': 19, 'uri': '', 'pos': 'u', 'ne': '', 'item': '的', 'basic_words': ['的'], 'byte_length': 2, 'formal': '' }, { 'loc_details': [], 'byte_offset': 21, 'uri': '', 'pos': 'n', 'ne': '', 'item': '中文名', 'basic_words': ['中文', '名'], 'byte_length': 6, 'formal': '' }, { 'loc_details': [], 'byte_offset': 27, 'uri': '', 'pos': 'v', 'ne': '', 'item': '是', 'basic_words': ['是'], 'byte_length': 2, 'formal': '' }, { 'loc_details': [], 'byte_offset': 29, 'uri': '', 'pos': '', 'ne': 'PER', 'item': '媛媛', 'basic_words': ['媛媛'], 'byte_length': 4, 'formal': '' }] } ~~~ ## 二、把一串拼音字符串分割成獨立的拼音 ~~~markdown 假如我們要將字符串 “zhoujielun” 分割成 “zhou-jie-lun”這樣的格式,那么我們可以采取逆向匹配的方法,即下圖中字符串的指針A向B移動過程中不斷匹配拼音表(拼音表可自行下載),直到字符串s[A:]在拼音表中,就匹配成功。重復這一過程就可以將這一字符串分隔成一個個拼音。 ~~~  算法直接看代碼 ~~~python def pinyin_word(string): ''' 將一段拼音,分解成一個個拼音 :param string: 匹配的字符串 :return: 匹配到的拼音列表 ''' max_len = 6 # 拼音最長為6 string = string.lower() stringlen = len(string) result = [] # 讀本地拼音表 with open('pinyin.txt', 'r', encoding='utf-8') as fi: pinyinLib = fi.readlines() for i in range(len(pinyinLib)): pinyinLib[i] = pinyinLib[i][:-1] # 去換行符 # 逆向匹配 while True: matched = 0 matched_word = '' if stringlen < max_len: max_len = stringlen for i in range(max_len, 0, -1): s = string[(stringlen-i):stringlen] # 字符串是否在拼音表中 if s in pinyinLib: matched_word = s matched = i break # 未匹配到拼音 if len(matched_word) == 0: break else: result.append(s) string = string[:(stringlen-matched)] stringlen = len(string) if stringlen == 0: break return result print(pinyin_or_word('zhoujielun')) 輸出結果:['lun', 'jie', 'zhou'] ~~~ ## 三、拼音韻母的提取 做法和上一點相同,建立一個韻母表,逆向匹配即可。 ~~~python def pinyin_word(string): ''' 將一段拼音,分解成一個個拼音 :param string: 匹配的字符串 :return: 匹配到的拼音列表 ''' max_len = 6 # 拼音最長為6 string = string.lower() stringlen = len(string) result = [] # 讀本地拼音表 with open('pinyin.txt', 'r', encoding='utf-8') as fi: pinyinLib = fi.readlines() for i in range(len(pinyinLib)): pinyinLib[i] = pinyinLib[i][:-1] # 去換行符 # 逆向匹配 while True: matched = 0 matched_word = '' if stringlen < max_len: max_len = stringlen for i in range(max_len, 0, -1): s = string[(stringlen-i):stringlen] # 字符串是否在拼音表中 if s in pinyinLib: matched_word = s matched = i break # 未匹配到拼音 if len(matched_word) == 0: break else: result.append(s) string = string[:(stringlen-matched)] stringlen = len(string) if stringlen == 0: break return result ~~~ ## 四、一些方法整理 ### 1.讀xlsx格式 ~~~python import xlrd data = xlrd.open_workbook('data.xlsx') table = data.sheet_by_index(0) # 按索引 nrows = table.nrows ncol = table.ncols rowvalue = table.row_values(0) # 第0行數據 colvalue = table.col_values(0) # 第0列數據 print(rowvalue, colvalue) ~~~ ### 2.判斷中文字符串 ~~~python import re Pattern = re.compile(u'[\u4e00-\u9fa5]+') # 判斷是否是中文的正則表達式對象 match = Pattern.match(string) # 判斷字符串是否為漢字 if match: zh_name = match.group() print(zh_name) ~~~ ### 3.漢字轉拼音 ~~~python 不帶語調的漢字轉拼音: from pypinyin import pinyin, lazy_pinyin # 漢字轉為拼音 new_name = '我愛編程' new_name = ''.join(lazy_pinyin(new_name)) print(new_name) 輸出結果:woaibiancheng ~~~