Writing an Inverted Index in C# for an information retrieval application

Question: I am writing an in-house application that holds several pieces of text information as well as a number of pieces of data about these pieces of text. These pieces of data will be held within a database (SQL Server, although this could change) in order of entry.
I'd like to be able to search for the most relevant of these pieces of information, with the most relevant of these to be at the top. I originally looked into using SQL Server Full-Text Search but it's not as flexible for my other needs as I had hoped so it seems that I'll need to develop my own solution to this.
From what I understand what is needed is an inverted index, then for the contents of said inverted index to be restored and modified based on the results of the additional information held (although for now this can be left for a later date as I just want the inverted index to index the main text from the database table/strings provided).
I've had a crack at writing this code in Java using a Hashtable with the key as the words and the value as a list of the occurrences of the word but in all honesty I'm still rather new at C# and have only really used things like DataSets and DataTables when handling information. If requested I'll upload the Java code soon once I've cleared this laptop of viruses.
If given a set of entries from a table or from a List of Strings, how could one create an inverted index in C# that will preferably save into a DataSet/DataTable?
EDIT: I forgot to mention that I have already tried Lucene and Nutch, but require my own solution as modifying Lucene to meet my needs would take far longer than writing an inverted index. I'll be handling a lot of meta-data that'll also need handling once the basic inverted index is completed, so all I require for now is a basic full-text search on one area using the inverted index. Finally, working on an inverted index isn't something I get to do every day so it'd be great to have a crack at it.
Comment: Here is another C# Inverted Index based on generics: http://www.aleandmusic.com/InvertedIndex.aspx
Answer: Here's a rough overview of an approach I've used successfully in C# in the past:
 struct WordInfo
 {
     public int position;
     public int fieldID;
 }

 Dictionary<string,List<WordInfo>> invertedIndex=new Dictionary<string,List<WordInfo>>();

       public void BuildIndex()
       {
            foreach (int  fieldID in GetDatabaseFieldIDS())
            {
                string textField=GetDatabaseTextFieldForID(fieldID);

                string word;

                int position=0;

                while(GetNextWord(textField,out word,ref position)==true)
                {
                     WordInfo wi=new WordInfo();

                     if (invertedIndex.TryGetValue(word,out wi)==false)
                     {
                         invertedIndex.Add(word,new List<WordInfo>());
                     }

                     wi.Position=position;
                     wi.fieldID=fieldID;
                     invertedIndex[word].Add(wi);

                }

            }
        }

Notes:
GetNextWord() iterates through the field and returns the next word and position.  To implement it look at using string.IndexOf() and char character type checking methods (IsAlpha etc).
GetDatabaseTextFieldForID() and GetDatabaseFieldIDS() are self explanatory, implement as required.
Comment: Sorry for the huge delay in getting back to this answer. This looks great! The one question I have with this is how one would then write your dictionary back to a database. I have edited the question with what I mean.
Comment: Sorry, I've just looked over the code and realised that I could just duplicate the words should they appear in more than one document. It should be quite easy to send this to my database handling classes; once I get this implemented I'll accept this answer.
Comment: @Ender, glad it was helpful.  Serialization is one option to save/load from the databases.  Alternatively iterating through the Dictionary Keys collection and getting each corresponding value would be another.
Comment: I've managed to successfully implement it by using LINQ to get each value out, so I've accepted this answer. Just out of interest, how would you have implemented the GetNextWord() method, as I've split the full field into separate words and iterated through until they were all gone.
Answer: Lucene.net might be your best bet. Its a mature full text search engine using inverted indexes.
http://codeclimber.net.nz/archive/2009/09/02/lucene.net-your-first-application.aspx
UPDATE:
I wrote a little library for indexing against in-memory collections using Lucene.net - it might be useful for this. https://github.com/mcintyre321/Linqdex
Comment: I should have explained in my question that I had already looked into using Lucene or replacing parts of its functionality with what I've written. Sadly Lucene isn't flexible enough for me to change what I need to meet the criteria for the information I need to hold, so I'll need to write the inverted index myself.
Answer: If you're looking to spin your own, the Dictionary<T> class is most likely going to be your base, like your Java hashtables. As far as what is stored as the values in the dictionary, its hard to tell based on the information you provide, but typically search algorithms use some type of Set structure so you can run unions and intersections. LINQ gives you a much of that functionality on any IEnumerable, although a specialized Set class may boost performance.
One such implementation of a Set is in the Wintellect PowerCollections. I'm not sure if that would give you any performance benefit or not over LINQ.
As far as saving to a DataSet, I'm not sure what you're envisioning. I'm not aware of anything that "automagically" writes to a DataSet. I suspect you will have to write this yourself, especially since you mentioned several times about other third-party options not being flexible enough.


Dyanamic list in .net

Question: How can we create a List of dyanamic type in .Net
ex:
var tableType = Type.GetType("class", true);
List<tableType> Service =new   List<tableType>();

Comment: This isn't exactly possible. What problem are you trying to solve?
Comment: Still not enough information. How will you use this list? How do you want to get items in and out of it?
Comment: I have edited above code. I wand to initialize a list fro a dyanamic type
Answer: You can't do that, at least not without reflection gymnastics. I recommend you use a List<object> or an ArrayList instead.
Answer: Well, you can use reflection:
Type listType = typeof(List<>).MakeGenericType(tableType);
object list = Activator.CreateInstance(listType);

... but you won't find it terribly easy to use. Please give us more information about what you're trying to achieve.
Comment: Actually, if you used `dynamic` instead of `object`, he would probably be able to use it just fine. It's not very idiomatic, though.

Dyanamic list in .net


Problema ao baixar codigo fonte appengine

Question: Quando escrevo o comando no cmd:
appcfg.py -A [YOUR_PROJECT_ID] -V [YOUR_VERSION_ID] download_app [OUTPUT_DIR]

