Coder Social home page Coder Social logo

Comments (2)

timClicks avatar timClicks commented on August 30, 2024

Perhaps there could be an argument to the TokenDocument constructor that allows for further pre-processing than what is provided by default.

from textanalysis.jl.

timClicks avatar timClicks commented on August 30, 2024

The following code might be able to be included in preprocessing.jl:

function replace_many(s::String, replacements::Dict{Regex, String})
  for replacement in replacements
        pattern, replacement = replacement
        s = replace(s, pattern, replacement)
  end
  s
end

function replace_many!(d::FileDocument)
    error("FileDocument cannot be modified")
end

function replace_many!(d::StringDocument, replacements::Dict{Regex, String})
    d.text = replace_many(d.text, replacements)
    nothing
end

function replace_many!(d::TokenDocument, replacements::Dict{Regex, String})
    for i in 1:length(d.tokens)
        d.tokens[i] = replace_many(d.tokens[i], replacements)
  end
end

function replace_many!(d::NGramDocument, replacements::Dict{Regex, String})
  for token in keys(d.ngrams)
    new_token = replace_many(new_token, regex, replacements)
    if new_token != token
      if haskey(d.ngrams, new_token)
        d.ngrams[new_token] = pop!(d.ngrams, token) + d.ngrams[new_token]
      else
        d.ngrams[new_token] = pop!(d.ngrams, token)
      end
    end
  end
end

function replace_many!(crps::Corpus, replacements::Dict{Regex, String})
  for doc in crps
    replace_many!(doc)
  end
end

It handles simple replacements quite well, but I'm not sure if Julia's replace function knows how to deal with regular expressions that refer back to groups.

REPLACEMENT_PATTERNS = Dict{Regex, String}(
  r"(?i)won\'t" => "will not",
  r"((?i)can\'t|(?i)can not)" => "cannot",
  r"(?i)i\'m" => "i am",
  r"(?i)ain\'t" => "am not",
  r"\'cause" => "because"

  # doesn't support these
  #r"(\w+)\'ll" => "\g will",
  #r"(\w+)n\'t" => "\g not",
  #r"(\w+)\'ve" => "\g have",
  #r"(\w+t)\'s" => "\g is",
  #r"(\w+)\'re" => "\g are",
  #r"(\w+)\'d" => "\g would",
)

doc = StringDocument("i won't have seen any of this, yet. i'm ready though. i'll be waiting.")
replace_many!(doc, REPLACEMENT_PATTERNS)
text(doc) # =>  "i will not have seen any of this, yet. i am ready though. i will be waiting."

from textanalysis.jl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.