在子弹和编号处拆分句子?

问题描述 投票:1回答:1

我试图将文本输入到我的文字处理器中,先将其分割成句子,然后再分割成单词。

例如一个段落。

When the blow was repeated,together with an admonition in
childish sentences, he turned over upon his back, and held his paws in a peculiar manner.

1) This a numbered sentence
2) This is the second numbered sentence

At the same time with his ears and his eyes he offered a small prayer to the child.

Below are the examples
- This an example of bullet point sentence
- This is also an example of bullet point sentence

我试过以下代码

from nltk.tokenize import TweetTokenizer, sent_tokenize

tokenizer_words = TweetTokenizer()
tokens_sentences = [tokenizer_words.tokenize(t) for t in 
nltk.sent_tokenize(input_text)]
print(tokens_sentences)
import nltk

sent_text = nltk.sent_tokenize(text) # this gives us a list of sentences
# now loop over each sentence and tokenize it separately
for sentence in sent_text:
    tokenized_text = nltk.word_tokenize(sentence)
    print(tokenized_text)

我得到的输出

[
['When', 'the', 'blow', 'was', 'repeated', ',', 'together', 'with', 'an', 'admonition', 'in', 'childish', 'sentences', ',', 'he', 'turned', 'over', 'upon', 'his', 'back', ',', 'and', 'held', 'his', 'paws', 'in', 'a', 'peculiar', 'manner', '.'], 
['1', ')', 'This', 'a', 'numbered', 'sentence', '2', ')', 'This', 'is', 'the', 'second', 'numbered', 'sentence','At', 'the', 'same', 'time', 'with', 'his', 'ears', 'and', 'his', 'eyes', 'he', 'offered', 'a', 'small', 'prayer', 'to', 'the', 'child', '.']
['Below', 'are', 'the', 'examples', '-', 'This', 'an', 'example', 'of', 'bullet', 'point', 'sentence',
'-', 'This', 'also','an', 'example', 'of', 'bullet', 'point', 'sentence']
]

所需输出

[
['When', 'the', 'blow', 'was', 'repeated', ',', 'together', 'with', 'an', 'admonition', 'in', 'childish', 'sentences', ',', 'he', 'turned', 'over', 'upon', 'his', 'back', ',', 'and', 'held', 'his', 'paws', 'in', 'a', 'peculiar', 'manner', '.'], 
['1', ')', 'This', 'a', 'numbered', 'sentence']
['2', ')', 'This', 'is', 'the', 'second', 'numbered', 'sentence']
['At', 'the', 'same', 'time', 'with', 'his', 'ears', 'and', 'his', 'eyes', 'he', 'offered', 'a', 'small', 'prayer', 'to', 'the', 'child', '.']
['Below', 'are', 'the', 'examples']
['-', 'This', 'an', 'example', 'of', 'bullet', 'point', 'sentence']
['-', 'This', 'also','an', 'example', 'of', 'bullet', 'point', 'sentence']
]

如何在Bullets和Numbering处分句?

关于spaCy的解决方案也将是非常有用的。

nlp nltk sentence
1个回答
0
投票

我不确定spaCy。在Ruby中,你可以使用 PragmaticSegmenter实用型记号器.

text = "When the blow was repeated,together with an admonition in childish sentences, he turned over upon his back, and held his paws in a peculiar manner.\n\n1) This a numbered sentence\n2) This is the second numbered sentence\n\nAt the same time with his ears and his eyes he offered a small prayer to the child.\n\nBelow are the examples\n- This an example of bullet point sentence\n- This is also an example of bullet point sentence"

final_array = []
segments = PragmaticSegmenter::Segmenter.new(text: text).segment
segments.each do |segment|
  final_array << PragmaticTokenizer::Tokenizer.new(downcase: false).tokenize(segment)
end

final_array
=> 
[
  ["When", "the", "blow", "was", "repeated", ",", "together", "with", "an", "admonition", "in", "childish", "sentences", ",", "he", "turned", "over", "upon", "his", "back", ",", "and", "held", "his", "paws", "in", "a", "peculiar", "manner", "."], 
  ["1", ")", "This", "a", "numbered", "sentence"], 
  ["2", ")", "This", "is", "the", "second", "numbered", "sentence"], 
  ["At", "the", "same", "time", "with", "his", "ears", "and", "his", "eyes", "he", "offered", "a", "small", "prayer", "to", "the", "child", "."], 
  ["Below", "are", "the", "examples"], 
  ["-", "This", "an", "example", "of", "bullet", "point", "sentence"], 
  ["-", "This", "is", "also", "an", "example", "of", "bullet", "point", "sentence"]
]

0
投票

这可以是一个解决方案.你可以根据你的数据来定制它。

text = """When the blow was repeated,together with an admonition in
childish sentences, he turned over upon his back, and held his paws in a peculiar manner.

1) This a numbered sentence
2) This is the second numbered sentence

At the same time with his ears and his eyes he offered a small prayer to the child.

Below are the examples
- This an example of bullet point sentence
- This is also an example of bullet point sentence"""



import re
import nltk

sentences = nltk.sent_tokenize(text)
results = []

for sent in sentences:
    sent = re.sub(r'(\n)(-|[0-9])', r"\1\n\2", sent)
    sent = sent.split('\n\n')
    for s in sent:
        results.append(nltk.word_tokenize(s))

results

[
['When', 'the', 'blow', 'was', 'repeated', ',', 'together', 'with', 'an', 'admonition', 'in', 'childish', 'sentences', ',', 'he', 'turned', 'over', 'upon', 'his', 'back', ',', 'and', 'held', 'his', 'paws', 'in', 'a', 'peculiar', 'manner', '.'], 
['1', ')', 'This', 'a', 'numbered', 'sentence']
['2', ')', 'This', 'is', 'the', 'second', 'numbered', 'sentence']
['At', 'the', 'same', 'time', 'with', 'his', 'ears', 'and', 'his', 'eyes', 'he', 'offered', 'a', 'small', 'prayer', 'to', 'the', 'child', '.']
['Below', 'are', 'the', 'examples']
['-', 'This', 'an', 'example', 'of', 'bullet', 'point', 'sentence']
['-', 'This', 'also','an', 'example', 'of', 'bullet', 'point', 'sentence']
]
© www.soinside.com 2019 - 2024. All rights reserved.