We present an algorithm for finding a system of recurrence relations for the number of -ary words of length that satisfy a certain set of conditions. A rewriting of these relations automatically gives a system of functional equations satisfied by the multivariate generating function that counts -ary words by their length and the indices of the corresponding recurrence relations. We propose an approach to describing such equations. In several interesting cases the algorithm recovers and refines results on -avoiding -ary words and -ary words containing exactly once, where is either a classical, a generalized, or a distanced pattern of length three.