Invés dele iniciar o download do código fonte, ele abre o arquivo appcfg.py que está no diretório.
Agora tentei escrever pelo powershell o mesmo comando, e invés de iniciar o download do codigo fonte, ele abriu o arquivo appcfg.py e começou a mostrar isso no prompt:
[main 2019-03-19T19:12:36.901Z] update#setState idle
[main 2019-03-19T19:13:06.902Z] update#setState checking for updates
[main 2019-03-19T19:13:07.107Z] update#setState idle

Answer: É so adicionar python antes do comando:
python appcfg.py -A [YOUR_PROJECT_ID] -V [YOUR_VERSION_ID] download_app [OUTPUT_DIR]


Como contar os elementos de um li em jquery

Question: Tem como contar os elemento que estão dentro da li, mas que seja por passo, por exemplo primeiro pegar a li1 e conta os elementos que no caso tem que voltar 3 e depois passe para outra li.

     elemento 1
     elemento 2
     elemento 3

     elemento 1
     elemento 2
     elemento 3

Comment: Um Array que contenha objetos {id: , contador: } te atenderia?  Com ele você poderia implementar um iterador para compor seja lá qual for a computação que você queira. E isso? Realmente precisa de JQuery?
Answer: Podes usar o método childrenque retorna todos os filhos de um determinado elemento e obter o length dele.

$('li').each(function() {
  var qtd = $(this).children().length;
  console.log(this.id, qtd);
});

     elemento 1
     elemento 2
     elemento 3

     elemento 1
     elemento 2
     elemento 3

Comment: Resolve o problema, mas isso pode contar mais do que o necessário se existir outras coisas que não devem ser contadas dentro do li. Não seria melhor especificar qual elemento contar?
Answer: Podes montar um array com o valor de quantos elementos cada li tem. Seria algo assim:

var contador = $('ul li').get().reduce(function(obj, li) {
  obj[li.id] = li.children.length;
  return obj;
}, {});

console.log(contador);

     elemento 1
     elemento 2
     elemento 3

     elemento 1
     elemento 2
     elemento 3

Com JavaScript moderno nem era preciso jQuery e ficaria assim:

var contador = [...document.querySelectorAll('ul li')].reduce(
  (obj, li) => (obj[li.id] = li.children.length, obj), {}
);

console.log(contador);

    elemento 1
    elemento 2
    elemento 3

    elemento 1
    elemento 2
    elemento 3


What does $\frac{dg}{dx}$ mean?

Question: What does $\frac{dg}{dx}$ mean?
Specifically, I'm trying to solve$$
 \frac{1}{3}\frac{dg}{dx}\frac{1}{1+g^2}
$$
where
$$
g(x) = \frac{3x\left(1-x^2\right)}{x^4-4x^2+1}
$$
I know $\frac{d}{dx}$ just means differentiate with respect to $x$ but I have no idea what this $\frac{dg}{dx}$ business is.
Comment: "Solve" is not the right word here.  It often gets used as a catch-all term by people who don't know what word to use, but it is not correct to do that.  "Evaluate" fits better.
Comment: @MichaelHardy Do you say that because I'm not trying to find a specific number?
Answer: Leibniz's notation $\frac{dg}{dx}$ represents the derivative of your function $g(x)$. You can write this as $$\frac{dg}{dx}=\frac{d}{dx} g(x).$$ As you know that $\frac{d}{dx}$ means, then you might know that $\frac{d}{dx} g(x)$ means "differentiate the function $g(x)$ with respect to $x$".
Comment: The standard word is "differentiate".  Admittedly that is something of a linguistic anomaly, but nonetheless it is standard.
Comment: @MichaelHardy I know. I was trying to quote what the OP said with a little bit of variation, hopefully to connect better with the OP. :) I do understand that the word "derive" is rather vague and might be confused with its lexical definition in a non-mathematical context.
Comment: So you're saying I should take $\frac{d}{dx} \frac{1}{1+(\frac{(3x(1-x^2)}{x^4-4x^2+1})^2}$? Because when I put this in Mathematica I do not get the answer I'm looking for.
Comment: No, you should take $\frac d{dx} \frac{3x\left(1-x^2\right)}{x^4-4x^2+1}$. I do not use Mathematica, but WolframAlpha says that this derivative (http://www.wolframalpha.com/input/?i=derivative+of+g%28x%29+%3D+%5Cfrac%7B3x%5Cleft%281-x%5E2%5Cright%29%7D%7Bx%5E4-4x%5E2%2B1%7D) is $$\frac{dg}{dx}=\frac{3(x^6+x^4+x^2+1)}{(x^4-4x^2+1)^2}.$$
Comment: @dragon You'd need to multiple by $\frac{1}{3}$ per the initial equation but yes I get that too. Can you go to this link and verify that the answer that was presented is correct? http://math.stackexchange.com/questions/1170993/fracddx-frac13arctan-frac3x1-x2x4-4x21c-fracx41x
Comment: @Dragon Please? It is the first part of this question and the reason I don't like the answer you've presented but I agree with. :)
Answer: Do you mean
$\frac{1}{3}\frac{dg}{dx}\frac{1}{1+g^2}$ or
$\frac{1}{3}\frac{d}{dx}\frac{g}{1+g^2}$?
In the first case, you compute
$\frac{d}{dx}\frac{3x(1 - x^2)}{x^4 - 4x^2 + 1}$
and multiply the answer by
$\frac{1}{1+g^2}$.
In the second case, you use the chain rule to compute
$\frac{1}{3}\left(\frac{d}{dg}\frac{g}{1+g^2}\right) \frac{dg}{dx}$
I just want to make sure that you are aware there is a difference.
Comment: The first case was the one I needed I think. Thanks for the tip!

