analytable.go 107 KB

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