-
Notifications
You must be signed in to change notification settings - Fork 34
Sentences split on newlines #177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Thomzoy
wants to merge
6
commits into
master
Choose a base branch
from
sentences_split_on_lnewlines
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1072b1e
feat: add option to sentenciser pipeline regarding newlines
Thomzoy 4b1454d
chore: changelog
Thomzoy dcaed0f
Update changelog.md
Thomzoy de77393
Linting
Thomzoy 8d56402
Merge branch 'sentences_split_on_lnewlines' of https://github.com/aph…
Thomzoy 5bfff51
updates
Thomzoy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,13 +3,13 @@ from typing import Iterable, List, Optional | |
from libcpp cimport bool | ||
|
||
# from spacy.typedefs cimport attr_t | ||
from spacy.attrs cimport IS_ALPHA, IS_ASCII, IS_DIGIT, IS_LOWER, IS_PUNCT, IS_SPACE | ||
from spacy.attrs cimport IS_ALPHA, IS_ASCII, IS_DIGIT, IS_LOWER, IS_PUNCT, IS_SPACE, IS_UPPER | ||
from spacy.lexeme cimport Lexeme | ||
from spacy.tokens.doc cimport Doc | ||
from spacy.tokens.token cimport TokenC | ||
from spacy.vocab cimport Vocab | ||
|
||
from .terms import punctuation | ||
from .terms import punctuation, uppercase | ||
|
||
|
||
cdef class SentenceSegmenter(object): | ||
|
@@ -37,6 +37,7 @@ cdef class SentenceSegmenter(object): | |
punct_chars: Optional[List[str]], | ||
use_endlines: bool, | ||
ignore_excluded: bool = True, | ||
split_on_newlines: Optional[str] = "with_capitalized", | ||
): | ||
|
||
if punct_chars is None: | ||
|
@@ -48,6 +49,23 @@ cdef class SentenceSegmenter(object): | |
self.endline_hash = vocab.strings["ENDLINE"] | ||
self.punct_chars_hash = {vocab.strings[c] for c in punct_chars} | ||
self.capitalized_shapes_hash = {vocab.strings[shape] for shape in ("Xx", "Xxx", "Xxxx", "Xxxxx")} | ||
self.capitalized_chars_hash = {vocab.strings[letter] for letter in uppercase} | ||
|
||
options = { | ||
"with_capitalized": 0, | ||
"with_uppercase": 1, | ||
False: 2 | ||
} | ||
chosen = options.get(split_on_newlines, None) | ||
if chosen is None: | ||
raise ValueError( | ||
( | ||
"Incorrect value for 'split_on_newlines'. " | ||
f"Provided: {split_on_newlines}\n" | ||
f"Available: {options}." | ||
) | ||
) | ||
self.split_on_newlines = chosen | ||
|
||
if use_endlines: | ||
print("The use_endlines is deprecated and has been replaced by the ignore_excluded parameter") | ||
|
@@ -90,16 +108,21 @@ cdef class SentenceSegmenter(object): | |
is_newline = Lexeme.c_check_flag(token.lex, IS_SPACE) and token.lex.orth == self.newline_hash | ||
|
||
if seen_period or seen_newline: | ||
if seen_period and Lexeme.c_check_flag(token.lex, IS_DIGIT): | ||
continue | ||
if is_in_punct_chars or is_newline or Lexeme.c_check_flag(token.lex, IS_PUNCT): | ||
continue | ||
if seen_period and Lexeme.c_check_flag(token.lex, IS_DIGIT): | ||
continue | ||
seen_newline = False | ||
seen_period = False | ||
Comment on lines
+124
to
+127
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a correction. Before, a text like "Mesure du H.3 négatif" produced two sentences, "Mesure du H.3" and "négatif" since |
||
if seen_period: | ||
doc.c[i].sent_start = 1 | ||
seen_newline = False | ||
seen_period = False | ||
else: | ||
doc.c[i].sent_start = 1 if self.capitalized_shapes_hash.const_find(token.lex.shape) != self.capitalized_shapes_hash.const_end() else -1 | ||
if self.split_on_newlines == WITH_UPPERCASE: | ||
doc.c[i].sent_start = 1 if self.capitalized_chars_hash.const_find(token.lex.prefix) != self.capitalized_chars_hash.const_end() else -1 | ||
elif self.split_on_newlines == WITH_CAPITALIZED: | ||
doc.c[i].sent_start = 1 if self.capitalized_shapes_hash.const_find(token.lex.shape) != self.capitalized_shapes_hash.const_end() else -1 | ||
seen_newline = False | ||
seen_period = False | ||
elif is_in_punct_chars: | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.