What does $\frac{dg}{dx}$ mean?


Fazer SELECT retornar só um trecho da string

Question: Preciso fazer uma consulta que traga apenas um trecho da string. Pensei em usar a função SUBSTR() mas não consegui.
Ex: coluna sendo algo como [abcd efg hig; efg hig abcd; jkl juh hig] e eu quero trazer na consulta apenas a quantidade de linhas com "hig".
Comment: Você quer filtrar a sua consulta com um where? Tem um exemplo do que você tentou fazer?
Comment: [Edit] a postagem com um [mcve] e um breve exemplo de dados e resultados esperados pode ajudar bastante.
Comment: Sim, quero filtrar com um where. Mas eu preciso que a consulta traga a quantidade de linhas com uma determinada substring. Não sei está confuso ainda
Answer: Se entendi corretamente sua pergunta:
<code>SELECT COUNT(*) FROM sua_tabela WHERE seu_campo LIKE '%hig%';
</code>
Comment: Pensei nisso, mas não compreendi bem o que ele quer... Mas acho que precisa do % nesse like.


Unique coroutine scope for each item collected in a flow

Question: Is it possible to create a flow operator similar to collect with its own coroutine scope for each item it receives?
Lets say I have an updating list of bicycles, a Flow<List<Bicycle>>. For each bicycle I can request a price, unfortunately this API requires me to request the price for a single bicycle at a time. This is all fine I can spin up some requests using a couple of async builders, no problem so far. The API for requesting prices might be slow at times, potentially slower than the stream of new bicycles that becomes available or taken. This creates the issue of still working or waiting on fetching prices for bikes that are no longer available or taken. Is there any way to create a coroutine scope for each item collected from my Flow<List<Bicycle>> and then cancel it when a new item is collected, so that my current requests are only for the last emitted item (or bicycle) from the flow? If I understand correctly due to the sequential nature of flows, the next item is not requested until the previous is finished/completed?
Answer: If you want to make a request only for the latest collected item then you can use collectLatest terminal operator. As soon as it collects a new item, the current request will be cancelled.
The collectLatest official documentation states:

Terminal flow operator that collects the given flow with a provided
action. The crucial difference from collect is that when the original
flow emits a new value, action block for previous value is cancelled.

Here's the example exposed in the official documentation:
flow {
    emit(1)
    delay(50)
    emit(2)
}.collectLatest { value ->
    println("Collecting $value")
    delay(100) // Emulate work
    println("$value collected")
}

And it prints:
Collecting 1
Collecting 2
2 collected

You can find collectLatest official documentation here.


How to model distribution of arrivals in chunks

Question: I want to model a queue system where customers tend to arrive in chunks. The queue represents vehicle arriving at a traffic light (consider just a single approach) and they tend to arrive in chunks because of green/red phases of upstream traffic lights.
What families of probability distributions are best suited for this scenario? Which parameters should I consider?
Answer: "Probability Models" by Sheldon Ross gives a great overview of Queueing Theory.
Your question is not well defined: distributions can describe interarrival times, or the number of units passing through a particular receiver over a time. There are too many assumptions to list. You might for instance describe the emission of radioactive isotopes detected by a Geiger counter, which follows very closely independent exponential interarrival times, and Poisson intensity at a single receiver. Generalizations involve multiple receivers, queueing in sequence vs in parallel, and non-independent interarrival times.


How to use tokens in the Completed URL?

Question: I need to send data in the URL to the receiving server so that two fields from a survey form is put in the Completed URL string. It looks like it should be possible to use tokens for this, but I can't seem to figure out the correct syntax to use.
I need to send the response to a URL on the form
https://server.tld/sub/value1/value2
where value1 and value2 are fetched from form fields and the rest is static.
According to https://www.drupal.org/project/webform/issues/3021818 this should be possible, if I only could find the correct syntax to use to add the tokens.
Background
The user gets a link to the survey form in a text message. After filling in the form, the Remote Post handler functionality in Webform should post the results to an external server that expects the survey ID and user alias as parts of the URL (for reasons unknown to me, but apparently expensive and difficult to change) for the purposes of knowing when the user has filled in the survey so as to avoid sending out a reminder.
What I have tried
I have tried formatting the Completed URL string in various ways; like for example
http://example.com/%value[my_fieldset][a_value_in_the_form]
and
http://example.com/%value
but I just get a URL invalid error response when I try to save the setting.
Comment: It's not clear what you are asking. What form ? What response ? Give us some context. Give an example or give some code that you have tried.
Comment: I have a survey form with a bunch of questions, it includes two hidden values (a survey id and a user alias) that needs to be present in the Completed URL string since that's how it should be sent to the receiving server that keeps track of sending out survey reminders. I haven't really found anything useful on how to format tokens in the Completed URL field, so I've tried stuff like

http://example.com/%value

http://example.com/%value[my_fieldset][a_value_in_the_form]

but I just get invalid format error when I try to save it.
Comment: This https://www.drupal.org/project/webform/issues/3021818 suggests it should work, but like I said, I'm not sure how to format the Completed URL string.

Also, I don't have control over how the receiving server wants the data.
Comment: Completed URL is the url that the person goes to after submission ?  What is the receiving server ? The website server or some other server ? Its always nice to have a clear example of what you mean. A step by step of this process might make it more clear.  Please update your question and try to make it as clear as possible in order to maximize the chances of getting an answer.  The comments above shed some light but try to make them part of the question please.
Answer: I managed to get it going using the standard tokens so in Completed URL I had https://URL/API?query=[webform_submission:values:fieldname] - so just swap out fieldname for the key in the webform you want to pass

How to use tokens in the Completed URL?


How can I test if the two parameter estimates in the same model are significantly different?

