Coverage for lib/lib_ml/lib_nlp/lib_pipeline_ner.py: 19%

16 statements  

« prev     ^ index     » next       coverage.py v7.9.1, created at 2026-01-26 23:58 +0100

1 

2 

3 

4def detect_name_ner(input_text, 

5 name_pretrained_model="Jean-Baptiste/camembert-ner", 

6 name_tokenizer="Jean-Baptiste/camembert-ner", 

7 what_I_want="PER", 

8 aggregation_strategy="simple"): 

9 

10 from transformers import AutoTokenizer, AutoModelForTokenClassification 

11 

12 tokenizer = AutoTokenizer.from_pretrained(name_tokenizer) 

13 model = AutoModelForTokenClassification.from_pretrained(name_pretrained_model) 

14 

15 ##### Process text sample (from wikipedia) 

16 

17 from transformers import pipeline 

18 

19 nlp = pipeline('ner', model=model, tokenizer=tokenizer, aggregation_strategy=aggregation_strategy) 

20 

21 res = nlp(input_text) 

22 

23# what_I_want = "PER" 

24 

25 print("res nlp : " + str(len(res))) 

26 

27 list_per = [] 

28 for r in res: 

29 if r["entity_group"] == what_I_want: 

30 name = r["word"] 

31 list_per.append(name) 

32 

33 print(str(res)) 

34 print("DONE") 

35 

36 return list_per 

37 

38