Returns a list of statements in the database that match the parameters specified.
(self, **kwargs)
| 84 | return Statement(**statement_data) |
| 85 | |
| 86 | def filter(self, **kwargs): |
| 87 | """ |
| 88 | Returns a list of statements in the database |
| 89 | that match the parameters specified. |
| 90 | """ |
| 91 | import pymongo |
| 92 | |
| 93 | page_size = kwargs.pop('page_size', 1000) |
| 94 | order_by = kwargs.pop('order_by', None) |
| 95 | tags = kwargs.pop('tags', []) |
| 96 | exclude_text = kwargs.pop('exclude_text', None) |
| 97 | exclude_text_words = kwargs.pop('exclude_text_words', []) |
| 98 | persona_not_startswith = kwargs.pop('persona_not_startswith', None) |
| 99 | search_text_contains = kwargs.pop('search_text_contains', None) |
| 100 | search_in_response_to_contains = kwargs.pop('search_in_response_to_contains', None) |
| 101 | |
| 102 | if tags: |
| 103 | kwargs['tags'] = { |
| 104 | '$in': tags |
| 105 | } |
| 106 | |
| 107 | if exclude_text: |
| 108 | if 'text' not in kwargs: |
| 109 | kwargs['text'] = {} |
| 110 | elif 'text' in kwargs and isinstance(kwargs['text'], str): |
| 111 | text = kwargs.pop('text') |
| 112 | kwargs['text'] = { |
| 113 | '$eq': text |
| 114 | } |
| 115 | kwargs['text']['$nin'] = exclude_text |
| 116 | |
| 117 | if exclude_text_words: |
| 118 | if 'text' not in kwargs: |
| 119 | kwargs['text'] = {} |
| 120 | elif 'text' in kwargs and isinstance(kwargs['text'], str): |
| 121 | text = kwargs.pop('text') |
| 122 | kwargs['text'] = { |
| 123 | '$eq': text |
| 124 | } |
| 125 | exclude_word_regex = '|'.join([ |
| 126 | '.*{}.*'.format(word) for word in exclude_text_words |
| 127 | ]) |
| 128 | kwargs['text']['$not'] = re.compile(exclude_word_regex) |
| 129 | |
| 130 | if persona_not_startswith: |
| 131 | if 'persona' not in kwargs: |
| 132 | kwargs['persona'] = {} |
| 133 | elif 'persona' in kwargs and isinstance(kwargs['persona'], str): |
| 134 | persona = kwargs.pop('persona') |
| 135 | kwargs['persona'] = { |
| 136 | '$eq': persona |
| 137 | } |
| 138 | kwargs['persona']['$not'] = re.compile('^bot:*') |
| 139 | |
| 140 | if search_text_contains: |
| 141 | or_regex = '|'.join([ |
| 142 | '{}'.format(re.escape(word)) for word in search_text_contains.split(' ') |
| 143 | ]) |