parent
df34765748
commit
6595ba1cdc
@ -0,0 +1 @@
|
||||
交互/MVC/flask/__pycache__/models.cpython-38.pyc
|
@ -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)
|
@ -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)
|
||||
|
@ -0,0 +1,22 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Word Frequency Counter</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Word Frequency Counter</h1>
|
||||
<form action="/count" method="post">
|
||||
<textarea name="text" rows="10" cols="50"></textarea><br>
|
||||
<input type="submit" value="Count Words">
|
||||
</form>
|
||||
{% if top_words %}
|
||||
<h2>Top 10 Most Common Words:</h2>
|
||||
<ul>
|
||||
{% for word, count in top_words %}
|
||||
<li>{{ word }}: {{ count }}</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
{% endif %}
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in new issue