免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
123下一页
最近访问板块 发新帖
查看: 4048 | 回复: 28
打印 上一主题 下一主题

[文本处理] 随机插入到文章? [复制链接]

论坛徽章:
1
白羊座
日期:2014-11-13 10:19:16
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-06-17 12:02 |只看该作者 |倒序浏览
有6个文章如下:
我命名为ccc1-a.db  ~ ccc6-a.db
  1. Now we have both options where you can get a dedicated reader for just your car or a generic one for OBD2. The availability of OBD1 readers available over the counter at local retailers is beginning to subside since these cars are becoming less the norm today. Most cars on the road have OBD2 and therefore you can get this kind of reader at almost any auto parts store. Norton was stingy with details (he usually is), saying simply: light of recent events, we added additional security procedures to our tournament practice to raise the level of security around chips to an even greater standard. We using multiple verification methods that didn exist previously. Poker room at Maryland Live uses multiple chip sets for tournaments. It an issue Warren is sensitive about, especially right now, as he makes a break from Dobson ministry. In late May, after Warren began to publicly distance himself from Focus on the Family, Dobson announced a formal separation of sorts on his radio program. It a significant split; the conservative, evangelical community nourished Warren nascent business, and now he appears to be leaving it behind for the secular world.
  2. Kona coffee is perhaps one of the true treasures of Hawaii. More to many than the vast natural beauty of these islands is the wonderfully delicious coffee drink that is found in its purest form within these island chains. In fact, if you interview several coffee connoisseurs about their favorite type of coffee bean, at some point in the interview process you will find that a vast majority fined the time to work in Kona.. From Figure 1, does pegging of Malaysian Ringgit which is causing of reducing the money supply in the market help to stabilize the KLCI? Reducing the money supply might help to stabilize the KLCI, which we will be looking onto in this study. This period has shown that the government policy has come in fruitful. In this period, the KLCI price volatility is slightly managed. It wasn so long ago that the iPhone was predicted to capture the smartphone market and never let go. With a one of a kind device, almost nobody expected a platform to rise to knock Apple from its perch. Along came Google Android, running on a multitude of handsets, offered on every major mobile carrier, and the fight was on.
  3. All want heroes, so we thump our chest and proclaim the kind of knight in shining armor coming over the horizon, saving the day, Shibley said in a phone interview last week. There a lot of people who can stand up and appropriately thump their chests and should be deeply respected for their enormous contributions to this. But ultimately, it a team sport and runs across a broad cross section of our community: public, private, not for profit, at every level of tiny to really substantial. The content, however, is about the same for all men: the ejaculate contains sperm and a long list of components that read like a vitamin pill label: ascorbic acid (vitamin C), calcium, chlorine, cholesterol, choline, citric acid, creatine, fructose, glutathione, hyaluronidase, inositol, lactic acid, magnesium, nitrogen, phosphorus, potassium, purine, pyrimidine, pyruvic acid, sodium, sorbitol, vitamin B12, and zinc. Each of these ingredients, though, represents just a tiny amount   and the total measure of a typical "load" is somewhere between 1.5 ml and 5 ml. The majority of the ejaculate volume is fluid from the prostate and the seminal vesicles.
  4. ...
复制代码
另一个文本为:1.txt
  1. AAA
  2. BBB
  3. CCC
  4. DDD
  5. EEE
  6. FFF
  7. ...
复制代码
我想将每一篇文章的第一段和第二段合并成一段,然后以 . (就是以句子的句号为分割点),顺序从1.txt从选择3~4个单词随机插入第一篇文章到. 的后面
单词的前后加上< b>< \b>

论坛徽章:
39
辰龙
日期:2013-08-21 15:45:192015亚冠之广州富力
日期:2015-05-12 16:34:52亥猪
日期:2015-03-03 17:22:00申猴
日期:2015-03-03 17:21:37未羊
日期:2014-10-10 13:45:41戌狗
日期:2014-06-17 09:53:29巨蟹座
日期:2014-06-12 23:17:17双鱼座
日期:2014-06-10 12:42:44寅虎
日期:2014-06-09 12:52:172015亚冠之卡尔希纳萨夫
日期:2015-05-24 15:24:35黄金圣斗士
日期:2015-12-02 17:25:0815-16赛季CBA联赛之吉林
日期:2017-06-24 16:43:52
2 [报告]
发表于 2014-06-17 13:59 |只看该作者
本帖最后由 关阴月飞 于 2014-06-17 13:59 编辑

没看明白,想得到的结果是什么??举个栗子可好?

