Giter VIP home page Giter VIP logo

Comments (4)

seancarmody avatar seancarmody commented on August 16, 2024

Hi Aurelie

I think that the problem you are running into is that the Google rate limits the requests to the ngram viewer: if you hit it with too many requests it will block your IP address's access for a period of time (if you visit https://books.google.com/ngrams after your error you'll find it says "Please try again later").

I will look into providing a more informative error message, but the most constructive suggestion I would have is to break your work into chunks. Since you managed to get through to 75, you could try looping 1:50, saving the results and then 51:100 and combining what you get.

Alternatively, you can download the full ngram sets here: https://storage.googleapis.com/books/ngrams/books/datasetsv3.html. However, you'll have to work through them yourself as I haven't written any code to help with that!

Sean.

from ngramr.

seancarmody avatar seancarmody commented on August 16, 2024

Aurelie,

I have had a closer look and here is a quick and dirty fix, adding some periods of "sleep" to avoid hitting the rate limit:

google_ngram_tmp <- NULL

for (ixs in chunk(1:nrow(Ngram_data), 50)){
  for (i in ixs) 
  {
    # launch Google Ngram query and extract the results that are displayed with viewer
    google_ngram_tmp <- bind_rows(google_ngram_tmp, 
                                  ngram(Ngram_data$Xgram_pattern[i], 
                                      corpus = "fre_2019", 
                                      year_start = 1800,
                                      year_end = 2019,
                                      smoothing = 0,
                                      case_ins = TRUE,
                                      aggregate = TRUE)
    )
    
    # returns the Ngram index at each iteration to keep track of the computation
    print (paste("Ngram index", i, sep = ": ") )
  }
  print("Sleeping for a while...")
  Sys.sleep(120)
}

class(google_ngram_tmp) <- c("ngram", class(google_ngram_tmp))

This could be improved (e.g. there's no need to sleep after the last loop), but it does the job.

Separately I noticed that, despite all of this hard work, most of these phrases will give no results (I only found results for four of the phrases). It's worth noting that there are no ngrams in Google's collection that are more than 5 words long, and some of these phrases may simply not appear. Did you mean to use Xgram_dependencies instead?

from ngramr.

aureincm avatar aureincm commented on August 16, 2024

Hi Sean,

thank you very much for your fast reply.

This was really helpful, because I was looking towards an R problem, and did not even think about a webpage rate limit.

As you suggested, I added a sleeping period at the end of each loop, and it works perfectly (I had to add a longer period of 600sec though) 👍

To answer your question, yes, I am aware that most of the Xgram_pattern entries are either too long or do not appear in the google database. This is actually part of my research question, which aims at comparing pattern vs. dependencies. So of course, I also plan to run a similar piece of code with Xgram_dependencies, which will be as easy as pie thanks to your code :-)

BTW, I should also mention that the using your library allows me to run some fancy analysis I gave up on because I did not have enough computer storage to load the entire ngram dataset.... Thanks again for this nice piece of code!!

Cheers

  • Aurelie

from ngramr.

seancarmody avatar seancarmody commented on August 16, 2024

Happy to help Aurelie - I'm glad the package is proving useful.

from ngramr.

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.