Skip to content

Instantly share code, notes, and snippets.

@lsiddiqsunny
Created June 16, 2023 15:04
Show Gist options
  • Save lsiddiqsunny/7c367ed15a2a5d39ef52c7c045bca05a to your computer and use it in GitHub Desktop.
Save lsiddiqsunny/7c367ed15a2a5d39ef52c7c045bca05a to your computer and use it in GitHub Desktop.
Detect partial sentence and interrogation using space library.
import spacy
nlp = spacy.load("en_core_web_sm")
python_comment = """
This function calculates the sum of two numbers.
What are the parameters of this function?
How does this function handle errors?
"""
doc = nlp(python_comment)
question_sentences = [
sentence.text.strip()
for sentence in doc.sents
if sentence.text.strip().endswith('?')
]
for question_sentence in question_sentences:
print(question_sentence)
import spacy
nlp = spacy.load("en_core_web_sm")
text = """
This is a complete sentence. Partial sentence without a verb. Another complete sentence.
"""
doc = nlp(text)
partial_sentences = []
for sentence in doc.sents:
if len(sentence) > 1 and not any(token.dep_ == 'ROOT' for token in sentence):
partial_sentences.append(sentence.text.strip())
for partial_sentence in partial_sentences:
print(partial_sentence)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment