[Hindi]NLP 18# Phrase Matching and Vocabulary P.1 |NLP|Python 3|Natural Language Processing|2019
Code:
# -*- coding: utf-8 -*-
"""NLP_Ex12.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1j3XIpD1UXOI0EU-8PMbBOB-zwTqSPtX9
"""
import spacy
nlp = spacy.load("en_core_web_sm")
from spacy.matcher import Matcher
matcher = Matcher(nlp.vocab)
#solarpower
pattern1 = [{'LOWER': 'solarpower'}]
#solar power
pattern2 = [{'LOWER': 'solar'}, {'LOWER': 'power'}]
#solar-power
pattern3 = [{'LOWER': 'solar'}, {'IS_PUNCT': True}, {'LOWER': 'power'}]
matcher.add('SolarPower',None,pattern1,pattern2,pattern3)
doc1 = nlp(u'The Solar Power industry continues to grow as demand for solarpower increases. Solar-power cars are gaining popularity.')
found_matches=matcher(doc1)
print(found_matches)
for matches_id, start,end in found_matches:
string_id = nlp.vocab.strings[matches_id]
span = doc1[start:end]
print(matches_id, string_id, start, end, span.text)
matcher.remove('SolarPower')
pattern1 = [{'LOWER': 'solarpower'}]
pattern2 = [{'LOWER': 'solar'}, {'IS_PUNCT': True, 'OP':'*'}, {'LOWER': 'power'}]
matcher.add('SolarPower', None, pattern1, pattern2)
doc2 = nlp(u'Solar--Power is solarpower yooo!!!')
found_matches = matcher(doc2)
print(found_matches)
"""NLP_Ex12.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1j3XIpD1UXOI0EU-8PMbBOB-zwTqSPtX9
"""
import spacy
nlp = spacy.load("en_core_web_sm")
from spacy.matcher import Matcher
matcher = Matcher(nlp.vocab)
#solarpower
pattern1 = [{'LOWER': 'solarpower'}]
#solar power
pattern2 = [{'LOWER': 'solar'}, {'LOWER': 'power'}]
#solar-power
pattern3 = [{'LOWER': 'solar'}, {'IS_PUNCT': True}, {'LOWER': 'power'}]
matcher.add('SolarPower',None,pattern1,pattern2,pattern3)
doc1 = nlp(u'The Solar Power industry continues to grow as demand for solarpower increases. Solar-power cars are gaining popularity.')
found_matches=matcher(doc1)
print(found_matches)
for matches_id, start,end in found_matches:
string_id = nlp.vocab.strings[matches_id]
span = doc1[start:end]
print(matches_id, string_id, start, end, span.text)
matcher.remove('SolarPower')
pattern1 = [{'LOWER': 'solarpower'}]
pattern2 = [{'LOWER': 'solar'}, {'IS_PUNCT': True, 'OP':'*'}, {'LOWER': 'power'}]
matcher.add('SolarPower', None, pattern1, pattern2)
doc2 = nlp(u'Solar--Power is solarpower yooo!!!')
found_matches = matcher(doc2)
print(found_matches)
0 Comments