analytable.go 125 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995
  1. package pretreated
  2. import (
  3. "fmt"
  4. "jy/clear"
  5. u "jy/util"
  6. qutil "qfw/util"
  7. "regexp"
  8. "strings"
  9. "unicode/utf8"
  10. "github.com/PuerkitoBio/goquery"
  11. )
  12. /**
  13. 全局变量,主要是一堆判断正则
  14. **/
  15. var (
  16. //清理品目中数字
  17. numclear = regexp.MustCompile("^[\\d一二三四五六七八九十.]+")
  18. num1 = regexp.MustCompile("(\\d)")
  19. //清理表格title中的不需要的内容
  20. tabletitleclear = regexp.MustCompile("[\\s\u3000\u2003\u00a0\\n\u001c、.,.。_/((人民币万元件个公斤户))]")
  21. tabletitleclear2 = regexp.MustCompile("[\\s\u3000\u2003\u00a0\\n\u001c、,。_??;;~\\-#\\\\()(){}【】\\[\\]<>《》{}〔〕]*")
  22. //清理表格中是key中包含的空格或数字等
  23. tablekeyclear = regexp.MustCompile("[\\s\u3000\u2003\u00a0\\n、.,.。_/]+|^[\\d一二三四五六七八九十]+[、.]*|[((【\\[].*?[))】\\]]")
  24. //清理表格td中的符号
  25. tabletdclear = regexp.MustCompile("[\\s\u3000\u2003\u00a0\\n\u001c、,。_??;;~\\-#\\\\()(){}【】\\[\\]<>《》{}〔〕¥$]*")
  26. //判断key是金额,对万元的处理
  27. moneyreg = regexp.MustCompile("(预算|费|价|额|规模|投资)")
  28. //根据表格的内容判断是不是表头,如果含有金额则不是表头
  29. MoneyReg = regexp.MustCompile("^[\\s  ::0-9.万元()()人民币¥$]+$")
  30. GSReg = regexp.MustCompile(".*公司.*")
  31. //判断分包时
  32. moneyNum = regexp.MustCompile("[元整¥万]")
  33. //对隐藏表格的判断
  34. display = regexp.MustCompile("(?i).*?display\\s?[:]\\s?none.*")
  35. //---------------
  36. //求是分包的概率
  37. //根据表格的标签对分包进行打分
  38. TableMultiPackageReg_4 = regexp.MustCompile("(标段|分包|包段|划分|子包|标包|合同段)")
  39. TableMultiPackageReg_2 = regexp.MustCompile("(概况|范围|情况|内容|详细|结果|信息)")
  40. //在判断分包打分前过虑表格key
  41. FilterKey_2 = regexp.MustCompile("招标|投标|项目")
  42. //根据表格的key进行分包打分
  43. FindKey_2 = regexp.MustCompile("([分子][包标](号)?|标[号项段包](划分)?|包件?[号段名数]|包组)")
  44. FindKey_3 = regexp.MustCompile("(标段编号|标包)")
  45. //对值进行分包判断
  46. FindVal_1 = regexp.MustCompile("[第]?([一二三四五六七八九十0-9A-Za-zⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩⅪⅫ]+)((子|合同|分|施工|监理)?(标段?|包|合同段|标包))|((子|合同|分|施工|监理)?(标|包)(段|号)?)[  \u3000\u2003\u00a0]*([一二三四五六七八九十0-9A-Za-zⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩⅪⅫ]+)")
  47. FindVal2_1 = regexp.MustCompile("([一二三四五六七八九十0-9A-Za-zⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩⅪⅫ\\-]+)|^(设计|施工|监理|验收)[分子]?[标包]?[段号]?$")
  48. //判断分包前排除
  49. excludeKey = regexp.MustCompile("(标识|数量|分包个数|标段代码|涉及包号|分包数量|包件号?|项目标号|规格|型号|招标范围|业绩|废标|标段选择要求)|(^编号$)|([^包段标]编号)") //编号|划分
  50. excludeKey2 = regexp.MustCompile("包/[0-9]{0,4}[箱纸张]")
  51. excludeKey3 = regexp.MustCompile("(分包个数|每包[0-9]*元|标线|国标|享受一包服务)")
  52. //-------------
  53. cut = u.NewCut()
  54. //清理表格标签正则
  55. ClearTagReg = regexp.MustCompile("<[^>]*?>|[\\s\\n\\r]*$")
  56. //查找表格标签正则
  57. ttagreg = regexp.MustCompile("(?s)([^\\n::。,;\\s\u3000\u2003\u00a0]{2,30})[::]?[^::。;!\\n]{0,35}[\\s\\n]*$")
  58. //判断表格是表头的概率
  59. checkval = float32(0.6)
  60. //tdval_reg = regexp.MustCompile(`([\p{Han}][\p{Han}\s、()\\(\\)]{1,9})[::]([^::\\n。]{5,60})(?:[;;,,.。\\n\\t\\s])?`)
  61. //空格替换
  62. repSpace = regexp.MustCompile("[\\s\u3000\u2003\u00a0::]+|\\\\t+")
  63. //对表格kv的处理
  64. //对不能标准化的key做批识
  65. filter_tag_zb = regexp.MustCompile("(中标|成交|投标)[\\p{Han}]{0,6}(情况|结果|信息|明细)?")
  66. //中标金额
  67. //包含以下字眼做标准化处理
  68. filter_zbje_k = regexp.MustCompile("(中标|成交|总|拦标|合同|供[应货]商|报)[\\p{Han}、]{0,6}(价|额|[大小]写|[万亿]?元).{0,4}$")
  69. //简单判断金额
  70. filter_zbje_jd = regexp.MustCompile("^[^(售|保证)]{0,4}(价|额).{0,4}$")
  71. //预算金额
  72. filter_ysje_jd = regexp.MustCompile("(预算|预控价|项目概.|项目信息)")
  73. //且排队以下字眼的key
  74. filter_zbje_kn = regexp.MustCompile("得分|打分|时间|业绩|须知|分|电话|要求|需求数量|发布规模$|第[2二3三4四5五]|地址|询价保证金|行号")
  75. //且值包含以下字眼
  76. filter_zbje_v = regexp.MustCompile("[¥$$0-9一二三四五六七八九十,,〇零点..壹贰叁肆伍陆柒捌玖拾百佰千仟万亿億元圆角分整正()::大小写]{2,16}")
  77. //中标单位的处理
  78. //包含以下字眼的Key标准化
  79. filter_zbdw_ky = regexp.MustCompile("(中标|成交|拦标|合同|选中|投标|拟|预|最终)[\\p{Han}、]{0,6}(供[应货]商|企业|单位|人|机构)(名称)?.{0,4}$")
  80. //识别中标单位相关信息
  81. filter_zbdw_info = regexp.MustCompile("(中标|成交|中选|供(货|应))[^候选]{0,}")
  82. //简单判断
  83. filter_zbdw_jd = regexp.MustCompile("(投标|成交|中标|合同)(供应商|单位|人|名称).{0,4}$")
  84. //且不包含以下字眼
  85. filter_zbdw_kn = regexp.MustCompile("第[2二3三4四5五]|得分|地址|询价保证金") //且值包含以下字眼
  86. //且值包含以下字眼
  87. filter_zbdw_v = regexp.MustCompile("(公司|集团|研究院|设计院|局|厂|部|站|城|店|市|所|处)$|([^购]中心|办公|用品)")
  88. //且值包含以下字眼
  89. filter_zbdw_v2 = regexp.MustCompile("(公司|集团|研究院|设计院|局|厂|部|站|城|店|市|所|处)$")
  90. //Tg = map[string]interface{}{}
  91. //一些表格没有表头,是空的,对值是排序的做处理对应 NullTxBid
  92. NullTdReg = regexp.MustCompile("(首选|第[一二三四五1-5])(中标|成交)?(名(称)?|(候选|排序)?(人|单位|供应商))")
  93. NullTxtBid = "成交供应商排名"
  94. projectnameReg = regexp.MustCompile("((公开)?招标)*[((第]*[一二三四五六七八九十a-zA-Z0-9]+(标段|包|标|段)[))]*$")
  95. MhSpilt = regexp.MustCompile("[::]")
  96. //识别采购单位联系人、联系电话、代理机构联系人、联系电话
  97. ContactInfoVagueReg = regexp.MustCompile("邮政编码|邮编|名称|(征求意见|报名审核购买)?((联系人?(及|和)?|办公|单位)?(((联系)?(电话|方式|号码)([//及]传真|及手机)?|手机)(号码)?|邮箱(地址)?|(详细)?(地(址|点)))|(联系|收料)(人(姓名)?|方式)|传真|电子邮件|(主要负责|项目(负责|联系)|经办)人)|采购方代表")
  98. ContactInfoMustReg = regexp.MustCompile("^(" + ContactInfoVagueReg.String() + ")$")
  99. ContactType = map[string]*regexp.Regexp{
  100. "采购单位": regexp.MustCompile("(采购(项目.{2}|服务)?|比选|询价|招标(服务)?|甲|建设|委托|发包|业主|使用|谈判|本招标项目经办|征求意见联系|项目实施)(人|单位|部门|机构|机关|(执行)?方$)|(项目|建(库|设))单位|招标人信息|采购中心(地址)?|业主|收料人|采购部"),
  101. "代理机构": regexp.MustCompile("(代理|受托|集中采购).{0,2}(人|方|单位|公司|机构)|招标机构|采购代理"),
  102. "中标单位": regexp.MustCompile("^((拟(定)?|预|最终|唯一)?(中标|成交|中选|供(货|应))((成交))?)[^候选]{0,2}(人|方|单位|公司|(服务|供应)?商|企业)"),
  103. "监督部门": regexp.MustCompile("投诉受理部门"),
  104. }
  105. ContactBuyerPersonFilterReg = regexp.MustCompile("(管理局)$")
  106. MultipleValueSplitReg = regexp.MustCompile("[,,、\\s\u3000\u2003\u00a0]")
  107. BuyerContacts = []string{"采购单位联系人", "采购单位联系电话", "采购单位联系地址"}
  108. FilterSerial = regexp.MustCompile(".+[、..::,]")
  109. underline = regexp.MustCompile("_+$")
  110. iswinnertabletag = regexp.MustCompile("(中标|候选人|成交|结果|磋商情况)")
  111. nswinnertabletag = regexp.MustCompile("评得分估|标的|班子成员|人员")
  112. jsonReg = regexp.MustCompile(`\{.+:[^}]*\} `) // \{".*\":\".+\"}
  113. regHz = regexp.MustCompile("[\u4e00-\u9fa5]")
  114. winnerOrderAndBidResult = regexp.MustCompile("((中标)?候选人|(中标|评标)结果)")
  115. WinnerOrderStr = regexp.MustCompile(`(集团|公司|学校|中心|家具城|门诊|[大中小]+学|部|院|局|厂|店|所|队|社|室|厅|段|会|场|行)$`)
  116. DoubtReg = regexp.MustCompile("(我中心|有(疑问|质疑|异议|意见)|(书面)?提出|不再受理|投诉|质疑|书面形式|监督|公示期(限)?)")
  117. )
  118. //在解析时,判断表格元素是否隐藏
  119. func IsHide(g *goquery.Selection) (b bool) {
  120. style, exists := g.Attr("style")
  121. if exists {
  122. b = display.MatchString(style)
  123. }
  124. return
  125. }
  126. //59.992664,33.495715,20.001306
  127. var clearnum *regexp.Regexp = regexp.MustCompile("(([0-9.]{1,6}[,,]+){4,}|(\\d{6}[,,]\\d{2}.){2,})")
  128. //对表格的key进行标准化处理,多个k相同时,出现覆盖问题
  129. //待扩展,暂不支持正则标签库
  130. func CommonDataAnaly(k, tabletag, tabledesc string, v interface{}, isSite bool, codeSite string) (kvTags map[string][]*u.Tag, returntag string) {
  131. kvTags = map[string][]*u.Tag{}
  132. v1 := ""
  133. if sv, sok := v.(string); sok { //取KV
  134. v1 = sv
  135. } else if sv, sok := v.([]string); sok { //是数组先默认取第一个
  136. if len(sv) >= 1 {
  137. v1 = sv[0]
  138. }
  139. }
  140. //对值单位的处理 (预算|费|价|额|规模|投资)
  141. if moneyreg.MatchString(k) {
  142. v1 += GetMoneyUnit(k, v1)
  143. }
  144. //先清理key
  145. //u.Debug(1, k, v1)
  146. k1 := ClearKey(k, 2)
  147. //u.Debug(2, k)
  148. //取标准key
  149. res := u.GetTags(k1, isSite, codeSite)
  150. if len(res) == 0 && k1 != k {
  151. res = u.GetTags(k, isSite, codeSite)
  152. k1 = k
  153. }
  154. //log.Println(k, res)
  155. // if len(res) == 0 {
  156. // go u.AddtoNoMatchMap(tk)
  157. // }
  158. //当取到标准化值时,放入数组
  159. if len(res) > 0 {
  160. for _, t1 := range res {
  161. //降低冒号值的权重
  162. if MhSpilt.MatchString(v1) {
  163. t1.Weight -= 50
  164. }
  165. if winnerOrderAndBidResult.MatchString(tabletag) && t1.Value == "采购单位联系人" { //处理table中项目负责人
  166. kvTags[k] = append(kvTags[k], &u.Tag{Key: k, Value: v1, IsInvalid: true})
  167. }else if regexp.MustCompile("(中标候选人|名单及其排序|排序)").MatchString(tabletag) && t1.Value == "采购单位" {
  168. kvTags[t1.Value] = append(kvTags[t1.Value], &u.Tag{Key: k1, Value: v1, Weight: t1.Weight - 150})
  169. } else {
  170. kvTags[t1.Value] = append(kvTags[t1.Value], &u.Tag{Key: k1, Value: v1, Weight: t1.Weight})
  171. }
  172. }
  173. res[0].IsInvalid = true
  174. //k1 = res[0].Value
  175. } else {
  176. kvTags[k] = append(kvTags[k], &u.Tag{Key: k, Value: v1, IsInvalid: true})
  177. //没有取到标准化key时,对中标金额和中标单位的逻辑处理
  178. if filter_zbje_k.MatchString(k) && !filter_zbje_kn.MatchString(k) && filter_zbje_v.MatchString(v1) && utf8.RuneCountInString(v1) < 20 {
  179. if tabletag == "" {
  180. returntag = "中标情况"
  181. }
  182. kvTags["中标金额"] = append(kvTags["中标金额"], &u.Tag{Key: k, Value: v1, Weight: -100, IsInvalid: true})
  183. } else if filter_zbdw_ky.MatchString(k) && !filter_zbdw_kn.MatchString(k) &&
  184. filter_zbdw_v.MatchString(v1) {
  185. kvTags["中标单位"] = append(kvTags["中标单位"], &u.Tag{Key: k, Value: v1, Weight: -100, IsInvalid: true})
  186. if tabletag == "" {
  187. returntag = "中标情况"
  188. }
  189. } else if !filter_zbje_jd.MatchString(tabletag) && !filter_zbje_jd.MatchString(k) && utf8.RuneCountInString(v1) < 13 {
  190. //对上一步没有取到标准化key的进一步处理
  191. if filter_tag_zb.MatchString(tabletag) || filter_tag_zb.MatchString(tabledesc) {
  192. //u.Debug(v1, k, "-----", filter_zbdw_jd.MatchString(k), filter_zbdw_v.MatchString(v1))
  193. if filter_zbje_jd.MatchString(k) && !filter_zbje_kn.MatchString(k) && filter_zbje_v.MatchString(v1) {
  194. if filter_ysje_jd.MatchString(k) {
  195. kvTags["预算金额"] = append(kvTags["预算金额"], &u.Tag{Key: k, Value: v1, Weight: -100})
  196. } else if !filter_zbdw_kn.MatchString(k) {
  197. kvTags["中标金额"] = append(kvTags["中标金额"], &u.Tag{Key: k, Value: v1, Weight: -100})
  198. }
  199. }
  200. }
  201. }
  202. }
  203. return
  204. }
  205. var glRex *regexp.Regexp = regexp.MustCompile("(成交|中标|候选|排名|名次|供应商排序|中标候选人|名单及其排序|排序)")
  206. var djReg *regexp.Regexp = regexp.MustCompile("^单价")
  207. //对解析后的表格的kv进行过滤
  208. func (table *Table) KVFilter(isSite bool, codeSite string) {
  209. //1.标准化值查找
  210. //2.对数组的处理
  211. //3.对分包的处理
  212. //4.对KV的处理
  213. //判断表格是否有用,调用abandontable正则数组进行判断
  214. //遍历每一行
  215. table.analyTdKV() //1.遍历每行每列td的sortkv添加到table.SorkVK中;2.td有子表格的处理
  216. as := NewSortMap()
  217. //遍历table.sortkv,进行过滤处理,并放入标准化KV中,如果值是数组跳到下一步处理
  218. for _, k := range table.SortKV.Keys {
  219. //表格描述处理,对成交结果的处理
  220. if glRex.MatchString(k) {
  221. table.Desc += "成交结果,"
  222. }
  223. if djReg.MatchString(k) {
  224. continue
  225. }
  226. v := table.SortKV.Map[k]
  227. if _, ok := v.(string); ok { //table.SortKV.Value为字符串,匹配抽取关键词table.SortKV.Key,匹配到添加k,v到table.StandKV,table.StandKVWeight
  228. k = pkgFilter.ReplaceAllString(k, "")
  229. k = regSpliteSegment.ReplaceAllString(regReplAllSpace.ReplaceAllString(k, ""), "")
  230. kvTags, tag := CommonDataAnaly(k, table.Tag, table.Desc, v, isSite, codeSite) //对key标准化处理,没有找到会走中标
  231. if tag != "" && table.Tag == "" {
  232. table.Tag = tag
  233. }
  234. MergeKvTags(table.StandKV, kvTags)
  235. } else {
  236. as.AddKey(k, v)
  237. }
  238. }
  239. //处理值是数组的kv放入标准化kv中//处理table.SortKV.value为数组的情况
  240. table.sortKVArr(as, isSite, codeSite)
  241. //
  242. if len(table.WinnerOrder) > 0 || !table.BPackage {
  243. winnerOrder := []map[string]interface{}{}
  244. maxSort := 0
  245. //调整顺序
  246. for i := 0; i < 2; i++ {
  247. for _, v := range table.WinnerOrder {
  248. sortstr, _ := v["sortstr"].(string)
  249. if (i == 0 && sortstr == "") || (i == 1 && sortstr != "") {
  250. continue
  251. }
  252. sort, _ := v["sort"].(int)
  253. if i == 0 {
  254. if maxSort == 0 || sort > maxSort {
  255. maxSort = sort
  256. }
  257. } else {
  258. maxSort++
  259. v["sort"] = maxSort
  260. }
  261. winnerOrder = append(winnerOrder, v)
  262. }
  263. if len(winnerOrder) == len(table.WinnerOrder) {
  264. break
  265. }
  266. }
  267. table.WinnerOrder = winnerOrder
  268. if len(table.WinnerOrder) == 0 {
  269. winnerOrder = []map[string]interface{}{}
  270. //遍历每个td,查询中标人
  271. for _, tr := range table.TRs {
  272. for _, td := range tr.TDs {
  273. winnerOrder = winnerOrderEntity.Find(td.Val, true, 3, isSite, codeSite)
  274. if len(winnerOrder) > 0 {
  275. //中标候选人合并
  276. winnerOrderEntity.Merge(table.WinnerOrder, winnerOrder)
  277. }
  278. }
  279. }
  280. }
  281. if !table.BPackage { //没有table.WinnerOrder也没有分包 将td中的WinnerOrder赋值给table.WinnerOrder
  282. if len(winnerOrder) > 1 {
  283. table.WinnerOrder = winnerOrder
  284. }
  285. }
  286. }
  287. //对中标候选人进行排序
  288. winnerOrderEntity.Order(table.WinnerOrder)
  289. //该表格有一个分包,并且有中标候选人排序的情况下,把中标候选人放到包里面
  290. if table.BlockPackage != nil && table.BlockPackage.Keys != nil && len(table.BlockPackage.Keys) == 1 {
  291. if table.BlockPackage.Map != nil {
  292. onePkgKey := table.BlockPackage.Keys[0]
  293. onePkg, _ := table.BlockPackage.Map[onePkgKey].(*u.BlockPackage)
  294. if onePkg != nil && (onePkg.WinnerOrder != nil || len(onePkg.WinnerOrder) == 0) {
  295. onePkg.WinnerOrder = table.WinnerOrder
  296. table.BlockPackage.AddKey(onePkgKey, onePkg)
  297. }
  298. }
  299. }
  300. }
  301. var winMoneyReg *regexp.Regexp = regexp.MustCompile("(报价|投标价|投标总价)")
  302. //处理table.SortKV.value为数组的情况
  303. func (table *Table) sortKVArr(as *SortMap, isSite bool, codeSite string) {
  304. winnertag := iswinnertabletag.MatchString(table.Tag) && !nswinnertabletag.MatchString(table.Tag) //table标签
  305. if !winnertag {
  306. winnertag = iswinnertabletag.MatchString(table.TableResult.BlockTag) && !nswinnertabletag.MatchString(table.TableResult.BlockTag) //块标签
  307. }
  308. if !winnertag {
  309. winnertag = iswinnertabletag.MatchString(table.Desc)
  310. }
  311. if !winnertag {
  312. winnertag = iswinnertabletag.MatchString(table.Html)
  313. }
  314. checkKey := map[int]bool{}
  315. //tmpBidmout := []string{}
  316. //log.Println(tmpBidmout)
  317. for kn, k := range as.Keys { //遍历table.SortKV.value为数组的key
  318. v := as.Map[k]
  319. if vm, ok := v.([]map[string]interface{}); ok && k == NullTxtBid {
  320. if table.WinnerOrder == nil {
  321. table.WinnerOrder = []map[string]interface{}{}
  322. }
  323. table.WinnerOrder = append(table.WinnerOrder, vm...)
  324. } else {
  325. //增加候选人排序逻辑
  326. if (table.WinnerOrder == nil || len(table.WinnerOrder) == 0) && !checkKey[kn] {
  327. if vs1, ok := v.([]string); ok {
  328. smap := make([]map[string]interface{}, len(vs1))
  329. for n1, _ := range vs1 {
  330. smap[n1] = map[string]interface{}{}
  331. }
  332. //hadSort := false
  333. tmpEntname := make([]string, len(vs1))
  334. tmpPrice := make([]string, len(vs1))
  335. for kn1, k := range as.Keys[kn:] {
  336. v := as.Map[k]
  337. if ContactType["采购单位"].MatchString(k) || ContactType["代理机构"].MatchString(k) {
  338. continue
  339. }
  340. //目前对数组数据的key做判断,但是某些额可以是不满足情况的
  341. //载明内容:[第一中标候选人 第二中标候选人] id:5d00587da5cb26b9b75e367b
  342. if vs, ok := v.([]string); ok && len(vs) == len(vs1) { //数组值的个数相同
  343. res, _, _, _, repl := CheckCommon(k, "bidorder")
  344. kv := ""
  345. if !res {
  346. kt := u.GetTags(k, isSite, codeSite)
  347. if kt.Len() > 0 {
  348. if kt[0].Value == "单品报价" && winnertag {
  349. kv = "中标金额"
  350. } else {
  351. kv = kt[0].Value
  352. }
  353. }
  354. }
  355. if !res && kv == "" { //key未验证出,验证数组的val值
  356. checkKey[kn+kn1] = true
  357. if winnertag { //如果是中标信息 在根据val数组信息解析候选人
  358. for vsk, vsv := range vs {
  359. if NullTdReg.MatchString(vsv) { //数据先验证val是否有排序
  360. //hadSort = true
  361. smap[vsk]["sortstr"] = vsv
  362. smap[vsk]["sort"] = GetBidSort(vsv, vsk+1)
  363. } else if findCandidate2.MatchString(vsv) && tmpEntname[vsk] == "" { //数据验证val是否是候选人
  364. entname, _ := winnerOrderEntity.clear("中标单位", vsv).(string)
  365. if entname != "" {
  366. tmpEntname[vsk] = entname
  367. }
  368. } else if winMoneyReg.MatchString(k) && len(tmpPrice[vsk]) == 0 {
  369. kv = "中标金额"
  370. } else { //验证val时如果数组中的第一条数据既不满足sort或者entname 判定此数组数据错误
  371. break
  372. }
  373. }
  374. }
  375. }
  376. if res || kv != "" { //连续往下找几个key
  377. checkKey[kn+kn1] = true
  378. SORT:
  379. if repl == "sort" {
  380. //hadSort = true
  381. for vsk, vsv := range vs {
  382. smap[vsk]["sortstr"] = vsv
  383. smap[vsk]["sort"] = GetBidSort(vsv, vsk+1)
  384. if findCandidate2.MatchString(vsv) && kv == "中标单位" && tmpEntname[vsk] == "" { //数据验证val是否是候选人
  385. entname, _ := winnerOrderEntity.clear("中标单位", vsv).(string)
  386. if entname != "" {
  387. tmpEntname[vsk] = entname
  388. }
  389. }
  390. }
  391. } else if repl == "entname" || kv == "中标单位" {
  392. for vsk, vsv := range vs {
  393. if winnerReg6.MatchString(vsv) { //k:中标候选人 v:["第一名","第二名"]
  394. repl = "sort"
  395. goto SORT
  396. }
  397. //if entname, _ := smap[vsk]["entname"].(string); entname != "" || len([]rune(vsv)) < 3 {
  398. // break
  399. //}
  400. //entname, _ := winnerOrderEntity.clear("中标单位", vsv).(string)
  401. //if entname != "" {
  402. // smap[vsk]["entname"] = entname
  403. if tmpEntname[vsk] != "" || len([]rune(vsv)) < 4 { //排除 单位:["台","个","套"]
  404. break
  405. }
  406. entname, _ := winnerOrderEntity.clear("中标单位", vsv).(string)
  407. if entname != "" {
  408. tmpEntname[vsk] = entname
  409. }
  410. }
  411. } else if kv == "中标金额" {
  412. for vsk, vsv := range vs {
  413. //过滤price 2348273.432元(万元)-->2348273.432
  414. //tmp1, _ := smap[vsk]["price"].(string)
  415. tmp1 := tmpPrice[vsk]
  416. p1num := numberReg2.FindString(tmp1)
  417. p2num := numberReg2.FindString(vsv)
  418. p1 := qutil.Float64All(p1num)
  419. p2 := qutil.Float64All(p2num)
  420. if p2 > p1 {
  421. //smap[vsk]["price"] = winnerOrderEntity.clear("中标金额", vsv+GetMoneyUnit(k, vsv))
  422. price := winnerOrderEntity.clear("中标金额", vsv+GetMoneyUnit(k, vsv))
  423. if pricestr, _ := price.(string); len(pricestr) < 30 && len(pricestr) > 0 {
  424. tmpPrice[vsk] = pricestr
  425. }
  426. }
  427. }
  428. } else if kv == "预算" {
  429. if strings.Contains(k, "万元") {
  430. for vsk, vsv := range vs {
  431. if !strings.Contains(vsv, "万元") {
  432. vs[vsk] = vsv + "万元"
  433. }
  434. }
  435. }
  436. }
  437. }
  438. } else {
  439. //break
  440. }
  441. }
  442. newSmap := []map[string]interface{}{}
  443. //qutil.Debug("smap=======", smap)
  444. //qutil.Debug("tmpEntname--", len(tmpEntname), tmpEntname)
  445. //qutil.Debug("tmpPrice--", len(tmpPrice), tmpPrice)
  446. for n, smap_v := range smap {
  447. //if hadSort { //有排序,再添加entname和price
  448. if len(tmpEntname) > 0 && n < len(tmpEntname) && tmpEntname[n] != "" {
  449. smap_v["entname"] = tmpEntname[n]
  450. if len(tmpPrice) > 0 && n < len(tmpPrice) && tmpPrice[n] != "" {
  451. smap_v["price"] = tmpPrice[n]
  452. }
  453. }
  454. //} else if len(tmpEntname) > 0 {
  455. //fmt.Println("table winnerorder only has entname", tmpEntname)
  456. //}
  457. if len(smap_v) >2{ //只有排序信息 sort和sortstr
  458. newSmap = append(newSmap, smap_v)
  459. }
  460. }
  461. if len(newSmap) > 0 {
  462. table.WinnerOrder = newSmap
  463. }
  464. }
  465. } else if vsss, ok := v.([]string); ok {
  466. if len(table.WinnerOrder) > 0 && table.WinnerOrder[0]["price"] == nil && len(vsss) == len(table.WinnerOrder) {
  467. kv := ""
  468. if winMoneyReg.MatchString(k) {
  469. kv = "中标金额"
  470. } else {
  471. kt := u.GetTags(k, isSite, codeSite)
  472. if kt.Len() > 0 {
  473. if kt[0].Value == "单品报价" && winnertag {
  474. kv = "中标金额"
  475. } else {
  476. kv = kt[0].Value
  477. }
  478. }
  479. }
  480. if kv == "中标金额" {
  481. for i, vx := range vsss {
  482. p1num := numberReg2.FindString(vx)
  483. if strings.Contains(p1num, ",") && strings.Contains(p1num, ".") {
  484. p1num = strings.ReplaceAll(p1num, ",", "")
  485. }
  486. p1 := qutil.Float64All(p1num)
  487. if p1 > 0 {
  488. //smap[vsk]["price"] = winnerOrderEntity.clear("中标金额", vsv+GetMoneyUnit(k, vsv))
  489. price := winnerOrderEntity.clear(kv, vx+GetMoneyUnit(k, vx))
  490. if pricestr, _ := price.(string); len(pricestr) < 30 && len(pricestr) > 0 && !clearnum.MatchString(pricestr) {
  491. table.WinnerOrder[i]["price"] = pricestr
  492. }
  493. }
  494. }
  495. }
  496. }
  497. }
  498. }
  499. }
  500. }
  501. //1.遍历每行每列td的sortkv添加到table.SorkVK中;2.td有子表格的处理
  502. func (table *Table) analyTdKV() {
  503. //遍历每一行
  504. for _, tr := range table.TRs {
  505. for _, td := range tr.TDs {
  506. //fmt.Println(td.BH, td.MustBH, td.Val, td.SortKV.Map)
  507. bc := false
  508. if !td.BH {
  509. //表头是否是无用内容
  510. if td.HeadTd != nil {
  511. bc, _, _, _, _ = CheckCommon(td.HeadTd.Val, "abandontable")
  512. }
  513. }
  514. if !bc {
  515. //td元素有内嵌kv,遍历放入table的Kv中
  516. if len(td.SortKV.Keys) > 0 {
  517. for _, k3 := range td.SortKV.Keys {
  518. _val := td.SortKV.Map[k3]
  519. //thisFlag := false
  520. if td.HeadTd != nil && len([]rune(k3)) < 4 {
  521. k3 = td.HeadTd.Val + k3
  522. }
  523. if table.SortKV.Map[k3] == nil && _val != nil && _val != "" {
  524. //u.Debug(k3, _val)
  525. //if !thisFlag || (thisFlag && table.SortKV.Map[k3] == nil) {
  526. table.SortKV.AddKey(k3, _val)
  527. }
  528. }
  529. }
  530. }
  531. //td有子表格的处理
  532. //u.Debug(td.BH, td.Val, td.SonTableResult)
  533. if td.SonTableResult != nil {
  534. //u.Debug(td.SonTableResult.SortKV.Map, "-------", td.SonTableResult.Tabs)
  535. for k3, v3 := range td.SonTableResult.KvTags {
  536. table.StandKV[k3] = append(table.StandKV[k3], v3...)
  537. }
  538. //中标候选人排序
  539. if table.WinnerOrder == nil || len(table.WinnerOrder) == 0 {
  540. table.WinnerOrder = td.SonTableResult.WinnerOrder
  541. } else {
  542. winnerOrderEntity.Merge(table.WinnerOrder, td.SonTableResult.WinnerOrder)
  543. }
  544. }
  545. }
  546. }
  547. }
  548. //表格结果合并到父表格集中
  549. func (table *Table) MergerToTableresult() {
  550. //对多包表格的多包值的合并处理
  551. if table.BPackage {
  552. table.TableResult.IsMultiPackage = true
  553. for _, v2 := range table.BlockPackage.Keys {
  554. package1 := table.TableResult.PackageMap.Map[v2]
  555. if package1 == nil {
  556. table.TableResult.PackageMap.AddKey(v2, table.BlockPackage.Map[v2])
  557. if vvv, ok := table.BlockPackage.Map[v2].(*u.BlockPackage); ok {
  558. if vvv.TableKV != nil && len(vvv.TableKV.KvTags) > 0 {
  559. MergeKvTags(table.TableResult.KvTags, vvv.TableKV.KvTags)
  560. }
  561. }
  562. } else {
  563. bp := package1.(*u.BlockPackage)
  564. if bp.TableKV == nil {
  565. bp.TableKV = u.NewJobKv()
  566. }
  567. v1 := table.BlockPackage.Map[v2].(*u.BlockPackage)
  568. if v1.TableKV != nil && len(v1.TableKV.KvTags) > 0 {
  569. for k2, v2 := range v1.TableKV.KvTags {
  570. if k2 == "" {
  571. continue
  572. }
  573. isExists := false
  574. for _, v2v := range v2 {
  575. if v2v.Value == "" {
  576. continue
  577. }
  578. for _, v2vv := range bp.TableKV.KvTags[k2] {
  579. if v2v.Value == v2vv.Value {
  580. isExists = true
  581. break
  582. }
  583. }
  584. if !isExists {
  585. bp.TableKV.KvTags[k2] = append(bp.TableKV.KvTags[k2], v2v)
  586. bp.Text += fmt.Sprintf("%v:%v\n", k2, v2)
  587. }
  588. }
  589. }
  590. }
  591. if bp.Bidamount <= 0 && !bp.IsTrueBidamount {
  592. bp.Bidamount = v1.Bidamount
  593. bp.IsTrueBidamount = v1.IsTrueBidamount
  594. }
  595. if bp.Budget <= 0 && !bp.IsTrueBudget {
  596. bp.Budget = v1.Budget
  597. bp.IsTrueBudget = v1.IsTrueBudget
  598. }
  599. bp.Text += bp.Text
  600. if len(v1.WinnerOrder) > 0 && len(bp.WinnerOrder) == 0 {
  601. bp.WinnerOrder = v1.WinnerOrder
  602. }
  603. }
  604. }
  605. }
  606. //遍历标准key到tableresult.sortkv中
  607. for _, v := range table.StandKV {
  608. for _, vv := range v {
  609. vv.Value = strings.Replace(vv.Value, "__", "", -1)
  610. }
  611. }
  612. MergeKvTags(table.TableResult.KvTags, table.StandKV)
  613. //表格的块标签
  614. if table.TableResult.BlockTag == "" && table.Tag != "" {
  615. table.TableResult.BlockTag = table.Tag
  616. }
  617. //中标候选人(多个table,现在默认取第一个table的信息,考虑需不需要多个table分析合并数据?)
  618. if table.TableResult.WinnerOrder == nil || len(table.TableResult.WinnerOrder) == 0 {
  619. table.TableResult.WinnerOrder = table.WinnerOrder
  620. }
  621. //增加brand 并列table
  622. if len(table.BrandData) > 0 {
  623. for _, v := range table.BrandData {
  624. if len(v) > 0 {
  625. table.TableResult.BrandData = append(table.TableResult.BrandData, v)
  626. }
  627. }
  628. }
  629. //抽取prince和number 并列table
  630. if len(table.PriceNumberData) > 0 {
  631. for _, v := range table.PriceNumberData {
  632. if len(v) > 0 {
  633. table.TableResult.PriceNumberData = append(table.TableResult.PriceNumberData, v)
  634. }
  635. }
  636. }
  637. if table.BlockPackage != nil && len(table.BlockPackage.Keys) == 0 {
  638. for _, v := range table.BlockPackage.Keys {
  639. if table.BlockPackage.Map[v] != nil {
  640. if vvv, ok := table.BlockPackage.Map[v].((*u.BlockPackage)); ok {
  641. if vvv.TableKV != nil && len(vvv.TableKV.KvTags) > 0 {
  642. for kk, vv := range vvv.TableKV.KvTags {
  643. if kk == "" {
  644. continue
  645. }
  646. if len(table.TableResult.KvTags[kk]) == 0 {
  647. table.TableResult.KvTags[kk] = vv
  648. }
  649. }
  650. }
  651. }
  652. }
  653. }
  654. }
  655. }
  656. /**
  657. 解析表格入口
  658. 返回:汇总表格对象
  659. **/
  660. func AnalyTableV2(tabs *goquery.Selection, toptype, blockTag, con string, itype int, _id interface{}, ruleBlock *u.RuleBlock, isSite bool, codeSite string) (tabres *TableResult) {
  661. defer qutil.Catch()
  662. //u.Debug(con)
  663. if itype == 1 {
  664. //修复表格
  665. con = RepairCon(con)
  666. }
  667. //生成tableresult对象
  668. tabres = NewTableResult(_id, toptype, blockTag, con, itype, ruleBlock)
  669. if fblbReg.MatchString(blockTag) {
  670. return
  671. }
  672. //可以有多个table
  673. //for _, table := range tabs {
  674. //隐藏表格跳过
  675. if IsHide(tabs) {
  676. return
  677. }
  678. tabres.GoqueryTabs = tabs
  679. //}
  680. //解析表格集
  681. tabres.Analy(isSite, codeSite)
  682. return
  683. }
  684. //开始解析表格集
  685. func (ts *TableResult) Analy(isSite bool, codeSite string) {
  686. tabs := []*Table{}
  687. contactFormat := &u.ContactFormat{
  688. IndexMap: map[int]string{},
  689. MatchMap: map[string]map[string]bool{},
  690. }
  691. //for _, table := range ts.GoqueryTabs {
  692. tn := NewTable(ts.Html, ts, ts.GoqueryTabs)
  693. //核心模块
  694. tsw := tn.Analy(contactFormat, isSite, codeSite)
  695. for _, tab := range tsw {
  696. if len(tab.TRs) > 0 {
  697. tabs = append(tabs, tab)
  698. }
  699. //fmt.Println("tab.SortKV.Map", tab.SortKV.Keys)
  700. }
  701. //tn.SonTables = append(tn.SonTables, tn)
  702. //}
  703. //统一合并,考虑统一多表格是多包的情况---新增
  704. if len(tabs) > 1 {
  705. pns := map[string]string{}
  706. pnarr := []string{}
  707. for _, table := range tabs {
  708. if len(table.StandKV["项目名称"]) == 0 {
  709. continue
  710. }
  711. pn := table.StandKV["项目名称"][0]
  712. if pn != nil && pn.Value != "" && TitleReg.MatchString(pn.Value) {
  713. pnarr = append(pnarr, pn.Value)
  714. matchres := TitleReg.FindAllStringSubmatch(pn.Value, -1)
  715. if len(matchres) == 1 && len(matchres[0]) > 0 {
  716. v1 := u.PackageNumberConvert(matchres[0][0])
  717. pns[v1] = matchres[0][0]
  718. bp := &u.BlockPackage{}
  719. bp.Index = v1
  720. bp.Origin = matchres[0][0]
  721. if bp.TableKV == nil {
  722. bp.TableKV = u.NewJobKv()
  723. }
  724. for _, k := range []string{"中标金额", "中标单位", "预算", "成交状态", "项目名称", "项目编号", "采购范围"} {
  725. if len(table.StandKV[k]) > 0 {
  726. bp.TableKV.KvTags[k] = append(bp.TableKV.KvTags[k], &u.Tag{Key: k, Value: table.StandKV[k][0].Value})
  727. }
  728. }
  729. bp.WinnerOrder = table.WinnerOrder
  730. if table.BlockPackage.Map[v1] == nil {
  731. table.BPackage = true
  732. table.BlockPackage.AddKey(v1, bp)
  733. } else {
  734. table.BlockPackage.RemoveKey(v1)
  735. table.BlockPackage.AddKey(v1, bp)
  736. }
  737. }
  738. }
  739. }
  740. if len(tabs) == len(pns) {
  741. //多个表格,每个表格都是一个分包 http://www.cxzwfw.gov.cn/info/1009/6963.htm
  742. //项目名称、项目编号、采购单位、招标机构、预算
  743. pname := projectnameReg.ReplaceAllString(pnarr[0], "")
  744. btrue := true
  745. for _, pn := range pnarr[1:] {
  746. pn = projectnameReg.ReplaceAllString(pn, "")
  747. //u.Debug(pn, pname)
  748. if pn != pname {
  749. //项目名称不一致
  750. btrue = false
  751. break
  752. }
  753. }
  754. if btrue {
  755. ts.KvTags["项目名称"] = append(ts.KvTags["项目名称"], &u.Tag{Key: "项目名称", Value: pname, Weight: 100})
  756. for _, table := range tabs {
  757. table.BPackage = true
  758. //预算、中标金额、NullTxtBid成交供应商排名 中标单位 成交状态
  759. if table.BlockPackage != nil && len(table.BlockPackage.Keys) == 1 {
  760. bp := table.BlockPackage.Map[table.BlockPackage.Keys[0]].(*u.BlockPackage)
  761. if table.TableResult.WinnerOrder != nil {
  762. bp.WinnerOrder = table.WinnerOrder
  763. }
  764. if bp != nil && table.StandKV != nil {
  765. if bp.TableKV == nil {
  766. bp.TableKV = u.NewJobKv()
  767. }
  768. for nk, k := range []string{"中标金额", "中标单位", "预算", "成交状态", "项目名称", "项目编号", "采购范围"} {
  769. if len(table.StandKV[k]) > 0 {
  770. bp.TableKV.KvTags[k] = append(bp.TableKV.KvTags[k], &u.Tag{Key: k, Value: table.StandKV[k][0].Value})
  771. }
  772. if nk < 4 {
  773. delete(table.StandKV, k)
  774. }
  775. }
  776. }
  777. }
  778. }
  779. }
  780. }
  781. }
  782. for _, table := range tabs {
  783. table.MergerToTableresult()
  784. // for k, v := range table.TableResult.SortKV.Map {
  785. // qutil.Debug(k, "=====", v)
  786. // }
  787. MergeKvTags(ts.KvTags, table.TableResult.KvTags)
  788. }
  789. }
  790. //解析表格
  791. func (table *Table) Analy(contactFormat *u.ContactFormat, isSite bool, codeSite string) []*Table {
  792. //查找表体中的tr对象
  793. trs := table.Goquery.ChildrenFiltered("tbody,thead,tfoot").ChildrenFiltered("tr")
  794. if trs.Size() == 0 {
  795. trs = table.Goquery.ChildrenFiltered("tr")
  796. }
  797. ztb := table.Goquery.Find("table").Size()
  798. if ztb >= 9 {
  799. return []*Table{}
  800. }
  801. //遍历节点,初始化table 结构
  802. table.createTabe(trs, isSite, codeSite)
  803. if len(table.TRs) == 0 {
  804. return []*Table{}
  805. }
  806. //重置行列
  807. table.ComputeRowColSpan()
  808. //对table结构体进行整体解析处理
  809. ts := table.AnalyTables(contactFormat, isSite, codeSite)
  810. return ts
  811. }
  812. var fblbReg *regexp.Regexp = regexp.MustCompile("(废标|流标|负责人资格|负责人业绩|相关业绩|开标记录|附件[:0-9]|越南盾|技术分|填报项目业绩|未通过.*原因)")
  813. //遍历节点,初始化table 结构体
  814. func (table *Table) createTabe(trs *goquery.Selection, isSite bool, codeSite string) {
  815. trs.Each(func(n int, sel *goquery.Selection) {
  816. //隐藏行不处理
  817. if IsHide(sel) {
  818. return
  819. }
  820. //遍历每行的td
  821. tds := sel.ChildrenFiltered("td,th")
  822. TR := NewTR(table)
  823. tdTextIsNull := false
  824. var empty int
  825. tds.Each(func(m int, selm *goquery.Selection) {
  826. //对隐藏列不处理!!!
  827. if IsHide(selm) {
  828. return
  829. }
  830. //进入每一个单元格
  831. td := NewTD(selm, TR, table, isSite, codeSite) //初始化td,kv处理,td中有table处理,td的方向
  832. //num++
  833. TR.AddTD(td)
  834. if td.Val == "" && td.SonTableResult == nil && len(td.SortKV.Map) == 0 { //删除一个tr,tr中所有td是空值的
  835. empty++
  836. if tds.Size() == empty {
  837. tdTextIsNull = true
  838. }
  839. }
  840. })
  841. //向table添加每行不为空的tr
  842. if !tdTextIsNull {
  843. table.AddTR(TR)
  844. }
  845. })
  846. }
  847. //对table进行整体解析处理
  848. func (tn *Table) AnalyTables(contactFormat *u.ContactFormat, isSite bool, codeSite string) []*Table {
  849. ts := tn.tableSubDemolitionTable() //分包,拆表
  850. for n, table := range ts {
  851. //处理每个table
  852. if len(table.TRs) > 0 {
  853. //删除尾部空白行
  854. table.deleteTrimTr()
  855. //table.Print()
  856. //校对表格
  857. table.Adjust(isSite, codeSite)
  858. //查找表格的标签,table.Tag字段
  859. table.FindTag()
  860. //log.Println(table.TableResult.Id, table.Html)
  861. //分割表格
  862. table.bSplit(n, ts, isSite, codeSite)
  863. table.TdContactFormat(contactFormat, isSite, codeSite) //contactFormat,处理采购单位,代理机构
  864. //开始查找kv,核心模块,table.SortKV
  865. table.FindKV(isSite, codeSite)
  866. //table中抽取品牌,table.BrandData
  867. if u.IsBrandGoods {
  868. table.analyBrand()
  869. }
  870. //table中抽取单价和个数
  871. if u.IsPriceNumber {
  872. //qutil.Debug("======================抽取price和number===========")
  873. table.extractPriceNumber()
  874. }
  875. res, _, _, _, _ := CheckCommon(table.Tag, "abandontable")
  876. if !res {
  877. //过滤、标准化、合并kv,table.StandKV,table.StandKVWeight
  878. table.KVFilter(isSite, codeSite)
  879. }
  880. //对没有表头表格的处理
  881. if table.Tag != "" {
  882. co, m, b := CheckMultiPackage(table.Tag, "")
  883. if b {
  884. table.BPackage = b
  885. if len(table.BlockPackage.Map) == 0 {
  886. for _, av := range m {
  887. kv := u.NewJobKv()
  888. kv.KvTags = table.StandKV
  889. bd := u.PackageNumberConvert(av[0])
  890. blockPackage := &u.BlockPackage{
  891. Origin: av[0],
  892. Name: av[0],
  893. Text: co,
  894. TableKV: kv,
  895. Index: bd,
  896. }
  897. if bd != "" {
  898. table.BlockPackage.AddKey(bd, blockPackage)
  899. } else {
  900. table.BlockPackage.AddKey(av[0], blockPackage)
  901. }
  902. }
  903. }
  904. table.StandKV["项目名称"] = append(table.StandKV["项目名称"], &u.Tag{Key: "项目名称", Value: table.Tag, Weight: -300})
  905. }
  906. }
  907. //判断是否是多包,并处理分包的//遍历td分块
  908. table.CheckMultiPackageByTable(isSite, codeSite)
  909. //MergeKvTags(table.TableResult.KvTags, table.StandKV)
  910. }
  911. }
  912. return ts
  913. }
  914. //分包,拆表
  915. func (table *Table) tableSubDemolitionTable() []*Table {
  916. tm := []map[string]interface{}{}
  917. tmk := map[string]bool{}
  918. tmn := map[int]map[string]interface{}{}
  919. for rownum, tr := range table.TRs {
  920. if len(tr.TDs) == 1 && table.ColNum > 1 { //tr里面有一列,table里面有多列
  921. td := tr.TDs[0] //取每行第一个td
  922. //td开始列等于0 && td结束列+1等于table列数 && td长度大于1小于50
  923. if td.StartCol == 0 && td.EndCol+1 == table.ColNum && len([]rune(td.Val)) > 1 && len([]rune(td.Val)) < 50 {
  924. con, m1, b := CheckMultiPackage(td.Val, "") //判断分包
  925. if b {
  926. for k, _ := range m1 {
  927. numstr := u.PackageNumberConvert(k)
  928. m2 := map[string]interface{}{
  929. "tag": con,
  930. //"num": numstr,
  931. //"numtxt": v[0],
  932. "startrow": rownum,
  933. }
  934. tmk[numstr] = true
  935. tmn[rownum] = m2
  936. tm = append(tm, m2)
  937. break
  938. }
  939. }
  940. }
  941. }
  942. }
  943. //拆表
  944. ts := []*Table{}
  945. if len(tmk) > 1 && len(tmk) == len(tm) {
  946. var tab1 *Table
  947. for rownum, tr := range table.TRs {
  948. if tab1 == nil {
  949. tab1 = NewTable("", table.TableResult, table.Goquery)
  950. tab1.BSplit = true
  951. if tmn[rownum] != nil {
  952. tab1.StandKV["项目名称"] = append(tab1.StandKV["项目名称"], &u.Tag{Key: "项目名称", Value: tmn[rownum]["tag"].(string), Weight: -100})
  953. }
  954. ts = append(ts, tab1)
  955. }
  956. if tmn[rownum] != nil {
  957. tab1.Tag = tmn[rownum]["tag"].(string)
  958. } else {
  959. tab1.AddTR(tr)
  960. }
  961. if tmn[rownum+1] != nil {
  962. tab1 = nil
  963. }
  964. }
  965. } else {
  966. ts = append(ts, table)
  967. }
  968. return ts
  969. }
  970. //分割表格
  971. func (table *Table) bSplit(n int, ts []*Table, isSite bool, codeSite string) {
  972. if table.BSplit {
  973. if !table.BHeader && n > 0 {
  974. for i := n - 1; i > -1; i-- {
  975. if ts[i].BHeader {
  976. if ts[i].BFirstRow {
  977. //取第一行插入到
  978. table.InsertTR(ts[i].TRs[0])
  979. table.Adjust(isSite, codeSite)
  980. }
  981. break
  982. }
  983. }
  984. }
  985. }
  986. }
  987. //删除尾部空白行
  988. func (table *Table) deleteTrimTr() {
  989. for len(table.TRs) > 0 {
  990. npos := len(table.TRs)
  991. tailTR := table.TRs[npos-1] //最后一个tr,取最后一行
  992. bspace := true
  993. for _, v := range tailTR.TDs {
  994. if v.Val != "" || v.SonTableResult != nil || len(v.SortKV.Keys) > 0 {
  995. bspace = false
  996. break
  997. }
  998. }
  999. //删除尾部空行,是空行的话就删除
  1000. if bspace {
  1001. table.TRs = table.TRs[:npos-1]
  1002. } else {
  1003. break
  1004. }
  1005. }
  1006. }
  1007. //校对表格
  1008. func (table *Table) Adjust(isSite bool, codeSite string) {
  1009. //计算行列起止位置,跨行跨列处理
  1010. table.ComputeRowColSpan()
  1011. // for k1, tr := range table.TRs {
  1012. // for k2, td := range tr.TDs {
  1013. // qutil.Debug(k1, k2, td.Val, td.StartRow, td.EndRow, td.StartCol, td.EndCol)
  1014. // }
  1015. // }
  1016. //大概计算每个起止行列的概率
  1017. table.GetKeyRation()
  1018. /*
  1019. for k, v := range table.StartAndEndRation {
  1020. for k1, v1 := range v.Poss {
  1021. bs, _ := json.Marshal(v1)
  1022. str := ""
  1023. for _, td := range v.Tdmap[v1] {
  1024. str += "__" + td.Val + fmt.Sprintf("%d_%d_%d_%d", td.StartRow, td.EndRow, td.StartCol, td.EndCol)
  1025. }
  1026. qutil.Debug(k, k1, string(bs), v.Rationmap[v1], str)
  1027. }
  1028. }
  1029. */
  1030. //u.Debug("tdnum:", num, table.RowNum, table.ColNum)
  1031. //是否是规则的表格,单元各个数=行数*列数
  1032. table.Brule = table.TDNum == table.RowNum*table.ColNum
  1033. count := 0
  1034. for _, trs := range table.TRs {
  1035. for _, td := range trs.TDs {
  1036. if td.BH {
  1037. count++
  1038. }
  1039. }
  1040. }
  1041. if float32(count)/float32(table.TDNum) < 0.85 {
  1042. //精确计算起止行列是表头的概率
  1043. table.ComputeRowColIsKeyRation(isSite, codeSite)
  1044. bhead := false
  1045. L:
  1046. for i, tr := range table.TRs {
  1047. for _, td := range tr.TDs {
  1048. if td.BH {
  1049. //qutil.Debug("----=====---", td.Val, len(table.TRs[len(table.TRs)-1].TDs), i, len(table.TRs)-1)
  1050. if i == len(table.TRs)-1 && len(table.TRs[len(table.TRs)-1].TDs) == 2 {
  1051. res, _, _, _, _ := CheckCommon(td.Val, "abandontable")
  1052. if res {
  1053. //删除此行
  1054. table.TRs = table.TRs[:len(table.TRs)-1]
  1055. table.Adjust(isSite, codeSite)
  1056. return
  1057. }
  1058. }
  1059. bhead = true
  1060. break L
  1061. }
  1062. }
  1063. }
  1064. table.BHeader = bhead
  1065. }
  1066. }
  1067. //计算行/列表格的结束位置 StartRow=0 EndRow=0,table.TDNum td个数 table.RowNum 行数
  1068. func (table *Table) ComputeRowColSpan() {
  1069. n := 0 //td总个数
  1070. mapRC := map[int]map[int]int{} //记录第几行pos,起始列对应的合并值
  1071. for k, v := range table.TRs {
  1072. n += len(v.TDs) //每行的td总数相加
  1073. nk := 0 //nk列的起始,k行的起始||如果有合并,起始就不是0
  1074. ball := true
  1075. rowspans := v.TDs[0].Rowspan //某一行第一个td的rowspan
  1076. for k1, v1 := range v.TDs {
  1077. if k1 == 0 && k == 0 {
  1078. table.TRs[k].TDs[k1].MustBH = true
  1079. table.TRs[k].TDs[k1].BH = true
  1080. }
  1081. if v1.Rowspan != rowspans {
  1082. ball = false
  1083. break
  1084. }
  1085. }
  1086. for _, v1 := range v.TDs {
  1087. if ball {
  1088. v1.Rowspan = 1
  1089. }
  1090. mc := mapRC[k]
  1091. for {
  1092. if mc != nil && mc[nk] > 0 {
  1093. nk += mc[nk]
  1094. } else {
  1095. break
  1096. }
  1097. }
  1098. v1.StartCol = nk
  1099. nk += v1.Colspan - 1
  1100. v1.EndCol = nk
  1101. if nk >= table.ColNum {
  1102. table.ColNum = nk + 1
  1103. }
  1104. nk++
  1105. v1.StartRow = k
  1106. v1.EndRow = k + v1.Rowspan - 1
  1107. ck := fmtkey("c", v1.StartCol, v1.EndCol)
  1108. tdcs := table.StartAndEndRation[ck]
  1109. if tdcs == nil {
  1110. tdcs = NewTDRationScope(ck)
  1111. table.StartAndEndRation[ck] = tdcs
  1112. table.StartAndEndRationKSort.AddKey(ck, 1)
  1113. }
  1114. tdcs.Addtd(v1)
  1115. rk := fmtkey("r", v1.StartRow, v1.EndRow)
  1116. tdrs := table.StartAndEndRation[rk]
  1117. if tdrs == nil {
  1118. tdrs = NewTDRationScope(rk)
  1119. table.StartAndEndRation[rk] = tdrs
  1120. table.StartAndEndRationKSort.AddKey(rk, 1)
  1121. }
  1122. tdrs.Addtd(v1)
  1123. if v1.Rowspan > 1 {
  1124. for i := 1; i < v1.Rowspan; i++ {
  1125. r := k + i
  1126. if r < len(table.TRs) {
  1127. mc := mapRC[r]
  1128. if mc == nil {
  1129. mc = map[int]int{}
  1130. }
  1131. mc[v1.StartCol] = v1.Colspan
  1132. mapRC[r] = mc
  1133. }
  1134. }
  1135. }
  1136. }
  1137. }
  1138. table.TDNum = n //td总个数
  1139. table.RowNum = len(table.TRs) //tr总行数
  1140. }
  1141. func fmtkey(t string, start, end int) string {
  1142. return fmt.Sprintf("%s_%d_%d", t, start, end)
  1143. }
  1144. //查找每个table的标签,如果有标签可按标签处理,否则根据表格去判断
  1145. func (table *Table) FindTag() {
  1146. //查找每个table的标签,如果有标签可按标签处理,否则根据表格去判断
  1147. if table.Tag != "" {
  1148. return
  1149. }
  1150. if table.Tag == "" {
  1151. table.Tag = table.TableResult.BlockTag
  1152. }
  1153. //u.Debug(table.Tag)
  1154. }
  1155. //计算r/c_start_end的概率
  1156. func (table *Table) GetKeyRation() {
  1157. for _, vn := range table.StartAndEndRationKSort.Keys {
  1158. v := table.StartAndEndRation[vn]
  1159. for _, v1 := range v.Poss {
  1160. count := 0
  1161. n := 0
  1162. for _, td := range v.Tdmap[v1] {
  1163. n++
  1164. if td.BH {
  1165. count++
  1166. }
  1167. }
  1168. v.Rationmap[v1] = float32(count) / float32(n)
  1169. }
  1170. }
  1171. }
  1172. //计算行列是表头的概率调用GetKeyRation
  1173. func (table *Table) ComputeRowColIsKeyRation(isSite bool, codeSite string) {
  1174. //增加对跨行校正限止
  1175. // u.Debug(table.Brule, table.ColNum, table.RowNum, table.TDNum)
  1176. bkeyfirstrow := false
  1177. bkeyfirstcol := false
  1178. if table.Brule { //不存在跨行跨列的情况,规则表格
  1179. checkCompute := map[string]bool{}
  1180. for k, tr := range table.TRs {
  1181. rk := fmtkey("r", tr.TDs[0].StartRow, tr.TDs[0].EndRow)
  1182. if k == 0 { //第1行的概率
  1183. ck := fmtkey("c", tr.TDs[0].StartCol, tr.TDs[0].EndCol)
  1184. //u.Debug(table.BFirstRow, "--", table.StartAndEndRation[rk], table.StartAndEndRation[ck])
  1185. ration1, _ := table.StartAndEndRation[rk].GetTDRation(tr.TDs[0])
  1186. ration2, _ := table.StartAndEndRation[ck].GetTDRation(tr.TDs[0])
  1187. if (len(tr.TDs) == 2 && ration2 < 0.55) && (len(tr.TDs) == 2 && ration1 > 0.5) { //第一行为key
  1188. bkeyfirstrow = true
  1189. ball := true
  1190. for _, td := range tr.TDs {
  1191. if MoneyReg.MatchString(td.Val) {
  1192. bkeyfirstrow = false
  1193. ball = false
  1194. td.BH = false
  1195. break
  1196. }
  1197. }
  1198. for _, td := range tr.TDs {
  1199. if ball {
  1200. //td.BH = true
  1201. td.KeyDirect = 1
  1202. td.KVDirect = 2
  1203. }
  1204. }
  1205. } else if ration2 > 0.55 { //第1列
  1206. bkeyfirstcol = true
  1207. if !checkCompute[ck] {
  1208. checkCompute[ck] = true
  1209. //重置第1列
  1210. for _, tr1 := range table.TRs {
  1211. for _, td1 := range tr1.TDs {
  1212. if td1.StartCol == 0 {
  1213. if !MoneyReg.MatchString(td1.Val) {
  1214. //td1.BH = true
  1215. td1.KeyDirect = 2
  1216. td1.KVDirect = 1
  1217. }
  1218. }
  1219. }
  1220. }
  1221. }
  1222. }
  1223. if !bkeyfirstrow && !bkeyfirstcol {
  1224. if len(tr.TDs) > 1 && ration1 > ration2 && ration1 > 0.5 {
  1225. bkeyfirstrow = true
  1226. for _, td := range tr.TDs {
  1227. if !MoneyReg.MatchString(td.Val) {
  1228. //td.BH = true
  1229. td.KeyDirect = 1
  1230. td.KVDirect = 2
  1231. }
  1232. }
  1233. } else if tr.Table.ColNum > 1 && ration2 > 0.5 {
  1234. bkeyfirstcol = true
  1235. if !checkCompute[ck] {
  1236. checkCompute[ck] = true
  1237. //重置第1列
  1238. for _, tr1 := range table.TRs {
  1239. for _, td1 := range tr1.TDs {
  1240. if td1.StartCol == 0 {
  1241. if !MoneyReg.MatchString(td1.Val) {
  1242. td1.BH = true
  1243. td1.KeyDirect = 2
  1244. td1.KVDirect = 1
  1245. }
  1246. }
  1247. }
  1248. }
  1249. }
  1250. }
  1251. }
  1252. } else {
  1253. if bkeyfirstrow {
  1254. //第一列的概率
  1255. ration1, _ := table.StartAndEndRation[rk].GetTDRation(tr.TDs[0])
  1256. if k == 1 || ration1 < checkval {
  1257. for _, td := range tr.TDs {
  1258. if !td.MustBH {
  1259. td.BH = false
  1260. td.KeyDirect = 0
  1261. td.KVDirect = 0
  1262. }
  1263. }
  1264. } //else {for _, td := range tr.TDs {}}
  1265. } else {
  1266. //列在起作用
  1267. if bkeyfirstcol {
  1268. for _, td := range tr.TDs {
  1269. ck := fmtkey("c", td.StartCol, td.EndCol)
  1270. ration1, _ := table.StartAndEndRation[ck].GetTDRation(td)
  1271. if !checkCompute[ck] {
  1272. checkCompute[ck] = true
  1273. if ration1 >= checkval && td.ColPos != 1 {
  1274. for _, tr1 := range table.TRs {
  1275. for _, td1 := range tr1.TDs {
  1276. if td1.StartCol == td.StartCol {
  1277. if !MoneyReg.MatchString(td1.Val) {
  1278. td1.BH = true
  1279. td1.KeyDirect = 2
  1280. td1.KVDirect = 1
  1281. }
  1282. }
  1283. }
  1284. }
  1285. } else {
  1286. for _, tr1 := range table.TRs[1:] {
  1287. for _, td1 := range tr1.TDs[1:] {
  1288. if td1.StartCol == td.StartCol && !td1.MustBH {
  1289. td1.BH = false
  1290. td1.KeyDirect = 0
  1291. td1.KVDirect = 0
  1292. }
  1293. }
  1294. }
  1295. }
  1296. }
  1297. }
  1298. }
  1299. }
  1300. }
  1301. }
  1302. }
  1303. //qutil.Debug("table.Brule", table.Brule, !bkeyfirstcol && !bkeyfirstrow)
  1304. if !table.Brule || (!bkeyfirstcol && !bkeyfirstrow) {
  1305. //断行问题,虽然同列或同行,但中间被跨行截断,表格方向调整
  1306. for _, k := range table.StartAndEndRationKSort.Keys {
  1307. v := table.StartAndEndRation[k]
  1308. //横向判断,要判断最多的方向,否则会出现不定的情况(map遍历问题)
  1309. k1 := k[:1]
  1310. for _, v2 := range v.Poss {
  1311. lentds := len(v.Tdmap[v2])
  1312. if v.Rationmap[v2] > checkval {
  1313. for _, td := range v.Tdmap[v2] {
  1314. if td.KeyDirect == 0 && !MoneyReg.MatchString(td.Val) && !GSReg.MatchString(td.Val) {
  1315. if k1 == "r" {
  1316. ck := fmtkey("c", td.StartCol, td.EndCol)
  1317. rt := table.StartAndEndRation[ck]
  1318. //clen := 0
  1319. var fv float32
  1320. var tdn []*TD
  1321. if rt != nil {
  1322. fv, tdn = rt.GetTDRation(td)
  1323. //clen = len(tdn)
  1324. }
  1325. if lentds > 1 {
  1326. if ((tdn != nil && v.Rationmap[v2] > fv) || tdn == nil) && td.Valtype != "BO" {
  1327. td.KeyDirect = 1
  1328. td.KVDirect = 2
  1329. //td.BH = true
  1330. }
  1331. }
  1332. } else {
  1333. ck := fmtkey("r", td.StartRow, td.EndRow)
  1334. rt := table.StartAndEndRation[ck]
  1335. var fv float32
  1336. var tdn []*TD
  1337. //clen := 0
  1338. if rt != nil {
  1339. fv, tdn = rt.GetTDRation(td)
  1340. //clen = len(tdn)
  1341. }
  1342. if lentds > 1 {
  1343. if td.Valtype != "NOHEAD" && utf8.RuneCountInString(td.Val) < 15 && ((tdn != nil && v.Rationmap[v2] > fv) || tdn == nil) && td.Valtype != "BO" {
  1344. td.KeyDirect = 2
  1345. td.KVDirect = 1
  1346. td.BH = true
  1347. }
  1348. }
  1349. }
  1350. } else {
  1351. break
  1352. }
  1353. }
  1354. } else if v.Rationmap[v2] < 0.5 && len(v.Tdmap[v2]) > 3 {
  1355. for _, td := range v.Tdmap[v2] {
  1356. // u.Debug(td.Val, "-----", td.BH)
  1357. if td.KeyDirect == 0 && td.BH && !td.MustBH {
  1358. if k1 == "r" {
  1359. ck := fmtkey("c", td.StartCol, td.EndCol)
  1360. rt := table.StartAndEndRation[ck]
  1361. clen := 0
  1362. var fv float32
  1363. var tdn []*TD
  1364. if rt != nil {
  1365. fv, tdn = rt.GetTDRation(td)
  1366. clen = len(tdn)
  1367. }
  1368. if lentds >= clen && lentds > 1 {
  1369. if (tdn != nil && v.Rationmap[v2] < fv) || tdn == nil {
  1370. td.BH = false
  1371. }
  1372. }
  1373. } else {
  1374. ck := fmtkey("r", td.StartRow, td.EndRow)
  1375. rt := table.StartAndEndRation[ck]
  1376. var fv float32
  1377. var tdn []*TD
  1378. clen := 0
  1379. if rt != nil {
  1380. fv, tdn = rt.GetTDRation(td)
  1381. clen = len(tdn)
  1382. }
  1383. if lentds >= clen && lentds > 1 {
  1384. if (tdn != nil && v.Rationmap[v2] < fv) || tdn == nil {
  1385. td.BH = false
  1386. }
  1387. }
  1388. }
  1389. } else {
  1390. break
  1391. }
  1392. }
  1393. }
  1394. }
  1395. }
  1396. }
  1397. table.GetKeyRation()
  1398. if len(table.TRs) > 0 && len(table.TRs[0].TDs) > 0 {
  1399. t0 := table.TRs[0].TDs[0]
  1400. key := fmtkey("r", t0.StartRow, t0.EndRow)
  1401. r, t := table.StartAndEndRation[key].GetTDRation(t0)
  1402. if r > 0.9 && len(t) > 1 {
  1403. table.BFirstRow = true
  1404. }
  1405. for k, tr := range table.TRs {
  1406. if len(tr.TDs) == 1 && tr.TDs[0].StartCol == 0 && tr.TDs[0].EndCol+1 == table.ColNum {
  1407. tr.TDs[0].BH = false
  1408. tr.TDs[0].KVDirect = 0
  1409. sv := FindKv(tr.TDs[0].Val, "", 2)
  1410. _, resm := colonkvEntity.entrance(tr.TDs[0].Val, "", nil, 2, isSite, codeSite)
  1411. for k, v := range resm {
  1412. sv.AddKey(k, v)
  1413. }
  1414. if len(sv.Keys) > 0 {
  1415. for _, v1 := range sv.Keys {
  1416. if tr.TDs[0].SortKV.Map[v1] == nil {
  1417. table.SortKV.AddKey(v1, sv.Map[v1])
  1418. }
  1419. }
  1420. } else if table.Tag == "" && k == 0 && len(tr.TDs[0].Val) > 11 {
  1421. table.Tag = tr.TDs[0].Val
  1422. }
  1423. }
  1424. }
  1425. }
  1426. }
  1427. //查找表格的kv,调用FindTdVal
  1428. func (table *Table) FindKV(isSite bool, codeSite string) {
  1429. //判断全是key的表格不再查找
  1430. if table.BHeader { //只要一个是key即为true
  1431. direct := If(table.BFirstRow, 2, 1).(int) //kv,2查找方向,向上查找
  1432. vdirect := If(direct == 2, 1, 2).(int)
  1433. //控制跨行表格
  1434. bcon := false
  1435. //增加表格切块判断,只判断切块分包
  1436. //控制中标人排序方向
  1437. //bodirect := 0
  1438. //控制中标人排序数值
  1439. //sort := 1
  1440. nextdirect, nextvdirect := 0, 0
  1441. //开始抽取
  1442. for _, tr := range table.TRs {
  1443. bcon = trSingleColumn(tr, bcon, table) //tr单列,是否丢弃内容
  1444. if bcon {
  1445. continue
  1446. }
  1447. if tr.TDs[0].StartRow >= 0 {
  1448. numbh := 0
  1449. for _, td := range tr.TDs {
  1450. if td.BH {
  1451. numbh++
  1452. }
  1453. }
  1454. if numbh != 0 && numbh == len(tr.TDs) { //5e0d53ef0cf41612e0640495
  1455. nextdirect, nextvdirect = 2, 1
  1456. continue
  1457. } else if nextdirect > 0 && nextvdirect > 0 {
  1458. direct, vdirect = 2, 1
  1459. } else if numbh > 0 && numbh <= len(tr.TDs)/2 {
  1460. direct, vdirect = 1, 2
  1461. } else {
  1462. direct, vdirect = 2, 1
  1463. }
  1464. }
  1465. for _, td := range tr.TDs {
  1466. if !td.BH && td.KVDirect < 3 {
  1467. if !table.FindTdVal(td, direct, vdirect) { //table.FindTdVal()存储了table.SortKV
  1468. if !table.FindTdVal(td, vdirect, direct) {
  1469. ////都识别不到时,对第一、二中标候选人的处理
  1470. //bo, res := GetBidOrder(td, bodirect, sort)
  1471. //if res {
  1472. // sort++
  1473. // bodirect = bo
  1474. //}
  1475. //if len(td.SortKV.Map) > 0 {
  1476. // for _, tdv := range td.SortKV.Keys {
  1477. // if tdv == "" || td.SortKV.Map[tdv] == "" { //value为空或者null不再添加到table.SortKV
  1478. // continue
  1479. // }
  1480. // table.SortKV.AddKey(tdv, td.SortKV.Map[tdv])
  1481. // }
  1482. //}
  1483. }
  1484. }
  1485. //fmt.Println("td:", td.Val, td.BH, td.HeadTd, td.KVDirect)
  1486. }
  1487. }
  1488. nextdirect, nextvdirect = 0, 0
  1489. }
  1490. //qutil.Debug("FindKV", table.SortKV.Map)
  1491. } else if len(table.TRs) > 0 { //没有表头的表格处理,默认纵向吧
  1492. res := initLongitudinalData(table) //拼装纵向数组
  1493. //再拆值,类似http://www.ggzy.hi.gov.cn/cgzbgg/16553.jhtml第二列,有多个值
  1494. nmapkeys := []int{}
  1495. nmap := map[int][]*u.Kv{}
  1496. L:
  1497. for _, r1 := range res {
  1498. for n, r := range r1 {
  1499. if len([]rune(r)) < 60 { // 长度小于60才去分
  1500. //res1, _ := GetKVAll(r, "", nil)
  1501. res1, _ := colonkvEntity.entrance(r, "", nil, 2, isSite, codeSite)
  1502. if res1 != nil {
  1503. nmap[n] = res1
  1504. nmapkeys = append(nmapkeys, n)
  1505. /**
  1506. //截取串
  1507. for _k1, _ := range res1 {
  1508. r = regexp.MustCompile(_k1+".*").ReplaceAllString(r, "")
  1509. }
  1510. r1[n] = r
  1511. res[pos] = r1
  1512. **/
  1513. } else if nmap[n] != nil {
  1514. //放空值
  1515. nmap[n] = append(nmap[n], &u.Kv{})
  1516. }
  1517. } else {
  1518. nmap = nil
  1519. nmapkeys = nil
  1520. break L
  1521. }
  1522. }
  1523. }
  1524. //调整
  1525. if len(nmap) > 0 {
  1526. kmapkeys := []string{}
  1527. kmap := map[string][]string{}
  1528. for _, mk := range nmapkeys { //同是第n列
  1529. for pos, m1 := range nmap[mk] {
  1530. k, v := m1.Key, m1.Value
  1531. kv := kmap[k]
  1532. if kv == nil {
  1533. kv = []string{}
  1534. }
  1535. kv = append(kv, v)
  1536. kmap[k] = kv
  1537. kmapkeys = append(kmapkeys, k)
  1538. for _, k := range kmapkeys {
  1539. arr := kmap[k]
  1540. if len(arr) < pos {
  1541. arr = append(arr, "")
  1542. kmap[k] = arr
  1543. kmapkeys = append(kmapkeys, k)
  1544. }
  1545. }
  1546. }
  1547. }
  1548. if len(kmap) > 0 {
  1549. for _, k := range kmapkeys {
  1550. if len(kmap[k]) == 1 {
  1551. table.SortKV.AddKey(k, kmap[k][0])
  1552. } else if len(kmap[k]) > 1 {
  1553. table.SortKV.AddKey(k, kmap[k])
  1554. }
  1555. }
  1556. }
  1557. }
  1558. //=================
  1559. //解析值放到map中
  1560. for _, arr := range res {
  1561. if len(arr) > 0 {
  1562. v1 := arr[0]
  1563. _, _, _, _, repl := CheckCommon(v1, "con")
  1564. if repl == "ENT" {
  1565. table.SortKV.AddKey("中标单位", arr)
  1566. continue
  1567. } else if repl == "BO" {
  1568. table.SortKV.AddKey("排名", arr)
  1569. continue
  1570. }
  1571. }
  1572. }
  1573. }
  1574. //qutil.Debug("Table-FindKV", table.SortKV.Map)
  1575. }
  1576. //初始化组装纵向数据
  1577. func initLongitudinalData(table *Table) [][]string {
  1578. res := make([][]string, len(table.TRs[0].TDs)) //创建table第一行的列数长度
  1579. for n, _ := range res {
  1580. res[n] = []string{}
  1581. }
  1582. for _, tr := range table.TRs {
  1583. for n, td := range table.TRs[0].TDs { //第一行的所有td
  1584. td1 := table.GetTdByRCNo(tr.TDs[0].StartRow, td.StartCol) //根据行号列号获取td对象
  1585. if td1 != nil {
  1586. res[n] = append(res[n], td1.Val)
  1587. } else {
  1588. res[n] = append(res[n], "")
  1589. }
  1590. }
  1591. }
  1592. return res
  1593. }
  1594. //tr单列,是否丢弃内容
  1595. func trSingleColumn(tr *TR, bcon bool, table *Table) bool {
  1596. if len(tr.TDs) == 1 {
  1597. bcon = false
  1598. td := tr.TDs[0]
  1599. if td.StartCol == 0 && td.EndCol+1 == table.ColNum && len([]rune(td.Val)) > 4 && len([]rune(td.Val)) < 50 {
  1600. res, _, _, _, _ := CheckCommon(td.Val, "abandontable")
  1601. if res { //以下内容丢弃
  1602. bcon = true
  1603. }
  1604. }
  1605. }
  1606. return bcon
  1607. }
  1608. //获取中标人顺序
  1609. //direct 0默认 1横向 2纵向
  1610. func GetBidOrder(td *TD, direct, n int) (d int, res bool) {
  1611. if td.Valtype != "BO" {
  1612. return
  1613. }
  1614. if td.Rowspan > 1 {
  1615. for i := 0; i < td.Rowspan; i++ {
  1616. nextcol := 1
  1617. L1:
  1618. for {
  1619. vtd := td.TR.Table.GetTdByRCNo(td.StartRow+i, td.EndCol+nextcol)
  1620. if vtd == nil {
  1621. break L1
  1622. }
  1623. nextcol += vtd.Colspan
  1624. if filter_zbdw_v2.MatchString(vtd.Val) {
  1625. arrbo := td.TR.Table.SortKV.Map[NullTxtBid]
  1626. if arrbo == nil {
  1627. arrbo = []map[string]interface{}{}
  1628. td.TR.Table.SortKV.AddKey(NullTxtBid, arrbo)
  1629. }
  1630. a1 := arrbo.([]map[string]interface{})
  1631. a1 = append(a1, map[string]interface{}{
  1632. "entname": vtd.Val,
  1633. "sortstr": td.Val,
  1634. "sort": GetBidSort(td.Val, n),
  1635. })
  1636. res = true
  1637. td.TR.Table.SortKV.AddKey(NullTxtBid, a1)
  1638. }
  1639. }
  1640. }
  1641. } else if td.Colspan > 1 {
  1642. for i := 1; i < td.Colspan; i++ {
  1643. nextcol := 0
  1644. L2:
  1645. for {
  1646. vtd := td.TR.Table.GetTdByRCNo(td.StartRow+i, td.StartCol+nextcol)
  1647. if vtd == nil || vtd.Colspan >= td.Colspan {
  1648. break L2
  1649. }
  1650. nextcol += vtd.Colspan
  1651. if filter_zbdw_v2.MatchString(vtd.Val) {
  1652. arrbo := td.TR.Table.SortKV.Map[NullTxtBid]
  1653. if arrbo == nil {
  1654. arrbo = []map[string]interface{}{}
  1655. td.TR.Table.SortKV.AddKey(NullTxtBid, arrbo)
  1656. }
  1657. a1 := arrbo.([]map[string]interface{})
  1658. a1 = append(a1, map[string]interface{}{
  1659. "entname": vtd.Val,
  1660. "sortstr": td.Val,
  1661. "sort": GetBidSort(td.Val, n),
  1662. })
  1663. res = true
  1664. td.TR.Table.SortKV.AddKey(NullTxtBid, a1)
  1665. }
  1666. }
  1667. }
  1668. } else {
  1669. rtd := td.TR.Table.GetTdByRCNo(td.StartRow, td.EndCol+1)
  1670. btd := td.TR.Table.GetTdByRCNo(td.EndRow+1, td.StartCol)
  1671. //if ((rtd != nil && !rtd.BH && rtd.Valtype == "BO") || direct == 1) && btd != nil && filter_zbdw_v.MatchString(btd.Val) {
  1672. if ((rtd != nil && !rtd.BH) || direct == 1) && btd != nil && filter_zbdw_v2.MatchString(btd.Val) {
  1673. d = 1
  1674. arrbo := td.TR.Table.SortKV.Map[NullTxtBid]
  1675. if arrbo == nil {
  1676. arrbo = []map[string]interface{}{}
  1677. td.TR.Table.SortKV.AddKey(NullTxtBid, arrbo)
  1678. }
  1679. a1 := arrbo.([]map[string]interface{})
  1680. a1 = append(a1, map[string]interface{}{
  1681. "entname": btd.Val,
  1682. "sortstr": td.Val,
  1683. "sort": GetBidSort(td.Val, n),
  1684. })
  1685. res = true
  1686. td.TR.Table.SortKV.AddKey(NullTxtBid, a1)
  1687. //} else if ((btd != nil && !btd.BH && btd.Valtype == "BO") || direct == 2) && rtd != nil && filter_zbdw_v.MatchString(rtd.Val) {
  1688. } else if ((btd != nil && !btd.BH) || direct == 2) && rtd != nil && filter_zbdw_v2.MatchString(rtd.Val) {
  1689. d = 2
  1690. arrbo := td.TR.Table.SortKV.Map[NullTxtBid]
  1691. if arrbo == nil {
  1692. arrbo = []map[string]interface{}{}
  1693. td.TR.Table.SortKV.AddKey(NullTxtBid, arrbo)
  1694. }
  1695. a1 := arrbo.([]map[string]interface{})
  1696. a1 = append(a1, map[string]interface{}{
  1697. "entname": rtd.Val,
  1698. "sortstr": td.Val,
  1699. "sort": GetBidSort(td.Val, n),
  1700. })
  1701. res = true
  1702. td.TR.Table.SortKV.AddKey(NullTxtBid, a1)
  1703. }
  1704. }
  1705. return
  1706. }
  1707. func GetBidSort(str string, n int) int {
  1708. val := n
  1709. if strings.Index(str, "首选") > -1 {
  1710. val = 1
  1711. } else {
  1712. val = winnerOrderEntity.toNumber(str, n)
  1713. }
  1714. return val
  1715. }
  1716. var cleardwReg *regexp.Regexp = regexp.MustCompile("[((]{1}\\d*[人元件个公斤户]/[人元件个公斤户][))]")
  1717. var zbhxrReg *regexp.Regexp = regexp.MustCompile("(中标候选人|投标单位名称)")
  1718. //查找每一个单元格的表头,调用FindNear
  1719. func (table *Table) FindTdVal(td *TD, direct, vdirect int) (b bool) {
  1720. if td.Val == "" || strings.TrimSpace(td.Val) == "" {
  1721. return
  1722. }
  1723. near := table.FindNear(td, direct)
  1724. if near != nil && near.BH && (near.KeyDirect == vdirect || near.KeyDirect == 0) && (near.KVDirect == direct || near.KVDirect == 0) && near.KVDirect < 3 {
  1725. near.KVDirect = direct
  1726. near.KeyDirect = vdirect
  1727. td.KVDirect = direct
  1728. key := repSpace.ReplaceAllString(near.Val, "")
  1729. if key == "名称" && near.StartCol == 0 && near.Rowspan > 0 {
  1730. for _, vn := range table.TRs[near.Rowspan-1].TDs {
  1731. if strings.Contains(vn.Val, "代理") {
  1732. key = "代理机构"
  1733. break
  1734. } else if strings.Contains(vn.Val, "招标") {
  1735. key = "采购单位"
  1736. break
  1737. } else if strings.Contains(vn.Val, "中标") {
  1738. key = "中标单位"
  1739. break
  1740. }
  1741. }
  1742. } else if zbhxrReg.MatchString(key) && findCandidate2.MatchString(td.Val) {
  1743. key = "中标单位"
  1744. } else if key == "单位名称" {
  1745. tmpnewnear := table.FindNear(near, 2)
  1746. if tmpnewnear != nil {
  1747. if tmpnewnear.MustBH || tmpnewnear.BH {
  1748. key = tmpnewnear.Val + near.Val
  1749. }
  1750. } else {
  1751. tmpnewnear = table.FindNear(near, 1)
  1752. if tmpnewnear != nil {
  1753. if tmpnewnear.MustBH || tmpnewnear.BH {
  1754. key = tmpnewnear.Val + near.Val
  1755. }
  1756. }
  1757. }
  1758. }
  1759. if near.Val == "" {
  1760. key = fmtkey("k", near.TR.RowPos, near.ColPos)
  1761. }
  1762. val := table.SortKV.Map[key]
  1763. //qutil.Debug("====================", "key:", key, "val:", val)
  1764. bthiskey := false
  1765. if val != nil {
  1766. curpos := table.SortKV.Index[key]
  1767. thistr := table.kTD[curpos]
  1768. if thistr != near {
  1769. if strings.TrimSpace(near.Val) == "名称" && near.TR != nil && len(near.TR.TDs) > 0 && near.ColPos-1 >= 0 {
  1770. rv := near.TR.TDs[near.ColPos-1].Val
  1771. if near.ColPos > 0 && (strings.Contains(rv, "招标") || strings.Contains(rv, "代理") || strings.Contains(rv, "采购") || strings.Contains(rv, "中标")) {
  1772. near = near.TR.TDs[near.ColPos-1]
  1773. }
  1774. } else {
  1775. bthiskey = true
  1776. }
  1777. } else {
  1778. bthiskey = true
  1779. }
  1780. }
  1781. bfind := false
  1782. barr := false
  1783. varrpos := -1
  1784. if bthiskey {
  1785. //处理是数组值,且有合并行或合并列的情况 kvscope,对数组值的处理
  1786. pos := table.SortKV.Index[key]
  1787. mval := table.kvscope[pos]
  1788. bvalfind := false
  1789. if direct == 1 { //kv是横向
  1790. L1:
  1791. for k3, v3 := range mval {
  1792. for _, v4 := range v3 {
  1793. if v4.EndRow+1 == td.StartRow && v4.EndCol == td.EndCol {
  1794. varrpos = k3
  1795. bvalfind = true
  1796. break L1
  1797. }
  1798. }
  1799. }
  1800. } else { //kv是纵向
  1801. L2:
  1802. for k3, v3 := range mval {
  1803. for _, v4 := range v3 {
  1804. if v4.EndCol+1 == td.StartCol && v4.EndRow == td.EndRow {
  1805. varrpos = k3
  1806. bvalfind = true
  1807. break L2
  1808. }
  1809. }
  1810. }
  1811. }
  1812. if vals, ok := val.([]string); ok {
  1813. if near.Val == "" {
  1814. bn := false
  1815. for _, vs := range vals {
  1816. if vs != "" && NullTdReg.MatchString(vs) {
  1817. bn = true
  1818. } else {
  1819. bn = false
  1820. break
  1821. }
  1822. }
  1823. if bn {
  1824. near.Val = NullTxtBid
  1825. key = NullTxtBid
  1826. bfind = true
  1827. }
  1828. }
  1829. if bvalfind && varrpos > -1 && len(vals) > varrpos {
  1830. tmapval := strings.TrimSpace(cleardwReg.ReplaceAllString(td.Val, ""))
  1831. if tmapval == "" {
  1832. vals = append(vals, td.Val) // 累加
  1833. } else {
  1834. vals = append(vals, tmapval) // 累加
  1835. }
  1836. val = vals
  1837. //vals[varrpos] = td.Val // += "__" + td.Val
  1838. } else {
  1839. //添加时候去除空值和nil
  1840. newVals := []string{}
  1841. for _, isval := range vals {
  1842. if isval == "" {
  1843. continue
  1844. }
  1845. newVals = append(newVals, isval)
  1846. }
  1847. //vals = append(vals, td.Val)
  1848. if td.Val != "" {
  1849. newVals = append(newVals, td.Val)
  1850. }
  1851. val = newVals
  1852. varrpos = len(vals) - 1
  1853. }
  1854. } else if vals, ok := val.(string); ok && vals != "" && td.Val != "" {
  1855. tmapval := strings.TrimSpace(cleardwReg.ReplaceAllString(vals, ""))
  1856. tmapvaltd := strings.TrimSpace(cleardwReg.ReplaceAllString(td.Val, ""))
  1857. if bvalfind {
  1858. if tmapvaltd == "" {
  1859. val = td.Val //vals + "__" + td.Val
  1860. } else {
  1861. val = tmapvaltd
  1862. }
  1863. } else {
  1864. tval := []string{}
  1865. if tmapval == "" {
  1866. tval = append(tval, vals)
  1867. } else {
  1868. tval = append(tval, tmapval)
  1869. }
  1870. if tmapvaltd == "" {
  1871. tval = append(tval, td.Val)
  1872. } else {
  1873. tval = append(tval, tmapvaltd)
  1874. }
  1875. val = tval
  1876. varrpos = 1
  1877. }
  1878. }
  1879. barr = true
  1880. } else {
  1881. if td.Val != "" {
  1882. tmapval := strings.TrimSpace(cleardwReg.ReplaceAllString(td.Val, ""))
  1883. if tmapval == "" {
  1884. val = td.Val
  1885. } else {
  1886. val = tmapval
  1887. }
  1888. } else if len(near.SortKV.Map) == 1 && near.SortKV.Map[near.Val] != "" {
  1889. val = near.SortKV.Map[near.Val]
  1890. }
  1891. }
  1892. td.HeadTd = near
  1893. if bfind {
  1894. tkey := fmtkey("k", near.TR.RowPos, near.ColPos)
  1895. table.SortKV.ReplaceKey(key, val, tkey)
  1896. } else {
  1897. if key == "单位名称" && len(near.TR.TDs) > 1 {
  1898. if near.TR.TDs[0].Val != "序号" {
  1899. key = near.TR.TDs[0].Val
  1900. }
  1901. }
  1902. table.SortKV.AddKey(key, val)
  1903. pos := table.SortKV.Index[key]
  1904. if barr {
  1905. mval := table.kvscope[pos]
  1906. if mval != nil {
  1907. tds := mval[varrpos]
  1908. if tds != nil {
  1909. tds = append(tds, td)
  1910. } else {
  1911. tds = []*TD{td}
  1912. }
  1913. if varrpos > -1 {
  1914. mval[varrpos] = tds
  1915. table.kvscope[pos] = mval
  1916. }
  1917. }
  1918. } else {
  1919. table.kvscope[pos] = map[int][]*TD{
  1920. 0: []*TD{td},
  1921. }
  1922. table.kTD[pos] = near
  1923. }
  1924. }
  1925. b = true
  1926. }
  1927. return
  1928. }
  1929. //查找单元格的表头时,横向或纵向
  1930. func (table *Table) FindNear(td *TD, direct int) *TD {
  1931. if direct == 1 && td.StartCol > 0 { //左临
  1932. tr := table.TRs[:td.TR.RowPos+1]
  1933. for i := len(tr) - 1; i > -1; i-- {
  1934. tds := tr[i].TDs
  1935. for _, td1 := range tds {
  1936. if td1.StartRow <= td.StartRow && td1.EndRow >= td.EndRow && td1.EndCol+1 == td.StartCol {
  1937. //找到左临节点
  1938. if td1.BH {
  1939. return td1
  1940. } else {
  1941. if td1.HeadTd != nil && td1.HeadTd.KVDirect == direct {
  1942. return td1.HeadTd
  1943. }
  1944. }
  1945. }
  1946. }
  1947. }
  1948. } else if direct == 2 && td.StartRow > 0 { //上临
  1949. tr := table.TRs[:td.TR.RowPos]
  1950. for i := len(tr) - 1; i > -1; i-- {
  1951. tds := tr[i].TDs
  1952. for it, td1 := range tds {
  1953. if td1.StartCol <= td.StartCol && td1.EndCol >= td.EndCol && td1.EndRow+1 == td.StartRow {
  1954. //找到左临节点
  1955. if td1.BH {
  1956. return td1
  1957. } else if len(tr[i].TDs) == len(td.TR.TDs) && td1.HeadTd != nil && td1.HeadTd.KVDirect == direct {
  1958. return td1.HeadTd
  1959. } else if it > 0 && td1.Val == "" && td1.TR.TopTR == nil && len(td.TR.TDs)-(td.StartCol-1) > 0 && strings.Contains(td.TR.TDs[td.StartCol-1].Val, "中标候选人") {
  1960. return tds[it-1]
  1961. } else if td1.HeadTd != nil && td1.HeadTd.KVDirect == direct && td.Colspan == td1.Colspan && td.Rowspan == td.Rowspan {
  1962. return td1.HeadTd
  1963. }
  1964. } else if td1.StartCol <= td.StartCol && td1.EndCol >= td.EndCol && td1.EndRow+1 == td.StartRow {
  1965. }
  1966. }
  1967. }
  1968. }
  1969. return nil
  1970. }
  1971. //根据行号列号获取td对象
  1972. func (tn *Table) GetTdByRCNo(row, col int) *TD {
  1973. for _, tr := range tn.TRs {
  1974. for _, td := range tr.TDs {
  1975. if td.StartCol <= col && td.EndCol >= col && td.StartRow <= row && td.EndRow >= row {
  1976. return td
  1977. }
  1978. }
  1979. }
  1980. return nil
  1981. }
  1982. //判断表格是否是分包
  1983. func (tn *Table) CheckMultiPackageByTable(isSite bool, codeSite string) (b bool, index []string) {
  1984. pac := 0 //包的数量
  1985. val := 0 //分值
  1986. index = []string{} //存储分包,使用tbale.SortKV的key和value使用正则等处理对值进行判断
  1987. index_pos := []int{} //下标
  1988. //是数组且能找到标段之类的提示
  1989. //arr_count := 0 //计数table.SortKV的value是数组的数量,后面没用
  1990. key_index := -1
  1991. hasPkgTd := map[string]bool{}
  1992. //初始化CheckMultiPackageByTable方法需要的数据
  1993. key_index, index, index_pos, val, pac, hasPkgTd = initCheckMultiPackageByTable(tn, key_index, index, index_pos, val, pac, hasPkgTd)
  1994. //key是分包的情况
  1995. //记录key对应的值
  1996. commonKeyVals := map[string][]string{}
  1997. //记录key出现的次数
  1998. keyExistsCount := map[string]int{}
  1999. if pac > 1 {
  2000. val = 10
  2001. } else {
  2002. //查找标签
  2003. if TableMultiPackageReg_4.MatchString(tn.Tag) {
  2004. val += 4
  2005. } else if TableMultiPackageReg_2.MatchString(tn.Tag) {
  2006. val += 4
  2007. }
  2008. //根据table.SortKV的key判断是否分包,如果没有再根据value判断
  2009. val, index, index_pos = foundPacBySortKV(tn, val, index, index_pos, &keyExistsCount, &commonKeyVals, key_index, hasPkgTd)
  2010. }
  2011. // u.Debug(index)
  2012. //过滤重复及标准化!
  2013. standIndex := []string{}
  2014. standIndex_pos := []int{}
  2015. oldIndex := []string{} //存放包的原始值
  2016. brepeat := map[string]bool{}
  2017. for k, v := range index {
  2018. v = u.PackageNumberConvert(v)
  2019. if !brepeat[v] {
  2020. brepeat[v] = true
  2021. standIndex = append(standIndex, v)
  2022. standIndex_pos = append(standIndex_pos, index_pos[k])
  2023. oldIndex = append(oldIndex, index[k])
  2024. }
  2025. }
  2026. index = standIndex
  2027. //有一个以上的包,并且相同的key出现一次以上,认为这个key是属于包里面的
  2028. if len(commonKeyVals) > 0 {
  2029. for k, v := range commonKeyVals {
  2030. if len(index) > 1 && keyExistsCount[k] < 2 {
  2031. continue
  2032. }
  2033. tn.SortKV.AddKey(k, v)
  2034. }
  2035. }
  2036. //
  2037. isGoonNext := false
  2038. if val > 4 && len(brepeat) > 0 {
  2039. b = true
  2040. //多包解析
  2041. if b {
  2042. tn.BPackage = true
  2043. //pnum := len(index)
  2044. //根据数组index分包长度添加table.BlockPackage子包数组
  2045. for nk, v := range index {
  2046. if tn.BlockPackage.Map[v] == nil {
  2047. kv := u.NewJobKv()
  2048. for tnk, tnv := range tn.StandKV {
  2049. if nk >= len(tnv) {
  2050. continue
  2051. } else if len(index) == len(tnv) {
  2052. kv.KvTags[tnk] = append(kv.KvTags[tnk], tnv[nk])
  2053. }
  2054. }
  2055. //kv.KvTags = tn.StandKV
  2056. bp := &u.BlockPackage{}
  2057. bp.Index = v //序号 (转换后编号,只有数字或字母)
  2058. bp.Origin = oldIndex[nk] //包的原始值
  2059. bp.TableKV = kv //table kv (分出的对应的KV值)
  2060. bp.Name = v
  2061. if bp.TableKV != nil && bp.TableKV.KvTags != nil && len(bp.TableKV.KvTags) > 0 {
  2062. for kc, cv := range bp.TableKV.KvTags {
  2063. if kc == "预算" && bp.Budget <= 0 {
  2064. moneys := clear.ObjToMoney([]interface{}{cv[0].Value, ""})
  2065. if len(moneys) > 0 {
  2066. if vf, ok := moneys[0].(float64); ok {
  2067. bp.Budget = vf
  2068. bp.IsTrueBudget = moneys[len(moneys)-1].(bool)
  2069. } else if vi, ok := moneys[0].(int); ok {
  2070. bp.Budget = float64(vi)
  2071. bp.IsTrueBudget = moneys[len(moneys)-1].(bool)
  2072. }
  2073. }
  2074. } else if kc == "中标金额" && bp.Bidamount <= 0 {
  2075. moneys := clear.ObjToMoney([]interface{}{cv[0].Value, ""})
  2076. if len(moneys) > 0 {
  2077. if vf, ok := moneys[0].(float64); ok {
  2078. bp.Bidamount = vf
  2079. bp.IsTrueBidamount = moneys[len(moneys)-1].(bool)
  2080. } else if vi, ok := moneys[0].(int); ok {
  2081. bp.Bidamount = float64(vi)
  2082. bp.IsTrueBidamount = moneys[len(moneys)-1].(bool)
  2083. }
  2084. }
  2085. } else if kc == "中标单位" && bp.Winner == "" {
  2086. bp.Winner = cv[0].Value
  2087. }
  2088. //拼接内容
  2089. if !excludeKey.MatchString(kc) {
  2090. bp.Text += fmt.Sprintf("%v:%v\n", kc, cv[0].Value)
  2091. }
  2092. }
  2093. }
  2094. tn.BlockPackage.AddKey(v, bp) //table子包数组
  2095. }
  2096. }
  2097. isGoonNext = tn.manyPackageProcessByIndex(index, standIndex_pos, isSite, codeSite) //多包处理,处理不同情况下的分包
  2098. }
  2099. } else {
  2100. isGoonNext = true
  2101. }
  2102. if isGoonNext { //没有处理成数组的情况下,继续调用正文查找分包的方法
  2103. tn.isGoonNext(isSite, codeSite)
  2104. }
  2105. //查找分包中的中标人排序
  2106. if tn.BlockPackage != nil && tn.BlockPackage.Keys != nil && len(tn.BlockPackage.Keys) > 0 {
  2107. for _, v := range tn.BlockPackage.Keys {
  2108. vv, ok := tn.BlockPackage.Map[v].(*u.BlockPackage)
  2109. if ok && (vv.WinnerOrder == nil || len(vv.WinnerOrder) == 0) {
  2110. vv.WinnerOrder = winnerOrderEntity.Find(vv.Text, true, 2, isSite, codeSite)
  2111. }
  2112. }
  2113. }
  2114. return
  2115. }
  2116. //多包处理,处理不同情况下的分包
  2117. func (tn *Table) manyPackageProcessByIndex(index []string, standIndex_pos []int, isSite bool, codeSite string) (isGoonNext bool) {
  2118. if len(index) == 1 { //是一个的情况
  2119. if len(tn.SortKV.Keys) < 10 && tn.ColNum < 10 && tn.RowNum < 4 { //table带排序的KV值小于10并且小于10列和小于4行
  2120. beq := true
  2121. for _, v2 := range tn.SortKV.Keys {
  2122. if _, ok := tn.SortKV.Map[v2].(string); !ok {
  2123. beq = false
  2124. break
  2125. }
  2126. }
  2127. if beq { //统一处理为数组
  2128. td := tn.GetTdByRCNo(tn.RowNum-1, 0)
  2129. if !td.BH && FindVal2_1.MatchString(td.Val) {
  2130. for _, v2 := range tn.SortKV.Keys {
  2131. tn.SortKV.AddKey(v2, []string{tn.SortKV.Map[v2].(string)})
  2132. }
  2133. } else {
  2134. //没有处理成数组的情况下,继续调用正文查找分包的方法
  2135. isGoonNext = true
  2136. }
  2137. }
  2138. }
  2139. }
  2140. for _, k1 := range tn.SortKV.Keys {
  2141. v1 := tn.SortKV.Map[k1]
  2142. var v1_arr []string
  2143. if vtmpv1, ok := v1.(string); ok {
  2144. v1_arr = PreCon4.FindAllString(qutil.ObjToString(vtmpv1), -1)
  2145. if len(v1_arr) > 0 {
  2146. if dw := Precon4dw.FindString(vtmpv1); dw != "" {
  2147. for i, v := range v1_arr {
  2148. v1_arr[i] = v + dw
  2149. }
  2150. }
  2151. }
  2152. } else if vtmpv1s, ok := v1.([]string); ok {
  2153. v1_arr = vtmpv1s
  2154. }
  2155. if len(v1_arr) > 0 && len(v1_arr) <= len(index) { //table.SortKV.Map.value数组小于等于分包index
  2156. for k, v := range v1_arr {
  2157. tn.assemblePackage(k1, v, index[k], isSite, codeSite) //组装解析到的分包
  2158. }
  2159. }
  2160. }
  2161. return isGoonNext
  2162. }
  2163. //没有处理成数组的情况下,继续调用正文查找分包的方法
  2164. func (tn *Table) isGoonNext(isSite bool, codeSite string) {
  2165. blockPackage := map[string]*u.BlockPackage{}
  2166. for _, k := range tn.SortKV.Keys {
  2167. if excludeKey.MatchString(k) || strings.Contains(k, "批复") || excludeKey3.MatchString(k) {
  2168. continue
  2169. }
  2170. str := "" //拼装为冒号kv
  2171. v := tn.SortKV.Map[k]
  2172. nk := regReplAllSpace.ReplaceAllString(k, "")
  2173. if vs, ok := v.([]string); ok {
  2174. str += fmt.Sprintf("%s:%s\n", nk, strings.Join(vs, " "))
  2175. } else {
  2176. str += fmt.Sprintf("%s:%s\n", nk, v)
  2177. }
  2178. if excludeKey2.MatchString(str) {
  2179. continue
  2180. }
  2181. b, _ := divisionPackageChild(&blockPackage, str, tn.Tag, false, false, isSite, codeSite) //分块之后分包
  2182. if b && len(blockPackage) > 0 {
  2183. tn.BPackage = true
  2184. for mk, mv := range blockPackage {
  2185. if tn.BlockPackage.Map[mk] == nil {
  2186. tn.BlockPackage.AddKey(mk, mv)
  2187. } else {
  2188. bp := tn.BlockPackage.Map[mk].(*u.BlockPackage)
  2189. if bp.TableKV == nil {
  2190. bp.TableKV = u.NewJobKv()
  2191. }
  2192. if bp.SpaceKV == nil {
  2193. bp.SpaceKV = u.NewJobKv()
  2194. }
  2195. for k2, v2 := range mv.ColonKV.KvTags {
  2196. for _, v2v := range v2 {
  2197. isExists := false
  2198. for _, v2vv := range bp.TableKV.KvTags[k2] {
  2199. if v2v.Value == v2vv.Value {
  2200. isExists = true
  2201. break
  2202. }
  2203. }
  2204. if !isExists {
  2205. bp.TableKV.KvTags[k2] = append(bp.TableKV.KvTags[k2], v2v)
  2206. bp.Text += fmt.Sprintf("%v:%v\n", k2, v2)
  2207. }
  2208. }
  2209. }
  2210. for k2, v2 := range mv.SpaceKV.KvTags {
  2211. for _, v2v := range v2 {
  2212. isExists := false
  2213. for _, v2vv := range bp.SpaceKV.KvTags[k2] {
  2214. if v2v.Value == v2vv.Value {
  2215. isExists = true
  2216. break
  2217. }
  2218. }
  2219. if !isExists {
  2220. bp.SpaceKV.KvTags[k2] = append(bp.SpaceKV.KvTags[k2], v2v)
  2221. bp.Text += fmt.Sprintf("%v:%v\n", k2, v2)
  2222. }
  2223. }
  2224. }
  2225. }
  2226. }
  2227. tn.BPackage = true
  2228. tn.SortKV.RemoveKey(k)
  2229. }
  2230. }
  2231. }
  2232. //根据table.SortKV的key判断是否分包,如果没有再根据value判断
  2233. func foundPacBySortKV(tn *Table, val int, index []string, index_pos []int, keyExistsCount *map[string]int, commonKeyVals *map[string][]string, key_index int, hasPkgTd map[string]bool) (rval int, rindex []string, rindex_pos []int) {
  2234. keyIsPkg := false
  2235. for in, k := range tn.SortKV.Keys {
  2236. if excludeKey.MatchString(BracketsTextReg.ReplaceAllString(k, "")) || excludeKey3.MatchString(k) || regFJWarap.MatchString(k) || regAZWarap.MatchString(k) { //判断分包前排除
  2237. continue
  2238. }
  2239. v := tn.SortKV.Map[k]
  2240. //key是分包的情况
  2241. if ismatch := FindVal_1.MatchString(k); keyIsPkg || ismatch {
  2242. if ismatch {
  2243. keyIsPkg = true
  2244. val += 4
  2245. pkgFlag := FindVal_1.FindString(k) //对值进行分包判断
  2246. k = strings.Replace(k, pkgFlag, "", -1)
  2247. index = append(index, pkgFlag)
  2248. index_pos = append(index_pos, len(index))
  2249. val += 1
  2250. //pac++
  2251. } else {
  2252. k = strings.TrimRight(k, "_")
  2253. }
  2254. (*keyExistsCount)[k] = (*keyExistsCount)[k] + 1
  2255. (*commonKeyVals)[k] = append((*commonKeyVals)[k], qutil.ObjToString(v))
  2256. } else if k1 := FilterKey_2.ReplaceAllString(k, ""); FindKey_2.MatchString(k1) {
  2257. val += 4
  2258. //value数组分包
  2259. if vs, bvs1 := v.([]string); bvs1 {
  2260. L:
  2261. for in2, v1 := range vs {
  2262. if len([]rune(v1)) < 20 && !moneyNum.MatchString(v1) && FindVal2_1.MatchString(v1) {
  2263. for _, serial := range tn.TableResult.RuleBlock.TitleRegs {
  2264. if serial.MatchString(v1) {
  2265. break L
  2266. }
  2267. }
  2268. if key_index == -1 {
  2269. key_index = in
  2270. } else if key_index != in {
  2271. break
  2272. }
  2273. index = append(index, v1)
  2274. index_pos = append(index_pos, in2)
  2275. val += 1
  2276. //pac++
  2277. }
  2278. }
  2279. } else if v1, ok := v.(string); ok && !hasPkgTd[k] {
  2280. //value字符串分包
  2281. v1 = replPkgConfusion(v1) //替换分包中混淆的词
  2282. for _, v2 := range strings.Split(v1, "/") {
  2283. if len([]rune(v2)) < 20 && !moneyNum.MatchString(v2) && FindVal2_1.MatchString(v2) {
  2284. key_index = in
  2285. index = append(index, v1)
  2286. index_pos = append(index_pos, 0)
  2287. val += 1
  2288. //pac++
  2289. underline := ""
  2290. for {
  2291. underline += "_"
  2292. if tn.SortKV.Map[k+underline] == nil {
  2293. break
  2294. } else if v3, v2_ok := tn.SortKV.Map[k+underline].(string); v2_ok && v3 != "" {
  2295. index = append(index, v3)
  2296. index_pos = append(index_pos, 1)
  2297. } else if v3, v2_ok := tn.SortKV.Map[k+underline].([]string); v2_ok {
  2298. for v2_k, v2_v := range v3 {
  2299. index = append(index, v2_v)
  2300. index_pos = append(index_pos, v2_k+1)
  2301. }
  2302. }
  2303. }
  2304. break
  2305. }
  2306. }
  2307. }
  2308. break
  2309. }
  2310. }
  2311. return val, index, index_pos
  2312. }
  2313. //初始化CheckMultiPackageByTable方法需要的数据
  2314. func initCheckMultiPackageByTable(tn *Table, key_index int, index []string, index_pos []int, val int, pac int, hasPkgTd map[string]bool) (rkey_index int, rindex []string, rindex_pos []int, rval int, rpac int, rhasPkgTd map[string]bool) {
  2315. for in, k := range tn.SortKV.Keys {
  2316. //涉及包号|包件号?|项目标号|规格|型号|招标范围|业绩|废标)|(^编号$)|([^包段标]编号)就跳过
  2317. if excludeKey.MatchString(BracketsTextReg.ReplaceAllString(k, "")) || excludeKey3.MatchString(k) || strings.Contains(k, "批复") {
  2318. continue
  2319. }
  2320. v := tn.SortKV.Map[k]
  2321. if vs, bvs := v.([]string); bvs {
  2322. //arr_count++
  2323. haspkgs := []string{}
  2324. for in2, v1 := range vs {
  2325. v1 = replPkgConfusion(v1) //替换分包中混淆的词
  2326. if len([]rune(v1)) < 8 && !moneyNum.MatchString(v1) && FindVal_1.MatchString(v1) {
  2327. if key_index == -1 {
  2328. key_index = in
  2329. } else if key_index != in {
  2330. break
  2331. }
  2332. index = append(index, FindVal_1.FindString(v1))
  2333. index_pos = append(index_pos, in2)
  2334. val += 1
  2335. pac++
  2336. } else if FindKey_3.MatchString(k) {
  2337. //5db2a101a5cb26b9b73054ac
  2338. index = append(index, v1)
  2339. index_pos = append(index_pos, in2)
  2340. val += 1
  2341. pac++
  2342. } else {
  2343. if ok, v1new := isHasOnePkgAndNoKv(v1); ok { //td的值里面有一个包,并且没有冒号kv
  2344. haspkgs = append(haspkgs, v1new)
  2345. }
  2346. }
  2347. }
  2348. /*处理这种情况:
  2349. <tr><td>包一:xxxxxxxxx</td></tr>
  2350. <tr><td>包二:xxxxxxxxx</td></tr>
  2351. */
  2352. if len(index) == 0 && len(haspkgs) > 0 && len(haspkgs) == len(vs) {
  2353. for in2, v1 := range haspkgs {
  2354. if key_index == -1 {
  2355. key_index = in
  2356. } else if key_index != in {
  2357. break
  2358. }
  2359. index = append(index, v1)
  2360. index_pos = append(index_pos, in2)
  2361. val += 1
  2362. pac++
  2363. }
  2364. }
  2365. } else if v1, ok := v.(string); ok {
  2366. v1 = replPkgConfusion(v1) //替换分包中混淆的词
  2367. if len([]rune(v1)) < 8 && !moneyNum.MatchString(v1) && FindVal_1.MatchString(v1) {
  2368. key_index = in
  2369. index = append(index, FindVal_1.FindString(v1))
  2370. index_pos = append(index_pos, 0)
  2371. val += 1
  2372. pac++
  2373. } else if getTd := tn.GetTdByRCNo(0, tn.SortKV.Index[k]); getTd != nil && getTd.KVDirect == 2 { //纵向
  2374. /*处理这种情况:
  2375. <tr><td>包一:xxxxxxxxx</td></tr>
  2376. */
  2377. if ok, v1new := isHasOnePkgAndNoKv(v1); ok {
  2378. hasPkgTd[k] = true
  2379. key_index = in
  2380. index = append(index, v1new)
  2381. index_pos = append(index_pos, 0)
  2382. val += 1
  2383. pac++
  2384. }
  2385. }
  2386. }
  2387. }
  2388. return key_index, index, index_pos, val, pac, hasPkgTd
  2389. }
  2390. //组装解析到的分包,//key如果匹配到抽取关键词就添加到table.SortKV
  2391. func (tn *Table) assemblePackage(k1, v1, key string, isSite bool, codeSite string) {
  2392. bp := tn.BlockPackage.Map[key].(*u.BlockPackage)
  2393. if bp.TableKV == nil {
  2394. bp.TableKV = u.NewJobKv()
  2395. }
  2396. if v1 != "" {
  2397. kvTags, _ := CommonDataAnaly(k1, "中标情况", "", v1, isSite, codeSite) //匹配抽取关键词
  2398. for k3, v3 := range kvTags {
  2399. if bp.TableKV.KvTags[k3] == nil {
  2400. bp.TableKV.KvTags[k3] = append(bp.TableKV.KvTags[k3], v3...)
  2401. } else if k3 == "预算" && bp.Budget <= 0 {
  2402. moneys := clear.ObjToMoney([]interface{}{v3[0].Value, ""})
  2403. if len(moneys) > 0 {
  2404. if vf, ok := moneys[0].(float64); ok {
  2405. bp.Budget = vf
  2406. bp.IsTrueBudget = moneys[len(moneys)-1].(bool)
  2407. } else if vi, ok := moneys[0].(int); ok {
  2408. bp.Budget = float64(vi)
  2409. bp.IsTrueBudget = moneys[len(moneys)-1].(bool)
  2410. }
  2411. }
  2412. } else if k3 == "中标金额" && bp.Bidamount <= 0 {
  2413. moneys := clear.ObjToMoney([]interface{}{v3[0].Value, ""})
  2414. if len(moneys) > 0 {
  2415. if vf, ok := moneys[0].(float64); ok {
  2416. bp.Bidamount = vf
  2417. bp.IsTrueBidamount = moneys[len(moneys)-1].(bool)
  2418. } else if vi, ok := moneys[0].(int); ok {
  2419. bp.Bidamount = float64(vi)
  2420. bp.IsTrueBidamount = moneys[len(moneys)-1].(bool)
  2421. }
  2422. }
  2423. }
  2424. }
  2425. }
  2426. k1 = regReplAllSpace.ReplaceAllString(k1, "")
  2427. //拼接内容
  2428. if !excludeKey.MatchString(k1) {
  2429. bp.Text += fmt.Sprintf("%v:%v\n", k1, v1)
  2430. }
  2431. tn.BlockPackage.AddKey(key, bp)
  2432. }
  2433. /**
  2434. 之前爬虫过来的数据对table表格的抓取异常问题
  2435. 查找并修正不规则表格的字符串,只对全文做处理,块内的表格不需要修正
  2436. **/
  2437. var thbf = regexp.MustCompile("(?i)</?t(head|body|foot)>")
  2438. //需要保留thead
  2439. var saveThead = regexp.MustCompile("(?is)<thead>(.+?)</thead>")
  2440. var clearpkg = regexp.MustCompile("(标示|标识)")
  2441. func RepairCon(con string) string {
  2442. con = clearpkg.ReplaceAllString(con, "")
  2443. res := saveThead.FindAllStringSubmatch(con, 1)
  2444. th := ""
  2445. if len(res) == 1 && len(res[0]) == 2 {
  2446. th = u.TrimLeftSpace(res[0][1], "")
  2447. }
  2448. con = thbf.ReplaceAllString(con, "")
  2449. con = u.TrimLeftSpace(con, "")
  2450. itbody := strings.Index(con, "<tr")
  2451. iLen := 3
  2452. if itbody == 0 {
  2453. con = findpos(con, iLen, itbody)
  2454. } else {
  2455. itable := strings.Index(con, "<table")
  2456. if itable == -1 || itable > itbody {
  2457. con = findpos(con, iLen, itbody)
  2458. }
  2459. }
  2460. //保留第一个thead
  2461. if th != "" {
  2462. con = strings.Replace(con, th, "<thead>"+th+"</thead>", 1)
  2463. }
  2464. //u.Debug(con)
  2465. return con
  2466. }
  2467. //修复表格
  2468. func findpos(con string, iLen, start int) (newcon string) {
  2469. defer qutil.Catch()
  2470. n := len(con)
  2471. layer := 0
  2472. pos := 0
  2473. if start >= 0 {
  2474. if iLen == 6 {
  2475. for i := iLen + start; i < len(con); i++ {
  2476. if con[i] == '<' && i+6 < n {
  2477. str := con[i : i+6]
  2478. if str == "</tbod" {
  2479. if layer == 0 {
  2480. pos = i
  2481. break
  2482. } else {
  2483. layer--
  2484. }
  2485. i += 6
  2486. } else if str == "<tbody" {
  2487. layer++
  2488. i += 6
  2489. }
  2490. }
  2491. }
  2492. if pos+7 <= n && start+6 < pos {
  2493. newcon = con[:start] + "<table" + con[start+6:pos] + "</table" + con[pos+7:]
  2494. }
  2495. } else {
  2496. layer++
  2497. nq := 0
  2498. lasttr := 0
  2499. for i := iLen + start; i < len(con); i++ {
  2500. if con[i] == '<' && i+4 < n {
  2501. if nq == 0 {
  2502. str := con[i : i+4]
  2503. if str == "</tr" {
  2504. if layer <= 0 {
  2505. pos = i //正常情况不会存在此类情况
  2506. break
  2507. } else {
  2508. layer--
  2509. lasttr = i
  2510. }
  2511. i += 4
  2512. } else if str[:3] == "<tr" {
  2513. layer++
  2514. i += 4
  2515. } else if str == "<tab" && i+6 < n && con[i+4:i+6] == "le" {
  2516. if layer == 0 {
  2517. break
  2518. } else {
  2519. //内嵌的表格
  2520. nq++
  2521. }
  2522. }
  2523. } else {
  2524. if i+6 < n {
  2525. str := con[i : i+6]
  2526. if str == "</tabl" {
  2527. nq--
  2528. } else if str == "<table" {
  2529. nq++
  2530. }
  2531. } else {
  2532. break
  2533. }
  2534. }
  2535. }
  2536. }
  2537. if pos == 0 && lasttr > 3 {
  2538. pos = lasttr + 5
  2539. } else if pos > 0 {
  2540. pos += 5
  2541. }
  2542. if pos <= n && pos < len(con) && start < pos {
  2543. newcon = con[:start] + "<table>" + con[start:pos] + "</table>" + con[pos:]
  2544. }
  2545. }
  2546. }
  2547. if newcon == "" {
  2548. newcon = con
  2549. }
  2550. return
  2551. }
  2552. //td的值里面有一个包,并且没有冒号kv
  2553. func isHasOnePkgAndNoKv(v1 string) (bool, string) {
  2554. v1s := FindVal_1.FindAllString(v1, -1)
  2555. colonCount := len(regDivision.FindAllString(v1, -1))
  2556. if len(v1s) == 1 && colonCount < 2 {
  2557. ispkgcolon := regexp.MustCompile(v1s[0] + "[::]").MatchString(v1)
  2558. if (ispkgcolon && colonCount == 1) || (!ispkgcolon && colonCount == 0) {
  2559. return true, v1s[0]
  2560. }
  2561. }
  2562. return false, v1
  2563. }
  2564. //替换分包中混淆的词
  2565. func replPkgConfusion(v1 string) string {
  2566. v1 = PreReg.ReplaceAllString(v1, "")
  2567. v1 = PreReg1.ReplaceAllString(v1, "")
  2568. v1 = PreCon.ReplaceAllString(v1, "")
  2569. v1 = PreCon2.ReplaceAllString(v1, "")
  2570. return v1
  2571. }
  2572. //对td中的值,进行再处理
  2573. func (tn *Table) TdContactFormat(contactFormat *u.ContactFormat, isSite bool, codeSite string) {
  2574. //处理表格中的联系人信息
  2575. indexMap := contactFormat.IndexMap
  2576. matchMap := contactFormat.MatchMap
  2577. //qutil.Debug("==============================td=======================", indexMap, matchMap)
  2578. weightMap := map[string]map[string]interface{}{} //权重
  2579. mustMatchFirst := len(indexMap) > 0 //第一个必须匹配上
  2580. reCreate := false
  2581. matchCount := 0
  2582. contactTypeTagMap := map[string]map[string][]interface{}{}
  2583. //qutil.Debug("============================", mustMatchFirst, indexMap, matchMap)
  2584. notMatchTrCount := 0
  2585. allAscFind := true //开启正序查询
  2586. //涉及变量allAscFind,indexMap
  2587. if len(indexMap) == 0 {
  2588. isCanAddToIndexMap := false
  2589. matchPrevFlag := false
  2590. prevCanAddToIndexMap := false
  2591. LS:
  2592. for _, tr := range tn.TRs {
  2593. for td_index, td := range tr.TDs {
  2594. thisTdKvs := tn.tdkv(td) //获取td冒号kv
  2595. //qutil.Debug(td.Val, len(thisTdKvs))
  2596. // for _, v := range thisTdKvs {
  2597. // qutil.Debug(v.Key, v.Value)
  2598. // }
  2599. if len(thisTdKvs) != 1 {
  2600. preTdIndex := td_index - 1
  2601. if preTdIndex >= 0 {
  2602. preTdVal := tr.TDs[td_index-1].Val
  2603. tdType := "" //前一个td中是否是采购、代理、中标
  2604. for k, v := range ContactType {
  2605. if v.MatchString(preTdVal) {
  2606. tdType = k
  2607. break
  2608. }
  2609. }
  2610. if tdType != "" {
  2611. for _, this := range thisTdKvs {
  2612. if str := ContactInfoVagueReg.FindString(this.Key); str != "" {
  2613. td.SortKV.AddKey(tdType+str, this.Value)
  2614. }
  2615. }
  2616. }
  2617. }
  2618. continue
  2619. }
  2620. //1.处理带括号的()[]【】采购单位,代理机构;2.识别采购单位联系人、联系电话、代理机构联系人、联系电话
  2621. goOnFunc, isContinue, td_k := tn.tdKV(thisTdKvs[0].Key, &matchPrevFlag, &isCanAddToIndexMap, &indexMap, "LS")
  2622. //qutil.Debug("goOnFunc---", goOnFunc, "isContinue---", isContinue, "indexMap---", indexMap, "isCanAddToIndexMap---", isCanAddToIndexMap)
  2623. if !goOnFunc {
  2624. break LS
  2625. }
  2626. if isContinue {
  2627. continue
  2628. }
  2629. //采购单位,代理机构,中标单位
  2630. //qutil.Debug("td_k---", td_k, HasOrderContactType(td_k))
  2631. for _, k := range HasOrderContactType(td_k) {
  2632. if !ContactType[k].MatchString(td_k) { //不是采购单位,代理机构,中标单位跳过
  2633. continue
  2634. }
  2635. if len(indexMap) == 0 {
  2636. //qutil.Debug("isCanAddToIndexMap---", isCanAddToIndexMap, "prevCanAddToIndexMap---", prevCanAddToIndexMap, len(tr.TDs))
  2637. if isCanAddToIndexMap || (prevCanAddToIndexMap && len(tr.TDs) == 1) {
  2638. myPrevTdVal := ""
  2639. if td_index-2 >= 0 {
  2640. myPrevTdVal = tr.TDs[td_index-2].Val
  2641. if myPrevTdVal != "" && len([]rune(myPrevTdVal)) < 10 && ContactInfoMustReg.MatchString(myPrevTdVal) {
  2642. matchPrevFlag = true
  2643. }
  2644. }
  2645. indexMap[0] = k
  2646. break
  2647. }
  2648. } else {
  2649. indexMap = map[int]string{}
  2650. break LS
  2651. }
  2652. }
  2653. }
  2654. prevCanAddToIndexMap = isCanAddToIndexMap
  2655. isCanAddToIndexMap = false
  2656. }
  2657. if len(indexMap) > 0 {
  2658. allAscFind = false
  2659. }
  2660. }
  2661. //////
  2662. L:
  2663. for tr_index, tr := range tn.TRs {
  2664. thisTrHasMatch := false
  2665. jumpNextTd := false
  2666. for td_index, td := range tr.TDs {
  2667. //和|以?及|与|、多个词和在一起
  2668. jumpNextTd, thisTrHasMatch = tn.tdsMultipleWords(jumpNextTd, td, td_index, tr, thisTrHasMatch, indexMap)
  2669. //分块之后的kv
  2670. thisTdKvs := kvAfterDivideBlock("", td.Text, 3, tn.TableResult.RuleBlock, isSite, codeSite)
  2671. if len(thisTdKvs) == 0 {
  2672. thisTdKvs = tn.tdkv(td) //获取冒号kv
  2673. }
  2674. tdAscFind := true //开启td正序查询
  2675. //qutil.Debug("---", td.Val, len(thisTdKvs), len(indexMap))
  2676. if len(thisTdKvs) == 0 {
  2677. continue
  2678. } else if allAscFind && len(thisTdKvs) >= 3 && len(indexMap) == 0 {
  2679. //采购人在联系人、电话后面的处理
  2680. tdAscFind = tn.hasIndexMap(thisTdKvs, &indexMap, tdAscFind)
  2681. }
  2682. //qutil.Debug(len(thisTdKvs), len(tr.TDs))
  2683. // if len(thisTdKvs) >= 2 && len(tr.TDs) == 1 { //td中包含多个kv值 5d6b2aa2a5cb26b9b73e79d2
  2684. // tmpIndexMap := map[int]string{}
  2685. // start := 0
  2686. // for _, td_kv := range thisTdKvs {
  2687. // qutil.Debug(td_kv.Key)
  2688. // for _, k := range HasOrderContactType(td_kv.Key) {
  2689. // tmpIndexMap[start] = k
  2690. // start++
  2691. // }
  2692. // }
  2693. // indexMap = tmpIndexMap
  2694. // }
  2695. prevKey := ""
  2696. oldIndexMapLength := len(indexMap)
  2697. thidTdIndex := td_index
  2698. //notmatchCount := 0
  2699. kvTitle := ""
  2700. //qutil.Debug("indexMap++++++++++++++++++", indexMap, "len(thisTdKvs)", len(thisTdKvs), oldIndexMapLength)
  2701. if len(thisTdKvs) >= 2 { //td中有多个kv重置indexMap
  2702. indexMap = map[int]string{}
  2703. allAscFind = true
  2704. }
  2705. for _, td_kv := range thisTdKvs {
  2706. iscontinue := false
  2707. td_v := td_kv.Value
  2708. td_k := FilterContactKey(td_kv.Key) //带括号()[]的采购单位,代理机构处理
  2709. td_k_length := len([]rune(td_k))
  2710. if td_k_length < 2 || td_k_length > 15 {
  2711. continue
  2712. }
  2713. //都为正序查询
  2714. //qutil.Debug("td_k+++", td_k, "td_v+++", td_v, "allAscFind+++", allAscFind, "tdAscFind+++", tdAscFind)
  2715. if allAscFind && tdAscFind {
  2716. //都为正序查询处理
  2717. matchCount, weightMap, matchMap, thisTrHasMatch, indexMap, iscontinue, reCreate, thidTdIndex = tn.asdFind(td_k, matchCount, weightMap, matchMap, td, thisTrHasMatch, td_kv, indexMap, iscontinue, reCreate, thidTdIndex, isSite, codeSite)
  2718. }
  2719. //qutil.Debug("indexMap++++++", indexMap, len(indexMap), "iscontinue+++", iscontinue)
  2720. if iscontinue {
  2721. continue
  2722. }
  2723. //不在同一块中
  2724. //qutil.Debug(td_kv.Title, kvTitle, !ContactInfoMustReg.MatchString(td_kv.Key))
  2725. if td_kv.Title != "" && kvTitle != td_kv.Title && len(indexMap) > 0 && !ContactInfoMustReg.MatchString(td_kv.Key) {
  2726. thidTdIndex = 0
  2727. matchMap = map[string]map[string]bool{}
  2728. indexMap = map[int]string{}
  2729. }
  2730. kvTitle = td_kv.Title
  2731. //qutil.Debug(td_k_length, td_k_length, len(indexMap))
  2732. if td_k_length < 2 || td_k_length > 10 {
  2733. continue
  2734. }
  2735. if len(indexMap) > 0 {
  2736. //没有识别到采购单位联系人、联系电话、代理机构联系人、联系电话
  2737. if !ContactInfoMustReg.MatchString(td_k) {
  2738. //notmatchCount++
  2739. //if notmatchCount < len(indexMap)*2 && false {//false???????
  2740. // notmatchCount = 0
  2741. // thidTdIndex = 0
  2742. // indexMap = map[int]string{}
  2743. // matchMap = map[string]map[string]bool{}
  2744. //}
  2745. if mustMatchFirst { //indexMap初始值大于0
  2746. break L
  2747. }
  2748. continue
  2749. }
  2750. reCreate = true
  2751. index := td_index
  2752. //oldIndexMapLength原来的indexMap等于0 ,现在的indexMap大于1
  2753. if oldIndexMapLength == 0 && len(indexMap) > 1 {
  2754. if prevKey != td_k {
  2755. prevKey = td_k
  2756. index = td_index
  2757. } else if prevKey == td_k {
  2758. index++
  2759. }
  2760. }
  2761. //kv.value为空
  2762. if filterValue.MatchString(td_v) {
  2763. thisTrHasMatch = true
  2764. continue
  2765. }
  2766. //u.Debug(indexMap, td_k, td_v, matchMap, index, modle)
  2767. //myContactType
  2768. myContactType := indexMap[index]
  2769. //qutil.Debug(indexMap, index, myContactType)
  2770. if myContactType == "" && len(indexMap) == 1 {
  2771. _, onlyContactType := u.FirstKeyValueInMap(indexMap)
  2772. myContactType, _ = onlyContactType.(string)
  2773. }
  2774. //qutil.Debug("indexMap+++", indexMap, "index+++", index, "myContactType+++", myContactType)
  2775. if myContactType == "" {
  2776. continue
  2777. }
  2778. matchCount++
  2779. if matchMap[myContactType] == nil {
  2780. matchMap[myContactType] = map[string]bool{}
  2781. }
  2782. if IsContactKvHandle(ContactInfoMustReg.FindString(td_k), matchMap[myContactType]) {
  2783. continue
  2784. }
  2785. matchMap[myContactType][ContactInfoMustReg.FindString(td_k)] = true
  2786. if ContactType[myContactType].MatchString(td_k) {
  2787. continue
  2788. }
  2789. thisTrHasMatch = true
  2790. //modle
  2791. modle(thisTdKvs, td, myContactType, td_k, td_v, &contactTypeTagMap, tn, &weightMap, tr_index, td_index, isSite, codeSite)
  2792. }
  2793. }
  2794. //qutil.Debug("map===", td.SortKV.Map)
  2795. }
  2796. if allAscFind && !thisTrHasMatch {
  2797. notMatchTrCount++
  2798. if notMatchTrCount >= 2 {
  2799. notMatchTrCount = 0
  2800. indexMap = map[int]string{}
  2801. }
  2802. }
  2803. }
  2804. //u.Debug("end", matchCount, indexMap, matchMap)
  2805. if matchCount == 0 {
  2806. indexMap = map[int]string{}
  2807. matchMap = map[string]map[string]bool{}
  2808. }
  2809. (*contactFormat).IndexMap = indexMap
  2810. (*contactFormat).MatchMap = matchMap
  2811. // for _, tr := range tn.TRs {
  2812. // for _, td := range tr.TDs {
  2813. // qutil.Debug("td.sort.map---", td.SortKV.Map)
  2814. // }
  2815. // }
  2816. }
  2817. //modle
  2818. func modle(thisTdKvs []*u.Kv, td *TD, myContactType, td_k, td_v string, contactTypeTagMap *map[string]map[string][]interface{}, tn *Table, weightMap *map[string]map[string]interface{}, tr_index, td_index int, isSite bool, codeSite string) {
  2819. modle := 0
  2820. if len(thisTdKvs) == 1 {
  2821. if regReplAllSpace.ReplaceAllString(thisTdKvs[0].Value, "") == "" {
  2822. modle = 1
  2823. } else {
  2824. modle = 2
  2825. }
  2826. }
  2827. if modle == 1 {
  2828. td.Text = myContactType + td_k
  2829. td.Val = td.Text
  2830. } else {
  2831. //
  2832. if !strings.HasSuffix(td_k, "方式") {
  2833. kvTags := GetKvTags([]*u.Kv{&u.Kv{Key: myContactType + td_k, Value: td_v}}, "", BuyerContacts, isSite, codeSite)
  2834. if len(kvTags) == 1 {
  2835. tagVal, _ := u.FirstKeyValueInMap(kvTags)
  2836. if tagVal == "采购单位联系人" && ContactBuyerPersonFilterReg.MatchString(td_v) {
  2837. return
  2838. }
  2839. if (*contactTypeTagMap)[myContactType] == nil {
  2840. (*contactTypeTagMap)[myContactType] = map[string][]interface{}{}
  2841. }
  2842. myOldKeyArray := (*contactTypeTagMap)[myContactType][tagVal]
  2843. if myOldKeyArray != nil {
  2844. tn.TRs[myOldKeyArray[0].(int)].TDs[myOldKeyArray[1].(int)].SortKV.RemoveKey(myContactType + myOldKeyArray[2].(string))
  2845. } else {
  2846. (*contactTypeTagMap)[myContactType][tagVal] = make([]interface{}, 3)
  2847. }
  2848. if (*weightMap)[myContactType] == nil {
  2849. (*weightMap)[myContactType] = map[string]interface{}{}
  2850. }
  2851. (*weightMap)[myContactType][tagVal] = 1
  2852. (*contactTypeTagMap)[myContactType][tagVal] = []interface{}{tr_index, td_index, td_k}
  2853. }
  2854. }
  2855. td.SortKV.AddKey(myContactType+td_k, td_v)
  2856. }
  2857. }
  2858. //都为正序查询
  2859. func (tn *Table) asdFind(td_k string, matchCount int, weightMap map[string]map[string]interface{}, matchMap map[string]map[string]bool, td *TD, thisTrHasMatch bool, td_kv *u.Kv, indexMap map[int]string, iscontinue bool, reCreate bool, thidTdIndex int, isSite bool, codeSite string) (int, map[string]map[string]interface{}, map[string]map[string]bool, bool, map[int]string, bool, bool, int) {
  2860. for _, k := range HasOrderContactType(td_k) { //采购单位,代理机构,中标单位
  2861. if !ContactType[k].MatchString(td_k) { //没有匹配到采购单位,代理机构,中标单位
  2862. continue
  2863. }
  2864. matchCount++
  2865. if weightMap[k] == nil {
  2866. weightMap[k] = map[string]interface{}{}
  2867. }
  2868. //匹配到进行处理
  2869. if ContactInfoVagueReg.MatchString(td_k) {
  2870. thisTrHasMatch = tn.matchContactType(&matchMap, k, td_k, td_kv.Value, td, &weightMap, thisTrHasMatch, isSite, codeSite)
  2871. } else if k == "采购单位" { //打标签,权重高的重新覆盖
  2872. kvTags := GetKvTags([]*u.Kv{td_kv}, "", []string{"采购单位"}, isSite, codeSite)
  2873. tagVal, weightVal := u.FirstKeyValueInMap(kvTags)
  2874. if tagVal == k {
  2875. if weightMap[k][k] == nil || (weightVal != nil && weightVal.(int) >= weightMap[k][k].(int)) || len(matchMap[k]) == 0 {
  2876. weightMap[k][k] = weightVal.(int)
  2877. matchMap[k] = map[string]bool{}
  2878. indexMap = map[int]string{}
  2879. }
  2880. }
  2881. }
  2882. if u.IsMapHasValue(k, indexMap) { //map中是否存在value
  2883. thisTrHasMatch = true
  2884. iscontinue = true
  2885. continue
  2886. }
  2887. if reCreate {
  2888. indexMap = map[int]string{}
  2889. reCreate = false
  2890. }
  2891. indexMap[thidTdIndex] = k
  2892. iscontinue = true
  2893. thisTrHasMatch = true
  2894. thidTdIndex++
  2895. break
  2896. }
  2897. if len(indexMap) == 0 && td_kv.PrevLine != "" {
  2898. //td_kv.PrevLine
  2899. prevLine := FilterSerial.ReplaceAllString(td_kv.PrevLine, "")
  2900. for k, v := range ContactType { //采购单位,代理机构正则
  2901. if u.IsArrayHasValue(prevLine, v.FindAllString(prevLine, -1)) {
  2902. indexMap[thidTdIndex] = k
  2903. thisTrHasMatch = true
  2904. thidTdIndex++
  2905. }
  2906. }
  2907. }
  2908. if len(indexMap) == 0 && td_kv.Title != "" {
  2909. //td_kv.Title
  2910. if titleMatchType := ContactTypeTitleMatch(td_kv.Title); titleMatchType != "" {
  2911. thidTdIndex = 0
  2912. matchMap = map[string]map[string]bool{}
  2913. indexMap = map[int]string{1: titleMatchType}
  2914. // for i, t := range titleMatchType {
  2915. // indexMap[i+1] = t
  2916. // }
  2917. }
  2918. }
  2919. return matchCount, weightMap, matchMap, thisTrHasMatch, indexMap, iscontinue, reCreate, thidTdIndex
  2920. }
  2921. //匹配到进行处理
  2922. func (tn *Table) matchContactType(matchMap *map[string]map[string]bool, k string, td_k string, td_v string, td *TD, weightMap *map[string]map[string]interface{}, thisTrHasMatch bool, isSite bool, codeSite string) bool {
  2923. if (*matchMap)[k] == nil {
  2924. (*matchMap)[k] = map[string]bool{}
  2925. }
  2926. isAddToMatchMap := true
  2927. if !strings.HasSuffix(td_k, "方式") {
  2928. kvTags := GetKvTags([]*u.Kv{&u.Kv{Key: td_k, Value: td_v}}, "", BuyerContacts, isSite, codeSite)
  2929. if len(kvTags) == 1 {
  2930. tagVal, weightVal := u.FirstKeyValueInMap(kvTags)
  2931. if tagVal == "采购单位联系人" && ContactBuyerPersonFilterReg.MatchString(td_v) {
  2932. isAddToMatchMap = false
  2933. }
  2934. if td.SortKV.Map[tagVal] != nil {
  2935. if (*weightMap)[k][tagVal] == nil || (weightVal != nil && weightVal.(int) >= (*weightMap)[k][tagVal].(int)) {
  2936. (*weightMap)[k][tagVal] = weightVal.(int)
  2937. td.SortKV.AddKey(tagVal, td_v)
  2938. thisTrHasMatch = true
  2939. }
  2940. } else {
  2941. (*weightMap)[k][tagVal] = weightVal.(int)
  2942. }
  2943. }
  2944. }
  2945. if isAddToMatchMap && !filterValue.MatchString(td_v) && td_v != "" {
  2946. (*matchMap)[k][ContactInfoVagueReg.FindString(td_k)] = true
  2947. }
  2948. return thisTrHasMatch
  2949. }
  2950. //采购人在联系人、电话后面的处理
  2951. func (tn *Table) hasIndexMap(thisTdKvs []*u.Kv, indexMap *map[int]string, tdAscFind bool) bool {
  2952. //采购人在联系人、电话后面的处理
  2953. isCanAddToIndexMap := false
  2954. LL:
  2955. for _, td_kv := range thisTdKvs {
  2956. //1.处理带括号的()[]【】采购单位,代理机构;2.识别采购单位联系人、联系电话、代理机构联系人、联系电话
  2957. goOnFunc, isContinue, td_k := tn.tdKV(td_kv.Key, nil, &isCanAddToIndexMap, indexMap, "LL")
  2958. if !goOnFunc {
  2959. break LL
  2960. }
  2961. if isContinue {
  2962. continue
  2963. }
  2964. if len(*indexMap) == 0 {
  2965. for _, k := range HasOrderContactType(td_k) { //采购单位,代理机构
  2966. if !ContactType[k].MatchString(td_k) {
  2967. continue
  2968. }
  2969. if isCanAddToIndexMap && len(*indexMap) == 0 {
  2970. (*indexMap)[0] = k
  2971. break
  2972. }
  2973. }
  2974. }
  2975. }
  2976. if len(*indexMap) > 0 {
  2977. tdAscFind = false
  2978. }
  2979. return tdAscFind
  2980. }
  2981. //和|以?及|与|、多个词和在一起
  2982. func (tn *Table) tdsMultipleWords(jumpNextTd bool, td *TD, td_index int, tr *TR, thisTrHasMatch bool, indexMap map[int]string) (jump, thisTr bool) {
  2983. if !jumpNextTd && len([]rune(td.Text)) >= 5 && len([]rune(td.Text)) <= 15 && regSplit.MatchString(td.Text) && td_index+1 < len(tr.TDs) {
  2984. thisTdVals := regSplit.Split(td.Text, -1)
  2985. nextTdVals := MultipleValueSplitReg.Split(tr.TDs[td_index+1].Val, -1)
  2986. if len(thisTdVals) == len(nextTdVals) { //本次和下个长度相等
  2987. for _, k := range HasOrderContactType(td.Text) { //采购单位,代理机构
  2988. if ContactType[k].MatchString(td.Text) { //采购单位,代理机构
  2989. for thisTdVals_k, thisTdVals_v := range thisTdVals {
  2990. thisTdVals_v = strings.TrimSpace(thisTdVals_v)
  2991. if ContactType[k].MatchString(thisTdVals_v) { //采购单位,代理机构
  2992. thisTrHasMatch = true
  2993. tr.TDs[td_index+1].SortKV.AddKey(thisTdVals_v, nextTdVals[thisTdVals_k])
  2994. continue
  2995. }
  2996. if !ContactInfoMustReg.MatchString(thisTdVals_v) {
  2997. continue
  2998. }
  2999. jumpNextTd = true
  3000. thisTrHasMatch = true
  3001. tr.TDs[td_index+1].SortKV.AddKey(k+thisTdVals_v, nextTdVals[thisTdVals_k])
  3002. }
  3003. break
  3004. }
  3005. }
  3006. if len(indexMap) > 0 {
  3007. _, onlyContactType := u.FirstKeyValueInMap(indexMap)
  3008. if myContactType, _ := onlyContactType.(string); myContactType != "" {
  3009. for thisTdVals_k, thisTdVals_v := range thisTdVals {
  3010. thisTdVals_v = strings.TrimSpace(thisTdVals_v)
  3011. if ContactInfoMustReg.MatchString(thisTdVals_v) {
  3012. jumpNextTd = true
  3013. thisTrHasMatch = true
  3014. tr.TDs[td_index+1].SortKV.AddKey(myContactType+thisTdVals_v, nextTdVals[thisTdVals_k])
  3015. }
  3016. }
  3017. }
  3018. }
  3019. }
  3020. } else {
  3021. jumpNextTd = false
  3022. }
  3023. return jumpNextTd, thisTrHasMatch
  3024. }
  3025. //采购单位,代理机构
  3026. func (tn *Table) tdHasOrderContactType(td_k string, indexMap *map[int]string, tr *TR, prevCanAddToIndexMap, isCanAddToIndexMap, matchPrevFlag *bool, td_index int) (gotoFunc bool) {
  3027. for _, k := range HasOrderContactType(td_k) { //采购单位,代理机构
  3028. if !ContactType[k].MatchString(td_k) {
  3029. continue
  3030. }
  3031. if len(*indexMap) == 0 {
  3032. if (*isCanAddToIndexMap) || (*prevCanAddToIndexMap && len(tr.TDs) == 1) {
  3033. myPrevTdVal := ""
  3034. if td_index-2 >= 0 {
  3035. myPrevTdVal = tr.TDs[td_index-2].Val
  3036. }
  3037. if myPrevTdVal != "" && len([]rune(myPrevTdVal)) < 10 && ContactInfoMustReg.MatchString(myPrevTdVal) {
  3038. (*matchPrevFlag) = true
  3039. }
  3040. (*indexMap)[0] = k
  3041. break
  3042. }
  3043. } else {
  3044. (*indexMap) = map[int]string{}
  3045. return false
  3046. }
  3047. }
  3048. return true
  3049. }
  3050. //1.处理带括号的()[]【】采购单位,代理机构;2.识别采购单位联系人、联系电话、代理机构联系人、联系电话
  3051. func (tn *Table) tdKV(key string, matchPrevFlag, isCanAddToIndexMap *bool, indexMap *map[int]string, gotoName string) (goOnFunc, isContinue bool, td_k string) {
  3052. td_k = FilterContactKey(key) //带括号的()[]【】采购单位,代理机构处理
  3053. td_k_length := len([]rune(td_k))
  3054. if td_k_length < 2 || td_k_length > 15 {
  3055. return true, true, "" //继续执行,跳过当前循环
  3056. }
  3057. isContinue = ContactInfoMustReg.MatchString(td_k) //识别采购单位联系人、联系电话、代理机构联系人、联系电话
  3058. if isContinue || (ContactInfoVagueReg.MatchString(td_k) && u.IsMapHasValue(td_k, ContactType)) {
  3059. if gotoName == "LS" && !(*matchPrevFlag) && len(*indexMap) > 0 {
  3060. (*indexMap) = map[int]string{}
  3061. return false, false, "" //中断外层循环
  3062. }
  3063. if gotoName == "LL" && len(*indexMap) > 0 {
  3064. (*indexMap) = map[int]string{}
  3065. return false, false, ""
  3066. }
  3067. (*isCanAddToIndexMap) = true
  3068. }
  3069. return true, false, td_k //继续执行,不结束当前循环,返回处理后的值
  3070. }
  3071. //获取td冒号kv
  3072. func (tn *Table) tdkv(td *TD) []*u.Kv {
  3073. thisTdKvs := colonkvEntity.GetKvs(td.Text, tn.Desc, 2) //获取冒号kv
  3074. //获取冒号
  3075. if len(thisTdKvs) == 0 {
  3076. tdValue := regReplAllSpace.ReplaceAllString(td.Text, "") //去除空格换行
  3077. if tdValue != "" && len([]rune(tdValue)) < 10 {
  3078. thisTdKvs = append(thisTdKvs, &u.Kv{
  3079. Key: tdValue,
  3080. Value: "",
  3081. })
  3082. }
  3083. }
  3084. return thisTdKvs
  3085. }
  3086. func (table *Table) extractPriceNumber() {
  3087. lineMapArr := make(map[string]*SortMap)
  3088. lineMap := make(map[string]*SortMap)
  3089. lineMapArr, lineMap = initLineMapLineMapArr(table) //不同数据类型的数据组合
  3090. //qutil.Debug("lineMapArr----", lineMapArr)
  3091. if len(lineMapArr) > 0 {
  3092. for _, arrMap := range lineMapArr {
  3093. resultArrMap := table.matchMapArrPrinceNumber(arrMap) //最终数据
  3094. //qutil.Debug("resultArrMap-------------------", resultArrMap)
  3095. //处理数组长度不一致情况
  3096. if len(resultArrMap) > 0 {
  3097. numLen := len(resultArrMap["number"])
  3098. priceLen := len(resultArrMap["price"])
  3099. itemLen := len(resultArrMap["item"])
  3100. maxNum := numLen //获取最大长度
  3101. if numLen == 0 { //没有
  3102. maxNum = priceLen
  3103. }
  3104. //取个数数据的长度为基准(数据长度可能不一致)
  3105. if numLen != priceLen && numLen > 0 && priceLen > 0 { //有number和price数据且长度不同,进行数据增减补齐
  3106. if priceLen > numLen { //price多,删
  3107. tmpArr := resultArrMap["price"]
  3108. resultArrMap["price"] = tmpArr[:numLen]
  3109. } else if priceLen < numLen { //price少,补空
  3110. for {
  3111. resultArrMap["price"] = append(resultArrMap["price"], "")
  3112. //qutil.Debug("=============price==============")
  3113. if len(resultArrMap["price"]) == numLen {
  3114. break
  3115. }
  3116. }
  3117. }
  3118. }
  3119. if maxNum > 0 && itemLen > 0 && maxNum != itemLen { //有price或者number,item长度保持一致
  3120. if itemLen > maxNum {
  3121. tmpArr := resultArrMap["item"]
  3122. resultArrMap["item"] = tmpArr[:maxNum]
  3123. } else if itemLen < maxNum {
  3124. for {
  3125. resultArrMap["item"] = append(resultArrMap["item"], "")
  3126. //qutil.Debug("=============item==============")
  3127. if len(resultArrMap["item"]) == maxNum {
  3128. break
  3129. }
  3130. }
  3131. }
  3132. }
  3133. //封装成一一对应数据
  3134. /*
  3135. {
  3136. "price" :["123","125"],
  3137. "number" :["1","12"]
  3138. }
  3139. 转换为:
  3140. [
  3141. {"price":"123","number":"1"},
  3142. {"price":"125","number":"12"}
  3143. ]
  3144. */
  3145. finishData := []map[string]interface{}{}
  3146. //qutil.Debug("maxNum--------------------", maxNum)
  3147. for t := 0; t < maxNum; t++ {
  3148. tmpMap := make(map[string]interface{})
  3149. if len(resultArrMap["number"]) > 0 {
  3150. number := resultArrMap["number"][t]
  3151. tmpMap["number"] = number
  3152. }
  3153. if len(resultArrMap["price"]) > 0 {
  3154. price := resultArrMap["price"][t]
  3155. tmpMap["price"] = price
  3156. }
  3157. if len(resultArrMap["item"]) > 0 {
  3158. item := resultArrMap["item"][t]
  3159. runeItem := []rune(qutil.ObjToString(item))
  3160. if len(runeItem) > 50 {
  3161. tmpMap["item"] = string(runeItem[:50])
  3162. } else {
  3163. tmpMap["item"] = item
  3164. }
  3165. }
  3166. finishData = append(finishData, tmpMap)
  3167. }
  3168. //qutil.Debug(finishData)
  3169. if len(finishData) > 0 {
  3170. table.PriceNumberData = append(table.PriceNumberData, finishData)
  3171. }
  3172. }
  3173. }
  3174. }
  3175. if len(lineMap) > 0 {
  3176. for _, strMap := range lineMap {
  3177. resultStrMap := table.matchMapPrinceNumber(strMap)
  3178. //qutil.Debug("resultStrMap---", resultStrMap)
  3179. if len(resultStrMap) > 0 {
  3180. if resultStrMap["price"] != nil || resultStrMap["number"] != nil { //有price或者number在保存
  3181. if item := qutil.ObjToString(resultStrMap["item"]); item != "" { //item过长截取
  3182. runeItem := []rune(item)
  3183. if len(runeItem) > 50 {
  3184. resultStrMap["item"] = string(runeItem[:50])
  3185. }
  3186. }
  3187. finishData := []map[string]interface{}{}
  3188. finishData = append(finishData, resultStrMap)
  3189. //qutil.Debug("finishData---", finishData)
  3190. if len(finishData) > 0 {
  3191. table.PriceNumberData = append(table.PriceNumberData, finishData)
  3192. }
  3193. }
  3194. }
  3195. }
  3196. }
  3197. //qutil.Debug("table.PriceNumberData---------", table.PriceNumberData)
  3198. }
  3199. //数组数据抽取price和number
  3200. func (table *Table) matchMapArrPrinceNumber(arrMap *SortMap) map[string][]interface{} {
  3201. result := make(map[string][]interface{}) //最终存储数据
  3202. for _, key := range arrMap.Keys {
  3203. val := arrMap.Map[key].([]string)
  3204. for f, reg := range u.PriceNumberReg {
  3205. key = tabletitleclear2.ReplaceAllString(key, "") //过滤
  3206. if reg.MatchString(key) { //匹配成功
  3207. //qutil.Debug("arr----key", key, val, f)
  3208. tmp := []interface{}{}
  3209. if f == "price" {
  3210. tmp = dealPriceInterface(key, val...) //处理金额,单位转换
  3211. } else if f == "number" {
  3212. tmp = dealNumberInterface(val...) //处理数量
  3213. } else {
  3214. for _, v := range val {
  3215. tmp = append(tmp, v)
  3216. }
  3217. }
  3218. if len(tmp) > 0 {
  3219. result[f] = tmp
  3220. }
  3221. }
  3222. }
  3223. }
  3224. return result
  3225. }
  3226. //字符串数据抽取price和number
  3227. func (table *Table) matchMapPrinceNumber(strMap *SortMap) map[string]interface{} {
  3228. result := make(map[string]interface{})
  3229. for _, key := range strMap.Keys {
  3230. val := qutil.ObjToString(strMap.Map[key])
  3231. for f, reg := range u.PriceNumberReg {
  3232. key = tabletitleclear2.ReplaceAllString(key, "") //过滤
  3233. if reg.MatchString(key) { //匹配成功
  3234. //qutil.Debug("str----key", key, val)
  3235. if f == "price" {
  3236. if len(regHz.FindAllString(val, -1)) > 5 { //price中汉字过多视为内容错误
  3237. continue
  3238. }
  3239. tmp := dealPriceInterface(key, val)[0] //处理金额,单位转换
  3240. result[f] = tmp
  3241. } else if f == "number" {
  3242. tmp := dealNumberInterface(val)[0]
  3243. result[f] = tmp
  3244. } else {
  3245. result[f] = val
  3246. }
  3247. }
  3248. }
  3249. }
  3250. return result
  3251. }
  3252. //table中抽取品牌,table.BrandData
  3253. func (table *Table) analyBrand() {
  3254. //5c2d8c05a5cb26b9b782572b
  3255. //产品名称 品牌 规格 单价 单位 数量 小计 质保期
  3256. lineMapArr := make(map[string]*SortMap)
  3257. lineMap := make(map[string]*SortMap)
  3258. brandRule := u.BrandRules
  3259. //初始化lineMapArr,lineMap;
  3260. lineMapArr, lineMap = initLineMapLineMapArr(table) //处理数组数据后,匹配必须title和替换要保存的title
  3261. //qutil.Debug("lineMapArr----", len(lineMapArr))
  3262. if len(lineMapArr) > 0 {
  3263. for _, aMap := range lineMapArr {
  3264. maxNum := 0 //记录最大长度
  3265. arrcount1 := 0 //记录key是否存在必须title(数组数据)
  3266. arrcount2 := 0
  3267. ka := make(map[string][]string) //最终存储数据
  3268. //qutil.Debug("aMap.Keys----", aMap.Keys)
  3269. //匹配商品规则
  3270. arrcount1, arrcount2, ka = table.matchMapArrBrandRule(aMap, brandRule, ka, arrcount1, arrcount2)
  3271. //找最终存储数据的最小len(arr)
  3272. // for _, vf := range ka {
  3273. // //找最短的数组
  3274. // lenVal := len(vf)
  3275. // if minNum == 0 || minNum > lenVal { //maxNum = len(最短数组)
  3276. // minNum = lenVal
  3277. // }
  3278. // }
  3279. //找最终存储数据的最大len(arr),小的补空
  3280. for _, vf1 := range ka {
  3281. lenVal := len(vf1)
  3282. if lenVal > maxNum {
  3283. maxNum = lenVal
  3284. }
  3285. }
  3286. //table.BrandData商品存储
  3287. finishKa := make(map[string][]string)
  3288. for vf2K, vf2 := range ka {
  3289. if len(vf2) < maxNum {
  3290. if vf2K == "unitprice" { //价格的当前总数比最大的总数小就跳过,可能是总价格而不是单个的价格
  3291. continue
  3292. }
  3293. lenMv := maxNum - len(vf2)
  3294. for i := 0; i < lenMv; i++ {
  3295. vf2 = append(vf2, "")
  3296. }
  3297. }
  3298. finishKa[vf2K] = vf2
  3299. }
  3300. hasKey(table, arrcount1) //是否匹配到table中的标题
  3301. //qutil.Debug("finishKa----", finishKa)
  3302. if arrcount1 >= 1 {
  3303. if arrcount1+arrcount2 == 1 { //删除只匹配到一个价钱(总价)
  3304. delete(finishKa, "unitprice")
  3305. }
  3306. finishData := dealArrData(maxNum, finishKa)
  3307. table.BrandData = append(table.BrandData, finishData) //存储table.BrandData
  3308. }
  3309. }
  3310. }
  3311. //处理string数据后,匹配必须title和替换要保存的title
  3312. //qutil.Debug("lineMap----", len(lineMap))
  3313. if len(lineMap) > 0 {
  3314. for _, sMap := range lineMap {
  3315. strcount1 := 0 //记录key是否存在必须title(字符串数据)
  3316. strcount2 := 0
  3317. endStrMap := make(map[string]string)
  3318. //qutil.Debug(k, "aMap.Keys----", sMap.Keys)
  3319. //匹配商品规则
  3320. strcount1, strcount2, endStrMap = table.matchMapBrandRule(sMap, brandRule, endStrMap, strcount1, strcount2)
  3321. //原始字符串数据处理
  3322. hasKey(table, strcount1) //是否匹配到table中的标题
  3323. //qutil.Debug("endStrMap----", endStrMap)
  3324. if strcount1 >= 1 {
  3325. if strcount1+strcount2 == 1 { //删除只匹配到一个价钱(总价)
  3326. delete(endStrMap, "unitprice")
  3327. }
  3328. finishData := dealStrData(endStrMap) //处理数据
  3329. if len(finishData) > 0 {
  3330. table.BrandData = append(table.BrandData, finishData)
  3331. }
  3332. }
  3333. }
  3334. }
  3335. }
  3336. //字符串匹配商品规则
  3337. func (table *Table) matchMapBrandRule(sMap *SortMap, brandRule map[string]map[string]string, endStrMap map[string]string, strcount1, strcount2 int) (int, int, map[string]string) {
  3338. for _, k1 := range sMap.Keys {
  3339. match := false //记录must是否匹配到
  3340. v1 := qutil.ObjToString(sMap.Map[k1])
  3341. // for k1, v1 := range sMap {
  3342. //qutil.Debug(k1, "++++++++++", v1)
  3343. if v1 == "" {
  3344. continue
  3345. }
  3346. //匹配必须title
  3347. for nameM, r := range brandRule["must"] {
  3348. if convert(k1, r) { //匹配成功
  3349. v1tmp1 := v1
  3350. match = true
  3351. if nameM == "itemname" || nameM == "modal" { //特殊处理itemname
  3352. hasGoods(table, v1)
  3353. if nameM == "itemname" {
  3354. v1tmp1 = filterItem(v1)[0] //过滤itemname
  3355. if v1tmp1 == "" {
  3356. break
  3357. }
  3358. }
  3359. }
  3360. if nameM == "brandname" || nameM == "modal" { //特殊处理brandname
  3361. if endStrMap["brandname"] == "" {
  3362. brand, allNull := hasBrand(table, v1)
  3363. if !allNull {
  3364. endStrMap["brandname"] = brand[0]
  3365. }
  3366. }
  3367. }
  3368. //unitprice
  3369. if nameM == "unitprice" { //处理金额
  3370. v1tmp1 = dealPrice(k1, v1)[0]
  3371. }
  3372. if nameM != "brandname" && endStrMap[nameM] == "" {
  3373. endStrMap[nameM] = v1tmp1
  3374. }
  3375. strcount1++
  3376. }
  3377. }
  3378. //替换其它要保存字段
  3379. if !match {
  3380. for nameR, r := range brandRule["replace"] {
  3381. if convert(k1, r) { //匹配成功
  3382. v1tmp2 := v1
  3383. //totalprice
  3384. if nameR == "totalprice" { //处理金额
  3385. v1tmp2 = dealPrice(k1, v1)[0]
  3386. }
  3387. //number
  3388. if nameR == "number" { //处理数量
  3389. varr1, uname1 := dealNumber(v1)
  3390. v1tmp2 = varr1[0]
  3391. //从number中获取到的单位
  3392. if endStrMap["unitname"] == "" && uname1[0] != "" {
  3393. endStrMap["unitname"] = uname1[0]
  3394. }
  3395. }
  3396. if v1tmp2 != "" {
  3397. endStrMap[nameR] = v1tmp2
  3398. }
  3399. strcount2++
  3400. }
  3401. }
  3402. }
  3403. //}
  3404. }
  3405. return strcount1, strcount2, endStrMap
  3406. }
  3407. //数组匹配商品规则
  3408. func (table *Table) matchMapArrBrandRule(aMap *SortMap, brandRule map[string]map[string]string, ka map[string][]string, arrcount1, arrcount2 int) (int, int, map[string][]string) {
  3409. for _, k0 := range aMap.Keys {
  3410. match := false //记录must是否匹配到
  3411. v0 := aMap.Map[k0].([]string)
  3412. //匹配必须title
  3413. for nameM, r := range brandRule["must"] {
  3414. if convert(k0, r) { //匹配成功
  3415. v0tmp1 := v0
  3416. match = true
  3417. if len(ka[nameM]) != 0 && strings.Contains(k0, "描述") { //防止k0匹配到多次 和特殊情况 物料名称 物料描述同时出现
  3418. continue
  3419. }
  3420. if nameM == "itemname" || nameM == "modal" {
  3421. hasGoods(table, v0...) //判断itemname和modal中有没有商品
  3422. if nameM == "itemname" {
  3423. v0tmp1 = filterItem(v0...) //过滤itemname
  3424. }
  3425. }
  3426. if nameM == "brandname" || nameM == "modal" {
  3427. if len(ka["brandname"]) == 0 {
  3428. brand, allNull := hasBrand(table, v0...)
  3429. if !allNull {
  3430. ka["brandname"] = brand
  3431. }
  3432. }
  3433. }
  3434. //unitprice
  3435. if nameM == "unitprice" { //处理金额
  3436. v0tmp1 = dealPrice(k0, v0...)
  3437. }
  3438. if nameM != "brandname" && len(ka[nameM]) == 0 {
  3439. ka[nameM] = v0tmp1
  3440. }
  3441. arrcount1++
  3442. }
  3443. }
  3444. //替换其它要保存字段
  3445. if !match { //must未匹配,匹配replace
  3446. for nameR, r := range brandRule["replace"] {
  3447. if convert(k0, r) { //匹配成功
  3448. v0tmp2 := v0
  3449. //totalprice
  3450. if nameR == "totalprice" { //处理金额
  3451. v0tmp2 = dealPrice(k0, v0...)
  3452. }
  3453. //number
  3454. if nameR == "number" { //处理数量
  3455. uname0 := []string{}
  3456. v0tmp2, uname0 = dealNumber(v0...)
  3457. if len(ka["unitname"]) == 0 && len(uname0) != 0 {
  3458. ka["unitname"] = uname0
  3459. }
  3460. }
  3461. if len(v0tmp2) > 0 {
  3462. ka[nameR] = v0tmp2
  3463. }
  3464. arrcount2++
  3465. }
  3466. }
  3467. }
  3468. }
  3469. return arrcount1, arrcount2, ka
  3470. }
  3471. //初始化lineMapArr,lineMap
  3472. func initLineMapLineMapArr(table *Table) (lineMapArr map[string]*SortMap, lineMap map[string]*SortMap) {
  3473. lineMapArr = make(map[string]*SortMap)
  3474. lineMap = make(map[string]*SortMap)
  3475. for _, key := range table.SortKV.Keys { //遍历table.SortKV.Keys而不是直接遍历table.SortKV.Map是为了得到table头的顺序
  3476. val := table.SortKV.Map[key]
  3477. key = regReplAllSpace.ReplaceAllString(key, "")
  3478. key = strings.Replace(key, "", "", -1) //处理一个特殊的采购量 经上层处理空格后未处理掉
  3479. //qutil.Debug(key, "---------------------------", val)
  3480. if realTypeVal, ok := val.([]string); ok { //val为数组 {"数量":["1","2","3"]}
  3481. /*
  3482. {
  3483. "商品":["",""],
  3484. "商品_"["",""],
  3485. }
  3486. */
  3487. valArr, allempty := filterVal(realTypeVal...) //过滤数据
  3488. if allempty {
  3489. continue
  3490. }
  3491. realTypeVal = valArr
  3492. line := underline.FindString(key)
  3493. lineValMap1 := lineMapArr[line]
  3494. // i := 1
  3495. // L:
  3496. // for { //去除数组空数据
  3497. // last := realTypeVal[len(realTypeVal)-i]
  3498. // if last == "" {
  3499. // i++
  3500. // if i > len(realTypeVal) {
  3501. // break
  3502. // }
  3503. // goto L
  3504. // } else {
  3505. // break
  3506. // }
  3507. // }
  3508. // dislodgeNull := realTypeVal[:(len(realTypeVal) - i + 1)] //去除数组中空数据
  3509. if len(realTypeVal) > 0 {
  3510. if lineValMap1 == nil {
  3511. tmp := NewSortMap()
  3512. tmp.AddKey(key, realTypeVal)
  3513. lineMapArr[line] = tmp
  3514. } else {
  3515. lineValMap1.AddKey(key, realTypeVal)
  3516. }
  3517. }
  3518. //qutil.Debug("lineMapArr---", lineMapArr[line].Keys, lineMapArr[line].Map)
  3519. } else if realTypeVal, b := val.(string); b { //val为字符串 {"数量":"1"}
  3520. /*
  3521. {
  3522. "商品:"",名称:"",
  3523. "商品_:"",名称_:"",
  3524. "商品__:"",名称__:"",
  3525. }
  3526. */
  3527. valArr, allempty := filterVal(realTypeVal) //过滤数据
  3528. if allempty {
  3529. continue
  3530. }
  3531. realTypeVal = valArr[0]
  3532. line := underline.FindString(key)
  3533. lineValMap2 := lineMap[line]
  3534. if lineValMap2 == nil {
  3535. tmp := NewSortMap()
  3536. tmp.AddKey(key, realTypeVal)
  3537. lineMap[line] = tmp
  3538. } else {
  3539. lineValMap2.AddKey(key, realTypeVal)
  3540. }
  3541. //qutil.Debug("lineMap---", lineMap[line].Keys, lineMap[line].Map)
  3542. } else {
  3543. // "_id" : ObjectId("5c2c3802a5cb26b9b78646c4")5c2b0551a5cb26b9b7cb05db否5c2a42e6a5cb26b9b763ba5a采购人:一、采购人5c2b06f5a5cb26b9b7cc4409
  3544. //成交供应商排名 [map[entname:昆明合优科技有限公司 sortstr:第一中标候选人 sort:1] map[sort:2 entname:昆明厚起科技有限公司 sortstr:第二中标候选人] map[entname:云南远安科技发展有限公司 sortstr:第三中标候选人 sort:3]]
  3545. //qutil.Debug("err data:", key, val)
  3546. }
  3547. }
  3548. return lineMapArr, lineMap
  3549. }
  3550. func dealArrData(maxNum int, ka map[string][]string) []map[string]string {
  3551. for k2, v2 := range ka {
  3552. //处理数组长度不相等,使长度一致
  3553. if len(v2) > maxNum {
  3554. ka[k2] = v2[:maxNum]
  3555. }
  3556. }
  3557. finalData := assembleData(ka, 1)
  3558. if len(finalData) > 0 {
  3559. return finalData
  3560. }
  3561. return nil
  3562. }
  3563. func dealStrData(kv map[string]string) []map[string]string {
  3564. finalData := []map[string]string{}
  3565. if len(kv) > 0 {
  3566. finalData = assembleData(kv, 2)
  3567. }
  3568. return finalData
  3569. }
  3570. //组装数据,每一行的数据为一数据集合
  3571. func assembleData(m interface{}, n int) []map[string]string {
  3572. defer qutil.Catch()
  3573. /*
  3574. {
  3575. "itemname":["计算机","打印机","机柜"],
  3576. "number" :["1","12","4"]
  3577. }
  3578. */
  3579. datas := []map[string]string{}
  3580. if n == 1 { //数组数据
  3581. realTypeM := m.(map[string][]string)
  3582. //根据数组数据的顺序 将多个数组中索引相同的数据拼装成一个map,并将这多个map放入一个arr
  3583. /*
  3584. arr1 ["a1","b1","c1"]
  3585. arr2 ["a2","b2","c2"]
  3586. [
  3587. {"a1","a2"},
  3588. {"b1","b2"},
  3589. {"c1","c2"}
  3590. ]
  3591. */
  3592. //start
  3593. for k3, v3 := range realTypeM {
  3594. for _, val := range v3 {
  3595. data := make(map[string]string)
  3596. data[k3] = val
  3597. datas = append(datas, data)
  3598. }
  3599. break
  3600. }
  3601. for i, data := range datas {
  3602. for k4, v4 := range realTypeM {
  3603. if i < len(v4) { //数组数据长度不一致
  3604. if v4[i] != " " {
  3605. data[k4] = v4[i]
  3606. } else {
  3607. delete(data, k4)
  3608. }
  3609. } else {
  3610. fmt.Println("err table")
  3611. }
  3612. }
  3613. datas[i] = data
  3614. }
  3615. //end
  3616. for _, fdv := range datas { //清除空数据和只含特殊符号的数据
  3617. for fmk, fmv := range fdv {
  3618. if tabletdclear.ReplaceAllString(fmv, "") == "" {
  3619. delete(fdv, fmk)
  3620. }
  3621. }
  3622. }
  3623. } else { //字符串数据
  3624. realTypeM := m.(map[string]string)
  3625. datas = append(datas, realTypeM)
  3626. }
  3627. return datas
  3628. }
  3629. ////组装数据,每一行的数据为一数据集合
  3630. //func assembleData(m interface{}, n int) []map[string]string {
  3631. // defer qutil.Catch()
  3632. // /*
  3633. // {
  3634. // "itemname":["计算机","打印机","机柜"],
  3635. // "number" :["1","12","4"]
  3636. // }
  3637. // */
  3638. // datas := []map[string]string{}
  3639. // switch reflect.TypeOf(m).String() {
  3640. // case "map[string][]string": //数组数据
  3641. // realTypeM := m.(map[string][]string)
  3642. // //根据数组数据的顺序 将多个数组中索引相同的数据拼装成一个map,并将这多个map放入一个arr
  3643. // /*
  3644. // arr1 ["a1","b1","c1"]
  3645. // arr2 ["a2","b2","c2"]
  3646. // [
  3647. // {"a1","a2"},
  3648. // {"b1","b2"},
  3649. // {"c1","c2"}
  3650. // ]
  3651. // */
  3652. // //start
  3653. // for k3, v3 := range realTypeM {
  3654. // for _, val := range v3 {
  3655. // data := make(map[string]string)
  3656. // data[k3] = val
  3657. // datas = append(datas, data)
  3658. // }
  3659. // break
  3660. // }
  3661. // for i, data := range datas {
  3662. // for k4, v4 := range realTypeM {
  3663. // if i < len(v4) { //数组数据长度不一致
  3664. // if v4[i] != " " {
  3665. // data[k4] = v4[i]
  3666. // } else {
  3667. // delete(data, k4)
  3668. // //continue
  3669. // }
  3670. // } else {
  3671. // fmt.Println("err table")
  3672. // //continue
  3673. // }
  3674. // }
  3675. // datas[i] = data
  3676. // }
  3677. // //end
  3678. // // for _, fdv := range datas { //清除空数据和只含特殊符号的数据
  3679. // // for fmk, fmv := range fdv {
  3680. // // if tabletdclear.ReplaceAllString(fmv, "") == "" {
  3681. // // delete(fdv, fmk)
  3682. // // }
  3683. // // }
  3684. // // }
  3685. // case "map[string]string": //字符串数据
  3686. // realTypeM := m.(map[string]string)
  3687. // datas = append(datas, realTypeM)
  3688. // default:
  3689. // }
  3690. // return datas
  3691. //}
  3692. func convert(key, r string) bool {
  3693. defer qutil.Catch()
  3694. flag := false
  3695. key = tabletitleclear.ReplaceAllString(key, "")
  3696. reg, err := regexp.Compile(r)
  3697. if err != nil {
  3698. fmt.Println("reg err:", err)
  3699. return false
  3700. }
  3701. flag = reg.MatchString(key)
  3702. return flag
  3703. }
  3704. func hasKey(table *Table, n int) {
  3705. defer qutil.Catch()
  3706. if table.TableResult.HasKey == 1 {
  3707. return
  3708. }
  3709. if n >= 1 {
  3710. table.TableResult.HasKey = 1
  3711. }
  3712. }
  3713. func hasGoods(table *Table, data ...string) {
  3714. defer qutil.Catch()
  3715. goodsArr := make([]string, len(data))
  3716. //fmt.Println("table.TableResult.HasGoods=====", table.TableResult.HasGoods)
  3717. if table.TableResult.HasGoods == 1 {
  3718. return
  3719. }
  3720. for i, d := range data {
  3721. if d != "" {
  3722. goods := u.GoodsGet.CheckSensitiveWord(d)
  3723. //fmt.Println("goods======", goods)
  3724. goodsArr[i] = goods
  3725. if len(goods) > 0 {
  3726. table.TableResult.HasGoods = 1
  3727. break
  3728. }
  3729. }
  3730. }
  3731. }
  3732. //func hasBrand(table *Table, data ...string) {
  3733. // defer qutil.Catch()
  3734. // if table.TableResult.HasBrand == 1 {
  3735. // return
  3736. // }
  3737. // for i, d := range data {
  3738. // if d != "" {
  3739. // brand := u.BrandGet.CheckSensitiveWord(d)
  3740. // qutil.Debug(d, brand)
  3741. // if brand != "" {
  3742. // table.TableResult.HasBrand = 1
  3743. // break
  3744. // }
  3745. // }
  3746. // }
  3747. //}
  3748. func hasBrand(table *Table, data ...string) ([]string, bool) {
  3749. defer qutil.Catch()
  3750. //fmt.Println("table.TableResult.HasBrand---------", table.TableResult.HasBrand)
  3751. brandArr := make([]string, len(data))
  3752. // if table.TableResult.HasBrand == 1 {
  3753. // return brandArr, 1
  3754. // }
  3755. allNull := true
  3756. for i, d := range data {
  3757. //if d != "" {
  3758. brand := u.BrandGet.CheckSensitiveWord(d)
  3759. if brand != "" {
  3760. allNull = false
  3761. }
  3762. //fmt.Println("brand======", brand)
  3763. brandArr[i] = brand
  3764. if len(brand) > 0 {
  3765. table.TableResult.HasBrand = 1
  3766. }
  3767. //}
  3768. }
  3769. return brandArr, allNull
  3770. }
  3771. var clearnn *regexp.Regexp = regexp.MustCompile("([\\d.]*)[\\n\\s]*[\\((][\\d.]+[)\\)]")
  3772. //过滤td值
  3773. func filterVal(val ...string) ([]string, bool) {
  3774. defer qutil.Catch()
  3775. n := 0 //记录被过滤的个数
  3776. for i, v := range val {
  3777. if len(clearnn.FindStringSubmatch(v)) > 0 {
  3778. tmpv := clearnn.FindStringSubmatch(v)[1]
  3779. if tmpv != "" {
  3780. v = tmpv
  3781. }
  3782. }
  3783. afterFilter := tabletdclear.ReplaceAllString(v, "")
  3784. afterFilter = NullVal.ReplaceAllString(afterFilter, "")
  3785. if afterFilter == "" {
  3786. n++
  3787. }
  3788. val[i] = afterFilter
  3789. }
  3790. allempty := false
  3791. if n == len(val) { //所有都被过滤掉
  3792. allempty = true
  3793. }
  3794. return val, allempty
  3795. }
  3796. //过滤itemname全是数字
  3797. func filterItem(itemval ...string) []string {
  3798. defer qutil.Catch()
  3799. result := []string{}
  3800. for _, v := range itemval {
  3801. afterFilter := numclear.ReplaceAllString(v, "")
  3802. if afterFilter != "" {
  3803. result = append(result, v)
  3804. } else {
  3805. result = append(result, afterFilter)
  3806. }
  3807. }
  3808. return result
  3809. }
  3810. //处理价格
  3811. func dealPriceInterface(key string, val ...string) (result []interface{}) {
  3812. defer qutil.Catch()
  3813. for _, v := range val {
  3814. if num1.MatchString(v) { //含数字
  3815. tdIsWan := strings.Contains(v, "万")
  3816. if !tdIsWan {
  3817. if strings.Contains(key, "万") {
  3818. v = v + "万"
  3819. }
  3820. }
  3821. data := []interface{}{v, ""}
  3822. money := clear.ObjToMoney(data)[0]
  3823. result = append(result, money)
  3824. } else {
  3825. result = append(result, "")
  3826. }
  3827. }
  3828. return
  3829. }
  3830. //处理number
  3831. func dealNumberInterface(val ...string) (result []interface{}) {
  3832. defer qutil.Catch()
  3833. for _, v := range val { //1个 1.00个
  3834. n := numclear.FindString(v)
  3835. if n == "" {
  3836. result = append(result, "")
  3837. } else if tmp := clear.NumChar[n]; tmp != nil { //一二三...
  3838. result = append(result, tmp)
  3839. } else { //数字
  3840. result = append(result, qutil.IntAll(strings.Split(n, ".")[0]))
  3841. }
  3842. }
  3843. return
  3844. }
  3845. //处理价格
  3846. func dealPrice(key string, val ...string) []string {
  3847. defer qutil.Catch()
  3848. result := []string{}
  3849. for _, v := range val {
  3850. data := []interface{}{v, key}
  3851. money := clear.ObjToMoney(data)[0]
  3852. result = append(result, fmt.Sprintf("%v", money))
  3853. }
  3854. // result := []string{}
  3855. // for _, v := range val { //1.00万元 1元 2.25元/斤
  3856. // tmparr := strings.Split(v, ".")
  3857. // tmparr[0] = moneyNum.ReplaceAllString(tmparr[0], "")
  3858. // if iswan {
  3859. // result = append(result, tmparr[0]+"0000")
  3860. // } else { //td val值带万
  3861. // if strings.Contains(v, "万") { //价格中带有万
  3862. // result = append(result, tmparr[0]+"0000")
  3863. // } else {
  3864. // result = append(result, tmparr[0])
  3865. // }
  3866. // }
  3867. // }
  3868. return result
  3869. }
  3870. //处理number
  3871. func dealNumber(val ...string) ([]string, []string) {
  3872. defer qutil.Catch()
  3873. unitnameArr := []string{}
  3874. result := []string{}
  3875. for _, v := range val { //1个 1.00个
  3876. n := numclear.FindString(v)
  3877. unitname := numclear.ReplaceAllString(v, "") //匹配个数后的单位
  3878. unitnameArr = append(unitnameArr, unitname)
  3879. //val[i] = strings.Split(n, ".")[0]
  3880. result = append(result, strings.Split(n, ".")[0])
  3881. }
  3882. return result, unitnameArr
  3883. }
  3884. func (tn *Table) analyProNameAndItemNumber() {
  3885. }