File size: 1,233 Bytes
5120311
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from entity import Docs, Cluster
from fastapi import FastAPI
import time
import json
from fastapi.middleware.cors import CORSMiddleware
from function import topic_clustering as tc


app = FastAPI()
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)


@app.post("/newsanalysis/topic_clustering")
def topic_clustering(item: Docs):
    docs = item.response["docs"]
    # threshold = item.threshold
    hash_id = str(time.time())
    top_cluster = item.top_cluster
    top_sentence = item.top_sentence
    topn_summary = item.topn_summary
    with open("log/input_{0}.txt".format(hash_id), "w+") as f:
        f.write(json.dumps({"docs": docs, "key": item.keyword}))
    st_time = time.time()
    print("start ")
    print("len doc: ", len(docs))
    threshold = 0.1
    results = tc.topic_clustering(docs, threshold, top_cluster=top_cluster, top_sentence=top_sentence,
                                  topn_summary=topn_summary)
    with open("log/result_{0}.txt".format(hash_id), "w+") as f:
        f.write(json.dumps(results))
    print("time analysis: ", time.time() - st_time)
    return results