### Install Pragmatic Segmenter Gem Source: https://github.com/diasks2/pragmatic_segmenter/blob/master/README.md Installs the Pragmatic Segmenter gem for Ruby. Requires Ruby 2.1.5 or above. ```bash gem install pragmatic_segmenter ``` -------------------------------- ### Add Pragmatic Segmenter to Rails Gemfile Source: https://github.com/diasks2/pragmatic_segmenter/blob/master/README.md Adds the Pragmatic Segmenter gem to a Ruby on Rails application's Gemfile. Ensure to run 'bundle install' after adding. ```ruby gem 'pragmatic_segmenter' ``` -------------------------------- ### Basic Sentence Segmentation in Ruby Source: https://context7.com/diasks2/pragmatic_segmenter/llms.txt Demonstrates basic sentence segmentation using the default English rules. It shows how the library handles common edge cases like abbreviations, email addresses, URLs, and currency. ```ruby require 'pragmatic_segmenter' # Basic segmentation (defaults to English) text = "Hello World. My name is Jonas. What is your name?" ps = PragmaticSegmenter::Segmenter.new(text: text) sentences = ps.segment # => ["Hello World.", "My name is Jonas.", "What is your name?"] # Handles abbreviations correctly text = "I work for the U.S. Government in Virginia. I have lived in the U.S. for 20 years." ps = PragmaticSegmenter::Segmenter.new(text: text) ps.segment # => ["I work for the U.S. Government in Virginia.", "I have lived in the U.S. for 20 years."] # Handles email addresses and URLs text = "Her email is Jane.Doe@example.com. I sent her an email." ps = PragmaticSegmenter::Segmenter.new(text: text) ps.segment # => ["Her email is Jane.Doe@example.com.", "I sent her an email."] # Handles numbers and currency text = "She has $100.00. It is in her bag." ps = PragmaticSegmenter::Segmenter.new(text: text) ps.segment # => ["She has $100.00.", "It is in her bag."] ``` -------------------------------- ### Multi-Language Sentence Segmentation in Ruby Source: https://context7.com/diasks2/pragmatic_segmenter/llms.txt Illustrates how to segment text in various languages using their respective ISO 639-1 codes. This feature allows for accurate sentence boundary detection tailored to specific language rules. ```ruby require 'pragmatic_segmenter' # German text segmentation text = "„Ich habe heute keine Zeit", sagte die Frau. Wir haben 1.000.000 Euro." ps = PragmaticSegmenter::Segmenter.new(text: text, language: 'de') ps.segment # => ["„Ich habe heute keine Zeit", sagte die Frau.", "Wir haben 1.000.000 Euro."] # Japanese text segmentation text = "これはペンです。それはマーカーです。" ps = PragmaticSegmenter::Segmenter.new(text: text, language: 'ja') ps.segment # => ["これはペンです。", "それはマーカーです。"] # Spanish text segmentation text = "¿Cómo está hoy? Espero que muy bien." ps = PragmaticSegmenter::Segmenter.new(text: text, language: 'es') ps.segment # => ["¿Cómo está hoy?", "Espero que muy bien."] # Armenian text segmentation text = "Այdelays delays delays delays delays:delays:" ps = PragmaticSegmenter::Segmenter.new(text: text, language: 'hy') ps.segment # => ["Այdelays delays delays delays :", "delays:"] # Arabic text segmentation text = "سؤال وجواب: ماذا حدث بعد الانتخابات؟" ps = PragmaticSegmenter::Segmenter.new(text: text, language: 'ar') ps.segment # => ["سؤال وجواب:", "ماذا حدث بعد الانتخابات؟"] # Supported language codes: # 'en' => English, 'de' => German, 'es' => Spanish, 'fr' => French # 'it' => Italian, 'ja' => Japanese, 'ru' => Russian, 'ar' => Arabic # 'am' => Amharic, 'hi' => Hindi, 'hy' => Armenian, 'fa' => Persian # 'my' => Burmese, 'ur' => Urdu, 'nl' => Dutch, 'pl' => Polish # 'zh' => Chinese, 'el' => Greek, 'bg' => Bulgarian, 'da' => Danish # 'kk' => Kazakh ``` -------------------------------- ### Text Cleaning in Ruby Source: https://context7.com/diasks2/pragmatic_segmenter/llms.txt Demonstrates the use of the `Cleaner` class for preprocessing text without performing sentence segmentation. This is useful for tasks like removing extraneous newlines. ```ruby require 'pragmatic_segmenter' # Clean text with errant newlines text = "It was a cold \nnight in the city." cleaner = PragmaticSegmenter::Cleaner.new(text: text, language: 'en') cleaner.clean # => "It was a cold night in the city." ``` -------------------------------- ### Segment Text with Named Entities and Exclamation Points (Ruby) Source: https://context7.com/diasks2/pragmatic_segmenter/llms.txt Demonstrates correct segmentation when exclamation points are part of named entities (e.g., 'Yahoo!'). Requires the 'pragmatic_segmenter' gem. ```ruby require 'pragmatic_segmenter' text = "She works at Yahoo! in the accounting department." ps = PragmaticSegmenter::Segmenter.new(text: text) ps.segment # => ["She works at Yahoo! in the accounting department."] ``` -------------------------------- ### Segment Text with Ellipsis as Sentence Boundary (Ruby) Source: https://context7.com/diasks2/pragmatic_segmenter/llms.txt Demonstrates how Pragmatic Segmenter treats an ellipsis followed by a space as a sentence boundary. Requires the 'pragmatic_segmenter' gem. ```ruby require 'pragmatic_segmenter' text = "I never meant that.... She left the store." ps = PragmaticSegmenter::Segmenter.new(text: text) ps.segment # => ["I never meant that....", "She left the store."] ``` -------------------------------- ### Lowercase List Segmentation by Newline Source: https://github.com/diasks2/pragmatic_segmenter/blob/master/README.md Shows how Pragmatic Segmenter can segment a list of lowercase items separated by newlines. ```python from pragmatic_segmenter.segmenter import Segmenter segmenter = Segmenter() text = "features\ncontact manager\nevents, activities\n" print(segmenter.segment(text)) # Output: ['features', 'contact manager', 'events, activities'] ``` -------------------------------- ### List Segmentation with Parentheses and Periods Source: https://github.com/diasks2/pragmatic_segmenter/blob/master/README.md Demonstrates how Pragmatic Segmenter can parse lists where items are enclosed in parentheses and may or may not end with a period. ```python from pragmatic_segmenter.segmenter import Segmenter segmenter = Segmenter() text1 = "1) The first item 2) The second item" print(segmenter.segment(text1)) # Output: ['1) The first item', '2) The second item'] text2 = "1) The first item. 2) The second item." print(segmenter.segment(text2)) # Output: ['1) The first item.', '2) The second item.'] ``` -------------------------------- ### PDF Document Sentence Segmentation in Ruby Source: https://context7.com/diasks2/pragmatic_segmenter/llms.txt Shows how to process text from PDF documents, specifically addressing issues with line breaks within sentences. The `doc_type: 'pdf'` option helps in rejoining fragmented sentences. ```ruby require 'pragmatic_segmenter' # PDF documents often have line breaks mid-sentence text = "This is a sentence\ncut off in the middle because pdf." ps = PragmaticSegmenter::Segmenter.new(text: text, language: 'en', doc_type: 'pdf') ps.segment # => ["This is a sentence cut off in the middle because pdf."] # Compare with non-PDF processing (without doc_type) ps_normal = PragmaticSegmenter::Segmenter.new(text: text, language: 'en') ps_normal.segment # => ["This is a sentence cut off in the middle because pdf."] # Complex PDF with lists and formatting text = "• 9. Stop smoking \n• 10. Get some rest \n \nYou have the best chance of having a healthy baby." ps = PragmaticSegmenter::Segmenter.new(text: text, language: 'en', doc_type: 'pdf') ps.segment # => ["• 9. Stop smoking", "• 10. Get some rest", "You have the best chance of having a healthy baby."] ``` -------------------------------- ### Segment Text with Mixed Double Punctuation (Ruby) Source: https://context7.com/diasks2/pragmatic_segmenter/llms.txt Illustrates handling of sentences ending with mixed double punctuation (e.g., '!?'). Requires the 'pragmatic_segmenter' gem. ```ruby require 'pragmatic_segmenter' text = "Hello!? Is that you?" ps = PragmaticSegmenter::Segmenter.new(text: text) ps.segment # => ["Hello!?", "Is that you?"] ``` -------------------------------- ### Standard Ellipsis as Sentence Boundary Source: https://github.com/diasks2/pragmatic_segmenter/blob/master/README.md Shows how Pragmatic Segmenter identifies standard ellipses (. . .) followed by a period as sentence boundaries. ```python from pragmatic_segmenter.segmenter import Segmenter segmenter = Segmenter() text = "If words are left off at the end of a sentence, and that is all that is omitted, indicate the omission with ellipsis marks (preceded and followed by a space) and then indicate the end of the sentence with a period . . . . Next sentence." print(segmenter.segment(text)) # Output: ['If words are left off at the end of a sentence, and that is all that is omitted, indicate the omission with ellipsis marks (preceded and followed by a space) and then indicate the end of the sentence with a period . . . .', 'Next sentence.'] ``` -------------------------------- ### Segment Text with Parenthetical Sentences (Ruby) Source: https://context7.com/diasks2/pragmatic_segmenter/llms.txt Shows how Pragmatic Segmenter handles sentences containing parenthetical clauses, maintaining correct sentence boundaries. Requires the 'pragmatic_segmenter' gem. ```ruby require 'pragmatic_segmenter' text = "He teaches science (He previously worked for 5 years as an engineer.) at the local University." ps = PragmaticSegmenter::Segmenter.new(text: text) ps.segment # => ["He teaches science (He previously worked for 5 years as an engineer.) at the local University."] ``` -------------------------------- ### List Segmentation with Bullets and Hyphens Source: https://github.com/diasks2/pragmatic_segmenter/blob/master/README.md Shows how Pragmatic Segmenter can segment lists that use bullet points (•) or hyphens (⁃) as list markers. ```python from pragmatic_segmenter.segmenter import Segmenter segmenter = Segmenter() text1 = "• 9. The first item • 10. The second item" print(segmenter.segment(text1)) # Output: ['• 9. The first item', '• 10. The second item'] text2 = "⁃9. The first item ⁃10. The second item" print(segmenter.segment(text2)) # Output: ['⁃9. The first item', '⁃10. The second item'] ``` -------------------------------- ### Alphabetical List Segmentation Source: https://github.com/diasks2/pragmatic_segmenter/blob/master/README.md Demonstrates Pragmatic Segmenter's capability to parse alphabetical lists, correctly identifying each item. ```python from pragmatic_segmenter.segmenter import Segmenter segmenter = Segmenter() text = "a. The first item b. The second item c. The third list item" print(segmenter.segment(text)) # Output: ['a. The first item', 'b. The second item', 'c. The third list item'] ``` -------------------------------- ### Geo Coordinate and Sentence Boundary Detection Source: https://github.com/diasks2/pragmatic_segmenter/blob/master/README.md Demonstrates Pragmatic Segmenter's ability to correctly identify sentence boundaries even when numbers with periods are present, like in geographical coordinates. ```python from pragmatic_segmenter.segmenter import Segmenter segmenter = Segmenter() text = "You can find it at N°. 1026.253.553. That is where the treasure is." print(segmenter.segment(text)) # Output: ['You can find it at N°. 1026.253.553.', 'That is where the treasure is.'] ``` -------------------------------- ### Named Entity with Exclamation Point Segmentation Source: https://github.com/diasks2/pragmatic_segmenter/blob/master/README.md Illustrates how Pragmatic Segmenter handles sentences containing named entities followed by an exclamation point, ensuring correct sentence segmentation. ```python from pragmatic_segmenter.segmenter import Segmenter segmenter = Segmenter() text = "She works at Yahoo! in the accounting department." print(segmenter.segment(text)) # Output: ['She works at Yahoo! in the accounting department.'] ``` -------------------------------- ### Ellipsis Segmentation with Square Brackets Source: https://github.com/diasks2/pragmatic_segmenter/blob/master/README.md Illustrates Pragmatic Segmenter's handling of ellipses enclosed in square brackets within a sentence. ```python from pragmatic_segmenter.segmenter import Segmenter segmenter = Segmenter() text = "Bohr [...] used the analogy of parallel stairways [...]" (Smith 55)." print(segmenter.segment(text)) # Output: ['"Bohr [...] used the analogy of parallel stairways [...]" (Smith 55).'] ``` -------------------------------- ### List Segmentation with Periods as Delimiters Source: https://github.com/diasks2/pragmatic_segmenter/blob/master/README.md Illustrates Pragmatic Segmenter's ability to handle lists where periods are used to mark list items, with or without trailing periods. ```python from pragmatic_segmenter.segmenter import Segmenter segmenter = Segmenter() text1 = "1. The first item 2. The second item" print(segmenter.segment(text1)) # Output: ['1. The first item', '2. The second item'] text2 = "1. The first item. 2. The second item." print(segmenter.segment(text2)) # Output: ['1. The first item.', '2. The second item.'] ``` -------------------------------- ### Segment Text with No Whitespace Between Sentences (Ruby) Source: https://context7.com/diasks2/pragmatic_segmenter/llms.txt Illustrates Pragmatic Segmenter's ability to segment sentences even when there is no whitespace separating them. Requires the 'pragmatic_segmenter' gem. ```ruby require 'pragmatic_segmenter' text = "Hello world.Today is Tuesday.Mr. Smith went to the store." ps = PragmaticSegmenter::Segmenter.new(text: text) ps.segment # => ["Hello world.", "Today is Tuesday.", "Mr. Smith went to the store."] ``` -------------------------------- ### Ellipsis as Non-Sentence Boundary Source: https://github.com/diasks2/pragmatic_segmenter/blob/master/README.md Illustrates how Pragmatic Segmenter treats ellipses (...) within a sentence as non-sentence boundaries, keeping the text as a single segment. ```python from pragmatic_segmenter.segmenter import Segmenter segmenter = Segmenter() text = "I wasn’t really ... well, what I mean...see . . . what I'm saying, the thing is . . . I didn’t mean it." print(segmenter.segment(text)) # Output: ['I wasn’t really ... well, what I mean...see . . . what I'm saying, the thing is . . . I didn’t mean it.'] ``` -------------------------------- ### No Whitespace Between Sentences Segmentation Source: https://github.com/diasks2/pragmatic_segmenter/blob/master/README.md Demonstrates Pragmatic Segmenter's ability to correctly segment sentences even when there is no whitespace between them, including cases with abbreviations like 'Mr.'. ```python from pragmatic_segmenter.segmenter import Segmenter segmenter = Segmenter() text = "Hello world.Today is Tuesday.Mr. Smith went to the store and bought 1,000.That is a lot." print(segmenter.segment(text)) # Output: ['Hello world.', 'Today is Tuesday.', 'Mr. Smith went to the store and bought 1,000.', 'That is a lot.'] ``` -------------------------------- ### Non-Standard Ellipsis as Sentence Boundary Source: https://github.com/diasks2/pragmatic_segmenter/blob/master/README.md Demonstrates Pragmatic Segmenter's ability to segment sentences where ellipses (....) are used non-standardly to mark the end of a sentence. ```python from pragmatic_segmenter.segmenter import Segmenter segmenter = Segmenter() text = "I never meant that.... She left the store." print(segmenter.segment(text)) # Output: ['I never meant that....', 'She left the store.'] ``` -------------------------------- ### Ellipsis Segmentation at End of Quotation Source: https://github.com/diasks2/pragmatic_segmenter/blob/master/README.md Demonstrates how Pragmatic Segmenter correctly segments sentences ending with an ellipsis within a quotation. ```python from pragmatic_segmenter.segmenter import Segmenter segmenter = Segmenter() text = "Thoreau argues that by simplifying one’s life, “the laws of the universe will appear less complex. . . .”" print(segmenter.segment(text)) # Output: ['Thoreau argues that by simplifying one’s life, “the laws of the universe will appear less complex. . . .”'] ``` -------------------------------- ### Distinguishing 'I' as Sentence Boundary vs. Abbreviation Source: https://github.com/diasks2/pragmatic_segmenter/blob/master/README.md Shows Pragmatic Segmenter's logic in differentiating between the pronoun 'I' at the end of a sentence and 'I.' as part of an abbreviation. ```python from pragmatic_segmenter.segmenter import Segmenter segmenter = Segmenter() text = "We make a good team, you and I. Did you see Albert I. Jones yesterday?" print(segmenter.segment(text)) # Output: ['We make a good team, you and I.', 'Did you see Albert I. Jones yesterday?'] ``` -------------------------------- ### Segment Sentences in English Source: https://github.com/diasks2/pragmatic_segmenter/blob/master/README.md Segments a given text into sentences using the default English language settings. The library defaults to English if no language is specified. ```ruby text = "Hello world. My name is Mr. Smith. I work for the U.S. Government and I live in the U.S. I live in New York." ps = PragmaticSegmenter::Segmenter.new(text: text) ps.segment # => ["Hello world.", "My name is Mr. Smith.", "I work for the U.S. Government and I live in the U.S.", "I live in New York."] ``` -------------------------------- ### Handling Errant Newlines in Sentences Source: https://github.com/diasks2/pragmatic_segmenter/blob/master/README.md Illustrates how Pragmatic Segmenter deals with sentences that have unexpected newlines within them, often due to PDF formatting. ```python from pragmatic_segmenter.segmenter import Segmenter segmenter = Segmenter() text1 = "This is a sentence\ncut off in the middle because pdf." print(segmenter.segment(text1)) # Output: ['This is a sentence\ncut off in the middle because pdf.'] text2 = "It was a cold \nnight in the city." print(segmenter.segment(text2)) # Output: ['It was a cold night in the city.'] ``` -------------------------------- ### Segment Text with 4-Dot Ellipsis (Ruby) Source: https://context7.com/diasks2/pragmatic_segmenter/llms.txt Illustrates handling of a 4-dot ellipsis (period + ellipsis) which can sometimes act as a sentence boundary. Requires the 'pragmatic_segmenter' gem. ```ruby require 'pragmatic_segmenter' text = "One further habit which was somewhat weakened . . . was that of combining words. . . . The practice was not abandoned. . . ." ps = PragmaticSegmenter::Segmenter.new(text: text) ps.segment # => ["One further habit which was somewhat weakened . . . was that of combining words.", ". . . The practice was not abandoned. . . ."] ``` -------------------------------- ### Segment Sentences in Armenian Source: https://github.com/diasks2/pragmatic_segmenter/blob/master/README.md Segments a given text into sentences by explicitly specifying the language as Armenian ('hy'). This ensures accurate segmentation for Armenian text. ```ruby text = "Այսօր երկուշաբթի է: Ես գնում եմ աշխատանքի:" ps = PragmaticSegmenter::Segmenter.new(text: text, language: 'hy') ps.segment # => ["Այսօր երկուշաբթի է:", "Ես գնում եմ աշխատանքի:"] ``` -------------------------------- ### 4-Dot Ellipsis Segmentation Source: https://github.com/diasks2/pragmatic_segmenter/blob/master/README.md Shows Pragmatic Segmenter's handling of 4-dot ellipses (. . . .) as sentence boundaries. ```python from pragmatic_segmenter.segmenter import Segmenter segmenter = Segmenter() text = "One further habit which was somewhat weakened . . . was that of combining words into self-interpreting compounds. . . . The practice was not abandoned. . . ." print(segmenter.segment(text)) # Output: ['One further habit which was somewhat weakened . . . was that of combining words into self-interpreting compounds.', '. . . The practice was not abandoned. . . .'] ``` -------------------------------- ### Segment Sentences in PDF (No Cleaning) Source: https://github.com/diasks2/pragmatic_segmenter/blob/master/README.md Segments text as a PDF document without applying text cleaning and preprocessing. This can result in sentences being split at line breaks. ```ruby text = "This is a sentence\ncut off in the middle because pdf." ps = PragmaticSegmenter::Segmenter.new(text: text, language: 'en', doc_type: 'pdf', clean: false) ps.segment # => ["This is a sentence cut", "off in the middle because pdf."] ``` -------------------------------- ### Segment Text with Ellipsis as Non-Sentence Boundary (Ruby) Source: https://context7.com/diasks2/pragmatic_segmenter/llms.txt Shows Pragmatic Segmenter's ability to recognize ellipses used as hesitation or continuation within a sentence, not as a boundary. Requires the 'pragmatic_segmenter' gem. ```ruby require 'pragmatic_segmenter' text = "I wasn't really ... well, what I mean...see . . . what I'm saying, the thing is . . . I didn't mean it." ps = PragmaticSegmenter::Segmenter.new(text: text) ps.segment # => ["I wasn't really ... well, what I mean...see . . . what I'm saying, the thing is . . . I didn't mean it."] ``` -------------------------------- ### Segment Text with Double Punctuation (Ruby) Source: https://context7.com/diasks2/pragmatic_segmenter/llms.txt Shows Pragmatic Segmenter's capability to correctly segment sentences ending with double punctuation marks. Requires the 'pragmatic_segmenter' gem. ```ruby require 'pragmatic_segmenter' text = "Hello!! Long time no see." ps = PragmaticSegmenter::Segmenter.new(text: text) ps.segment # => ["Hello!!", "Long time no see."] ``` -------------------------------- ### Segment Sentences in PDF Document Type (English) Source: https://github.com/diasks2/pragmatic_segmenter/blob/master/README.md Segments text, treating it as a PDF document, which affects how line breaks are handled. English language is specified. ```ruby text = "This is a sentence\ncut off in the middle because pdf." ps = PragmaticSegmenter::Segmenter.new(text: text, language: 'en', doc_type: 'pdf') ps.segment # => ["This is a sentence cut off in the middle because pdf."] ``` -------------------------------- ### Segment Text with Ellipsis at End of Quotation (Ruby) Source: https://context7.com/diasks2/pragmatic_segmenter/llms.txt Demonstrates how Pragmatic Segmenter handles ellipses within quoted text, preserving the sentence structure. Requires the 'pragmatic_segmenter' gem. ```ruby require 'pragmatic_segmenter' text = "Thoreau argues that \"the laws of the universe will appear less complex. . . .\"" ps = PragmaticSegmenter::Segmenter.new(text: text) ps.segment # => ["Thoreau argues that \"the laws of the universe will appear less complex. . . .\""] ``` -------------------------------- ### Clean Text for PDF Document Type Source: https://github.com/diasks2/pragmatic_segmenter/blob/master/README.md Cleans and preprocesses text specifically for PDF documents, handling line breaks and other formatting issues. This does not perform sentence segmentation. ```ruby text = "This is a sentence\ncut off in the middle because pdf." ps = PragmaticSegmenter::Cleaner.new(text: text, doc_type: 'pdf') ps.clean # => "This is a sentence cut off in the middle because pdf." ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.