Compromise NLP

repository·master·Indexed 11 days ago

https://github.com/spencermountain/compromise

A modest natural language processing (NLP) library version 14.16.0 designed to turn text into structured data. It is optimized to be small and fast for parsing, transforming, and extracting information. The ecosystem includes plugins such as compromise-dates for temporal extraction, compromise-paragraphs for text segmentation, and compromise-payload for metadata storage.

Tokens
54.5K
Snippets
200
Records
286
Agent score
95%

What's inside Compromise

  1. What is the compromise-wikipedia plugin?

    master

    The compromise-wikipedia plugin is an experimental tool designed for efficient Named-entity Recognition (NER). It uses a highly-compressed list of ~38,000 popular Wikipedia articles to scan text.

    Key characteristics:

    • Size: Approximately 300kb (minified).
    • Purpose: Acts as a proof-of-concept for compressing large lexicons for client-side use.
    • Limitation: It does not currently include Wikipedia's redirects.
  2. Lexicon data constraints and formatting

    master

    When working with or adding to the lexicon, be aware of the following rules:

    • Case Sensitivity: Lexicon words are lowercased.
    • Compression: Words are compressed using efrt.
    • Reserved Characters: The following characters are reserved and should not be used: [0-9,;!:|¦].
    • Ambiguity: Avoid adding ambiguous words to the main lexicon. For example, if a word like 'ray' fits better as a specific type (like a person or date), it should be handled in specific switch files (e.g., ./switches/person-date.js) rather than the general lexicon.
    • Conjugations: Many word-lists have conjugations applied automatically (e.g., #Singular words are automatically pluralized).
  3. Use selection methods to filter and extract specific parts of text

    master

    Compromise allows you to create specialized sub-views of a document using selection methods. These methods return a View containing only the matched terms, which can then be further processed with specific methods related to that type of content.

    Common selection methods include:

    • .nouns(): Noun phrases.
    • .verbs(): Verb phrases.
    • .people(): Person names.
    • .places(): Location names.
    • .organizations(): Companies and organizations.
    • .topics(): A combination of people, places, and organizations.
    • .numbers(): Numeric values.
    • .sentences(): Full sentences.
    • .adjectives(): Words like "clean".
    • .adverbs(): Words like "quickly".
    • .urls(), .emails(), .atMentions(), .phoneNumbers(), .addresses().
  4. Use .paragraphs() to segment text

    master

    The .paragraphs() method is a plugin for compromise that segments a document into paragraph objects.

    Mental Model: This plugin acts as a wrapper for sentence objects. This allows you to treat a group of sentences as a single paragraph unit (using methods like .text() or .json()), while still being able to 'drop back down' to the sentence level using .sentences() to continue standard NLP processing.

    Note on Mutability: Paragraph objects are mutable. When you apply transformations like .filter(), you are modifying the document structure.

    let str = `What's with these homies dissin' my girl? Why do they gotta front? 
    
    What did we ever do to these guys that made them so violent?
    
    Woo-hoo, but you know I'm yours.`
    
    let doc = nlp(str).paragraphs()
  5. Match across sentence boundaries using compromise-paragraphs

    master

    By default, matching in compromise does not cross sentence boundaries. For example, nlp("that's it. Back to Winnipeg!").has('it back') will return false because 'it' and 'back' are in different sentences.

    To perform matching across multiple sentences or paragraphs, use the compromise-paragraphs plugin.

    nlp("that's it. Back to Winnipeg!").has('it back')   // false
  6. Avoid accidental mutations using .clone()

    master

    Warning: Transform methods mutate the underlying Document in place.

    Even though transform methods return a View, they change the shared document that the View points to.

    • Read-only methods (e.g., .match(), .if(), .found(), .text(), .json(), .has(), and accessors) do not change the document.
    • Transform methods (e.g., .toPastTense(), .replace(), .remove(), .tag(), .normalize(), and case/whitespace methods) do change the document.

    To perform transformations without affecting the original document, call .clone() before your transformations.

    let doc = nlp('I walk to work')
    
    // This mutates 'doc'
    doc.verbs().toPastTense()
    doc.text() // 'I walked to work'
    
    // This keeps 'doc' untouched
    let past = doc.clone().verbs().toPastTense().text()
    let doc = nlp('I walk to work')
    doc.verbs().toPastTense()       // mutates doc, even though we didn't reassign
    doc.text()                      // 'I walked to work'  ← doc changed
    
    // To work on a copy without touching the original, call .clone() first:
    let past = doc.clone().verbs().toPastTense().text()   // doc is untouched
  7. Understand the mutability rule in Compromise

    master

    Crucial for all users: Transform methods in Compromise change the underlying document in place. When you call a method like .verbs().toPastTense(), the original document is mutated.

    Additionally, the object returned by a selection (a 'View') represents only the matched subset, not the entire document. To get the full modified text, you must call .text() on the original document object.

    To perform transformations without modifying the original document, use .clone().

    // ⚠️ COMMON MISTAKE
    // The returned view is just the selection, not the whole document
    nlp('I walk to work').verbs().toPastTense().text() // 'walked work'
    
    // ✅ CORRECT WAY
    let doc = nlp('I walk to work')
    doc.verbs().toPastTense() 
    doc.text() // 'I walked to work'
    
    // ✅ TRANSFORMING A COPY
    let doc = nlp('I walk')
    let past = doc.clone().verbs().toPastTense().text() // 'walked'
    doc.text() // 'I walk' (untouched)
  8. How compromise/three works (Phrase and Sentence Tooling)

    master

    The compromise/three entry point provides advanced tooling to zoom into and operate on specific parts of a text. It includes specialized methods for extracting and manipulating data like numbers, money, and fractions.

    import nlp from 'compromise/three'
    
    let doc = nlp("Wayne's World, party time")
    let str = doc.people().normalize().text()
    // "wayne"
  9. Understand the limitations of compromise

    master

    Before using compromise, be aware of the following technical limitations:

    • Slash-support: Slashes are treated as word separators (similar to hyphens). Matches spanning across slashes will fail.
      • Example: nlp('the koala eats/shoots/leaves').has('koala leaves') returns false.
    • Inter-sentence match: By default, the library treats sentences as the top-level abstraction. Matches that span across multiple sentences are not supported unless you use the compromise-paragraphs plugin.
      • Example: nlp("that's it. Back to Winnipeg!").has('it back') returns false.
    • Nested match syntax: The match syntax does not support recursive or nested regex-style grouping (e.g., (a (b|c))). Complex matches must be achieved by chaining successive .match() calls.
    • Dependency parsing: The library does not currently perform full syntax tree/dependency parsing for sentence transformations.