Question: I have the model
$$
y=x^a \times z^b + e
$$
where $y$ is the dependent variable, $x$ and $z$ are explanatory variables, $a$ and $b$ are the parameters and $e$ is an error term. I have parameter estimates of $a$ and $b$ and a covariance matrix of these estimates. How do I test if $a$ and $b$ are significantly different?
Answer: Assessing the hypothesis that  $a$ and $b$ are different is equivalent to testing the null hypothesis $a - b = 0$  (against the alternative that $a-b\ne 0$).
The following analysis presumes it is reasonable for you to estimate $a-b$ as $$U = \hat a - \hat b.$$  It also accepts your model formulation (which often is a reasonable one), which--because the errors are additive (and could even produce negative observed values of $y$)--does not permit us to linearize it by taking logarithms of both sides.
The variance of $U$ can be expressed in terms of the covariance matrix $(c_{ij})$ of $(\hat a, \hat b)$ as
$$\operatorname{Var}(U) = \operatorname{Var}(\hat a - \hat b) = \operatorname{Var}(\hat a) + \operatorname{Var}(\hat b) - 2 \operatorname{Cov}(\hat a, \hat b) = c_{11} + c_{22} - 2c_{12}^2.$$
When $(\hat a, \hat b)$ is estimated with least squares, one usually uses a "t test;" that is, the distribution of  $$t = U / \sqrt{\operatorname{Var(U)}}$$ is approximated by a Student t distribution with $n-2$ degrees of freedom (where $n$ is the data count and $2$ counts the number of coefficients).  Regardless, $t$ usually is the basis of any test.  You may perform a Z test (when $n$ is large or when fitting with Maximum Likelihood) or bootstrap it, for instance.
To be specific, the p-value of the t test is given by
$$p = 2t_{n-2}(-|t|)$$
where $t_{n-2}$ is the Student t (cumulative) distribution function.  It is one expression for the "tail area:" the chance that a Student t variable (of $n-2$ degrees of freedom) equals or exceeds the size of the test statistic, $|t|.$

More generally, for numbers $c_1,$ $c_2,$ and $\mu$ you can use exactly the same approach to test any hypothesis
$$H_0: c_1 a + c_2 b = \mu$$
against the two-sided alternative.  (This encompasses the special but widespread case of a "contrast".)  Use the estimated variance-covariance matrix $(c_{ij})$ to estimate the variance of $U = c_1 a + c_2 b$ and form the statistic
$$t = (c_1 \hat a + c_2 \hat b - \mu) / \sqrt{\operatorname{Var}(U)}.$$
The foregoing is the case $(c_1,c_2) = (1,-1)$ and $\mu=0.$

To check that this advice is correct, I ran the following R code to create data according to this model (with Normally distributed errors e), fit them, and compute the values of $t$ many times.  The check is that the probability plot of $t$ (based on the assumed Student t distribution) closely follows the diagonal.  Here is that plot in a simulation of size $500$ where $n=5$ (a very small dataset, chosen because the $t$ distribution is far from Normal) and $a=b=-1/2.$

In this example, at least, the procedure works beautifully.  Consider re-running the simulation using parameters $a,$ $b,$ $\sigma$ (the error standard deviation), and $n$ that reflect your situation.
Here is the code.
#
# Specify the true parameters.
#
set.seed(17)
a <- -1/2
b <- -1/2
sigma <- 0.25 # Variance of the errors
n <- 5        # Sample size
n.sim <- 500  # Simulation size
#
# Specify the hypothesis.
#
H.0 <- c(1, -1) # Coefficients of `a` and `b`.
mu <- 0
#
# Provide x and z values in terms of their logarithms.
#
log.x <- log(rexp(n))
log.z <- log(rexp(n))
#
# Compute y without error.
#
y.0 <- exp(a * log.x + b * log.z)
#
# Conduct a simulation to estimate the sampling distribution of the t statistic.
#
sim <- replicate(n.sim, {
  #
  # Add the errors.
  #
  e <- rnorm(n, 0, sigma)
  df <- data.frame(log.x=log.x, log.z=log.z, y.0, y=y.0 + e)
  #
  # Guess the solution.
  #
  fit.ols <- lm(log(y) ~ log.x + log.z - 1, subset(df, y > 0))
  start <- coefficients(fit.ols) # Initial values of (a.hat, b.hat)
  #
  # Polish it using nonlinear least squares.
  #
  fit <- nls(y ~ exp(a * log.x + b * log.z), df, list(a=start[1], b=start[2]))
  #
  # Test a hypothesis.
  #
  cc <- vcov(fit)
  s <- sqrt((H.0 %*% cc %*% H.0))
  (crossprod(H.0, coef(fit)) - mu) / s
})
#
# Display the simulation results.
#
summary(lm(sort(sim) ~ 0 + ppoints(length(sim))))
qqplot(qt(ppoints(length(sim)), df=n-2), sim,
       pch=21, bg="#00000010", col="#00000040",
       xlab="Student t reference value",
       ylab="Test statistic")
abline(0:1, col="Red", lwd=2)

Comment: This is excellent.  The answer with the theory, with steps to follow to repeat for other tests, with a numerical approach for clarity, and with code.  This is the gold standard.
Comment: I find "**The** hypothesis that a and b are different" ambiguous in your opening sentence, because it is not clear whether that is a null or alternative hypothesis. The OP's question makes clear they are looking for evidence of difference, and the second clause of your sentence speaks to that. Pedagogically I think it helps folks newer to hypothesis testing to be super explicit. (But +1 for your answer overall :)
Comment: @Alexis thank you--I see what you're saying.  Because I do have such people in mind, I'll clarify.


Get out of maintenance mode using database query (without using drush or admin area)

