Spaces:
Building
Building
| #!/usr/bin/env python3 | |
| """ | |
| Extended test script to validate application functionality. | |
| """ | |
| import sys | |
| import os | |
| import tempfile | |
| # Add the parent directory to the Python path for imports | |
| sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | |
| from text_analyzer.lexical_sophistication import LexicalSophisticationAnalyzer | |
| from text_analyzer.pos_parser import POSParser | |
| import pandas as pd | |
| def test_lexical_sophistication(): | |
| """Test lexical sophistication analysis.""" | |
| print("Testing Lexical Sophistication Analysis...") | |
| try: | |
| # Create analyzer | |
| analyzer = LexicalSophisticationAnalyzer(language="en", model_size="trf") | |
| # Test text | |
| test_text = "The quick brown fox jumps over the lazy dog. This is a simple test sentence." | |
| # Create dummy reference lists | |
| with tempfile.TemporaryDirectory() as temp_dir: | |
| # Create a dummy token file | |
| token_file = os.path.join(temp_dir, "test_token.csv") | |
| with open(token_file, 'w') as f: | |
| f.write("word,score\n") | |
| f.write("the,100\n") | |
| f.write("quick,50\n") | |
| f.write("brown,25\n") | |
| f.write("fox,10\n") | |
| f.write("jumps,5\n") | |
| # Create a dummy lemma file | |
| lemma_file = os.path.join(temp_dir, "test_lemma.csv") | |
| with open(lemma_file, 'w') as f: | |
| f.write("word,score\n") | |
| f.write("the,100\n") | |
| f.write("quick,50\n") | |
| f.write("brown,25\n") | |
| f.write("fox,10\n") | |
| f.write("jump,5\n") | |
| # Load reference lists | |
| ref_lists = { | |
| "test": { | |
| "token": token_file, | |
| "lemma": lemma_file | |
| } | |
| } | |
| analyzer.load_reference_lists(ref_lists) | |
| # Analyze text | |
| results = analyzer.analyze_text(test_text, ["test"], apply_log=False) | |
| print(f"β Processed {results['text_stats']['total_tokens']} tokens") | |
| print(f"β Found {len(results['summary'])} summary statistics") | |
| print(f"β Generated {len(results['token_details'])} token details") | |
| return True | |
| except Exception as e: | |
| print(f"β Lexical sophistication test failed: {e}") | |
| import traceback | |
| traceback.print_exc() | |
| return False | |
| def test_pos_parser(): | |
| """Test POS parser functionality.""" | |
| print("\nTesting POS Parser...") | |
| try: | |
| # Create parser | |
| parser = POSParser(language="en", model_size="trf") | |
| # Test text | |
| test_text = "The quick brown fox jumps over the lazy dog." | |
| # Analyze text | |
| results = parser.analyze_text(test_text) | |
| print(f"β Processed {results['statistics']['total_tokens']} tokens") | |
| print(f"β Found {results['statistics']['total_sentences']} sentences") | |
| print(f"β Generated token analysis with {len(results['token_analysis'])} rows") | |
| # Test visualization | |
| html_outputs = parser.generate_displacy_html(test_text) | |
| print(f"β Generated {len(html_outputs)} HTML visualizations") | |
| return True | |
| except Exception as e: | |
| print(f"β POS parser test failed: {e}") | |
| import traceback | |
| traceback.print_exc() | |
| return False | |
| def main(): | |
| """Run all functionality tests.""" | |
| print("Running comprehensive application tests...") | |
| print("=" * 60) | |
| # Test lexical sophistication | |
| lex_success = test_lexical_sophistication() | |
| # Test POS parser | |
| pos_success = test_pos_parser() | |
| print("\n" + "=" * 60) | |
| if lex_success and pos_success: | |
| print("β All tests passed! Application is ready to use.") | |
| return True | |
| else: | |
| print("β Some tests failed. Please check the errors above.") | |
| return False | |
| if __name__ == "__main__": | |
| success = main() | |
| sys.exit(0 if success else 1) | |