tdubon commited on
Commit
12d1f22
1 Parent(s): 2b6ed11

Upload ExecutableCode.py

Browse files
Files changed (1) hide show
  1. ExecutableCode.py +64 -0
ExecutableCode.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import spacy
3
+ from spacy.pipeline import EntityRuler
4
+ from spacy.language import Language
5
+ from spacy.matcher import PhraseMatcher
6
+ from spacy.tokens import Span
7
+
8
+ nlp = spacy.load("en_core_web_md")
9
+
10
+ user_input = input(str(""))
11
+ doc1 = nlp(user_input)
12
+
13
+ #print list of entities captured by pertained model
14
+ for ent in doc1.ents:
15
+ print(ent.text, ent.label_)
16
+
17
+ #inspect labels and their meaning
18
+ for ent in doc1.ents:
19
+ print(ent.label_, spacy.explain(ent.label_))
20
+
21
+ #Use PhraseMatcher to find all references of interest
22
+ #Define the different references to Covid
23
+ user_entries = input(str("")) #gradio text box here to enter sample terms
24
+ pattern_list = []
25
+
26
+ for i in user_entries.strip().split():
27
+ pattern_list.append(i)
28
+
29
+ patterns = list(nlp.pipe(pattern_list))
30
+ print("patterns:", patterns)
31
+
32
+ #Instantiate PhraseMatcher
33
+ matcher = PhraseMatcher(nlp.vocab)
34
+
35
+ #Create label for pattern
36
+ user_named = input(str("").strip()) #gradio text box here to enter pattern label
37
+ matcher.add(user_named, patterns)
38
+
39
+ # Define the custom component
40
+ @Language.component("added_component")
41
+ def added_component_function(doc):
42
+ #Apply the matcher to the doc
43
+ matches = matcher(doc)
44
+ #Create a Span for each match and assign the label
45
+ spans = [Span(doc, start, end, label=user_named) for match_id, start, end in matches]
46
+ # Overwrite the doc.ents with the matched spans
47
+ doc.ents = spans
48
+ return doc
49
+
50
+ # Add the component to the pipeline after the "ner" component
51
+ nlp.add_pipe("added_component"), after="ner")
52
+ print(nlp.pipe_names)
53
+
54
+
55
+ #Verify that your model now detects all specified mentions of Covid on another text
56
+ user_doc = input(str("").strip())
57
+ apply_doc = nlp(user_doc)
58
+ print([(ent.text, ent.label_) for ent in apply_doc.ents])
59
+
60
+ #Count total mentions of label COVID in the 3rd document
61
+ from collections import Counter
62
+ labels = [ent.label_ for ent in apply_doc.ents]
63
+ Counter(labels)
64
+