Counts the words in the given string and saves the probabilities at the given path. This can be used to generate a new model for the Spelling() constructor.
(self, s, path="spelling.txt")
| 1730 | |
| 1731 | @classmethod |
| 1732 | def train(self, s, path="spelling.txt"): |
| 1733 | """ Counts the words in the given string and saves the probabilities at the given path. |
| 1734 | This can be used to generate a new model for the Spelling() constructor. |
| 1735 | """ |
| 1736 | model = {} |
| 1737 | for w in re.findall("[a-z]+", s.lower()): |
| 1738 | model[w] = w in model and model[w] + 1 or 1 |
| 1739 | model = ("%s %s" % (k, v) for k, v in sorted(model.items())) |
| 1740 | model = "\n".join(model) |
| 1741 | f = open(path, "w") |
| 1742 | f.write(model) |
| 1743 | f.close() |
| 1744 | |
| 1745 | def _edit1(self, w): |
| 1746 | """ Returns a set of words with edit distance 1 from the given word. |