论坛徽章:
769
金牛座
日期:2014-02-26 17:49:58水瓶座
日期:2014-02-26 18:10:15白羊座
日期:2014-04-15 19:29:52寅虎
日期:2014-04-17 19:43:21酉鸡
日期:2014-04-19 21:24:10子鼠
日期:2014-04-22 13:55:24卯兔
日期:2014-04-22 14:20:58亥猪
日期:2014-04-22 16:13:09狮子座
日期:2014-05-05 22:31:17摩羯座
日期:2014-05-06 10:32:53处女座
日期:2014-05-12 09:23:11子鼠
日期:2014-05-21 18:21:27
3 [报告]
发表于 2014-06-17 14:10 |只看该作者
大神都没懂,我们更不清楚了,没例子不确定因素太多。

论坛徽章:
1
白羊座
日期:2014-11-13 10:19:16
4 [报告]
发表于 2014-06-17 14:24 |只看该作者
本帖最后由 iocg 于 2014-06-17 14:29 编辑

回复 2# 关阴月飞


    那简单的例子。现在只要将1.txt的前3个顺序提取,随机插入到首段文章的句子后面。

    如下是ccc1-a.db第一篇文章

    Now we have both options where you can get a dedicated reader for just your car or a generic one for OBD2. 1The availability of OBD1 readers available over the counter at local retailers is beginning to subside since these cars are becoming less the norm today. 2Most cars on the road have OBD2 and therefore you can get this kind of reader at almost any auto parts store. 3Norton was stingy with details (he usually is), saying simply: light of recent events, we added additional security procedures to our tournament practice to raise the level of security around chips to an even greater standard. 4We using multiple verification methods that didn exist previously. 5Poker room at Maryland Live uses multiple chip sets for tournaments. 6It an issue Warren is sensitive about, especially right now, as he makes a break from Dobson ministry. 7In late May, after Warren began to publicly distance himself from Focus on the Family, Dobson announced a formal separation of sorts on his radio program. 8It a significant split; the conservative,evangelical community nourished Warren nascent business, and now he appears to be leaving it behind for the secular world.

    有8个句子,提取1.txt的AAA  BBB  CCC 先变成<b>AAA</b>  <b>BBB</b>  <b>CCC</b>
随机插入的到8个句子其中3个句子的后面

    Now we have both options where you can get a dedicated reader for just your car or a generic one for OBD2. The availability of OBD1 readers available over the counter at local retailers is beginning to subside since these cars are becoming less the norm today. <b>AAA</b>Most cars on the road have OBD2 and therefore you can get this kind of reader at almost any auto parts store. Norton was stingy with details (he usually is), saying simply: light of recent events, we added additional security procedures to our tournament practice to raise the level of security around chips to an even greater standard. We using multiple verification methods that didn exist previously. Poker room at Maryland Live uses multiple chip sets for tournaments. <b>BBB</b>It an issue Warren is sensitive about, especially right now, as he makes a break from Dobson ministry. In late May, after Warren began to publicly distance himself from Focus on the Family, Dobson announced a formal separation of sorts on his radio program. <b>CCC</b>It a significant split; the conservative,evangelical community nourished Warren nascent business, and now he appears to be leaving it behind for the secular world.

现在有ccc1-a.db ~ ccc6-a.db 对第二个db就是提取DDD EEE FFF以此类推

论坛徽章:
769
金牛座
日期:2014-02-26 17:49:58水瓶座
日期:2014-02-26 18:10:15白羊座
日期:2014-04-15 19:29:52寅虎
日期:2014-04-17 19:43:21酉鸡
日期:2014-04-19 21:24:10子鼠
日期:2014-04-22 13:55:24卯兔
日期:2014-04-22 14:20:58亥猪
日期:2014-04-22 16:13:09狮子座
日期:2014-05-05 22:31:17摩羯座
日期:2014-05-06 10:32:53处女座
日期:2014-05-12 09:23:11子鼠
日期:2014-05-21 18:21:27
5 [报告]
发表于 2014-06-17 14:45 |只看该作者
回复 4# iocg
按我对你1楼文本的观察,6篇文章分别命名为
1.db, 2.db,... 6.db,每篇文章内容都是单独的一行,
两篇文章之间以换行来划分?

   

论坛徽章:
1
白羊座
日期:2014-11-13 10:19:16
6 [报告]
发表于 2014-06-17 15:54 |只看该作者
本帖最后由 iocg 于 2014-06-17 15:56 编辑

回复 5# Herowinter


    是的。我在想用AWK 应该很容易实现,将. 作为域分隔符。
   
    先读取1.txt的内容到数组a,产生一个随机数k1,$k1=$k1 a\[i\]  (i=1~3)....

    不过我比较笨~~