Question: I understand there are very many stackoverflow questions on this very topic.
How can get my site out of maintenance mode? for example.
However I've never seen one that explains how to turn off maintenance mode using only a database query. I run a complex site that does not allow you to login directly using /user since it's using a special sso. So there are times when I do not have access to the admin area and I do not have access to the user login either. I don't always have ssh access to run drush, but I do usually have direct access to the mysql database.
I did find an entry in the config table that seems related.
The name is system.maintenance and the data is
a:3:{s:7:"message";s:93:"@site is currently under maintenance. We should be back shortly. Thank you for your patience.";s:8:"langcode";s:2:"en";s:5:"_core";a:1:{s:19:"default_config_hash";s:43:"Z5MXifrF77GEAgx0GQ6iWT8wStjFuY8BD9OruofWTJ8";}}

I see where it shows the message, langcode and hash settings but I do not see where it defines whether it's on or off.
Is there a location in the database that would allow me to turn maintenance mode on and off directly?
Comment: You can't do it with a direct query, the data is stored in a php serialised string. It requires php to unserialise it (hence the standard solutions being via php like drush or the admin area). You could manually get the data, manually unserialise it, and manually put it back again - but that's just a `SELECT` followed by an `UPDATE` in terms of sql
Comment: Actually that might be too simplistic - I think maintenance mode is a state var, which means it's probably in the key_value or key_value_expire table as a single row. You'll probably still need to unserialise and reserialise the data with php, but it'll be slightly easier than updating a whole complicated array
Comment: @Clive I don't mind manually serializing and unserializing things, or manipulating multiple tables. I can do whatever I need with a local php script if necessary. I mainly want to know if it's physically possible and what tables would be involved.
Answer: It's never the best idea to manipulate the database directly (for the usual reasons), but if you're out of other options, it's not that unsafe:
UPDATE key_value
SET value = '<value>'
WHERE collection = 'state'
AND name = 'system.maintenance_mode'

Where <value> is replaced with a serialised PHP string representing the integer value 1 for enabled, or 0 for disabled, e.g.
$value = serialize(1);

You'll probably want to invoke a cache rebuild after manually updating the variable, it's probably cached somewhere.
Comment: Turns out a cache rebuild is not necessary. Thanks!


Adding a loading indicator to form submission module

Question: I have a custom module that is altering a webform to add custom submission handling using:
<code>$form['#submit'][]='paymentComplete';
</code>
From the time the user clicks the submit button, until the paymentComplete() function completes it takes 2-3 seconds, but the user experience doesn't really indicate what is going on. How can I add some type of 'waiting indicator' that starts right BEFORE the request to execute paymentComplete()?
Answer: AFAIK there are no OOTB features in Drupal yet but perhaps you can implement ProgressButtonStyles by Codrops. See https://github.com/codrops/ProgressButtonStyles for more info...


Ajax sending _POST sem maximo de caracters

Question: Neste ajax o post manda mais que 1000 caracters:
$.ajax({
            url: 'inserir.php',
            type: 'POST',
            data: { inseason: document.getElementById('comment').value },
            success: function(result) {
                alert('the request was successfully sent to the server');
            }
        });

Mas neste outro tem um maximo de 1000 caracters e não consigo perceber porque.
var http = new XMLHttpRequest();

        var parametros = "inseason=" + document.getElementById('comment').value;

        http.open("POST", "inserir.php", true);

        http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

        http.onreadystatechange = function() {
            if(http.readyState == 4 && http.status == 200)
            {

                //console.log(http.responseText);
                $('#logs').text(http.responseText);

            }
        }
        http.send(parametros);

Comment: Como verificou que o primeiro manda mais de 1000 caracteres e o segundo não?
Comment: tenho uma textarea para por o resultado do script , e no segundo o post so envia 1000 caracters
Comment: E qual é o código do `inserir.php`?
Comment: Coloeque `console.log(http.responseText.length);` após `$('#logs').text(http.responseText);` e veja o que mostra no console
Comment: agora tava a testar quantos cars passava com console.log(http.responseText.length); e descobri que quando passo um & no codigo aquilo confunde como fosse mais parametros e depois so me da os caracters ate ao &
Comment: Coloca `escape()`, assim: `escape(document.getElementById('comment').value)`
Comment: Isso irá escapar os caracteres especiais, como o &.
Comment: É isso mesmo , obrigado
Comment: Já que que dei uma dica e vc achou o erro principal, seria legal se vc postasse uma resposta dizendo como o problema foi resolvido e depois marcar como certa, para dar a pergunta como finalizada. Abs!
Comment: Vou fazer isso mas só posso aceitar a minha resposta depois de dois dias.
Answer: O erro que eu tinha era que passava & no post e o ajax confundia como fosse outro parâmetro e o parâmetro que eu queria que recebesse o texto só me mostrava os caracteres até ao &.
Então eu percebi o erro e o DvD disse-me como resolve-lo:
escape(document.getElementById('comment').value)

Basta mudar isso nos parâmetros.


Coin flipping with subsequent random report of the result

Question: A coin having probability .8 of landing on heads is flipped.
A observes the result either heads or tails and rushes off to tell
B. However, with probability .4, A will have forgotten the re-
sult by the time he reaches B. If A has forgotten, then, rather
than admitting this to B, he is equally likely to tell B that
the coin landed on heads or that it landed tails. (If he does
remember, then he tells B the correct result.)
(a) What is the probability that B is told that the coin landed
on heads?
(b) What is the probability that B is told the correct result?
(c) Given that B is told that the coin landed on heads, what
is the probability that it did in fact land on heads?
for (a)
 i did something like this :
P(b is told head)=P(head)xP(remember result)+P(make a guess)xP(forget the result)
            =0.8x0.6+0.5x0.4

            =0.68

i don't know how to solve (b) and (c) part and please correct (a) weather wrong or right.
Comment: Do you agree with the title I propose which is less "neutral" than the one you had chosen ?
Answer: Result of the flip:

