using AllBase;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Work.Api
{
public class FromTitleToCatTaobao
{
///
/// 用标题计算分类
///
///
///
public static int Get(string title, bool IsGetMoreCatid = false)
{
title = BaseRegex.Clear(title, @"([\d]+)");
char[] k = "[]\\/ -+=.【】{};,。,.::;*()()@!!&{}".ToCharArray();
foreach (char c in k)
title = title.Replace(c.ToString(), "");
string[] keys = "批发,新款,特价,热卖,定制,定做,热卖,正品,包邮,正品,出售,新品,厂家,直销,自营,跨境,爆款,批发,品牌,一件,代发,清仓,直供,外贸".Split(',');
foreach (string c in keys)
title = title.Replace(c, "");
if (string.IsNullOrEmpty(title) || title.Length < 2)
return 0;
Dictionary cats = new Dictionary();
string apiUrl = "http://tcat.qixiaoke.com/taobao/keyword.php?keyword=";
string html = GetHtml.Utf8(apiUrl + GetHtml.UrlEncodeUtf8(title));
if (!string.IsNullOrEmpty(html))
{
string[] l = html.Split(',');
foreach (string ss in l)
{
if (string.IsNullOrEmpty(ss))
continue;
int s = int.Parse(ss);
if (cats.ContainsKey(s))
cats[s]++;
else
cats.Add(s, 1);
}
int top = 0;
int all = 0;
int catId = 0;
foreach (KeyValuePair cat in cats)
{
if (top < cat.Value)
{
top = cat.Value;
catId = cat.Key;
}
all += cat.Value;
}
return catId;
}
int Step = 2;
int half = title.Length / 2;
string key1 = title.Substring(0, half);
string key2 = title.Substring(half);
SetCats(key1, ref cats);
SetCats(key2, ref cats);
int cid = GetMaxCount(ref cats, title.Length > 8 ? 4 : 16);
if (IsGetMoreCatid && cats.Count > 5)
{
return cid;
}
else if (cats.Count > 0 && !IsGetMoreCatid)
{
return cid;
}
Step = 4;
AddCount(ref cats);
int four = title.Length / 4;
if (four < 2)//没法继续细分了
return GetMaxCount(ref cats, 4096);
for (int i = 0; i < 4; i++)
{
string key = title.Substring(i * four, four);
SetCats(key, ref cats);
}
cid = GetMaxCount(ref cats, title.Length > 16 ? 16 : 64);
if (cid > 0)
return cid;
AddCount(ref cats);
Step = 8;
int eigth = title.Length / 8;
if (eigth < 2)
return GetMaxCount(ref cats, 4096);
for (int i = 0; i < 8; i++)
{
string key = title.Substring(i * eigth, eigth);
SetCats(key, ref cats);
}
return GetMaxCount(ref cats, 4096);
}
public static string GetCategory(string title)
{
if (string.IsNullOrEmpty(title)) return string.Empty;
string name = title;
title = BaseRegex.Clear(title, @"([\d]+)");
char[] k = "[]\\/ -+=.【】{};,。,.::;*()()@!!&{}".ToCharArray();
foreach (char c in k)
title = title.Replace(c.ToString(), "");
string[] keys = "批发,新款,特价,热卖,定制,定做,热卖,正品,包邮,正品,出售,新品,厂家,直销,自营,跨境,爆款,批发,品牌,一件,代发,清仓,直供,外贸".Split(',');
foreach (string c in keys)
title = title.Replace(c, "");
if (string.IsNullOrEmpty(title) || title.Length < 2)
return string.Empty;
Dictionary cats = new Dictionary();
string apiUrl = "http://tcat.qixiaoke.com/taobao/keyword.php?keyword=";
string html = GetHtml.Utf8(apiUrl + GetHtml.UrlEncodeUtf8(title));
if (!string.IsNullOrEmpty(html))
{
string[] l = html.Split(',');
foreach (string ss in l)
{
if (string.IsNullOrEmpty(ss))
continue;
int s = int.Parse(ss);
if (cats.ContainsKey(s))
cats[s]++;
else
cats.Add(s, 1);
}
int top = 0;
int all = 0;
int catId = 0;
foreach (KeyValuePair cat in cats)
{
if (top < cat.Value)
{
top = cat.Value;
catId = cat.Key;
}
all += cat.Value;
}
return name + ":" + catId;
}
int Step = 2;
int half = title.Length / 2;
string key1 = title.Substring(0, half);
string key2 = title.Substring(half);
SetCats(key1, ref cats);
SetCats(key2, ref cats);
int cid = GetMaxCount(ref cats, title.Length > 8 ? 4 : 16);
if (cid > 0)
return name + ":" + cid;
Step = 4;
AddCount(ref cats);
int four = title.Length / 4;
if (four < 2)//没法继续细分了
return name + ":" + GetMaxCount(ref cats, 4096);
for (int i = 0; i < 4; i++)
{
string key = title.Substring(i * four, four);
SetCats(key, ref cats);
}
cid = GetMaxCount(ref cats, title.Length > 16 ? 16 : 64);
if (cid > 0)
return name + ":" + cid;
AddCount(ref cats);
Step = 8;
int eigth = title.Length / 8;
if (eigth < 2)
return name + ":" + GetMaxCount(ref cats, 4096);
for (int i = 0; i < 8; i++)
{
string key = title.Substring(i * eigth, eigth);
SetCats(key, ref cats);
}
return name + ":" + GetMaxCount(ref cats, 4096);
}
///
/// 用标题计算分类(为了保证防止跟之前冲突 特意写个方法)
///
///
///
public int GetCataryId(string title, out Dictionary DicTbCatId)
{
Dictionary DicTbCatIdTemp = new Dictionary();
title = BaseRegex.Clear(title, @"([\d]+)");
char[] k = "[]\\/ -+=.【】{};,。,.::;*()()@!!&{}".ToCharArray();
foreach (char c in k)
title = title.Replace(c.ToString(), "");
string[] keys = "批发,新款,特价,热卖,定制,定做,热卖,正品,包邮,正品,出售,新品,厂家,直销,自营,跨境,爆款,批发,品牌,一件,代发,清仓,直供,外贸".Split(',');
foreach (string c in keys)
title = title.Replace(c, "");
DicTbCatId = DicTbCatIdTemp;
if (string.IsNullOrEmpty(title) || title.Length < 2)
return 0;
Dictionary cats = new Dictionary();
string apiUrl = "http://tcat.qixiaoke.com/taobao/keyword.php?keyword=";
string html = GetHtml.Utf8(apiUrl + GetHtml.UrlEncodeUtf8(title));
if (!string.IsNullOrEmpty(html))
{
string[] l = html.Split(',');
foreach (string ss in l)
{
if (string.IsNullOrEmpty(ss))
continue;
int s = int.Parse(ss);
if (cats.ContainsKey(s))
cats[s]++;
else
cats.Add(s, 1);
}
int top = 0;
int all = 0;
int catId = 0;
foreach (KeyValuePair cat in cats)
{
if (top < cat.Value)
{
top = cat.Value;
catId = cat.Key;
}
all += cat.Value;
}
return catId;
}
int Step = 2;
int half = title.Length / 2;
string key1 = title.Substring(0, half);
string key2 = title.Substring(half);
SetCats(key1, ref cats);
SetCats(key2, ref cats);
int cid = GetMaxCount(ref cats, title.Length > 8 ? 4 : 16);
DicTbCatId = cats;
if (cats.Count > 5)
{
return cid;
}
Step = 4;
AddCount(ref cats);
DicTbCatId = cats;
int four = title.Length / 4;
if (four < 2)//没法继续细分了
return GetMaxCount(ref cats, 4096);
for (int i = 0; i < 4; i++)
{
string key = title.Substring(i * four, four);
SetCats(key, ref cats);
}
cid = GetMaxCount(ref cats, title.Length > 16 ? 16 : 64);
DicTbCatId = cats;
if (cid > 0)
return cid;
AddCount(ref cats);
Step = 8;
int eigth = title.Length / 8;
DicTbCatId = cats;
if (eigth < 2)
return GetMaxCount(ref cats, 4096);
for (int i = 0; i < 8; i++)
{
string key = title.Substring(i * eigth, eigth);
SetCats(key, ref cats);
}
DicTbCatId = cats;
return GetMaxCount(ref cats, 4096);
}
///
/// 根据关键词获取相关分类
///
///
///
public static void SetCats(string key, ref Dictionary cats)
{
string apiUrl = "http://tcat.qixiaoke.com/taobao/keyword.php?keyword=";
string html = GetHtml.Utf8(apiUrl + GetHtml.UrlEncodeUtf8(key));
if (!string.IsNullOrEmpty(html))
{
string[] l = html.Split(',');
foreach (string ss in l)
{
if (string.IsNullOrEmpty(ss))
continue;
int s = int.Parse(ss);
if (cats.ContainsKey(s))
cats[s]++;
else
cats.Add(s, 1);
}
}
}
///
/// 根据倍数判断是否要取得准确结果
///
///
///
///
public static int GetMaxCount(ref Dictionary cats, int times)
{
int top = 0;
int all = 0;
int catId = 0;
foreach (KeyValuePair cat in cats)
{
if (top < cat.Value)
{
top = cat.Value;
catId = cat.Key;
}
all += cat.Value;
}
if (all / times < top)
return catId;
return 0;
}
///
/// 权值翻倍
///
///
public static void AddCount(ref Dictionary cats)
{
try
{
foreach (KeyValuePair cat in cats)
{
cats[cat.Key] *= 2;
}
}
catch
{
}
}
}
}