Spaces:
Sleeping
Sleeping
File size: 98,587 Bytes
38f2ab8 baf4a02 3f1d205 38f2ab8 3f1d205 38f2ab8 3f1d205 38f2ab8 3f1d205 38f2ab8 3f1d205 38f2ab8 3f1d205 38f2ab8 3f1d205 38f2ab8 3f1d205 38f2ab8 3f1d205 38f2ab8 3f1d205 38f2ab8 3f1d205 38f2ab8 3f1d205 99a8de6 3f1d205 99a8de6 baf4a02 3f1d205 baf4a02 99a8de6 3f1d205 99a8de6 3f1d205 baf4a02 3f1d205 baf4a02 3f1d205 38f2ab8 3f1d205 38f2ab8 3f1d205 38f2ab8 3f1d205 38f2ab8 3f1d205 38f2ab8 3f1d205 baf4a02 3f1d205 38f2ab8 3f1d205 38f2ab8 3f1d205 38f2ab8 3f1d205 38f2ab8 3f1d205 38f2ab8 3f1d205 baf4a02 3f1d205 38f2ab8 3f1d205 38f2ab8 3f1d205 38f2ab8 3f1d205 38f2ab8 3f1d205 38f2ab8 3f1d205 38f2ab8 3f1d205 baf4a02 3f1d205 baf4a02 3f1d205 baf4a02 3f1d205 baf4a02 3f1d205 baf4a02 3f1d205 baf4a02 3f1d205 baf4a02 3f1d205 baf4a02 3f1d205 baf4a02 3f1d205 baf4a02 3f1d205 38f2ab8 3f1d205 38f2ab8 3f1d205 baf4a02 3f1d205 baf4a02 3f1d205 baf4a02 3f1d205 38f2ab8 3f1d205 baf4a02 38f2ab8 3f1d205 38f2ab8 3f1d205 38f2ab8 3f1d205 38f2ab8 3f1d205 38f2ab8 3f1d205 38f2ab8 3f1d205 38f2ab8 3f1d205 38f2ab8 3f1d205 38f2ab8 3f1d205 38f2ab8 3f1d205 38f2ab8 3f1d205 38f2ab8 3f1d205 baf4a02 3f1d205 baf4a02 3f1d205 baf4a02 3f1d205 baf4a02 3f1d205 baf4a02 3f1d205 baf4a02 3f1d205 baf4a02 3f1d205 baf4a02 3f1d205 38f2ab8 3f1d205 38f2ab8 3f1d205 baf4a02 3f1d205 38f2ab8 3f1d205 38f2ab8 3f1d205 baf4a02 3f1d205 baf4a02 3f1d205 38f2ab8 3f1d205 38f2ab8 3f1d205 38f2ab8 3f1d205 38f2ab8 3f1d205 38f2ab8 3f1d205 38f2ab8 3f1d205 38f2ab8 3f1d205 38f2ab8 3f1d205 38f2ab8 3f1d205 38f2ab8 3f1d205 38f2ab8 3f1d205 38f2ab8 3f1d205 5250a59 3f1d205 baf4a02 3f1d205 baf4a02 3f1d205 baf4a02 3f1d205 38f2ab8 3f1d205 5250a59 3f1d205 38f2ab8 3f1d205 38f2ab8 3f1d205 38f2ab8 3f1d205 5250a59 3f1d205 |
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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 |
import gradio as gr
import pandas as pd
import numpy as np
import json
import re
import io
import asyncio
import threading
import time
import gc
from datetime import datetime, timedelta
from typing import List, Dict, Tuple, Optional, Any
from collections import Counter, defaultdict
import sqlite3
import hashlib
import logging
from dataclasses import dataclass
from enum import Enum
# Lazy import heavy modules
transformers = None
plotly = None
torch = None
def lazy_import():
"""Lazy load heavy modules to reduce startup time"""
global transformers, plotly, torch
if transformers is None:
import transformers as tf
transformers = tf
if plotly is None:
import plotly.graph_objects as go
from plotly.subplots import make_subplots
plotly = type('plotly', (), {'go': go, 'make_subplots': make_subplots})()
if torch is None:
try:
import torch as t
torch = t
except ImportError:
torch = None
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class AnalysisType(Enum):
SENTIMENT = "sentiment"
ASPECT = "aspect"
EMOTION = "emotion"
FAKE_DETECTION = "fake_detection"
QUALITY = "quality"
RECOMMENDATION = "recommendation"
TREND = "trend"
COMPETITION = "competition"
@dataclass
class ReviewData:
"""Review data structure"""
text: str
timestamp: Optional[str] = None
rating: Optional[float] = None
username: Optional[str] = None
product_id: Optional[str] = None
verified_purchase: Optional[bool] = None
helpful_votes: Optional[int] = None
class ModelManager:
"""Model manager - supports lazy loading and resource management"""
def __init__(self):
self._models = {}
self._loading = {}
self.max_models_in_memory = 3
self.model_usage = {}
def get_model(self, model_name: str, model_type: str = "sentiment"):
"""Get model with lazy loading support"""
if model_name in self._models:
self.model_usage[model_name] = time.time()
return self._models[model_name]
if model_name in self._loading:
# Wait for other threads to finish loading
while model_name in self._loading:
time.sleep(0.1)
return self._models.get(model_name)
return self._load_model(model_name, model_type)
def _load_model(self, model_name: str, model_type: str):
"""Load model"""
self._loading[model_name] = True
try:
lazy_import()
if model_type == "sentiment":
model = transformers.pipeline(
"sentiment-analysis",
model=model_name,
device=-1 # CPU
)
elif model_type == "emotion":
model = transformers.pipeline(
"text-classification",
model=model_name,
device=-1
)
elif model_type == "ner":
model = transformers.pipeline(
"ner",
model=model_name,
aggregation_strategy="simple",
device=-1
)
else:
raise ValueError(f"Unsupported model type: {model_type}")
# Memory management
if len(self._models) >= self.max_models_in_memory:
self._cleanup_oldest_model()
self._models[model_name] = model
self.model_usage[model_name] = time.time()
logger.info(f"Successfully loaded model: {model_name}")
except Exception as e:
logger.error(f"Failed to load model {model_name}: {str(e)}")
model = None
finally:
self._loading.pop(model_name, None)
return model
def _cleanup_oldest_model(self):
"""Clean up the least recently used model"""
if not self.model_usage:
return
oldest_model = min(self.model_usage.items(), key=lambda x: x[1])[0]
self._models.pop(oldest_model, None)
self.model_usage.pop(oldest_model, None)
# Force garbage collection
gc.collect()
if torch and torch.cuda.is_available():
torch.cuda.empty_cache()
class AdvancedReviewAnalyzer:
"""Advanced Review Analyzer"""
def __init__(self):
self.model_manager = ModelManager()
self.db_path = "advanced_reviews.db"
self._init_db()
# Configure different models
self.models_config = {
"sentiment": "cardiffnlp/twitter-roberta-base-sentiment-latest",
"emotion": "j-hartmann/emotion-english-distilroberta-base",
"chinese_sentiment": "uer/roberta-base-finetuned-chinanews-chinese",
}
# Cache system
self.cache = {}
self.cache_ttl = 3600 # 1 hour
# Sentiment lexicon
self.sentiment_lexicon = self._load_sentiment_lexicon()
# Aspect keyword mapping
self.aspect_keywords = {
'product_quality': ['quality', 'build', 'material', 'durable', 'sturdy', 'solid', 'cheap', 'flimsy', 'fragile'],
'price_value': ['price', 'cost', 'expensive', 'cheap', 'value', 'money', 'affordable', 'overpriced', 'worth'],
'shipping_delivery': ['delivery', 'shipping', 'fast', 'slow', 'quick', 'late', 'packaging', 'arrived'],
'customer_service': ['service', 'support', 'staff', 'helpful', 'rude', 'friendly', 'responsive'],
'design_appearance': ['design', 'look', 'beautiful', 'ugly', 'style', 'appearance', 'color', 'attractive'],
'usability': ['easy', 'difficult', 'simple', 'complex', 'user-friendly', 'intuitive', 'confusing'],
'performance': ['performance', 'speed', 'fast', 'slow', 'efficient', 'reliable', 'works', 'functions'],
'size_fit': ['size', 'fit', 'large', 'small', 'perfect', 'tight', 'loose', 'dimensions']
}
# Emotion emojis
self.emotion_emojis = {
'joy': '😊', 'sadness': '😢', 'anger': '😠', 'fear': '😨',
'surprise': '😮', 'disgust': '🤢', 'love': '❤️'
}
def _init_db(self):
"""Initialize database"""
conn = sqlite3.connect(self.db_path)
conn.execute('''
CREATE TABLE IF NOT EXISTS analysis_cache (
id TEXT PRIMARY KEY,
analysis_type TEXT,
data TEXT,
timestamp DATETIME,
expires_at DATETIME
)
''')
conn.execute('''
CREATE TABLE IF NOT EXISTS usage_analytics (
id INTEGER PRIMARY KEY,
user_session TEXT,
analysis_type TEXT,
review_count INTEGER,
processing_time REAL,
timestamp DATETIME
)
''')
conn.execute('''
CREATE TABLE IF NOT EXISTS feedback (
id INTEGER PRIMARY KEY,
session_id TEXT,
rating INTEGER,
comment TEXT,
timestamp DATETIME
)
''')
conn.close()
def _load_sentiment_lexicon(self):
"""Load sentiment lexicon"""
# Simplified sentiment lexicon
return {
'positive': ['excellent', 'amazing', 'great', 'good', 'perfect', 'wonderful', 'fantastic',
'outstanding', 'superb', 'brilliant', 'awesome', 'love', 'recommend'],
'negative': ['terrible', 'awful', 'bad', 'horrible', 'disgusting', 'disappointing',
'waste', 'useless', 'regret', 'hate', 'worst', 'broken']
}
def _get_cache_key(self, data: str, analysis_type: str) -> str:
"""Generate cache key"""
return hashlib.md5(f"{analysis_type}:{data}".encode()).hexdigest()
def _get_from_cache(self, cache_key: str) -> Optional[Dict]:
"""Get results from cache"""
conn = sqlite3.connect(self.db_path)
cursor = conn.execute(
"SELECT data FROM analysis_cache WHERE id = ? AND expires_at > ?",
(cache_key, datetime.now())
)
result = cursor.fetchone()
conn.close()
if result:
return json.loads(result[0])
return None
def _save_to_cache(self, cache_key: str, data: Dict, analysis_type: str):
"""Save to cache"""
expires_at = datetime.now() + timedelta(seconds=self.cache_ttl)
conn = sqlite3.connect(self.db_path)
conn.execute(
"INSERT OR REPLACE INTO analysis_cache (id, analysis_type, data, timestamp, expires_at) VALUES (?, ?, ?, ?, ?)",
(cache_key, analysis_type, json.dumps(data), datetime.now(), expires_at)
)
conn.commit()
conn.close()
def preprocess_reviews(self, reviews: List[str]) -> List[ReviewData]:
"""Preprocess review data"""
processed_reviews = []
for review in reviews:
if not review or len(review.strip()) < 10:
continue
# Clean text
clean_text = re.sub(r'http\S+', '', review) # Remove URLs
clean_text = re.sub(r'@\w+', '', clean_text) # Remove mentions
clean_text = re.sub(r'#\w+', '', clean_text) # Remove hashtags
clean_text = re.sub(r'\s+', ' ', clean_text).strip() # Normalize whitespace
if clean_text:
processed_reviews.append(ReviewData(text=clean_text))
return processed_reviews
def analyze_sentiment_advanced(self, reviews: List[str], language: str = "en") -> Dict:
"""Advanced sentiment analysis"""
cache_key = self._get_cache_key(str(reviews), "sentiment_advanced")
cached_result = self._get_from_cache(cache_key)
if cached_result:
return cached_result
processed_reviews = self.preprocess_reviews(reviews)
if not processed_reviews:
return {"error": "No valid reviews to analyze"}
# Select appropriate model
model_name = self.models_config.get("chinese_sentiment" if language == "zh" else "sentiment")
sentiment_model = self.model_manager.get_model(model_name, "sentiment")
if not sentiment_model:
return {"error": "Failed to load sentiment model"}
results = []
sentiment_counts = defaultdict(int)
confidence_scores = []
try:
for review_data in processed_reviews:
# Use model for analysis
model_result = sentiment_model(review_data.text)[0]
# Normalize labels
label = model_result['label'].lower()
if 'pos' in label:
sentiment = 'positive'
elif 'neg' in label:
sentiment = 'negative'
else:
sentiment = 'neutral'
confidence = float(model_result['score'])
# Lexicon enhancement
lexicon_boost = self._get_lexicon_sentiment(review_data.text)
if lexicon_boost:
confidence = min(confidence + 0.1, 1.0)
sentiment_counts[sentiment] += 1
confidence_scores.append(confidence)
results.append({
'text': review_data.text[:100] + '...' if len(review_data.text) > 100 else review_data.text,
'sentiment': sentiment,
'confidence': round(confidence, 3),
'lexicon_matched': lexicon_boost is not None
})
except Exception as e:
logger.error(f"Sentiment analysis error: {str(e)}")
return {"error": f"Analysis failed: {str(e)}"}
# Calculate statistics
total_reviews = len(results)
sentiment_percentages = {k: round(v/total_reviews*100, 1) for k, v in sentiment_counts.items()}
avg_confidence = round(np.mean(confidence_scores), 3) if confidence_scores else 0
result = {
'summary': sentiment_percentages,
'average_confidence': avg_confidence,
'total_reviews': total_reviews,
'details': results,
'insights': self._generate_sentiment_insights(sentiment_percentages, avg_confidence)
}
self._save_to_cache(cache_key, result, "sentiment_advanced")
return result
def _get_lexicon_sentiment(self, text: str) -> Optional[str]:
"""Get sentiment based on lexicon"""
text_lower = text.lower()
pos_count = sum(1 for word in self.sentiment_lexicon['positive'] if word in text_lower)
neg_count = sum(1 for word in self.sentiment_lexicon['negative'] if word in text_lower)
if pos_count > neg_count:
return 'positive'
elif neg_count > pos_count:
return 'negative'
return None
def _generate_sentiment_insights(self, percentages: Dict, avg_confidence: float) -> List[str]:
"""Generate sentiment analysis insights"""
insights = []
positive_pct = percentages.get('positive', 0)
negative_pct = percentages.get('negative', 0)
if positive_pct > 70:
insights.append("🎉 Product receives overwhelmingly positive reviews with high customer satisfaction")
elif positive_pct > 50:
insights.append("✅ Product has generally positive reviews but there's room for improvement")
elif negative_pct > 50:
insights.append("⚠️ Product has significant issues that need attention based on customer feedback")
else:
insights.append("📊 Product reviews are relatively neutral, requiring more data for analysis")
if avg_confidence > 0.8:
insights.append("🎯 High confidence in analysis results with good prediction accuracy")
elif avg_confidence < 0.6:
insights.append("❓ Some reviews have ambiguous sentiment, recommend manual review")
return insights
def analyze_emotions(self, reviews: List[str]) -> Dict:
"""Emotion analysis (fine-grained emotions)"""
cache_key = self._get_cache_key(str(reviews), "emotions")
cached_result = self._get_from_cache(cache_key)
if cached_result:
return cached_result
processed_reviews = self.preprocess_reviews(reviews)
if not processed_reviews:
return {"error": "No valid reviews to analyze"}
emotion_model = self.model_manager.get_model(self.models_config["emotion"], "emotion")
if not emotion_model:
return {"error": "Failed to load emotion model"}
emotion_counts = defaultdict(int)
results = []
try:
for review_data in processed_reviews:
emotion_result = emotion_model(review_data.text)[0]
emotion = emotion_result['label'].lower()
confidence = float(emotion_result['score'])
emotion_counts[emotion] += 1
results.append({
'text': review_data.text[:100] + '...' if len(review_data.text) > 100 else review_data.text,
'emotion': emotion,
'emoji': self.emotion_emojis.get(emotion, '😐'),
'confidence': round(confidence, 3)
})
except Exception as e:
logger.error(f"Emotion analysis error: {str(e)}")
return {"error": f"Analysis failed: {str(e)}"}
total_reviews = len(results)
emotion_percentages = {k: round(v/total_reviews*100, 1) for k, v in emotion_counts.items()}
result = {
'summary': emotion_percentages,
'total_reviews': total_reviews,
'details': results,
'dominant_emotion': max(emotion_percentages.items(), key=lambda x: x[1])[0] if emotion_percentages else 'neutral'
}
self._save_to_cache(cache_key, result, "emotions")
return result
def analyze_aspects_advanced(self, reviews: List[str]) -> Dict:
"""Advanced aspect-based sentiment analysis (ABSA)"""
cache_key = self._get_cache_key(str(reviews), "aspects_advanced")
cached_result = self._get_from_cache(cache_key)
if cached_result:
return cached_result
processed_reviews = self.preprocess_reviews(reviews)
if not processed_reviews:
return {"error": "No valid reviews to analyze"}
sentiment_model = self.model_manager.get_model(self.models_config["sentiment"], "sentiment")
if not sentiment_model:
return {"error": "Failed to load sentiment model"}
aspect_sentiments = defaultdict(lambda: defaultdict(int))
aspect_mentions = defaultdict(list)
detailed_aspects = []
try:
for review_data in processed_reviews:
review_text = review_data.text.lower()
# Get overall review sentiment
overall_sentiment = sentiment_model(review_data.text)[0]
overall_label = 'positive' if 'pos' in overall_sentiment['label'].lower() else 'negative'
# Detect aspect mentions
for aspect, keywords in self.aspect_keywords.items():
for keyword in keywords:
if keyword in review_text:
# Extract aspect-related sentences
sentences = re.split(r'[.!?]', review_data.text)
relevant_sentences = [s.strip() for s in sentences if keyword in s.lower()]
if relevant_sentences:
# Perform sentiment analysis on relevant sentences
sentence_text = ' '.join(relevant_sentences)
try:
aspect_sentiment_result = sentiment_model(sentence_text)[0]
aspect_sentiment = 'positive' if 'pos' in aspect_sentiment_result['label'].lower() else 'negative'
confidence = float(aspect_sentiment_result['score'])
except:
aspect_sentiment = overall_label
confidence = 0.5
aspect_sentiments[aspect][aspect_sentiment] += 1
aspect_mentions[aspect].append({
'text': sentence_text,
'sentiment': aspect_sentiment,
'confidence': round(confidence, 3)
})
detailed_aspects.append({
'aspect': aspect,
'keyword': keyword,
'sentence': sentence_text,
'sentiment': aspect_sentiment,
'confidence': round(confidence, 3)
})
break
except Exception as e:
logger.error(f"Aspect analysis error: {str(e)}")
return {"error": f"Analysis failed: {str(e)}"}
# Calculate aspect sentiment scores
aspect_scores = {}
for aspect, sentiments in aspect_sentiments.items():
total = sum(sentiments.values())
if total > 0:
positive_pct = sentiments['positive'] / total * 100
negative_pct = sentiments['negative'] / total * 100
aspect_scores[aspect] = {
'positive_percentage': round(positive_pct, 1),
'negative_percentage': round(negative_pct, 1),
'total_mentions': total,
'sentiment_score': round((positive_pct - negative_pct) / 100, 2) # Score from -1 to 1
}
# Sort aspects
top_positive_aspects = sorted(aspect_scores.items(),
key=lambda x: x[1]['sentiment_score'], reverse=True)[:5]
top_negative_aspects = sorted(aspect_scores.items(),
key=lambda x: x[1]['sentiment_score'])[:5]
result = {
'aspect_scores': aspect_scores,
'top_positive_aspects': [(k, v) for k, v in top_positive_aspects],
'top_negative_aspects': [(k, v) for k, v in top_negative_aspects],
'detailed_aspects': detailed_aspects[:50], # Limit detailed results
'total_aspects_found': len(aspect_scores),
'insights': self._generate_aspect_insights(aspect_scores)
}
self._save_to_cache(cache_key, result, "aspects_advanced")
return result
def _generate_aspect_insights(self, aspect_scores: Dict) -> List[str]:
"""Generate aspect analysis insights"""
insights = []
if not aspect_scores:
return ["No clear product aspects detected, recommend adding more review data"]
# Find best and worst aspects
best_aspect = max(aspect_scores.items(), key=lambda x: x[1]['sentiment_score'])
worst_aspect = min(aspect_scores.items(), key=lambda x: x[1]['sentiment_score'])
insights.append(f"🏆 Best performing aspect: {best_aspect[0]} (score: {best_aspect[1]['sentiment_score']})")
insights.append(f"⚠️ Needs improvement: {worst_aspect[0]} (score: {worst_aspect[1]['sentiment_score']})")
# Mention frequency analysis
most_mentioned = max(aspect_scores.items(), key=lambda x: x[1]['total_mentions'])
insights.append(f"📊 Most discussed aspect: {most_mentioned[0]} ({most_mentioned[1]['total_mentions']} mentions)")
return insights
def detect_fake_reviews_advanced(self, reviews: List[str], metadata: Dict = None) -> Dict:
"""Advanced fake review detection"""
cache_key = self._get_cache_key(str(reviews) + str(metadata), "fake_advanced")
cached_result = self._get_from_cache(cache_key)
if cached_result:
return cached_result
processed_reviews = self.preprocess_reviews(reviews)
if not processed_reviews:
return {"error": "No valid reviews to analyze"}
fake_indicators = []
for i, review_data in enumerate(processed_reviews):
indicators = self._analyze_fake_indicators(review_data, i, metadata)
fake_indicators.append(indicators)
# Overall pattern analysis
pattern_analysis = self._analyze_review_patterns(processed_reviews, metadata)
# Calculate final scores
total_suspicious = sum(1 for ind in fake_indicators if ind['risk_score'] > 0.6)
authenticity_rate = round((len(fake_indicators) - total_suspicious) / len(fake_indicators) * 100, 1)
result = {
'summary': {
'total_reviews': len(fake_indicators),
'suspicious_reviews': total_suspicious,
'authenticity_rate': authenticity_rate,
'risk_level': 'High' if authenticity_rate < 60 else 'Medium' if authenticity_rate < 80 else 'Low'
},
'individual_analysis': fake_indicators,
'pattern_analysis': pattern_analysis,
'recommendations': self._generate_fake_detection_recommendations(authenticity_rate, pattern_analysis)
}
self._save_to_cache(cache_key, result, "fake_advanced")
return result
def _analyze_fake_indicators(self, review_data: ReviewData, index: int, metadata: Dict) -> Dict:
"""Analyze fake indicators for individual review"""
text = review_data.text
risk_score = 0.0
flags = []
# Text length check
if len(text) < 30:
risk_score += 0.2
flags.append("too_short")
elif len(text) > 1000:
risk_score += 0.1
flags.append("unusually_long")
# Vocabulary diversity
words = text.lower().split()
unique_ratio = len(set(words)) / len(words) if words else 0
if unique_ratio < 0.4:
risk_score += 0.3
flags.append("repetitive_vocabulary")
# Extreme sentiment
extreme_positive = ['perfect', 'amazing', 'incredible', 'flawless', 'outstanding']
extreme_negative = ['terrible', 'horrible', 'disgusting', 'awful', 'worst']
extreme_count = sum(1 for word in extreme_positive + extreme_negative if word in text.lower())
if extreme_count > 3:
risk_score += 0.25
flags.append("extreme_sentiment")
# Generic phrases check
generic_phrases = ['highly recommend', 'five stars', 'buy it now', 'great product', 'very satisfied']
generic_count = sum(1 for phrase in generic_phrases if phrase in text.lower())
if generic_count > 2:
risk_score += 0.2
flags.append("generic_language")
# Language quality
punct_ratio = len(re.findall(r'[!?]', text)) / len(text) if text else 0
if punct_ratio > 0.05:
risk_score += 0.15
flags.append("excessive_punctuation")
# Check uppercase ratio
upper_ratio = sum(1 for c in text if c.isupper()) / len(text) if text else 0
if upper_ratio > 0.3:
risk_score += 0.15
flags.append("excessive_caps")
return {
'text': text[:100] + '...' if len(text) > 100 else text,
'risk_score': min(round(risk_score, 3), 1.0),
'status': 'suspicious' if risk_score > 0.6 else 'questionable' if risk_score > 0.3 else 'authentic',
'flags': flags,
'confidence': round(1 - risk_score, 3)
}
def _analyze_review_patterns(self, reviews: List[ReviewData], metadata: Dict) -> Dict:
"""Analyze overall review patterns"""
pattern_flags = []
# Time pattern analysis
if metadata and 'timestamps' in metadata:
time_analysis = self._analyze_time_patterns(metadata['timestamps'])
pattern_flags.extend(time_analysis)
# Username patterns
if metadata and 'usernames' in metadata:
username_analysis = self._analyze_username_patterns(metadata['usernames'])
pattern_flags.extend(username_analysis)
# Text similarity
similarity_analysis = self._analyze_text_similarity([r.text for r in reviews])
pattern_flags.extend(similarity_analysis)
return {
'detected_patterns': pattern_flags,
'pattern_count': len(pattern_flags),
'severity': 'High' if len(pattern_flags) > 5 else 'Medium' if len(pattern_flags) > 2 else 'Low'
}
def _analyze_time_patterns(self, timestamps: List[str]) -> List[str]:
"""Analyze time patterns"""
patterns = []
if len(timestamps) < 5:
return patterns
try:
# Parse timestamps
times = []
for ts in timestamps:
try:
dt = datetime.strptime(ts, "%Y-%m-%d %H:%M:%S")
times.append(dt)
except:
continue
if len(times) < 5:
return patterns
# Check time clustering
times.sort()
for i in range(len(times) - 4):
if (times[i + 4] - times[i]).total_seconds() < 600: # 5 reviews within 10 minutes
patterns.append("suspicious_time_clustering")
break
# Check work hours pattern
work_hour_reviews = sum(1 for t in times if 9 <= t.hour <= 17)
if work_hour_reviews / len(times) > 0.8:
patterns.append("work_hours_concentration")
except Exception as e:
logger.error(f"Time pattern analysis error: {str(e)}")
return patterns
def _analyze_username_patterns(self, usernames: List[str]) -> List[str]:
"""Analyze username patterns"""
patterns = []
# Check similar usernames
similar_count = 0
for i, username1 in enumerate(usernames):
for j, username2 in enumerate(usernames[i+1:], i+1):
# Check auto-generated username patterns
if re.match(r'user\d+', username1.lower()) and re.match(r'user\d+', username2.lower()):
similar_count += 1
# Check prefix similarity
elif len(username1) > 4 and len(username2) > 4 and username1[:4].lower() == username2[:4].lower():
similar_count += 1
if similar_count > len(usernames) * 0.3:
patterns.append("suspicious_username_patterns")
# Check default usernames
default_patterns = ['user', 'guest', 'anonymous', 'temp']
default_count = sum(1 for username in usernames
if any(pattern in username.lower() for pattern in default_patterns))
if default_count > len(usernames) * 0.4:
patterns.append("excessive_default_usernames")
return patterns
def _analyze_text_similarity(self, texts: List[str]) -> List[str]:
"""Analyze text similarity"""
patterns = []
if len(texts) < 3:
return patterns
# Simple text similarity check
similar_pairs = 0
total_pairs = 0
for i, text1 in enumerate(texts):
for j, text2 in enumerate(texts[i+1:], i+1):
total_pairs += 1
# Calculate word overlap ratio
words1 = set(text1.lower().split())
words2 = set(text2.lower().split())
if len(words1) > 0 and len(words2) > 0:
overlap = len(words1 & words2) / len(words1 | words2)
if overlap > 0.7: # 70% overlap
similar_pairs += 1
# Check for completely repeated short phrases
if len(text1) > 20 and text1.lower() in text2.lower():
similar_pairs += 1
if total_pairs > 0 and similar_pairs / total_pairs > 0.3:
patterns.append("high_text_similarity")
# Check template language
template_phrases = ['i bought this', 'would recommend', 'great product', 'fast shipping']
template_counts = Counter()
for text in texts:
for phrase in template_phrases:
if phrase in text.lower():
template_counts[phrase] += 1
if any(count > len(texts) * 0.6 for count in template_counts.values()):
patterns.append("template_language")
return patterns
def _generate_fake_detection_recommendations(self, authenticity_rate: float, pattern_analysis: Dict) -> List[str]:
"""Generate fake detection recommendations"""
recommendations = []
if authenticity_rate < 60:
recommendations.append("🚨 High Risk: Immediate review of all comments recommended, possible large-scale fake review activity")
recommendations.append("📋 Recommend enabling manual review process")
elif authenticity_rate < 80:
recommendations.append("⚠️ Medium Risk: Some reviews are suspicious, focus on extreme rating reviews")
else:
recommendations.append("✅ Low Risk: Overall review authenticity is high")
if pattern_analysis['pattern_count'] > 3:
recommendations.append("🔍 Multiple suspicious patterns detected, recommend strengthening review posting restrictions")
recommendations.append("💡 Recommend regular review quality monitoring and establish long-term anti-fraud mechanisms")
return recommendations
def assess_review_quality_comprehensive(self, reviews: List[str], custom_weights: Dict = None) -> Tuple[Dict, Any]:
"""Comprehensive review quality assessment"""
cache_key = self._get_cache_key(str(reviews) + str(custom_weights), "quality_comprehensive")
cached_result = self._get_from_cache(cache_key)
if cached_result and 'chart_data' not in cached_result: # Chart data not cached
return cached_result, None
processed_reviews = self.preprocess_reviews(reviews)
if not processed_reviews:
return {"error": "No valid reviews to analyze"}, None
default_weights = {
'length_depth': 0.2, # Length and depth
'specificity': 0.2, # Specificity
'structure': 0.15, # Structure
'helpfulness': 0.15, # Helpfulness
'objectivity': 0.15, # Objectivity
'readability': 0.15 # Readability
}
weights = custom_weights if custom_weights else default_weights
quality_assessments = []
for review_data in processed_reviews:
assessment = self._comprehensive_quality_assessment(review_data.text, weights)
quality_assessments.append(assessment)
# Calculate statistics
avg_scores = {}
for factor in weights.keys():
scores = [assessment['factors'][factor] for assessment in quality_assessments]
avg_scores[factor] = round(np.mean(scores), 3)
overall_avg = round(np.mean([assessment['overall_score'] for assessment in quality_assessments]), 3)
# Quality grade distribution
grade_distribution = Counter([assessment['grade'] for assessment in quality_assessments])
grade_percentages = {grade: round(count/len(quality_assessments)*100, 1)
for grade, count in grade_distribution.items()}
result = {
'summary': {
'average_quality': overall_avg,
'total_reviews': len(quality_assessments),
'grade_distribution': grade_percentages,
'high_quality_count': sum(1 for assessment in quality_assessments if assessment['overall_score'] > 0.75),
'weights_used': weights
},
'factor_averages': avg_scores,
'detailed_assessments': quality_assessments[:20], # Limit display count
'insights': self._generate_quality_insights(overall_avg, grade_percentages, avg_scores)
}
# Create chart data
chart_data = self._create_quality_chart_data(avg_scores, grade_percentages)
if not cached_result:
self._save_to_cache(cache_key, result, "quality_comprehensive")
return result, chart_data
def _comprehensive_quality_assessment(self, text: str, weights: Dict) -> Dict:
"""Comprehensive quality assessment for individual review"""
factors = {}
# Length and depth (0-1)
word_count = len(text.split())
char_count = len(text)
factors['length_depth'] = min(word_count / 100, 1.0) * 0.7 + min(char_count / 500, 1.0) * 0.3
# Specificity (0-1) - Check specific details
specific_indicators = ['because', 'however', 'specifically', 'for example', 'such as', 'like', 'unlike']
numbers = len(re.findall(r'\b\d+\b', text))
specific_words = sum(1 for indicator in specific_indicators if indicator in text.lower())
factors['specificity'] = min((specific_words * 0.15 + numbers * 0.1), 1.0)
# Structure (0-1) - Sentence structure and organization
sentences = len(re.split(r'[.!?]+', text))
paragraphs = len(text.split('\n\n'))
avg_sentence_length = word_count / sentences if sentences > 0 else 0
structure_score = min(sentences / 5, 1.0) * 0.6 + min(paragraphs / 3, 1.0) * 0.2
if 10 <= avg_sentence_length <= 20: # Ideal sentence length
structure_score += 0.2
factors['structure'] = min(structure_score, 1.0)
# Helpfulness (0-1) - Help for other buyers
helpful_indicators = ['recommend', 'suggest', 'tip', 'advice', 'pros', 'cons', 'compare', 'alternative']
helpful_score = sum(1 for indicator in helpful_indicators if indicator in text.lower())
factors['helpfulness'] = min(helpful_score / 4, 1.0)
# Objectivity (0-1) - Balanced viewpoint
extreme_words = ['perfect', 'terrible', 'amazing', 'awful', 'incredible', 'horrible']
balanced_indicators = ['but', 'however', 'although', 'despite', 'while']
extreme_count = sum(1 for word in extreme_words if word in text.lower())
balanced_count = sum(1 for indicator in balanced_indicators if indicator in text.lower())
objectivity_score = 1.0
if extreme_count > 2:
objectivity_score -= 0.3
if balanced_count > 0:
objectivity_score += 0.2
factors['objectivity'] = max(min(objectivity_score, 1.0), 0.0)
# Readability (0-1) - Grammar and spelling quality
punctuation_ratio = len(re.findall(r'[,.!?;:]', text)) / len(text) if text else 0
capital_ratio = sum(1 for c in text if c.isupper()) / len(text) if text else 0
readability_score = 1.0
if punctuation_ratio > 0.1: # Too much punctuation
readability_score -= 0.2
if capital_ratio > 0.2: # Too many capitals
readability_score -= 0.3
if len(re.findall(r'\s+', text)) / len(text.split()) > 2: # Abnormal spacing
readability_score -= 0.2
factors['readability'] = max(readability_score, 0.0)
# Calculate weighted total score
overall_score = sum(factors[factor] * weights[factor] for factor in factors.keys())
# Grading
if overall_score >= 0.85:
grade = 'A+'
elif overall_score >= 0.75:
grade = 'A'
elif overall_score >= 0.65:
grade = 'B'
elif overall_score >= 0.55:
grade = 'C'
elif overall_score >= 0.45:
grade = 'D'
else:
grade = 'F'
return {
'text': text[:100] + '...' if len(text) > 100 else text,
'overall_score': round(overall_score, 3),
'grade': grade,
'factors': {k: round(v, 3) for k, v in factors.items()}
}
def _create_quality_chart_data(self, factor_averages: Dict, grade_distribution: Dict) -> Dict:
"""Create quality analysis chart data"""
return {
'factor_averages': factor_averages,
'grade_distribution': grade_distribution
}
def _generate_quality_insights(self, overall_avg: float, grade_distribution: Dict, factor_averages: Dict) -> List[str]:
"""Generate quality analysis insights"""
insights = []
# Overall quality assessment
if overall_avg >= 0.75:
insights.append("🏆 Excellent overall review quality, providing valuable information for potential customers")
elif overall_avg >= 0.6:
insights.append("✅ Good review quality, but room for improvement remains")
else:
insights.append("⚠️ Review quality needs improvement, recommend encouraging more detailed feedback")
# Grade distribution analysis
high_quality_pct = grade_distribution.get('A+', 0) + grade_distribution.get('A', 0)
if high_quality_pct > 50:
insights.append(f"📊 {high_quality_pct}% of reviews meet high quality standards")
# Factor analysis
best_factor = max(factor_averages.items(), key=lambda x: x[1])
worst_factor = min(factor_averages.items(), key=lambda x: x[1])
insights.append(f"💪 Strongest review aspect: {best_factor[0]} (score: {best_factor[1]})")
insights.append(f"🎯 Needs improvement: {worst_factor[0]} (score: {worst_factor[1]})")
return insights
def predict_recommendation_intent(self, reviews: List[str]) -> Dict:
"""Predict recommendation intent"""
cache_key = self._get_cache_key(str(reviews), "recommendation_intent")
cached_result = self._get_from_cache(cache_key)
if cached_result:
return cached_result
processed_reviews = self.preprocess_reviews(reviews)
if not processed_reviews:
return {"error": "No valid reviews to analyze"}
recommendation_indicators = {
'strong_positive': ['highly recommend', 'definitely buy', 'must have', 'love it', 'perfect'],
'positive': ['recommend', 'good choice', 'satisfied', 'happy with', 'worth it'],
'negative': ['not recommend', 'disappointed', 'regret', 'waste of money', 'avoid'],
'strong_negative': ['never buy again', 'terrible', 'worst purchase', 'completely disappointed']
}
results = []
intent_counts = defaultdict(int)
for review_data in processed_reviews:
text_lower = review_data.text.lower()
intent_score = 0
matched_indicators = []
# Check recommendation intent indicators
for intent_type, indicators in recommendation_indicators.items():
for indicator in indicators:
if indicator in text_lower:
if intent_type == 'strong_positive':
intent_score += 2
elif intent_type == 'positive':
intent_score += 1
elif intent_type == 'negative':
intent_score -= 1
elif intent_type == 'strong_negative':
intent_score -= 2
matched_indicators.append(indicator)
# Determine recommendation intent level
if intent_score >= 2:
intent = 'strongly_recommend'
elif intent_score >= 1:
intent = 'recommend'
elif intent_score <= -2:
intent = 'strongly_not_recommend'
elif intent_score <= -1:
intent = 'not_recommend'
else:
intent = 'neutral'
intent_counts[intent] += 1
results.append({
'text': review_data.text[:100] + '...' if len(review_data.text) > 100 else review_data.text,
'recommendation_intent': intent,
'confidence_score': min(abs(intent_score) / 2, 1.0),
'matched_indicators': matched_indicators
})
# Calculate recommendation rate
total = len(results)
recommend_count = intent_counts['recommend'] + intent_counts['strongly_recommend']
not_recommend_count = intent_counts['not_recommend'] + intent_counts['strongly_not_recommend']
recommendation_rate = round(recommend_count / total * 100, 1) if total > 0 else 0
result = {
'summary': {
'recommendation_rate': recommendation_rate,
'total_reviews': total,
'distribution': {k: round(v/total*100, 1) for k, v in intent_counts.items()}
},
'detailed_results': results,
'insights': self._generate_recommendation_insights(recommendation_rate, intent_counts)
}
self._save_to_cache(cache_key, result, "recommendation_intent")
return result
def _generate_recommendation_insights(self, recommendation_rate: float, intent_counts: Dict) -> List[str]:
"""Generate recommendation intent insights"""
insights = []
if recommendation_rate > 80:
insights.append("🎉 Product receives extremely high recommendation rate with excellent customer satisfaction")
elif recommendation_rate > 60:
insights.append("👍 Good product recommendation rate, customers are generally satisfied")
elif recommendation_rate < 30:
insights.append("⚠️ Low product recommendation rate, need to focus on product quality or service issues")
# Analyze intent strength
strong_positive = intent_counts.get('strongly_recommend', 0)
strong_negative = intent_counts.get('strongly_not_recommend', 0)
if strong_positive > strong_negative * 2:
insights.append("💪 Strong positive recommendations dominate, product has strong customer loyalty")
elif strong_negative > strong_positive:
insights.append("🚨 Significant strong negative recommendations exist, need immediate attention to core issues")
return insights
def analyze_review_trends(self, reviews: List[str], timestamps: List[str] = None) -> Dict:
"""Analyze review trends"""
if not timestamps:
return {"error": "Timestamp data required for trend analysis"}
cache_key = self._get_cache_key(str(reviews) + str(timestamps), "trends")
cached_result = self._get_from_cache(cache_key)
if cached_result:
return cached_result
# Parse timestamps and sort by time
review_time_pairs = []
for review, timestamp in zip(reviews, timestamps):
try:
dt = datetime.strptime(timestamp, "%Y-%m-%d %H:%M:%S")
review_time_pairs.append((review, dt))
except:
continue
review_time_pairs.sort(key=lambda x: x[1])
if len(review_time_pairs) < 10:
return {"error": "Need at least 10 valid timestamped reviews for trend analysis"}
# Group by month for analysis
monthly_data = defaultdict(list)
for review, dt in review_time_pairs:
month_key = dt.strftime("%Y-%m")
monthly_data[month_key].append(review)
# Calculate monthly trends
monthly_trends = {}
for month, month_reviews in monthly_data.items():
sentiment_analysis = self.analyze_sentiment_advanced(month_reviews)
if 'error' not in sentiment_analysis:
monthly_trends[month] = {
'review_count': len(month_reviews),
'positive_rate': sentiment_analysis['summary'].get('positive', 0),
'negative_rate': sentiment_analysis['summary'].get('negative', 0),
'average_confidence': sentiment_analysis.get('average_confidence', 0)
}
# Trend analysis
months = sorted(monthly_trends.keys())
if len(months) >= 3:
trend_analysis = self._analyze_sentiment_trend(months, monthly_trends)
else:
trend_analysis = {"error": "Need at least 3 months of data for trend analysis"}
result = {
'monthly_trends': monthly_trends,
'trend_analysis': trend_analysis,
'time_range': {
'start': review_time_pairs[0][1].strftime("%Y-%m-%d"),
'end': review_time_pairs[-1][1].strftime("%Y-%m-%d"),
'total_months': len(months)
},
'insights': self._generate_trend_insights(monthly_trends, trend_analysis)
}
self._save_to_cache(cache_key, result, "trends")
return result
def _analyze_sentiment_trend(self, months: List[str], monthly_data: Dict) -> Dict:
"""Analyze sentiment trends"""
positive_rates = [monthly_data[month]['positive_rate'] for month in months]
if len(positive_rates) < 3:
return {"error": "Insufficient data"}
# Simple trend calculation
recent_avg = np.mean(positive_rates[-3:]) # Average of last 3 months
earlier_avg = np.mean(positive_rates[:-3]) if len(positive_rates) > 3 else positive_rates[0]
trend_direction = 'improving' if recent_avg > earlier_avg + 5 else 'declining' if recent_avg < earlier_avg - 5 else 'stable'
trend_strength = abs(recent_avg - earlier_avg)
return {
'direction': trend_direction,
'strength': round(trend_strength, 1),
'recent_average': round(recent_avg, 1),
'earlier_average': round(earlier_avg, 1)
}
def _generate_trend_insights(self, monthly_trends: Dict, trend_analysis: Dict) -> List[str]:
"""Generate trend insights"""
insights = []
if 'error' in trend_analysis:
insights.append("📊 Insufficient data for trend analysis, recommend collecting more historical data")
return insights
direction = trend_analysis.get('direction', 'unknown')
strength = trend_analysis.get('strength', 0)
if direction == 'improving':
insights.append(f"📈 Sentiment trend improving, recent satisfaction increased by {strength:.1f} percentage points")
elif direction == 'declining':
insights.append(f"📉 Sentiment trend declining, recent satisfaction decreased by {strength:.1f} percentage points")
else:
insights.append("➡️ Sentiment trend relatively stable, no significant changes observed")
# Analyze review volume trends
review_counts = [data['review_count'] for data in monthly_trends.values()]
if len(review_counts) >= 3:
recent_volume = np.mean(review_counts[-2:])
earlier_volume = np.mean(review_counts[:-2])
if recent_volume > earlier_volume * 1.5:
insights.append("🔥 Review volume significantly increased, product attention rising")
elif recent_volume < earlier_volume * 0.5:
insights.append("📉 Review volume decreased, need to monitor product popularity")
return insights
# Global analyzer instance
analyzer = None
def get_analyzer():
"""Get analyzer instance (lazy initialization)"""
global analyzer
if analyzer is None:
analyzer = AdvancedReviewAnalyzer()
return analyzer
def process_file_upload(file) -> Tuple[List[str], Dict]:
"""Process file upload"""
if file is None:
return [], {}
try:
if file.name.endswith('.csv'):
df = pd.read_csv(file.name)
elif file.name.endswith(('.xlsx', '.xls')):
df = pd.read_excel(file.name)
else:
return [], {"error": "Unsupported file format, please upload CSV or Excel files"}
# Auto-detect column names
review_col = None
time_col = None
user_col = None
rating_col = None
for col in df.columns:
col_lower = col.lower().strip()
if any(keyword in col_lower for keyword in ['review', 'comment', 'text', 'content']):
review_col = col
elif any(keyword in col_lower for keyword in ['time', 'date', 'created', 'timestamp']):
time_col = col
elif any(keyword in col_lower for keyword in ['user', 'name', 'author', 'customer']):
user_col = col
elif any(keyword in col_lower for keyword in ['rating', 'score', 'star', 'stars']):
rating_col = col
if review_col is None:
return [], {"error": "Review content column not found, please ensure file contains review text"}
# Extract data
reviews = df[review_col].dropna().astype(str).tolist()
metadata = {}
if time_col and time_col in df.columns:
metadata['timestamps'] = df[time_col].dropna().astype(str).tolist()
if user_col and user_col in df.columns:
metadata['usernames'] = df[user_col].dropna().astype(str).tolist()
if rating_col and rating_col in df.columns:
metadata['ratings'] = df[rating_col].dropna().tolist()
metadata['total_rows'] = len(df)
metadata['valid_reviews'] = len(reviews)
return reviews, metadata
except Exception as e:
logger.error(f"File processing error: {str(e)}")
return [], {"error": f"File processing failed: {str(e)}"}
# Gradio interface functions
def sentiment_analysis_interface(reviews_text: str, file_upload, language: str):
"""Sentiment analysis interface"""
try:
analyzer = get_analyzer()
reviews = []
if file_upload is not None:
reviews, metadata = process_file_upload(file_upload)
if 'error' in metadata:
return metadata['error'], None, None
else:
reviews = [line.strip() for line in reviews_text.split('\n') if line.strip() and len(line.strip()) > 10]
if not reviews:
return "Please enter review text or upload a file", None, None
if len(reviews) > 1000:
reviews = reviews[:1000] # Limit processing count
result = analyzer.analyze_sentiment_advanced(reviews, language)
if 'error' in result:
return result['error'], None, None
# Create charts
lazy_import()
fig1 = plotly.go.Figure(data=[
plotly.go.Pie(
labels=list(result['summary'].keys()),
values=list(result['summary'].values()),
hole=0.3
)
])
fig1.update_layout(title="Sentiment Distribution")
# Confidence distribution
confidences = [item['confidence'] for item in result['details']]
fig2 = plotly.go.Figure(data=[
plotly.go.Histogram(x=confidences, nbinsx=20)
])
fig2.update_layout(title="Confidence Distribution", xaxis_title="Confidence", yaxis_title="Frequency")
return json.dumps(result, indent=2, ensure_ascii=False), fig1, fig2
except Exception as e:
logger.error(f"Sentiment analysis error: {str(e)}")
return f"Analysis error: {str(e)}", None, None
def emotion_analysis_interface(reviews_text: str, file_upload):
"""Emotion analysis interface"""
try:
analyzer = get_analyzer()
reviews = []
if file_upload is not None:
reviews, metadata = process_file_upload(file_upload)
if 'error' in metadata:
return metadata['error'], None
else:
reviews = [line.strip() for line in reviews_text.split('\n') if line.strip() and len(line.strip()) > 10]
if not reviews:
return "Please enter review text or upload a file", None
if len(reviews) > 500:
reviews = reviews[:500]
result = analyzer.analyze_emotions(reviews)
if 'error' in result:
return result['error'], None
# Create emotion distribution chart
lazy_import()
fig = plotly.go.Figure(data=[
plotly.go.Bar(
x=list(result['summary'].keys()),
y=list(result['summary'].values()),
text=[analyzer.emotion_emojis.get(emotion, '😐') for emotion in result['summary'].keys()],
textposition='auto'
)
])
fig.update_layout(title="Emotion Distribution", xaxis_title="Emotion Type", yaxis_title="Percentage")
return json.dumps(result, indent=2, ensure_ascii=False), fig
except Exception as e:
logger.error(f"Emotion analysis error: {str(e)}")
return f"Analysis error: {str(e)}", None
def aspect_analysis_interface(reviews_text: str, file_upload):
"""Aspect analysis interface"""
try:
analyzer = get_analyzer()
reviews = []
if file_upload is not None:
reviews, metadata = process_file_upload(file_upload)
if 'error' in metadata:
return metadata['error'], None
else:
reviews = [line.strip() for line in reviews_text.split('\n') if line.strip() and len(line.strip()) > 10]
if not reviews:
return "Please enter review text or upload a file", None
if len(reviews) > 800:
reviews = reviews[:800]
result = analyzer.analyze_aspects_advanced(reviews)
if 'error' in result:
return result['error'], None
# Create aspect sentiment chart
lazy_import()
if result['aspect_scores']:
aspects = list(result['aspect_scores'].keys())
scores = [result['aspect_scores'][aspect]['sentiment_score'] for aspect in aspects]
fig = plotly.go.Figure(data=[
plotly.go.Bar(
x=aspects,
y=scores,
marker_color=['green' if score > 0 else 'red' for score in scores]
)
])
fig.update_layout(
title="Product Aspect Sentiment Scores",
xaxis_title="Product Aspects",
yaxis_title="Sentiment Score (-1 to 1)",
xaxis_tickangle=-45
)
else:
fig = None
return json.dumps(result, indent=2, ensure_ascii=False), fig
except Exception as e:
logger.error(f"Aspect analysis error: {str(e)}")
return f"Analysis error: {str(e)}", None
def fake_detection_interface(reviews_text: str, file_upload):
"""Fake detection interface"""
try:
analyzer = get_analyzer()
reviews = []
metadata = {}
if file_upload is not None:
reviews, metadata = process_file_upload(file_upload)
if 'error' in metadata:
return metadata['error'], None
else:
reviews = [line.strip() for line in reviews_text.split('\n') if line.strip() and len(line.strip()) > 10]
if not reviews:
return "Please enter review text or upload a file", None
if len(reviews) > 1000:
reviews = reviews[:1000]
result = analyzer.detect_fake_reviews_advanced(reviews, metadata if metadata else None)
if 'error' in result:
return result['error'], None
# Create risk distribution chart
lazy_import()
risk_scores = [item['risk_score'] for item in result['individual_analysis']]
fig = plotly.go.Figure(data=[
plotly.go.Histogram(
x=risk_scores,
nbinsx=20,
marker_color='red',
opacity=0.7
)
])
fig.update_layout(
title="Fake Risk Distribution",
xaxis_title="Risk Score",
yaxis_title="Number of Reviews"
)
return json.dumps(result, indent=2, ensure_ascii=False), fig
except Exception as e:
logger.error(f"Fake detection error: {str(e)}")
return f"Analysis error: {str(e)}", None
def quality_assessment_interface(reviews_text: str, file_upload, length_weight, detail_weight,
structure_weight, help_weight, objectivity_weight, readability_weight):
"""Quality assessment interface"""
try:
analyzer = get_analyzer()
reviews = []
if file_upload is not None:
reviews, metadata = process_file_upload(file_upload)
if 'error' in metadata:
return metadata['error'], None, None
else:
reviews = [line.strip() for line in reviews_text.split('\n') if line.strip() and len(line.strip()) > 10]
if not reviews:
return "Please enter review text or upload a file", None, None
if len(reviews) > 800:
reviews = reviews[:800]
# Normalize weights
total_weight = length_weight + detail_weight + structure_weight + help_weight + objectivity_weight + readability_weight
if total_weight == 0:
total_weight = 1
custom_weights = {
'length_depth': length_weight / total_weight,
'specificity': detail_weight / total_weight,
'structure': structure_weight / total_weight,
'helpfulness': help_weight / total_weight,
'objectivity': objectivity_weight / total_weight,
'readability': readability_weight / total_weight
}
result, chart_data = analyzer.assess_review_quality_comprehensive(reviews, custom_weights)
if 'error' in result:
return result['error'], None, None
# Create radar chart and grade distribution chart
lazy_import()
# Radar chart
factors = list(result['factor_averages'].keys())
values = list(result['factor_averages'].values())
fig1 = plotly.go.Figure()
fig1.add_trace(plotly.go.Scatterpolar(
r=values,
theta=factors,
fill='toself',
name='Quality Factors'
))
fig1.update_layout(
polar=dict(radialaxis=dict(visible=True, range=[0, 1])),
showlegend=True,
title="Quality Factors Radar Chart"
)
# Grade distribution chart
if result['summary']['grade_distribution']:
grades = list(result['summary']['grade_distribution'].keys())
grade_counts = list(result['summary']['grade_distribution'].values())
fig2 = plotly.go.Figure(data=[
plotly.go.Bar(x=grades, y=grade_counts, marker_color='skyblue')
])
fig2.update_layout(title="Quality Grade Distribution", xaxis_title="Grade", yaxis_title="Percentage")
else:
fig2 = None
return json.dumps(result, indent=2, ensure_ascii=False), fig1, fig2
except Exception as e:
logger.error(f"Quality assessment error: {str(e)}")
return f"Analysis error: {str(e)}", None, None
def recommendation_intent_interface(reviews_text: str, file_upload):
"""Recommendation intent analysis interface"""
try:
analyzer = get_analyzer()
reviews = []
if file_upload is not None:
reviews, metadata = process_file_upload(file_upload)
if 'error' in metadata:
return metadata['error'], None
else:
reviews = [line.strip() for line in reviews_text.split('\n') if line.strip() and len(line.strip()) > 10]
if not reviews:
return "Please enter review text or upload a file", None
if len(reviews) > 800:
reviews = reviews[:800]
result = analyzer.predict_recommendation_intent(reviews)
if 'error' in result:
return result['error'], None
# Create recommendation intent distribution chart
lazy_import()
distribution = result['summary']['distribution']
fig = plotly.go.Figure(data=[
plotly.go.Pie(
labels=list(distribution.keys()),
values=list(distribution.values()),
hole=0.3
)
])
fig.update_layout(title=f"Recommendation Intent Distribution (Recommendation Rate: {result['summary']['recommendation_rate']}%)")
return json.dumps(result, indent=2, ensure_ascii=False), fig
except Exception as e:
logger.error(f"Recommendation intent error: {str(e)}")
return f"Analysis error: {str(e)}", None
def trend_analysis_interface(reviews_text: str, file_upload):
"""Trend analysis interface"""
try:
analyzer = get_analyzer()
reviews = []
timestamps = []
if file_upload is not None:
reviews, metadata = process_file_upload(file_upload)
if 'error' in metadata:
return metadata['error'], None
timestamps = metadata.get('timestamps', [])
else:
return "Trend analysis requires uploading a file with timestamps", None
if not reviews or not timestamps:
return "Need both review text and timestamp data", None
result = analyzer.analyze_review_trends(reviews, timestamps)
if 'error' in result:
return result['error'], None
# Create trend chart
lazy_import()
monthly_data = result['monthly_trends']
if monthly_data:
months = sorted(monthly_data.keys())
positive_rates = [monthly_data[month]['positive_rate'] for month in months]
review_counts = [monthly_data[month]['review_count'] for month in months]
fig = plotly.make_subplots(
rows=2, cols=1,
subplot_titles=('Sentiment Trend', 'Review Volume Trend'),
specs=[[{"secondary_y": False}], [{"secondary_y": False}]]
)
# Sentiment trend
fig.add_trace(
plotly.go.Scatter(x=months, y=positive_rates, mode='lines+markers', name='Positive Sentiment Rate'),
row=1, col=1
)
# Review volume trend
fig.add_trace(
plotly.go.Bar(x=months, y=review_counts, name='Review Count'),
row=2, col=1
)
fig.update_layout(title="Review Trend Analysis", height=600)
else:
fig = None
return json.dumps(result, indent=2, ensure_ascii=False), fig
except Exception as e:
logger.error(f"Trend analysis error: {str(e)}")
return f"Analysis error: {str(e)}", None
def competitive_analysis_interface(product_a_text: str, product_b_text: str, file_a, file_b):
"""Competitive analysis interface"""
try:
analyzer = get_analyzer()
# Process Product A data
if file_a is not None:
reviews_a, metadata_a = process_file_upload(file_a)
if 'error' in metadata_a:
return metadata_a['error'], None
else:
reviews_a = [line.strip() for line in product_a_text.split('\n') if line.strip() and len(line.strip()) > 10]
# Process Product B data
if file_b is not None:
reviews_b, metadata_b = process_file_upload(file_b)
if 'error' in metadata_b:
return metadata_b['error'], None
else:
reviews_b = [line.strip() for line in product_b_text.split('\n') if line.strip() and len(line.strip()) > 10]
if not reviews_a or not reviews_b:
return "Both products need review data", None
# Limit data volume
if len(reviews_a) > 500:
reviews_a = reviews_a[:500]
if len(reviews_b) > 500:
reviews_b = reviews_b[:500]
# Analyze both products
result_a = analyzer.analyze_sentiment_advanced(reviews_a)
result_b = analyzer.analyze_sentiment_advanced(reviews_b)
if 'error' in result_a or 'error' in result_b:
return "Analysis error, please check data", None
# Comparison analysis
comparison = {
'product_a': {
'summary': result_a['summary'],
'total_reviews': result_a['total_reviews'],
'average_confidence': result_a['average_confidence']
},
'product_b': {
'summary': result_b['summary'],
'total_reviews': result_b['total_reviews'],
'average_confidence': result_b['average_confidence']
},
'winner': {
'by_positive_rate': 'Product A' if result_a['summary']['positive'] > result_b['summary']['positive'] else 'Product B',
'by_confidence': 'Product A' if result_a['average_confidence'] > result_b['average_confidence'] else 'Product B'
},
'insights': [
f"Product A positive sentiment rate: {result_a['summary']['positive']}%",
f"Product B positive sentiment rate: {result_b['summary']['positive']}%",
f"Sentiment analysis confidence: A({result_a['average_confidence']:.2f}) vs B({result_b['average_confidence']:.2f})"
]
}
# Create comparison chart
lazy_import()
fig = plotly.make_subplots(
rows=1, cols=2,
specs=[[{'type': 'pie'}, {'type': 'pie'}]],
subplot_titles=['Product A', 'Product B']
)
fig.add_trace(plotly.go.Pie(
labels=list(result_a['summary'].keys()),
values=list(result_a['summary'].values()),
name="Product A"
), row=1, col=1)
fig.add_trace(plotly.go.Pie(
labels=list(result_b['summary'].keys()),
values=list(result_b['summary'].values()),
name="Product B"
), row=1, col=2)
fig.update_layout(title="Competitive Sentiment Analysis")
return json.dumps(comparison, indent=2, ensure_ascii=False), fig
except Exception as e:
logger.error(f"Competitive analysis error: {str(e)}")
return f"Analysis error: {str(e)}", None
def generate_professional_report(analysis_result: str, report_type: str, company_name: str, product_name: str):
"""Generate professional report"""
try:
if not analysis_result.strip():
return "No analysis data available, please run analysis first"
data = json.loads(analysis_result)
timestamp = datetime.now().strftime("%B %d, %Y at %H:%M")
if report_type == "sentiment":
report = f"""# 📊 Sentiment Analysis Professional Report
**Report Generated**: {timestamp}
**Company Name**: {company_name or 'Not Specified'}
**Product Name**: {product_name or 'Not Specified'}
## 📈 Executive Summary
This report provides a comprehensive sentiment analysis based on {data.get('total_reviews', 0)} customer reviews. Analysis results show:
- **Positive Sentiment**: {data.get('summary', {}).get('positive', 0)}%
- **Negative Sentiment**: {data.get('summary', {}).get('negative', 0)}%
- **Neutral Sentiment**: {data.get('summary', {}).get('neutral', 0)}%
- **Average Confidence**: {data.get('average_confidence', 0):.2f}
## 🎯 Key Findings
{chr(10).join(['• ' + insight for insight in data.get('insights', [])])}
## 📊 Detailed Analysis
### Sentiment Distribution Analysis
Based on AI model analysis, customer sentiment breakdown:
- Positive feedback accounts for {data.get('summary', {}).get('positive', 0)}%, indicating overall product/service performance
- Negative feedback accounts for {data.get('summary', {}).get('negative', 0)}%, requiring focused improvement attention
- Neutral reviews account for {data.get('summary', {}).get('neutral', 0)}%
### Confidence Analysis
Model prediction average confidence is {data.get('average_confidence', 0):.2f},
{'indicating high confidence with reliable analysis results' if data.get('average_confidence', 0) > 0.7 else 'indicating medium confidence, recommend combining with manual review'}.
## 💡 Recommendations & Action Plan
1. **Short-term Actions** (1-3 months)
- Develop improvement plans for major negative feedback
- Strengthen customer service training
- Establish customer feedback tracking mechanisms
2. **Medium-term Strategy** (3-6 months)
- Product/service optimization
- Competitive benchmarking analysis
- Customer satisfaction improvement plans
3. **Long-term Planning** (6-12 months)
- Brand image enhancement
- Customer loyalty programs
- Continuous monitoring and improvement systems
## 📋 Methodology
This analysis employs advanced natural language processing technologies, including:
- RoBERTa pre-trained models for sentiment classification
- Multi-dimensional text feature extraction
- Confidence assessment mechanisms
- Lexicon-enhanced analysis
---
*This report was automatically generated by SmartReview Pro. Recommend combining with business expert opinions for decision-making.*
"""
elif report_type == "fake_detection":
authenticity_rate = data.get('summary', {}).get('authenticity_rate', 0)
report = f"""# 🔍 Fake Review Detection Professional Report
**Report Generated**: {timestamp}
**Company Name**: {company_name or 'Not Specified'}
**Product Name**: {product_name or 'Not Specified'}
## 📈 Detection Summary
This report analyzed {data.get('summary', {}).get('total_reviews', 0)} reviews for fake detection:
- **Authenticity Rate**: {data.get('summary', {}).get('authenticity_rate', 0)}%
- **Suspicious Reviews**: {data.get('summary', {}).get('suspicious_reviews', 0)}
- **Risk Level**: {data.get('summary', {}).get('risk_level', 'Unknown')}
## ⚠️ Risk Assessment
{'🚨 **High Risk Warning**: Large number of suspicious reviews detected, immediate action recommended' if authenticity_rate < 60 else
'⚠️ **Medium Risk Alert**: Some suspicious reviews exist, attention needed' if authenticity_rate < 80 else
'✅ **Low Risk**: Review authenticity is high, generally trustworthy'}
## 🔎 Detection Details
### Common Fake Indicators
{chr(10).join(['• ' + rec for rec in data.get('recommendations', [])])}
### Pattern Analysis Results
{f"Detected {data.get('pattern_analysis', {}).get('pattern_count', 0)} suspicious patterns" if 'pattern_analysis' in data else 'No pattern analysis performed'}
## 💡 Improvement Recommendations
1. **Immediate Actions**
- Review high-risk flagged reviews
- Strengthen review posting verification mechanisms
- Establish blacklist systems
2. **System Optimization**
- Implement real-time monitoring systems
- Raise review standards for new users
- Build review quality scoring mechanisms
3. **Long-term Protection**
- Conduct regular fake review detection
- Train customer service teams on identification capabilities
- Establish user reputation systems
---
*Detection based on multi-dimensional text analysis and behavioral pattern recognition technologies*
"""
elif report_type == "quality":
avg_quality = data.get('summary', {}).get('average_quality', 0)
report = f"""# ⭐ Review Quality Assessment Professional Report
**Report Generated**: {timestamp}
**Company Name**: {company_name or 'Not Specified'}
**Product Name**: {product_name or 'Not Specified'}
## 📊 Quality Overview
This report assessed the quality of {data.get('summary', {}).get('total_reviews', 0)} customer reviews:
- **Average Quality Score**: {avg_quality:.2f}/1.0
- **Quality Rating**: {'Excellent' if avg_quality > 0.8 else 'Good' if avg_quality > 0.6 else 'Average' if avg_quality > 0.4 else 'Poor'}
- **High Quality Reviews**: {data.get('summary', {}).get('high_quality_count', 0)}
## 🎯 Quality Dimension Analysis
### Dimension Scores
{chr(10).join([f'• {k}: {v:.2f}' for k, v in data.get('factor_averages', {}).items()])}
### Grade Distribution
{chr(10).join([f'• Grade {grade}: {pct}%' for grade, pct in data.get('summary', {}).get('grade_distribution', {}).items()])}
## 💎 Key Insights
{chr(10).join(['• ' + insight for insight in data.get('insights', [])])}
## 🚀 Quality Improvement Recommendations
1. **Encourage Detailed Feedback**
- Design guided questions
- Provide review reward mechanisms
- Showcase quality review examples
2. **Optimize User Experience**
- Simplify review posting process
- Provide review template guidance
- Respond and interact promptly
3. **Continuous Quality Monitoring**
- Regular review quality assessment
- Analyze quality trend changes
- Adjust review strategies
---
*Assessment based on multi-dimensional quality evaluation model, weights adjustable according to business needs*
"""
else:
report = f"""# 📋 Comprehensive Analysis Report
**Report Generated**: {timestamp}
**Company Name**: {company_name or 'Not Specified'}
**Product Name**: {product_name or 'Not Specified'}
## Analysis Results
{json.dumps(data, indent=2, ensure_ascii=False)}
---
*Report generated by SmartReview Pro*
"""
return report
except Exception as e:
logger.error(f"Report generation error: {str(e)}")
return f"Report generation failed: {str(e)}"
# Create Gradio interface
def create_gradio_interface():
"""Create Gradio interface"""
theme = gr.themes.Soft(
primary_hue="blue",
secondary_hue="sky",
neutral_hue="slate",
)
with gr.Blocks(title="SmartReview Pro - Comprehensive Review Analysis Platform", theme=theme) as demo:
gr.HTML("""
<div style="text-align: center; padding: 20px;">
<h1>🛒 SmartReview Pro</h1>
<h3>AI-Powered Comprehensive E-commerce Review Analysis Platform</h3>
<p>Integrated sentiment analysis, fake detection, quality assessment, trend analysis and more</p>
</div>
""")
with gr.Tab("📊 Sentiment Analysis"):
gr.Markdown("### Advanced Sentiment Analysis - Multi-language support with confidence assessment")
with gr.Row():
with gr.Column():
sentiment_text = gr.Textbox(
lines=8,
placeholder="Enter review text (one per line) or upload file...",
label="Review Text"
)
sentiment_file = gr.File(
label="Upload CSV/Excel File",
file_types=[".csv", ".xlsx", ".xls"]
)
sentiment_lang = gr.Dropdown(
choices=[("English", "en"), ("Chinese", "zh")],
value="en",
label="Language Selection"
)
sentiment_btn = gr.Button("Start Analysis", variant="primary", size="lg")
with gr.Column():
sentiment_result = gr.Textbox(label="Analysis Results", lines=12)
with gr.Row():
sentiment_chart1 = gr.Plot(label="Sentiment Distribution")
sentiment_chart2 = gr.Plot(label="Confidence Distribution")
sentiment_btn.click(
sentiment_analysis_interface,
inputs=[sentiment_text, sentiment_file, sentiment_lang],
outputs=[sentiment_result, sentiment_chart1, sentiment_chart2]
)
with gr.Tab("� Emotion Analysis"):
gr.Markdown("### Fine-grained Emotion Analysis - Identify joy, sadness, anger and other emotions")
with gr.Row():
with gr.Column():
emotion_text = gr.Textbox(
lines=8,
placeholder="Enter review text...",
label="Review Text"
)
emotion_file = gr.File(
label="Upload File",
file_types=[".csv", ".xlsx", ".xls"]
)
emotion_btn = gr.Button("Analyze Emotions", variant="primary")
with gr.Column():
emotion_result = gr.Textbox(label="Emotion Analysis Results", lines=12)
emotion_chart = gr.Plot(label="Emotion Distribution Chart")
emotion_btn.click(
emotion_analysis_interface,
inputs=[emotion_text, emotion_file],
outputs=[emotion_result, emotion_chart]
)
with gr.Tab("🎯 Aspect Analysis"):
gr.Markdown("### Aspect-Based Sentiment Analysis (ABSA) - Analyze sentiment for different product aspects")
with gr.Row():
with gr.Column():
aspect_text = gr.Textbox(
lines=8,
placeholder="Enter review text...",
label="Review Text"
)
aspect_file = gr.File(
label="Upload File",
file_types=[".csv", ".xlsx", ".xls"]
)
aspect_btn = gr.Button("Analyze Aspects", variant="primary")
with gr.Column():
aspect_result = gr.Textbox(label="Aspect Analysis Results", lines=12)
aspect_chart = gr.Plot(label="Aspect Sentiment Chart")
aspect_btn.click(
aspect_analysis_interface,
inputs=[aspect_text, aspect_file],
outputs=[aspect_result, aspect_chart]
)
with gr.Tab("🔍 Fake Detection"):
gr.Markdown("### Advanced Fake Review Detection - Based on text analysis and behavioral patterns")
with gr.Row():
with gr.Column():
fake_text = gr.Textbox(
lines=8,
placeholder="Enter reviews to be detected...",
label="Review Text"
)
fake_file = gr.File(
label="Upload File (supports metadata analysis like usernames, timestamps)",
file_types=[".csv", ".xlsx", ".xls"]
)
fake_btn = gr.Button("Detect Fake Reviews", variant="primary")
with gr.Column():
fake_result = gr.Textbox(label="Detection Results", lines=12)
fake_chart = gr.Plot(label="Risk Distribution")
fake_btn.click(
fake_detection_interface,
inputs=[fake_text, fake_file],
outputs=[fake_result, fake_chart]
)
with gr.Tab("⭐ Quality Assessment"):
gr.Markdown("### Comprehensive Review Quality Assessment - Multi-dimensional quality analysis")
with gr.Row():
with gr.Column():
quality_text = gr.Textbox(
lines=8,
placeholder="Enter review text...",
label="Review Text"
)
quality_file = gr.File(
label="Upload File",
file_types=[".csv", ".xlsx", ".xls"]
)
gr.Markdown("**Custom Weight Settings**")
with gr.Row():
length_w = gr.Slider(0, 1, 0.2, label="Length & Depth")
detail_w = gr.Slider(0, 1, 0.2, label="Specificity")
structure_w = gr.Slider(0, 1, 0.15, label="Structure")
with gr.Row():
help_w = gr.Slider(0, 1, 0.15, label="Helpfulness")
obj_w = gr.Slider(0, 1, 0.15, label="Objectivity")
read_w = gr.Slider(0, 1, 0.15, label="Readability")
quality_btn = gr.Button("Assess Quality", variant="primary")
with gr.Column():
quality_result = gr.Textbox(label="Quality Assessment Results", lines=12)
with gr.Row():
quality_radar = gr.Plot(label="Quality Factors Radar Chart")
quality_grade = gr.Plot(label="Grade Distribution")
quality_btn.click(
quality_assessment_interface,
inputs=[quality_text, quality_file, length_w, detail_w, structure_w, help_w, obj_w, read_w],
outputs=[quality_result, quality_radar, quality_grade]
)
with gr.Tab("💡 Recommendation Intent"):
gr.Markdown("### Recommendation Intent Prediction - Analyze customer tendency to recommend products")
with gr.Row():
with gr.Column():
rec_text = gr.Textbox(
lines=8,
placeholder="Enter review text...",
label="Review Text"
)
rec_file = gr.File(
label="Upload File",
file_types=[".csv", ".xlsx", ".xls"]
)
rec_btn = gr.Button("Analyze Recommendation Intent", variant="primary")
with gr.Column():
rec_result = gr.Textbox(label="Recommendation Intent Analysis", lines=12)
rec_chart = gr.Plot(label="Recommendation Intent Distribution")
rec_btn.click(
recommendation_intent_interface,
inputs=[rec_text, rec_file],
outputs=[rec_result, rec_chart]
)
with gr.Tab("📈 Trend Analysis"):
gr.Markdown("### Time Trend Analysis - Analyze how review sentiment changes over time")
with gr.Row():
with gr.Column():
gr.Markdown("**Note**: Trend analysis requires uploading CSV/Excel file with timestamps")
trend_file = gr.File(
label="Upload File with Timestamps (Required columns: review text, timestamp)",
file_types=[".csv", ".xlsx", ".xls"]
)
trend_btn = gr.Button("Analyze Trends", variant="primary")
with gr.Column():
trend_result = gr.Textbox(label="Trend Analysis Results", lines=12)
trend_chart = gr.Plot(label="Trend Charts")
trend_btn.click(
trend_analysis_interface,
inputs=[gr.Textbox(visible=False), trend_file],
outputs=[trend_result, trend_chart]
)
with gr.Tab("🆚 Competitive Analysis"):
gr.Markdown("### Competitive Sentiment Analysis - Compare customer feedback between two products")
with gr.Row():
with gr.Column():
gr.Markdown("**Product A**")
comp_text_a = gr.Textbox(
lines=6,
placeholder="Product A reviews...",
label="Product A Reviews"
)
comp_file_a = gr.File(
label="Upload Product A File",
file_types=[".csv", ".xlsx", ".xls"]
)
with gr.Column():
gr.Markdown("**Product B**")
comp_text_b = gr.Textbox(
lines=6,
placeholder="Product B reviews...",
label="Product B Reviews"
)
comp_file_b = gr.File(
label="Upload Product B File",
file_types=[".csv", ".xlsx", ".xls"]
)
comp_btn = gr.Button("Start Competitive Analysis", variant="primary", size="lg")
with gr.Row():
comp_result = gr.Textbox(label="Comparison Analysis Results", lines=12)
comp_chart = gr.Plot(label="Comparison Charts")
comp_btn.click(
competitive_analysis_interface,
inputs=[comp_text_a, comp_text_b, comp_file_a, comp_file_b],
outputs=[comp_result, comp_chart]
)
with gr.Tab("📋 Professional Reports"):
gr.Markdown("### Generate Professional Analysis Reports - Create exportable detailed reports")
with gr.Row():
with gr.Column():
report_data = gr.Textbox(
lines=10,
placeholder="Paste JSON results from any analysis above here...",
label="Analysis Data (JSON format)"
)
with gr.Row():
report_type = gr.Dropdown(
choices=[
("Sentiment Analysis Report", "sentiment"),
("Fake Detection Report", "fake_detection"),
("Quality Assessment Report", "quality"),
("Comprehensive Report", "comprehensive")
],
value="sentiment",
label="Report Type"
)
with gr.Row():
company_name = gr.Textbox(
placeholder="Your company name (optional)",
label="Company Name"
)
product_name = gr.Textbox(
placeholder="Product name (optional)",
label="Product Name"
)
report_btn = gr.Button("Generate Professional Report", variant="primary")
with gr.Column():
report_output = gr.Textbox(
label="Generated Professional Report",
lines=20,
show_copy_button=True
)
report_btn.click(
generate_professional_report,
inputs=[report_data, report_type, company_name, product_name],
outputs=[report_output]
)
with gr.Tab("�📖 User Guide"):
gr.Markdown("""
## 🚀 SmartReview Pro User Guide
### 📊 Feature Overview
**SmartReview Pro** is an integrated AI-powered e-commerce review analysis platform providing the following core features:
1. **Sentiment Analysis** - Identify positive, negative, neutral sentiment in reviews
2. **Emotion Analysis** - Fine-grained emotion recognition (joy, sadness, anger, etc.)
3. **Aspect Analysis** - Analyze sentiment for different product aspects (price, quality, service, etc.)
4. **Fake Detection** - Identify potential fake reviews and spam behavior
5. **Quality Assessment** - Multi-dimensional evaluation of review content quality
6. **Recommendation Intent** - Predict customer tendency to recommend products
7. **Trend Analysis** - Analyze how review sentiment changes over time
8. **Competitive Analysis** - Compare customer feedback between different products
9. **Professional Reports** - Generate detailed analysis reports for business use
### 📁 Data Input Methods
**Text Input**: Copy and paste review text directly (one review per line)
**File Upload**: Support CSV and Excel files with the following column names:
- Review text: `review`, `comment`, `text`, `content`
- Timestamp: `time`, `date`, `created`, `timestamp`
- Username: `user`, `name`, `author`, `customer`
- Rating: `rating`, `score`, `star`, `stars`
### 🎯 Usage Tips
1. **Data Quality**: Ensure reviews are complete and readable
2. **Volume Limits**: Each analysis supports up to 1000 reviews for optimal performance
3. **File Format**: Use UTF-8 encoding for better multilingual support
4. **Result Interpretation**: Combine AI analysis with business expertise for decision-making
5. **Regular Monitoring**: Establish periodic analysis for trend tracking
### 🔧 Technical Features
- **AI Models**: Uses advanced transformer models (RoBERTa, DistilBERT)
- **Multi-language**: Supports English and Chinese
- **Real-time Processing**: Optimized for fast analysis
- **Caching System**: Reduces repeated analysis time
- **Visualization**: Interactive charts and graphs
### 📞 Support
For technical issues or feature requests, please contact our support team.
""")
with gr.Tab("ℹ️ About"):
gr.Markdown("""
## 🛒 SmartReview Pro
**Version**: 2.0.0
**Powered by**: Advanced Natural Language Processing & Machine Learning
### 🎯 Mission
To provide businesses with comprehensive, intelligent review analysis tools that transform customer feedback into actionable business insights.
### 🔬 Technology Stack
- **NLP Models**: RoBERTa, DistilBERT, Custom Fine-tuned Models
- **Framework**: Transformers, PyTorch, Gradio
- **Visualization**: Plotly, Interactive Charts
- **Database**: SQLite for caching and analytics
- **Languages**: Python, Advanced AI/ML Libraries
### 🏆 Key Advantages
- **Comprehensive Analysis**: 8+ analysis dimensions
- **High Accuracy**: State-of-the-art AI models
- **Fast Processing**: Optimized for large-scale data
- **Easy to Use**: Intuitive web interface
- **Professional Reports**: Business-ready outputs
- **Multilingual Support**: English and Chinese
### 📊 Use Cases
- **E-commerce Platforms**: Product feedback analysis
- **Brand Management**: Reputation monitoring
- **Market Research**: Consumer sentiment tracking
- **Quality Control**: Review authenticity verification
- **Competitive Intelligence**: Market comparison analysis
### 🔐 Privacy & Security
- No data storage beyond session
- Local processing when possible
- Secure file handling
- GDPR compliant processing
### 📈 Performance Metrics
- **Processing Speed**: Up to 1000 reviews/minute
- **Accuracy**: 90%+ sentiment classification
- **Fake Detection**: 85%+ precision
- **Supported Formats**: CSV, Excel, Text
---
**© 2024 SmartReview Pro. All rights reserved.**
*This platform is designed for business intelligence and research purposes. Always combine AI insights with human expertise for critical business decisions.*
""")
# Footer
gr.HTML("""
<div style="text-align: center; padding: 20px; margin-top: 40px; border-top: 1px solid #e0e0e0;">
<p style="color: #666; font-size: 14px;">
🚀 <strong>SmartReview Pro</strong> - AI-Powered Review Analysis Platform<br>
💡 Transform Customer Feedback into Business Intelligence<br>
🔬 Powered by Advanced Natural Language Processing
</p>
</div>
""")
return demo
# Initialize and launch the application
if __name__ == "__main__":
# Set up logging for production
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
# Create the interface
demo = create_gradio_interface()
# Launch configuration for Hugging Face Spaces - FIXED VERSION
demo.launch(
share=False, # Set to False for HF Spaces
server_name="0.0.0.0", # Required for HF Spaces
server_port=7860, # Default port for HF Spaces
show_api=False, # Disable API docs for cleaner interface
show_error=True, # Show errors for debugging
quiet=False, # Show startup logs
favicon_path=None, # Can add custom favicon
ssl_verify=False, # For development
max_threads=10, # Limit concurrent requests
)
|