I want to find sentence element types such as subjects, predicates , objects , time expressions. I am using .net core for splitting sentence into its elements.If I get a sentence I need to mark it as subject + verb + object and also sentences with before,after,if,but,and actually they are not just a one sentence they are two senteces. I want to find them all
I have code like this
from django.http import JsonResponse
import spacy
nlp = spacy.load('en_core_web_sm')
"""
def example_morph(sentence):
text = nlp(sentence)
return {'status': 'SUCCESS', 'words': [{'word': str(token), 'lemma': str(token.lemma_), 'pos': str(token.pos_), 'dependency': str(token.dep_), 'children': str([child for child in token.children]),'shape_': str(token.shape_),'is_alpha': str(token.is_alpha),'is_stop': str(token.is_stop),'tag': str(token),'explain': str(spacy.explain(token.tag_))} for token in text]}
def morph_api_view(request):
if request.GET:
get = request.GET
if 'sentence' in get:
try:
sentence = get.get('sentence')
morph_result = example_morph(sentence)
return JsonResponse(morph_result, json_dumps_params={'indent': 2})
except Exception as exc:
return JsonResponse({'status': 'ERROR', 'message': str(exc)})
return JsonResponse({'status': 'ERROR'})
def example_morph(sentence):
text = nlp(sentence)
return {'status': 'SUCCESS', 'words': [{'word': str(token), 'lemma': str(token.lemma_), 'pos': str(token.pos_), "dependency": str(token.dep_), 'children': str([child for child in token.children]), 'explain': str(spacy.explain(token.tag_)), 'tag':str(nlp.vocab.morphology.tag_map[token.tag_])} for token in text]}
def morph_api_view(request):
if request.GET:
get = request.GET
if 'sentence' in get:
try:
sentence = get.get('sentence')
morph_result = example_morph(sentence)
return JsonResponse(morph_result, json_dumps_params={'indent': 2})
except Exception as exc:
return JsonResponse({'status': 'ERROR', 'message': str(exc)})
return JsonResponse({'status': 'ERROR'})
"""
def example_morph(sentence):
text = nlp(sentence)
return {'status': 'SUCCESS', 'words': [{'word': str(token), 'lemma': str(token.lemma_), 'pos': str(token.pos_), "dependency": str(token.dep_), 'children': str([child for child in token.children]), 'explain': str(spacy.explain(token.tag_)), 'tag':str(nlp.vocab.morphology.tag_map[token.tag_])} for token in text]}
def morph_api_view(request):
if request.GET:
get = request.GET
if 'sentence' in get:
try:
sentence = get.get('sentence')
morph_result = example_morph(sentence)
return JsonResponse(morph_result, json_dumps_params={'indent': 2})
except Exception as exc:
return JsonResponse({'status': 'ERROR', 'message': str(exc)})
return JsonResponse({'status': 'ERROR'})
and output is like this
{
"status": "SUCCESS",
"words": [
{
"word": "what",
"lemma": "what",
"pos": "DET",
"dependency": "det",
"children": "[]",
"explain": "wh-determiner",
"tag": "{74: 90}"
},
{
"word": "kind",
"lemma": "kind",
"pos": "NOUN",
"dependency": "dobj",
"children": "[what, of]",
"explain": "noun, singular or mass",
"tag": "{74: 92, 'Number_sing': True}"
},
{
"word": "of",
"lemma": "of",
"pos": "ADP",
"dependency": "prep",
"children": "[movie]",
"explain": "conjunction, subordinating or preposition",
"tag": "{74: 85}"
},
{
"word": "movie",
"lemma": "movie",
"pos": "NOUN",
"dependency": "pobj",
"children": "[]",
"explain": "noun, singular or mass",
"tag": "{74: 92, 'Number_sing': True}"
},
{
"word": "do",
"lemma": "do",
"pos": "AUX",
"dependency": "aux",
"children": "[]",
"explain": "verb, non-3rd person singular present",
"tag": "{74: 100, 'Tense_pres': True, 'VerbForm_fin': True}"
},
{
"word": "you",
"lemma": "-PRON-",
"pos": "PRON",
"dependency": "nsubj",
"children": "[]",
"explain": "pronoun, personal",
"tag": "{74: 95, 'PronType_prs': True}"
},
{
"word": "watch",
"lemma": "watch",
"pos": "VERB",
"dependency": "ROOT",
"children": "[kind, do, you]",
"explain": "verb, base form",
"tag": "{74: 100, 'VerbForm_inf': True}"
}
]
}
In this example, if you follow root kind is root's children. From kind you can get what and of , and from of you can get movie this is your question phrase. IF you get nsubj it is you subject and if you get root itself with type of verb you will get predicate, but in complicated sentences it is hard to find them. is there any way to find it correctly?
I can find the sentence elements of simple sentences,but if sentence has relative clause, noun clause, or conjunctional clause. It is hard to find them all correctly. For example how will I find that one ?
{
"status": "SUCCESS",
"words": [
{
"word": "The",
"lemma": "the",
"pos": "DET",
"dependency": "det",
"children": "[]",
"explain": "determiner",
"tag": "{74: 90}"
},
{
"word": "Pledgor",
"lemma": "Pledgor",
"pos": "PROPN",
"dependency": "compound",
"children": "[]",
"explain": "noun, proper singular",
"tag": "{74: 96, 'NounType_prop': True, 'Number_sing': True}"
},
{
"word": "Debtor",
"lemma": "Debtor",
"pos": "PROPN",
"dependency": "nsubj",
"children": "[The, Pledgor]",
"explain": "noun, proper singular",
"tag": "{74: 96, 'NounType_prop': True, 'Number_sing': True}"
},
{
"word": "irrevocably",
"lemma": "irrevocably",
"pos": "ADV",
"dependency": "advmod",
"children": "[]",
"explain": "adverb",
"tag": "{74: 86, 'Degree_pos': True}"
},
{
"word": "declares",
"lemma": "declare",
"pos": "VERB",
"dependency": "ROOT",
"children": "[Debtor, irrevocably, ,, accepts, .]",
"explain": "verb, 3rd person singular present",
"tag": "{74: 100, 'Number_sing': True, 'Person_three': True, 'Tense_pres': True, 'VerbForm_fin': True}"
},
{
"word": ",",
"lemma": ",",
"pos": "PUNCT",
"dependency": "punct",
"children": "[]",
"explain": "punctuation mark, comma",
"tag": "{74: 97, 'PunctType_comm': True}"
},
{
"word": "accepts",
"lemma": "accept",
"pos": "VERB",
"dependency": "conj",
"children": "[,, and, undertakes]",
"explain": "verb, 3rd person singular present",
"tag": "{74: 100, 'Number_sing': True, 'Person_three': True, 'Tense_pres': True, 'VerbForm_fin': True}"
},
{
"word": ",",
"lemma": ",",
"pos": "PUNCT",
"dependency": "punct",
"children": "[]",
"explain": "punctuation mark, comma",
"tag": "{74: 97, 'PunctType_comm': True}"
},
{
"word": "and",
"lemma": "and",
"pos": "CCONJ",
"dependency": "cc",
"children": "[]",
"explain": "conjunction, coordinating",
"tag": "{74: 89, 'ConjType_comp': True}"
},
{
"word": "undertakes",
"lemma": "undertake",
"pos": "NOUN",
"dependency": "conj",
"children": "[in, taken]",
"explain": "noun, plural",
"tag": "{74: 92, 'Number_plur': True}"
},
{
"word": "in",
"lemma": "in",
"pos": "ADP",
"dependency": "prep",
"children": "[advance]",
"explain": "conjunction, subordinating or preposition",
"tag": "{74: 85}"
},
{
"word": "advance",
"lemma": "advance",
"pos": "NOUN",
"dependency": "pobj",
"children": "[]",
"explain": "noun, singular or mass",
"tag": "{74: 92, 'Number_sing': True}"
},
{
"word": "that",
"lemma": "that",
"pos": "SCONJ",
"dependency": "mark",
"children": "[]",
"explain": "conjunction, subordinating or preposition",
"tag": "{74: 85}"
},
{
"word": "in",
"lemma": "in",
"pos": "ADP",
"dependency": "prep",
"children": "[case]",
"explain": "conjunction, subordinating or preposition",
"tag": "{74: 85}"
},
{
"word": "case",
"lemma": "case",
"pos": "NOUN",
"dependency": "pobj",
"children": "[of]",
"explain": "noun, singular or mass",
"tag": "{74: 92, 'Number_sing': True}"
},
{
"word": "of",
"lemma": "of",
"pos": "ADP",
"dependency": "prep",
"children": "[dispute]",
"explain": "conjunction, subordinating or preposition",
"tag": "{74: 85}"
},
{
"word": "any",
"lemma": "any",
"pos": "DET",
"dependency": "det",
"children": "[]",
"explain": "determiner",
"tag": "{74: 90}"
},
{
"word": "dispute",
"lemma": "dispute",
"pos": "NOUN",
"dependency": "pobj",
"children": "[any, in]",
"explain": "noun, singular or mass",
"tag": "{74: 92, 'Number_sing': True}"
},
{
"word": "in",
"lemma": "in",
"pos": "ADP",
"dependency": "prep",
"children": "[terms]",
"explain": "conjunction, subordinating or preposition",
"tag": "{74: 85}"
},
{
"word": "terms",
"lemma": "term",
"pos": "NOUN",
"dependency": "pobj",
"children": "[of]",
"explain": "noun, plural",
"tag": "{74: 92, 'Number_plur': True}"
},
{
"word": "of",
"lemma": "of",
"pos": "ADP",
"dependency": "prep",
"children": "[determination]",
"explain": "conjunction, subordinating or preposition",
"tag": "{74: 85}"
},
{
"word": "determination",
"lemma": "determination",
"pos": "NOUN",
"dependency": "pobj",
"children": "[of]",
"explain": "noun, singular or mass",
"tag": "{74: 92, 'Number_sing': True}"
},
{
"word": "of",
"lemma": "of",
"pos": "ADP",
"dependency": "prep",
"children": "[value]",
"explain": "conjunction, subordinating or preposition",
"tag": "{74: 85}"
},
{
"word": "the",
"lemma": "the",
"pos": "DET",
"dependency": "det",
"children": "[]",
"explain": "determiner",
"tag": "{74: 90}"
},
{
"word": "value",
"lemma": "value",
"pos": "NOUN",
"dependency": "pobj",
"children": "[the, of]",
"explain": "noun, singular or mass",
"tag": "{74: 92, 'Number_sing': True}"
},
{
"word": "of",
"lemma": "of",
"pos": "ADP",
"dependency": "prep",
"children": "[receivables]",
"explain": "conjunction, subordinating or preposition",
"tag": "{74: 85}"
},
{
"word": "receivables",
"lemma": "receivables",
"pos": "NOUN",
"dependency": "pobj",
"children": "[]",
"explain": "noun, singular or mass",
"tag": "{74: 92, 'Number_sing': True}"
},
{
"word": ",",
"lemma": ",",
"pos": "PUNCT",
"dependency": "punct",
"children": "[]",
"explain": "punctuation mark, comma",
"tag": "{74: 97, 'PunctType_comm': True}"
},
{
"word": "the",
"lemma": "the",
"pos": "DET",
"dependency": "det",
"children": "[]",
"explain": "determiner",
"tag": "{74: 90}"
},
{
"word": "Bank",
"lemma": "Bank",
"pos": "PROPN",
"dependency": "nsubjpass",
"children": "[the, \u2019s, accounting]",
"explain": "noun, proper singular",
"tag": "{74: 96, 'NounType_prop': True, 'Number_sing': True}"
},
{
"word": "\u2019s",
"lemma": "\u2019s",
"pos": "PART",
"dependency": "punct",
"children": "[]",
"explain": "possessive ending",
"tag": "{74: 94, 'Poss_yes': True}"
},
{
"word": "accounting",
"lemma": "accounting",
"pos": "NOUN",
"dependency": "appos",
"children": "[,, books]",
"explain": "noun, singular or mass",
"tag": "{74: 92, 'Number_sing': True}"
},
{
"word": ",",
"lemma": ",",
"pos": "PUNCT",
"dependency": "punct",
"children": "[]",
"explain": "punctuation mark, comma",
"tag": "{74: 97, 'PunctType_comm': True}"
},
{
"word": "books",
"lemma": "book",
"pos": "NOUN",
"dependency": "conj",
"children": "[,, and, records]",
"explain": "noun, plural",
"tag": "{74: 92, 'Number_plur': True}"
},
{
"word": ",",
"lemma": ",",
"pos": "PUNCT",
"dependency": "punct",
"children": "[]",
"explain": "punctuation mark, comma",
"tag": "{74: 97, 'PunctType_comm': True}"
},
{
"word": "and",
"lemma": "and",
"pos": "CCONJ",
"dependency": "cc",
"children": "[]",
"explain": "conjunction, coordinating",
"tag": "{74: 89, 'ConjType_comp': True}"
},
{
"word": "all",
"lemma": "all",
"pos": "DET",
"dependency": "det",
"children": "[]",
"explain": "determiner",
"tag": "{74: 90}"
},
{
"word": "records",
"lemma": "record",
"pos": "NOUN",
"dependency": "conj",
"children": "[all]",
"explain": "noun, plural",
"tag": "{74: 92, 'Number_plur': True}"
},
{
"word": "shall",
"lemma": "shall",
"pos": "VERB",
"dependency": "aux",
"children": "[]",
"explain": "verb, modal auxiliary",
"tag": "{74: 100, 'VerbType_mod': True}"
},
{
"word": "be",
"lemma": "be",
"pos": "AUX",
"dependency": "auxpass",
"children": "[]",
"explain": "verb, base form",
"tag": "{74: 100, 'VerbForm_inf': True}"
},
{
"word": "taken",
"lemma": "take",
"pos": "VERB",
"dependency": "ccomp",
"children": "[that, in, ,, Bank, shall, be, as]",
"explain": "verb, past participle",
"tag": "{74: 100, 'Aspect_perf': True, 'Tense_past': True, 'VerbForm_part': True}"
},
{
"word": "as",
"lemma": "as",
"pos": "SCONJ",
"dependency": "prep",
"children": "[basis]",
"explain": "conjunction, subordinating or preposition",
"tag": "{74: 85}"
},
{
"word": "the",
"lemma": "the",
"pos": "DET",
"dependency": "det",
"children": "[]",
"explain": "determiner",
"tag": "{74: 90}"
},
{
"word": "basis",
"lemma": "basis",
"pos": "NOUN",
"dependency": "pobj",
"children": "[the, for]",
"explain": "noun, singular or mass",
"tag": "{74: 92, 'Number_sing': True}"
},
{
"word": "for",
"lemma": "for",
"pos": "ADP",
"dependency": "prep",
"children": "[determining]",
"explain": "conjunction, subordinating or preposition",
"tag": "{74: 85}"
},
{
"word": "determining",
"lemma": "determine",
"pos": "VERB",
"dependency": "pcomp",
"children": "[amount]",
"explain": "verb, gerund or present participle",
"tag": "{74: 100, 'Aspect_prog': True, 'Tense_pres': True, 'VerbForm_part': True}"
},
{
"word": "the",
"lemma": "the",
"pos": "DET",
"dependency": "det",
"children": "[]",
"explain": "determiner",
"tag": "{74: 90}"
},
{
"word": "receivable",
"lemma": "receivable",
"pos": "ADJ",
"dependency": "amod",
"children": "[]",
"explain": "adjective",
"tag": "{74: 84, 'Degree_pos': True}"
},
{
"word": "amount",
"lemma": "amount",
"pos": "NOUN",
"dependency": "dobj",
"children": "[the, receivable]",
"explain": "noun, singular or mass",
"tag": "{74: 92, 'Number_sing': True}"
},
{
"word": ".",
"lemma": ".",
"pos": "PUNCT",
"dependency": "punct",
"children": "[]",
"explain": "punctuation mark, sentence closer",
"tag": "{74: 97, 'PunctType_peri': True}"
}
]
}