diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d4af4c5 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +交互/MVC/flask/__pycache__/models.cpython-38.pyc diff --git a/交互/MVC/flask/app.py b/交互/MVC/flask/app.py new file mode 100644 index 0000000..8b5d48b --- /dev/null +++ b/交互/MVC/flask/app.py @@ -0,0 +1,19 @@ +from flask import Flask, render_template, request +from models import WordFrequencyModel + +app = Flask(__name__) +app.config['SECRET_KEY'] = 'your-secret-key' + +@app.route('/') +def index(): + return render_template('index.html') + +@app.route('/count', methods=['POST']) +def count_words(): + text = request.form.get('text', '').lower() + model = WordFrequencyModel() + top_words = model.get_top_n_words(text, 10) + return render_template('index.html', top_words=top_words) + +if __name__ == '__main__': + app.run(debug=True) \ No newline at end of file diff --git a/交互/MVC/flask/models.py b/交互/MVC/flask/models.py new file mode 100644 index 0000000..5ee293f --- /dev/null +++ b/交互/MVC/flask/models.py @@ -0,0 +1,9 @@ +import re +from collections import Counter + +class WordFrequencyModel: + def get_top_n_words(self, text, top_n=10): + words = re.findall(r'\b\w+\b', text) + word_counts = Counter(words) + return word_counts.most_common(top_n) + diff --git a/交互/MVC/flask/templates/index.html b/交互/MVC/flask/templates/index.html new file mode 100644 index 0000000..28bd0f8 --- /dev/null +++ b/交互/MVC/flask/templates/index.html @@ -0,0 +1,22 @@ + + + + + Word Frequency Counter + + +

Word Frequency Counter

+
+
+ +
+ {% if top_words %} +

Top 10 Most Common Words:

+ + {% endif %} + + \ No newline at end of file