Source code for cc4q.pipeline.segment.inline
#!/usr/bin/env python3
"""Sentence segmentation for inline ``#`` comments.
Inline comments are short but may contain several sentences (and common
abbreviations). spaCy's sentence boundary detector is used so a single
comment can yield multiple SCCUs while keeping the original text intact.
"""
import spacy
from typing import List
__all__ = ["segment_inline_comment", "segment_inline_comment_advanced"]
try:
nlp = spacy.load('en_core_web_sm')
except OSError:
try:
nlp = spacy.load('en_core_web_md')
except OSError:
try:
nlp = spacy.load('en_core_web_lg')
except OSError:
raise ImportError('Please install the spaCy English model: python -m spacy download en_core_web_sm')
def split_comment_into_sentences(text: str) -> List[str]:
doc = nlp(text)
return [sent.text.strip() for sent in doc.sents if sent.text.strip()]
if __name__ == '__main__':
test_comments = ['Initialize the quantum register. Set default parameters.', 'This is a single sentence comment.', 'First sentence! Second sentence? Third sentence.', "Check if Dr. Smith's algorithm works. It should be fine.", 'Short.', '# Remove comment symbols and split properly.', '// Multiple comment styles supported here.', '* Bullet point style comment with multiple sentences. This is the second one.']
print('Testing inline comment segmentation:')
for i, comment in enumerate(test_comments, 1):
sentences = segment_inline_comment(comment)
print(f'\n{i}. original: {comment}')
print(f' segmentation result ({len(sentences)} sentences):')
for j, sentence in enumerate(sentences, 1):
print(f' {j}. {sentence}')