论坛徽章:
769
金牛座
日期:2014-02-26 17:49:58水瓶座
日期:2014-02-26 18:10:15白羊座
日期:2014-04-15 19:29:52寅虎
日期:2014-04-17 19:43:21酉鸡
日期:2014-04-19 21:24:10子鼠
日期:2014-04-22 13:55:24卯兔
日期:2014-04-22 14:20:58亥猪
日期:2014-04-22 16:13:09狮子座
日期:2014-05-05 22:31:17摩羯座
日期:2014-05-06 10:32:53处女座
日期:2014-05-12 09:23:11子鼠
日期:2014-05-21 18:21:27
7 [报告]
发表于 2014-06-17 16:09 |只看该作者
本帖最后由 Herowinter 于 2014-06-17 16:13 编辑

回复 6# iocg
对个人来言,难点在于从1-8中随机取3个不同的数,
还好我做过一道类似的1-5随机数转化为1-7的题。
  1. awk 'BEGIN{FS=".";srand()} NR==FNR{a[count++]=$0;next} {for(i=1;i<=NF-2;i++)b[i];for(i=1;i<=3;i++){do{k=int(rand()*100%(NF-2)+1)}while(!(k in b));c[k];delete b[k]};for(i=1;i<=NF-2;i++){if(i in c)s=$i"<b>"a[n++]"</b>";else s=$i".";printf s};print "";delete b;delete c}' 1.txt i
  2.     Now we have both options where you can get a dedicated reader for just your car or a generic one for OBD2<b>AAA</b> The availability of OBD1 readers available over the counter at local retailers is beginning to subside since these cars are becoming less the norm today. Most cars on the road have OBD2 and therefore you can get this kind of reader at almost any auto parts store<b>BBB</b> Norton was stingy with details (he usually is), saying simply: light of recent events, we added additional security procedures to our tournament practice to raise the level of security around chips to an even greater standard. We using multiple verification methods that didn exist previously. Poker room at Maryland Live uses multiple chip sets for tournaments<b>CCC</b> It an issue Warren is sensitive about, especially right now, as he makes a break from Dobson ministry. In late May, after Warren began to publicly distance himself from Focus on the Family, Dobson announced a formal separation of sorts on his radio program.
  3.     Kona coffee is perhaps one of the true treasures of Hawaii<b>DDD</b> More to many than the vast natural beauty of these islands is the wonderfully delicious coffee drink that is found in its purest form within these island chains. In fact, if you interview several coffee connoisseurs about their favorite type of coffee bean, at some point in the interview process you will find that a vast majority fined the time to work in Kona.. From Figure 1, does pegging of Malaysian Ringgit which is causing of reducing the money supply in the market help to stabilize the KLCI? Reducing the money supply might help to stabilize the KLCI, which we will be looking onto in this study. This period has shown that the government policy has come in fruitful. In this period, the KLCI price volatility is slightly managed<b>EEE</b> It wasn so long ago that the iPhone was predicted to capture the smartphone market and never let go<b>FFF</b> With a one of a kind device, almost nobody expected a platform to rise to knock Apple from its perch.
  4.     All want heroes, so we thump our chest and proclaim the kind of knight in shining armor coming over the horizon, saving the day, Shibley said in a phone interview last week<b>GGG</b> There a lot of people who can stand up and appropriately thump their chests and should be deeply respected for their enormous contributions to this<b>HHH</b> But ultimately, it a team sport and runs across a broad cross section of our community: public, private, not for profit, at every level of tiny to really substantial. The content, however, is about the same for all men: the ejaculate contains sperm and a long list of components that read like a vitamin pill label: ascorbic acid (vitamin C), calcium, chlorine, cholesterol, choline, citric acid, creatine, fructose, glutathione, hyaluronidase, inositol, lactic acid, magnesium, nitrogen, phosphorus, potassium, purine, pyrimidine, pyruvic acid, sodium, sorbitol, vitamin B12, and zinc. Each of these ingredients, though, represents just a tiny amount   and the total measure of a typical "load" is somewhere between 1<b>III</b>5 ml and 5 ml.
复制代码
  1. cat 1.txt
  2. AAA
  3. BBB
  4. CCC
  5. DDD
  6. EEE
  7. FFF
  8. GGG
  9. HHH
  10. III
复制代码

论坛徽章:
769
金牛座
日期:2014-02-26 17:49:58水瓶座
日期:2014-02-26 18:10:15白羊座
日期:2014-04-15 19:29:52寅虎
日期:2014-04-17 19:43:21酉鸡
日期:2014-04-19 21:24:10子鼠
日期:2014-04-22 13:55:24卯兔
日期:2014-04-22 14:20:58亥猪
日期:2014-04-22 16:13:09狮子座
日期:2014-05-05 22:31:17摩羯座
日期:2014-05-06 10:32:53处女座
日期:2014-05-12 09:23:11子鼠
日期:2014-05-21 18:21:27
8 [报告]
发表于 2014-06-17 16:11 |只看该作者
回复 7# Herowinter
这段文本中有个. 代码在这里可能会误判。
  1. between 1.5 ml and 5 ml
