简体   繁体   中英

split() in perl

How to divide text into sentences. In my opinion, I should use split() and print them, but I donˇt now have. I am just started learning Perl.

My text

A block of text is a stack of line boxes. In the case of 'left', 'right' and 'center', this property specifies how the inline-level boxes within each line box align with respect to the line box's left and right sides; alignment is not with respect to the viewport. In the case of 'justify', this property specifies that the inline-level boxes are to be made flush with both sides of the line box if possible, by expanding or contracting the contents of inline boxes, else aligned as for the initial value. See also 'letter-spacing' and 'word-spacing'.

如果这实际上不是家庭作业,那么我只会使用处理该问题的CPAN模块之一,例如Lingua :: Sentence ,它似乎正在积极开发中。

One way to do it is using split in combination with look-behind.

 perl -nlwe 'print for split /(?<=\S[.!?])\s+/' < data.txt

This works for your sample data.

What you want to do here is eliminate the space separating sentences. An end of sentence is defined as one of .!? preceded by a non-whitespace character. Tweak as desired.

try

$paragraph = "Text. Text";
@sentences = split(/\./, $paragraph);
print @sentences;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM