import json from spacy.lang.zh import Chinese with open("exercises/zh/countries.json", encoding="utf8") as f: COUNTRIES = json.loads(f.read()) nlp = Chinese() doc = nlp("智利可能会从斯洛伐克进口货物") # 导入PhraseMatcher并实例化 from spacy.____ import ____ matcher = ____(____) # 创建Doc实例的模板然后加入matcher中 # 下面的代码比这样的表达方式更快: [nlp(country) for country in COUNTRIES] patterns = list(nlp.pipe(COUNTRIES)) matcher.add("COUNTRY", None, *patterns) # 在测试文档中调用matcher并打印结果 matches = ____(____) print([doc[start:end] for match_id, start, end in matches])
from spacy.lang.es import Spanish nlp = Spanish() # Importa las clases Doc y Span from spacy.____ import ____, ____ words = ["Me", "gusta", "David", "Bowie"] spaces = [True, True, True, False] # Crea un doc a partir de las palabras y los espacios doc = ____(____, ____, ____) print(doc.text) # Crea un span para "David Bowie" a partir del doc y asígnalo al label "PERSON" span = ____(____, ____, ____, label=____) print(span.text, span.label_) # Añade el span a las entidades del doc ____.____ = [____] # Imprime en pantalla el texto y los labels de las entidades print([(ent.text, ent.label_) for ent in doc.ents])
import spacy # Importe le Matcher from spacy.____ import ____ nlp = spacy.load("en_core_web_sm") doc = nlp("Upcoming iPhone X release date leaked as Apple reveals pre-orders") # Initialise le matcher avec le vocabulaire partagé matcher = ____(____.____) # Crée un motif qui recherche les deux tokens : "iPhone" et "X" pattern = [____] # Ajoute le motif au matcher ____.____("IPHONE_X_PATTERN", None, ____) # Utilise le matcher sur le doc matches = ____ print("Résultats :", [doc[start:end].text for match_id, start, end in matches])