Tôi chưa thử nhưng nếu tiếng Anh là ngôn ngữ duy nhất bạn quan tâm, tôi khuyên bạn nên xem Lingua::EN::Readability.
Lingua :: EN :: Dễ đọc là mô-đun Ruby tính toán số liệu thống kê trên văn bản tiếng Anh. Nó có thể cung cấp số lượng từ, câu và âm tiết. Nó cũng có thể tính toán một số biện pháp dễ đọc, chẳng hạn như chỉ số Sương mù và mức Flesch-Kincaid. Các gói phần mềm bao gồm các mô-đun Lingua :: EN :: Sentence, mà phá vỡ văn bản tiếng Anh thành câu chú ý đến chữ viết tắt, và Lingua :: EN :: Âm tiết, có thể đoán số lượng âm tiết trong một từ tiếng Anh bằng văn bản.Nếu một cuốn từ điển phát âm có sẵn nó có thể tra cứu số lượng âm tiết trong từ điển cho chính xác hơn
Các bit bạn muốn là trong sentence.rb
như sau:
module Lingua
module EN
# The module Lingua::EN::Sentence takes English text, and attempts to split it
# up into sentences, respecting abbreviations.
module Sentence
EOS = "\001" # temporary end of sentence marker
Titles = [ 'jr', 'mr', 'mrs', 'ms', 'dr', 'prof', 'sr', 'sen', 'rep',
'rev', 'gov', 'atty', 'supt', 'det', 'rev', 'col','gen', 'lt',
'cmdr', 'adm', 'capt', 'sgt', 'cpl', 'maj' ]
Entities = [ 'dept', 'univ', 'uni', 'assn', 'bros', 'inc', 'ltd', 'co',
'corp', 'plc' ]
Months = [ 'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul',
'aug', 'sep', 'oct', 'nov', 'dec', 'sept' ]
Days = [ 'mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun' ]
Misc = [ 'vs', 'etc', 'no', 'esp', 'cf' ]
Streets = [ 'ave', 'bld', 'blvd', 'cl', 'ct', 'cres', 'dr', 'rd', 'st' ]
@@abbreviations = Titles + Entities + Months + Days + Streets + Misc
# Split the passed text into individual sentences, trim these and return
# as an array. A sentence is marked by one of the punctuation marks ".", "?"
# or "!" followed by whitespace. Sequences of full stops (such as an
# ellipsis marker "..." and stops after a known abbreviation are ignored.
def Sentence.sentences(text)
text = text.dup
# initial split after punctuation - have to preserve trailing whitespace
# for the ellipsis correction next
# would be nicer to use look-behind and look-ahead assertions to skip
# ellipsis marks, but Ruby doesn't support look-behind
text.gsub!(/([\.?!](?:\"|\'|\)|\]|\})?)(\s+)/) { $1 << EOS << $2 }
# correct ellipsis marks and rows of stops
text.gsub!(/(\.\.\.*)#{EOS}/) { $1 }
# correct abbreviations
# TODO - precompile this regex?
text.gsub!(/(#{@@abbreviations.join("|")})\.#{EOS}/i) { $1 << '.' }
# split on EOS marker, strip gets rid of trailing whitespace
text.split(EOS).map { | sentence | sentence.strip }
end
# add a list of abbreviations to the list that's used to detect false
# sentence ends. Return the current list of abbreviations in use.
def Sentence.abbreviation(*abbreviations)
@@abbreviations += abbreviations
@@abbreviations
end
end
end
end
Có bất kỳ quy tắc cụ thể.Nếu bạn có thể cho chúng tôi biết các quy tắc bằng tiếng Anh, tôi chắc chắn chúng tôi (hoặc bạn) sẽ có thể viết mã giải pháp. Ví dụ: các từ viết tắt như 'abbr' có một điểm dừng đầy đủ sau chúng không? Nếu bạn định phân tích sách giáo khoa ngữ pháp, bạn có thể ổn với các giải pháp đơn giản, nhưng nếu bạn đang dùng văn bản tùy ý thì mọi giải pháp sẽ có những thiếu sót, như ... bạn biết không? – Marcin
Trình gắn thẻ POS quá mức cần thiết. Sử dụng trình mã hóa dựa trên NLP và các quy tắc của bạn sẽ đơn giản hơn. –