复制代码

论坛徽章:
1
白羊座
日期:2014-11-13 10:19:16
9 [报告]
发表于 2014-06-17 16:16 |只看该作者
回复 8# Herowinter


    因为每个句子的后面其实是一个点号 + 一个空格。awk的分割符可以设置成 点空格  吗?

论坛徽章:
769
金牛座
日期:2014-02-26 17:49:58水瓶座
日期:2014-02-26 18:10:15白羊座
日期:2014-04-15 19:29:52寅虎
日期:2014-04-17 19:43:21酉鸡
日期:2014-04-19 21:24:10子鼠
日期:2014-04-22 13:55:24卯兔
日期:2014-04-22 14:20:58亥猪
日期:2014-04-22 16:13:09狮子座
日期:2014-05-05 22:31:17摩羯座
日期:2014-05-06 10:32:53处女座
日期:2014-05-12 09:23:11子鼠
日期:2014-05-21 18:21:27
10 [报告]
发表于 2014-06-17 16:26 |只看该作者
本帖最后由 Herowinter 于 2014-06-17 16:38 编辑

回复 9# iocg
可以啊,再把源代码中的NF-2改成NF-1就可以了,因为按你的需求
最后一个句子是不进行插入的。最后代码这样的。
  1. awk 'BEGIN{FS="\\. ";srand()} NR==FNR{a[count++]=$0;next} {for(i=1;i<=NF-1;i++)b[i];for(i=1;i<=3;i++){do{k=int(rand()*100%(NF-1)+1)}while(!(k in b));c[k];delete b[k]};for(i=1;i<=NF-1;i++){if(i in c)s=$i"<b>"a[n++]"</b>";else s=$i". ";printf s};print $NF;delete b;delete c}' 1.txt i
  2.     Now we have both options where you can get a dedicated reader for just your car or a generic one for OBD2<b>AAA</b>The availability of OBD1 readers available over the counter at local retailers is beginning to subside since these cars are becoming less the norm today<b>BBB</b>Most cars on the road have OBD2 and therefore you can get this kind of reader at almost any auto parts store. Norton was stingy with details (he usually is), saying simply: light of recent events, we added additional security procedures to our tournament practice to raise the level of security around chips to an even greater standard<b>CCC</b>We using multiple verification methods that didn exist previously. Poker room at Maryland Live uses multiple chip sets for tournaments. It an issue Warren is sensitive about, especially right now, as he makes a break from Dobson ministry. In late May, after Warren began to publicly distance himself from Focus on the Family, Dobson announced a formal separation of sorts on his radio program. It a significant split; the conservative, evangelical community nourished Warren nascent business, and now he appears to be leaving it behind for the secular world.
  3.     Kona coffee is perhaps one of the true treasures of Hawaii. More to many than the vast natural beauty of these islands is the wonderfully delicious coffee drink that is found in its purest form within these island chains<b>DDD</b>In fact, if you interview several coffee connoisseurs about their favorite type of coffee bean, at some point in the interview process you will find that a vast majority fined the time to work in Kona.. From Figure 1, does pegging of Malaysian Ringgit which is causing of reducing the money supply in the market help to stabilize the KLCI? Reducing the money supply might help to stabilize the KLCI, which we will be looking onto in this study<b>EEE</b>This period has shown that the government policy has come in fruitful. In this period, the KLCI price volatility is slightly managed<b>FFF</b>It wasn so long ago that the iPhone was predicted to capture the smartphone market and never let go. With a one of a kind device, almost nobody expected a platform to rise to knock Apple from its perch. Along came Google Android, running on a multitude of handsets, offered on every major mobile carrier, and the fight was on.
  4.     All want heroes, so we thump our chest and proclaim the kind of knight in shining armor coming over the horizon, saving the day, Shibley said in a phone interview last week<b>GGG</b>There a lot of people who can stand up and appropriately thump their chests and should be deeply respected for their enormous contributions to this. But ultimately, it a team sport and runs across a broad cross section of our community: public, private, not for profit, at every level of tiny to really substantial<b>HHH</b>The content, however, is about the same for all men: the ejaculate contains sperm and a long list of components that read like a vitamin pill label: ascorbic acid (vitamin C), calcium, chlorine, cholesterol, choline, citric acid, creatine, fructose, glutathione, hyaluronidase, inositol, lactic acid, magnesium, nitrogen, phosphorus, potassium, purine, pyrimidine, pyruvic acid, sodium, sorbitol, vitamin B12, and zinc. Each of these ingredients, though, represents just a tiny amount   and the total measure of a typical "load" is somewhere between 1.5 ml and 5 ml<b>III</b>The majority of the ejaculate volume is fluid from the prostate and the seminal vesicles.
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP