Python uses spaCy to implement Named-entity recognition
Environmental preparation:
1. Ensure that Python and pip have been installed.
2. Use pip to install spaCy: 'pip install space'.
3. Download the English model of spaCy: 'Python - m spacy download en'.
Dependent class libraries:
-SpaCy: used for Named-entity recognition.
-Pandas: Used for processing and displaying data.
Dataset:
SpaCy has built in some data sets. We use the "en_core_web_sm" data set to perform Named-entity recognition.
Sample data:
We use the following sentences as sample data for Named-entity recognition:
"Apple is looking at buying U.K. startup for $1 billion"
The complete source code is as follows:
python
import spacy
import pandas as pd
def main():
#Load English model
nlp = spacy.load("en_core_web_sm")
#Define sentences to be recognized
sentence = "Apple is looking at buying U.K. startup for $1 billion"
#Named-entity recognition of Sentences
doc = nlp(sentence)
#Extract labels and text for named entities
entities = [(entity.label_, entity.text) for entity in doc.ents]
#Convert the result to DataFrame and print it
df = pd.DataFrame(entities, columns=["Label", "Text"])
print(df)
if __name__ == "__main__":
main()
After running the code, the output result is:
Label Text
0 ORG Apple
1 GPE U.K.
2 ORG startup
3 MISC $1 billion
Note: When using spaCy for Named-entity recognition, you need to install the corresponding language model. The above source code uses the "en_core_web_sm" model. You can choose other language models as needed, such as "en_core_web_md" or "en_core_web_lg", and then load them using the corresponding model name.