Guest User

Untitled

a guest
Oct 17th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. import nltk
  2. import spacy
  3. nlp = spacy.load('en_core_web_sm')
  4.  
  5. sent="The speed limit is 90 kilometres per hour on roads outside built-up areas."
  6. doc=nlp(sent)
  7.  
  8. def sent_root(sent):
  9. for index,token in enumerate(sent):
  10. if token.head == token:
  11. return token, index
  12.  
  13. def sent_subj(sent):
  14. for index,token in enumerate(sent):
  15. if token.dep_ == 'nsubj':
  16. return token, index
  17.  
  18. def sent_child(token):
  19. complete_subj = ''
  20. for child in token.children:
  21. if(child.is_punct == False):
  22. if(child.dep_ == 'compound'):
  23. complete_subj += child.text + ' ' + token.text+' '
  24. else:
  25. complete_subj += child.text + ' '
  26. for child_token in child.children:
  27. if(child.is_punct == False):
  28. complete_subj += child_token.text+' '
  29.  
  30. return complete_subj
  31.  
  32. def doc_ents_root(sent, root):
  33. ents_root = root.text+' '
  34. for token in sent.ents:
  35. ents_root += token.text + ' '
  36.  
  37. return ents_root
  38.  
  39. def action(sent):
  40. #Obtaining the sent root
  41. root, root_idx = sent_root(sent)
  42.  
  43. #Obtaining the subject
  44. subj, subj_idx = sent_subj(sent)
  45.  
  46. #Obtaining the whole subject (subj + comps)
  47. complete_subj = sent_child(subj)
  48.  
  49. complete_ents = doc_ents_root(sent, root)
  50.  
  51. return complete_subj + complete_ents
  52.  
  53. action(doc)
Add Comment
Please, Sign In to add comment