def test_analysis_fails_when_no_subtitle(): with pytest.raises(RuntimeError, message='no subtitle found for movie <id>'): loader_mock = MagicMock() loader_mock.load = MagicMock(return_value=None) analyser = Analyser(loader_mock, Parser(), CORPUS) analyser.analyse('<id>')
def test_parse_sentences_with_dash_and_space(): assert Parser().parse('''\ 1 00:01:00,000 --> 00:01:03,000 - I hope to see my friend. - And shake his hand. ''') == [ Sentence('I hope to see my friend.', timedelta(minutes=1)), Sentence('And shake his hand.', timedelta(minutes=1)) ]
def test_parse_two_sentences(): assert Parser().parse('''\ 1 00:01:00,000 --> 00:01:03,000 I hope to see my friend. And shake his hand! ''') == [ Sentence('I hope to see my friend.', timedelta(minutes=1)), Sentence('And shake his hand!', timedelta(minutes=1)) ]
def test_parse_simple_multiline_sentence(): assert Parser().parse('''\ 1 00:01:00,000 --> 00:01:03,000 I hope to see my friend and shake his hand. ''') == [ Sentence('I hope to see my friend and shake his hand.', timedelta(minutes=1)) ]
def test_parse_sentence_connected_with_ellipses(): assert Parser().parse('''\ 1 00:01:00,000 --> 00:01:03,000 I hope to see my friend... 2 00:01:00,000 --> 00:01:03,000 ...and shake his hand. ''') == [ Sentence('I hope to see my friend and shake his hand.', timedelta(minutes=1)) ]
def test_parse_two_sentences_across_multiple_lines(): assert Parser().parse('''\ 1 00:01:00,000 --> 00:01:03,000 I hope to see my friend. 2 00:02:00,000 --> 00:02:03,000 And shake his hand. ''') == [ Sentence('I hope to see my friend.', timedelta(minutes=1)), Sentence('And shake his hand.', timedelta(minutes=2)) ]
def cached_analyse(text): if text in CACHE: return CACHE[text] loader_mock = MagicMock() subtitle_mock = MagicMock(text=text) loader_mock.load = MagicMock(return_value=subtitle_mock) analyser = Analyser(loader_mock, Parser(), CORPUS) result = analyser.analyse('<id>') CACHE[text] = result return result
def create_routes(app): app.jinja_loader = jinja2.FileSystemLoader(os.path.join(os.getcwd(), TEMPLATE_ROOT)) opensubtitle_credentials = (app.config['OPENSUBTITLES_USER'], app.config['OPENSUBTITLES_PASS']) subtitle_api = OpenSubtitles(opensubtitle_credentials) fanart_key = app.config['FANART_TV_KEY'] poster_api = FanArt(fanart_key) wordnik_key = app.config['WORDNIK_KEY'] wordnik_api = Wordnik(wordnik_key) searcher = Searcher(subtitle_api, poster_api) corpus = Corpus(CorpusDatabase.FULL) loader = Loader(subtitle_api) parser = Parser() analyser = Analyser(loader, parser, corpus) @app.route('/') def home_route(): return bootstrap() @app.route('/m/<id>') def analysis_route(id): return bootstrap() @app.route('/m/<id>/w/<word>') def word_route(id, word): return bootstrap() @app.route('/error') def error_route(): return error() @app.route('/api/search/<query>') @as_json def search_api_route(query): return search_api(searcher, query) @app.route('/api/analysis/<id>') @as_json def analysis_api_route(id): return analysis_api(analyser, poster_api, id) @app.route('/api/words/<token>') @as_json def words_api_route(token): return words_api(wordnik_api, token)
def test_parse_full_subtitle_file(): with open('fixtures/opensubtitles/subtitle/1951992295.txt') as text: result = Parser().parse(''.join(text.readlines())) assert len(result) > 0
def test_parse_sentences_with_html(): assert Parser().parse('''\ 1 00:01:00,000 --> 00:01:03,000 <i>I hope to see my friend.</i> ''') == [Sentence('I hope to see my friend.', timedelta(minutes=1))]