Heads (.8)
A have forgotten:

Yes (.4)
a) A tells "Heads" (.5) - result "Heads", true, $.8\cdot .4\cdot.5=.16$
b) A tells "Tails" (.5) - result "Tails", false, $.8\cdot .4\cdot.5=.16$
No (.6) - result "Heads", true, $.8\cdot.6=.48$

Tails (.2)

Yes (.4)
a) A tells "Tails" (.5) - result "Tails", true, $.2\cdot .4\cdot.5=.04$
b) A tells "Heads" (.5) - result "Heads", false, $.2\cdot .4\cdot.5=.04$
No (.6) - result "Tails", true, $.2\cdot.6=.12$

To obtain "Heads" we have three paths with probabilities $.16, .48, .04$, so $$P(\text{"Heads"})=.16 + .48+ .04 = .68$$
To obtaint correct result we have 4 paths with probabilities $.16,.48,.04,.16$, so
$$P(\text{Correct result})=.16+.48+.04+.12=.80$$
If B was told "Heads" (with probability $.68$) we have 2 situations, that leads to "Heads" and correct result with probabilities $.16, .48$.
$$P(\text{"Heads" & correct result})=.16+.48=.64$$
So
$$P(\text{correct result if "Heads"})=\frac{P(\text{"Heads" & correct result})}{P(\text{"Heads"})}=\frac{.64}{.68}=\frac{16}{17}\approx .94 $$
Answer: Since you have not shown any effort to work out the problem yourself i will give you hints for the first part.
Probability that b is told that coin lands on head is the probability that coin lands on head given that a has not forgotten and probabilty that b has reported head given that b has forgotten.
Transform all this to mathematical equation you will reach the answer.


Is the libmysqlclient version core requirement actually required?

Question: So it's been a while since I was doing things with Drupal. For a number of reasons, I decided to put my toe back in the water and install Drupal 10 on my hosting service. All went well, or I thought it did. The status report post-install showed this.

10.5.20-MariaDB-cll-lve-log
The libmysqlclient driver version 3.1.21 is less than the minimum required version. Upgrade to libmysqlclient version 5.5.3 or up, or alternatively switch mysql drivers to MySQLnd version 5.0.9 or up.

Of course, I contacted my hosting service. Their reply was, essentially, that can't be upgraded on a Shared Service and I have to go to a VPS or Dedicated Hosting. They linked me to their installed software list for shared servers.
How much critical is the error in the status report?

Litespeed: 6.0.12
Codeguard: included to all Shared plans
cPanel: 102
cURL: 7.19.7
CXS: installed on all Shared servers
Git: enabled
HTTP/2: supported
Imagick module: 3.7.0
IonCube PHP Loader: 12.0.2
Mod_Rewrite: enabled
Mod_Security: enabled
MariaDB: 10.5
Node.js: 6.17, 8.17, 9.11, 10.24, 11.15, 12.22, 14.20
Mysqli Support: enabled
Perl: 5.10.1
PHP: 5.6, 7.0-7.4, 8.0, 8.1
PostgreSQL: 8.4.20
Python: 2.7, 3.3 - 3.9
Rails: 2.3.18
Ruby: 1.8 - 2.7
ZendGuard Loader: 4.1
Comment: That error is given from the installer, so the libmysqlclient error did stop the install. The *utf8mb4* character encoding is required by Drupal core, and that is only handled with more recent versions of the *libmysqlclient* library.
Comment: Are you asking if the requirements are really requirements?
Comment: At the risk of sounding like an idiot - I guess I am. When I was installing (using Softalicious), it failed because of another dependency (PHP level. It asked if I wanted to enable a higher version, which I did, and it continued to install. The libmysqlclient error didn't stop the install. I know no install script is perfect, and can't cover every dependency. But there are dependencies and dependencies. Some impact function in a black and white way, some impact performance. I figured I'd ask which this was. Apologies for what may well be a dumb question :-).
Comment: @apaderno My thanks indeed. I wondered - however, Softalicious reported completed install, with no errors, and at the end I did have a Drupal site I could log in to and see/ use the admin menus (on an admin account). So _something_ appeared to install.
Comment: For information, in case anyone finds it of use, the issue was resolved and the error cleared by changing the PHP library from libmysqlclient to MySQLnd - that is, enabling nd_pdo_mysql instead of pdo_mysql. My thanks to everyone who dropped in to assist :-).
Answer: The issue was resolved and the error cleared by changing the PHP library from libmysqlclient to MySQLnd - that is, enabling nd_pdo_mysql instead of pdo_mysql.
Comment: Thanks bro. I had a problem and it seems it is gone by your help.
Comment: Note for noobs like me: enabling nd_pdo_mysql instead of pdo_mysql is done inside the "Select PHP Version" tool of cPanel (also called PHP Selector)
Answer: Tried changing the library to nd_dpo_mysql in CPanel as suggested, and got an error until I cleared all caches, including the LSCache for LiteSpeed.  Perhaps obvious for some, but for those like me that do site maintenance infrequently, a useful reminder.


Search & LazyLoad can't keep up w/ typing speed?

Question: So, all of the code works quite well. A database a queried, the node/parent IDs are lazily passed to jsTree, and, if one types a term into the search bar—a similar process goes on, but the nodes passed to jsTree are those returned by another SQL query (using something like SELECT nodeID FROM table WHERE name LIKE %searchTerm%).
There's only one problem:
If I type too quickly into the search bar, the results get all mixed up with one another. If I type slowly (I'd estimate 2 letters a second max), everything works well. Any faster and everything is blurred together. (That is, if I'm searching for names which contain the term "test", and type this quickly, I'll get names that contain "t", "te", "tes", and "test" instead of just names that contain "test".)
Anyone have this problem before? Is there some "wait until ready" function I should be using?
Comment: You might want to look into your SQL logic. When you do "name LIKE %searchTerm%" you will be doing full table scan which is slow. You might want to cache the strings in you app and do a quick regex match to avoid running into long wait times in db.
Answer: I can suggest you to do the 'Timeout' workaround. Basically, you create a SetTimeout function with a delay of 200-400 miliseconds and launch your lazyload ajax there. Every inputbox.change event restarts the time. See example in pseudo-javascript:
$('#your-input').keydown(function(){
    if(ajaxTimer != undefined) {
        clearTimeout(ajaxTimer);
    }
    ajaxTimer = setTimeout(function(){
        $.ajax({...}).done(function() {...}
    },400);
})

Answer: Use a throttle on the typing input, basically guarantees that a function will be called a maxmimum of once every X seconds. Sure you could write your own however there is a great library out there already.
check it out if you're interested http://benalman.com/projects/jquery-throttle-debounce-plugin/


A question about some sequence in a Banach space

Question: Let $X$ be an infinite dimensional Banach space and let $\left( x_{n}\right)
_{n\in
%TCIMACRO{\U{2115} }%
%BeginExpansion
\mathbb{N}
%EndExpansion
}$ be a sequence of vectors such that :
$d\left( x_{n,}span\left\{ x_{1},x_{2},...,x_{n-1}\right\} \right)
=\left\Vert x_{n}\right\Vert $, for every $n\in
%TCIMACRO{\U{2115} }%
%BeginExpansion
\mathbb{N}
%EndExpansion
$.
My question is : can we conclude that for every vector $x$ in $span\left\{
x_{n},x_{n+1},...\right\} $ we have $d\left( x_{,}span\left\{
x_{1},x_{2},...,x_{n-1}\right\} \right) =\left\Vert x\right\Vert$ ?
Answer: No.
For example, in $\ell^\infty$, take $x_n$ with $(x_n)_n \ge |(x_n)_j|$ for $j < n$ and $(x_n)_j = 0$ for $j > n$.  This satisfies the requirements.  But you could have e.g. $x_1 = (1, 0\ldots)$, $x_2 = (1,1,0\ldots)$, $x_3 = (1,0,1,0\ldots)$, and $x_2 + x_3 = (2,1,1,0,\ldots)$ with $d(x_2+x_3,
\text{span}(x_1) = 1$ while $\|x_2 + x_3\| = 2$.


Limits (Three Variable function).

Question: We're given :
$f(x,y,z) = \dfrac{xyz}{x^{2}+y^{2}+z^{2}}$ ,
Also , it's given that $\lim_{(x,y,z) \to (0,0,0)} f(x,y,z)$ exists.
We need to prove that $\lim_{(x,y,z) \to (0,0,0)} f(x,y,z) = 0$.
What I figured :
First I approach $(0,0,0)$ along $x$ -axis , and thus the limit becomes :
$\lim_{(x,y,z) \to (0,0,0)\dfrac{x.0.0}{x^{2}+0^{2}+0^{2}}}$ which is $0$.
Similarly approaching along $y$ and $z$ axis gives the limit to be $0$.
Now , since the limit at $(0,0,0)$ exists , it should be unique throughout , thus ,
$\lim_{(x,y,z) \to (0,0,0)} f(x,y,z) = 0$
Could anyone tell , am I right in making the above statement ?
Answer: Yes. But what I don't understand is why you were told that the limit exists. It is a totally redundant assumption.
Let $r = \sqrt{x^2+y^2+z^2}$.
Then $|x| \le r$ and $|y| \le r$ and $|z| \le r$.
Thus $|xyz| \le r^3$ and hence $\left| \frac{xyz}{x^2+y^2+z^2} \right| \le r \to 0$ as $r \to 0$.
Therefore $\lim_{(x,y,z) \to (0,0,0)} \frac{xyz}{x^2+y^2+z^2} = \lim_{r \to 0} \frac{xyz}{x^2+y^2+z^2} = 0$.
Answer: Yes, if the limit exists it's unique and you are correct. However you only need to check one particular direction, so just checking the $x$ axis would be enough
Answer: Little observation: in general (if you do not know whether the limit exists), if the limit exists along one path it does NOT mean that it exists, so your proof for $(x,0,0)$ is not sufficient.
For the general case, therefore, I would either use the demonstration of answer $1$ , or majorize at the numerator $|xy|$ by $\frac{x^2+y^2}{2}$, and minorize at the denominator $x^2+y^2+z^2$ by $x^2+y^2$, in order to get that $0\leq |f(x,y,z)|\leq \frac{|z|}{2}\leq\frac{\sqrt{x^2+y^2+z^2}}{2}$, which goes to zero for $(x,y,z)$ going to $(0,0,0)$.
For the sandwich theorem, the limit of your function at the origin is zero.
Comment: But the assumption was: Find the limit given the limit exists.

Limits (Three Variable function).


window.onload method, how to call multiple functions

Question: Writing some JavaScript so the form does not submit in less the fields have been filled out, and if the user has not, a message pops up saying 'please fill out the name field' or 'please fill out the email field' etc. I am writing a separate function for each field (don't know if that is the right way) and when I add each function to the window.onload, it only pops up with one of the messages. Any advice would be great.
HTML
 <form id="frmContact" name="frmContact" method="post" action="thanks.htm">
    <fieldset id="quickSupport">
      <legend>Quick Support</legend>

 If your request isn't urgent, drop us a quick line and we'll get back to you within 24 hours

        Name:
        <input type="text" value="Your Name" name="name" id="name" tabindex="10" />

        Email:
        <input type="text" value="Your Email" name="email" id="email" tabindex="20" />

        Comments

        <textarea name="comments" value="Message" id="comments" cols="45" rows="5" tabindex="60" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;">Message</textarea>

        <input type="submit" name="submit" id="submit" value="Send" tabindex="70" />

    </fieldset>
  </form>

JavaScript
function prepareEventHandlers () {
document.getElementById("frmContact").addEventListener("submit", function(event) {

        // Show message
        if (document.getElementById("email").value == "Your Email") {
            document.getElementById("errorMessage").innerHTML = "Please provide at least an email address!";
            // to STOP the form from submitting
            return false;
        } else {
            // reset and allow the form to submit
            document.getElementById("errorMessage").innerHTML = "";
            return true;
        }

        event.preventDefault(); // Prevent form from submitting
    }
});
}

function prepareEventHandlersName () {
document.getElementById("frmContact").addEventListener("submit",         function(event) {

        // Show message
        if (document.getElementById("name").value == "Your Name") {
            document.getElementById("errorMessage1").innerHTML = "Please provide a name!";
            // to STOP the form from submitting
            return false;
        } else {
            // reset and allow the form to submit
            document.getElementById("errorMessage1").innerHTML = "";
            return true;
        }

        event.preventDefault(); // Prevent form from submitting
    }
});
}

function start() {
prepareEventHandlers();
prepareEventHandlersName();
}

window.onload = start;
Comment: Use the built-in browser validation. Check the `required` attribute for input fields.
Comment: This way, you are discarding the first function attached to the event onsubmit of frmContact. Instead, define a function that does all the required checks and attach that function (only one) to the event above.
Answer: You should use the browser's built-in validation attributes.
<form id="frmContact" name="frmContact" method="post" action="thanks.htm">
    <fieldset id="quickSupport">
        <legend>Quick Support</legend>

If your request isn't urgent, drop us a quick line and we'll
            get back to you within 24 hours

            Name:
            <input type="text" value="Your Name" name="name" id="name"
                tabindex="10" required />

            Email:
            <input type="text" value="Your Email" name="email" id="email"
                tabindex="20" required />

        Comments

        <textarea name="comments" value="Message" id="comments" cols="45"
            rows="5" tabindex="60"
            placeholder="PLACEHOLDER WHEN NO MESSAGE IS WRITTEN" required>Message</textarea>

            <input type="submit" name="submit" id="submit" value="Send"
                tabindex="70" />

    </fieldset>
</form>
When you try to submit the form, the browser will automatically display nice error messages for you, in the user's language. No JavaScript required.

The reason your code is failing, because
document.getElementById("frmContact").onsubmit = ...

will override any previous onsubmit handler you've added.
You need to use .addEventListener() instead, if you want to add multiple functions.
A more correct way would be to create a single function that does all checks, and attach that as the handler, once.
Comment: did not know about required. Thank you!! Have to say though, I am new to JavaScript and I am not going to lie, that doesn't make much sense with the.addEventListener..... so write that line of code, then add both function to it???
Comment: Think about it this way, with `something.onsubmit = something`, you're overriding a variable with a new value each time. With `something.addEventListener('submit', something)`, you're adding a new event listener to the list of event listeners.
Comment: I have swapped the .onsubmit, for the addEventListener and when I click submit, it takes me to the local page, rather than displaying the messages. I am feeling slightly stupid here. Should the code look something like this??

    function prepareEventHandlers() {
    document.getElementById("frmContact").addEventListener("submit", function {
Answer: Both functions are running, but you're overwriting the onsubmit handler for frmContact in your second function.  To avoid this, don't assign event handlers that way.  Instead, use addEventListener.
document.getElementById("frmContact").addEventListener("submit", function(event) {
    if (document.getElementById("email").value == "Your Email") {
        // Show message
        event.preventDefault(); // Prevent form from submitting
    }
});

Note that when you do it this way, you can't prevent the form from submitting by simply returning false.  Instead, you have to use the event's preventDefault method.
Comment: I'm sorry mate but I have been trying to implement your line of code but it doesn't seem to be working. Would you be kind enough to show me what you meant? I am a bit of a newbie when it comes to JavaScript. Sorry to be a pain
Comment: @Patrick What's not working?  Is the form still submitting?  Or is there a JavaScript error in your console?
Comment: no errors coming up and its submitting and directing me to the local page
Comment: Might as well give up now. Still not working mate. Ill put up what I have changed to see if you can work out where I am going wrong?


Dynamic decision networks

Question: I was wondering if anybody had some good references on dynamic decision networks,
is there a nice recent survey on this topic?
Thanks in advance.
Answer: I have been reading this books on BN and DDN and they  are very usefull.
Just follow the link and you will find more .
Enjoy!
"Artificial Intelligence: Modern Approach" textbook by Russell and Norvig

Dynamic decision networks


Can someone suggest how to - JS World Map/First Person Game Project

Question: I am planning a project for a game where the user can free roam a world in a number of different vehicles etc.
The world model needs to be relatively simple but I am trying to research the best way of doing it.
So far I had thought of an SVG world map that can be navigated in real-time (a kind of Google Street view arrangement) with the first person section being rendered using three.js.
While I have already created a database of select cities to navigate to I am unsure how to apply these to the SVG map/3D world/etc.
Answer: If you're going to be rendering something that moves around a lot, canvas is preferable to SVG. MSDN actually has a good article on choosing between the two.
Using a canvas, you can still draw geometric shapes and the like, and there are a number of decent frameworks out there for it. KineticJS is quite popular (though I have not used it).
Comment: I stumbled upon an SVG to canvas converter which essentially provided me with all the coordinates I needed so in going to run with that for now
Answer: Vizcities seems to be doing exactly what you want. They have chosen WebGL to visualise city data from geographical coordinates.
Comment: Looks interesting but not up and running yet
Comment: Why not join in and help?