analytable.go 118 KB

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