這是代碼,但錯誤的部分是在驗證了regex模式結構后,在哪里提取子串
這是代碼,但錯誤的部分是在驗證了regex模式結構后,在哪里提取子串?
def name_and_img_identificator(input_text, text)。
input_text = re.sub(r"([^nu0300-u036f]|n(?! u0303(? [u0300-u036f]))[u0300-u036f] ", r "1", normalize("NFD"/span>, input_text), 0, re.I)
input_text = normalize( 'NFC', input_text) # -> NFC。
input_text_to_check = input_text.lower() #Convierte a minuscula todo#regex_patron_01 = r "s*??(?:dime los|dime las|dime unos|dime unas|dime|di|cuales son los|cuales son las|cuales son|cuales|que animes|que|top) s*((? :w s*) )s*(?:de series anime|de anime series|de animes|de animes|anime)s*(? :Similares al|similares a|similar al|similar a|parecidos al|parecidos a|parecido al|parecido a)s*(? 意思是說:"你的名字叫什么?""你的名字叫什么?""你的名字叫什么?""你的名字叫什么?""你的名字叫什么?""你的名字叫什么?""你的名字叫什么?
#Regex in english
regex_patron_01 = r " s * ?? (?: tell me the | tell me some| tell me | say | which are the | which are | which | animes | which | top) s * ((?: w s *) ) s * (?: anime series | anime series | anime | anime | anime) s * (? : 類似于|類似于|類似于|類似于|類似于|類似于|類似于|類似于|類似于) s * (?: 動漫系列|動漫系列|動漫系列|動漫系列|動漫|) s * (稱為|已知像|其名稱為|哪些被稱為|) s * ((?: w s *) ) s * ? "/span>
m = re.search(regex_patron_01, input_text_to_check, re.IGNORECASE) #Con esto valido la regex haber si entra o no en el bloque de code
if m。
num, anime_name = m.group()[2]
num = num.strip()
anime_name = anime_name.strip()
print(num)
print(anime_name)
return text
input_text_str = input("ingrese: "/span>)
text = ""/span>
print(name_and_img_identificator(input_text_str, text))
它給了我這個錯誤,事實是我不知道如何構造這個重碼模式,以便它只從該輸入中提取這兩個值(子串)
Traceback (most recent call last):
檔案 "serie_recommendarion_for_chatbot.py", line 154, in <module>
print(serie_and_img_identificator(input_text_str, text))
檔案 "anime_recommendarion_for_chatbot.py", line 142, in name_and_img_identificator
num, anime_name = m.group()
ValueError: 太多的值需要解壓(預計2)
如果我放一個這樣的輸入。 'Dame el top 8 de animes parecidos a Gundam' '給我排名前8位的影片。 '給我排名前8的像高達的影片片'
我需要你提取:num = '8'/span>
anime_name = 'Gundam'/span>
在這種情況下,我必須如何修正我的重碼序列呢?
uj5u.com熱心網友回復:
你可以試著提取前2個值,也許你缺少一個冒號。
num, anime_name = m.groups()[:2]
這可能是因為你正面臨著太多的值來解包的錯誤。
對數字和名稱使用兩個獨立的模式。為了簡單起見,我只列舉了幾個例子。
對于數字 測驗案例
(?<=(which are the|which|top)s)[0-9] (? =s(anime series|anime)
對于名稱 測驗案例
(?<=(like|called|which is called)s)[A-Za-z]
剩下的就是你的作業了,用西班牙語實作這些模式。
uj5u.com熱心網友回復:
在Regex操場上試試這個。鏈接
所以沒有什么變化,第一個捕獲組仍然是動漫數量的量詞,第二組是動漫本身的名稱。我只是簡化了一下這個詞組(為了演示的目的,去掉了一些不必要的部分)。它的大部分內容與你的版本沒有變化,你的版本實際上是非常可靠的詞組。
檢索法。(d ).*(?:called|that are like|known like|whose name is|which is called)s*((?:w s*) )s*??
用你的原問題進行測驗--我把它粗略地翻譯成了英文:-)
import re
from unicodedata import normalize
def name_and_img_identificator(input_text, text)。
input_text = re.sub(r"([^nu0300-u036f]|n(?!u0303(?![u0300-u036f])))[u0300-u036f] ", r"1",
normalize("NFD"/span>, input_text), 0, re.I)
input_text = normalize('NFC', input_text) # -> NFC。
input_text_to_check = input_text.lower() # Convierte a minuscula todo# Regex in english[/span]。
# original;/span>
# note: you have extra spaces here, which regex might not like.
# 你可以去掉空格,然后希望它應該是正常的。
# regex_patron_01 = r " s * ?? (?: tell me the | tell me some| tell me | say | which are the | which are the | which animes | which | top) s * ((? : w s *) ) s * (? : anime series | anime series | anime | anime | anime) s * (? : 類似于|類似于|類似于|類似于|類似于|類似于|類似于|類似于|類似于) s * (?: 動漫系列|動漫系列|動漫系列|動漫系列|動漫|) s * (稱為|已知像|其名稱為|哪些被稱為|) s * ((?: w s *) ) s * ? "/span>
#簡化
regex_patron_01 = r'(d ).*(?:called|that are like|known like|whose name is|which is called)s*((?:w s*) )s*?'
m = re.search(regex_patron_01, input_text_to_check,
re.IGNORECASE) # Con esto valido la regex haber si entra o no en el bloque de code[/span
if m。
num, anime_name = m.group()[:2]
num = num.strip()
anime_name = anime_name.strip()
print(num)
print(anime_name)
return text
#input_text_str = input("ingrese: " )
input_text_str = '告訴我前8個像高達的影片片?'。
text = ""
print(name_and_img_identificator(input_text_str, text))
uj5u.com熱心網友回復:
在regex模式中的錯誤
你忘了添加?:來不捕獲這個組。改變:
regex_patron_01 = r"...(llamada|conocida como|cuyo nombre es|la cual se llama|)..."
致:
regex_patron_01 = r"...(?:llamada|conocida como|cuyo nombre es|la cual se llama|) ..."
- 為了不捕獲額外的空格或單詞,你對
num的捕獲應該是非貪婪的,這樣它就不會捕獲像"de"這樣的單詞并讓后續的模式與之匹配。改變:
regex_patron_01 = r" ...((?:w s*) )..."
致:
regex_patron_01 = r" ...((?:w ?s*?) )..."
.groups()已經包含了字串的匹配,因此訪問一個索引將只給你一個字串,這是你錯誤的根本原因。改變:
num, anime_name = m.group()[2]
致:
num, anime_name = m.group()
有了上面的這些改動,就可以成功了:
8
gundam
改進之處
你的重碼法太復雜了,包含了很多硬編碼的詞,這些詞會因語言而不同。我的建議是對它所能接受的字串格式設定一個標準:Any text here (num) any text here (anime_name)
這已經是你的輸入格式:
Dame el top 8 de animes parecidos a Gundam
因此,你可以去掉那個長長的regex,用這個來代替,輸出結果是一樣的:
regex_patron_01 = r"^.*? (d ).*s(. )$"
注意,這要求(anime_name)是一個單字。為了支持多字,我們必須設定一個特殊的字符來標記動漫名稱的開始,如冒號:
Dame el top 8 de animes parecidos a: Gundam X
那么重碼將是:
regex_patron_01 = r"^.*? (d ).*:s(. )$"
輸出
8
高達 X
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/323157.html
標籤:
