9. A system comprising: one or more computers and one or more storage devices storing instructions that are operable, when executed by the one or more computers, to cause the one or more computers to perform operations comprising: receiving a query; identifying an original term occurring in the query; determining a plurality of substitution contexts for the original term occurring in the query, wherein a substitution context specifies a context in which the original term in the query is replaced by a substitute term, and wherein a substitution context includes one or more context terms and an indication of a position in the query of the original term relative to the one or more context terms; generating a context hierarchy of the plurality of substitution contexts, wherein any conditions of a parent context in the context hierarchy also apply to each of one or more child contexts of the parent context in the context hierarchy, and wherein each child context in the context hierarchy includes all of the terms of a parent context of the child context as well as an additional term that does not occur in the parent context of the child context; determining a score for each substitution context of the plurality of substitution contexts including comparing a particular substitution context to its parent context in the context hierarchy; ranking the plurality of substitution contexts based on the score of each substitution context; and determining one or more substitute terms for the original term occurring in the query in a context specified by a highest-ranking substitution context of the plurality of substitution contexts.
9. A system comprising: one or more computers and one or more storage devices storing instructions that are operable, when executed by the one or more computers, to cause the one or more computers to perform operations comprising: receiving a query; identifying an original term occurring in the query; determining a plurality of substitution contexts for the original term occurring in the query, wherein a substitution context specifies a context in which the original term in the query is replaced by a substitute term, and wherein a substitution context includes one or more context terms and an indication of a position in the query of the original term relative to the one or more context terms; generating a context hierarchy of the plurality of substitution contexts, wherein any conditions of a parent context in the context hierarchy also apply to each of one or more child contexts of the parent context in the context hierarchy, and wherein each child context in the context hierarchy includes all of the terms of a parent context of the child context as well as an additional term that does not occur in the parent context of the child context; determining a score for each substitution context of the plurality of substitution contexts including comparing a particular substitution context to its parent context in the context hierarchy; ranking the plurality of substitution contexts based on the score of each substitution context; and determining one or more substitute terms for the original term occurring in the query in a context specified by a highest-ranking substitution context of the plurality of substitution contexts. 11. The system of claim 9 , wherein determining a score for a substitution context comprises: comparing a particular substitution context to the general context; and computing a combined score using the comparison of the particular substitution context to the general context and the comparison of the particular substitution context to the general context.