Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
BM25F/BM25F/en.py /
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
51 lines (39 sloc)
1.46 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from BM25F.ja import _Filter | |
| from re import split | |
| from re import sub | |
| from stemming.porter2 import stem | |
| class Normalizer(object): | |
| def alphanumer(self, string): | |
| string = sub(r'([a-zA-Z])([0-9])', r'\1 \2', string) | |
| return sub(r'([0-9])([a-zA-Z])', r'\1 \2', string) | |
| def camelcase(self, string): | |
| return sub(r'([a-z])([A-Z])', r'\1 \2', string) | |
| def delimiter(self, string): | |
| return sub(r'\W', r' ', string) | |
| def lower(self, string): | |
| return string.lower() | |
| def normalize(self, string): | |
| string = self.posessive(string) | |
| string = self.alphanumer(self.camelcase(self.delimiter(string))) | |
| return self.lower(string) | |
| def posessive(self, string): | |
| string = sub(r'\'s(\W|$)', r'\1', string) | |
| return sub(r's\'(\W|$)', r'\1', string) | |
| class TokenFilter(_Filter): | |
| URL = 'https://raw.githubusercontent.com' \ | |
| '/apache/lucene-solr/master' \ | |
| '/solr/example/files/conf/lang/stopwords_en.txt' | |
| class Tokenizer(object): | |
| def __init__(self, | |
| token_filter=set()): | |
| super().__init__() | |
| self.normalizer = Normalizer() | |
| self.token_filter = token_filter | |
| def tokenize_smartly(self, string): | |
| string = self.normalizer.normalize(string) | |
| result = [] | |
| for token in split(r'\s', string): | |
| if token == '' or token in self.token_filter: | |
| continue | |
| result.append((stem(token),)) | |
| return result |