使用lucene query的CharFilter 去掉字符中的script脚本和html标签

1.准备数据,这里我从数据库读取一个带有html标签和script脚本的数据

代码:

@Before
    public void init(){
        SQLService sqlService = new SQLService();
        sqlService.regist(null);
        BaseDao bd = new BaseDao();
        String sql = "select * from t where title like ‘% 每天读一遍,舌头更无敌%‘";
        lists = bd.getList(sql);
        System.out.println(lists.size());
        content = lists.get(0).get("content").toString();
//        System.out.println(content);

    }

2. 使用字符过滤器-HTMLStripCharFilter 和 MappingCharFilter.由于这些字符过滤器都是继承Reader的.所以可以像读取reader那样处理.

代码:

    @Test
    public void test2() throws IOException{

        StringBuilder sb = new StringBuilder();
        // html过滤
        HTMLStripCharFilter htmlscript = new HTMLStripCharFilter(new StringReader(content));

        //增加映射过滤  主要过滤掉换行符
        NormalizeCharMap.Builder builder = new NormalizeCharMap.Builder();
        builder.add( "\r", "" );//回车
        builder.add( "\t", "" );//横向跳格
        builder.add( "\n", "" );//换行
        CharFilter cs = new MappingCharFilter( builder.build(),htmlscript );

        char[] buffer = new char[10240];
        int count;
        while ((count = cs.read(buffer)) != -1) {
            sb.append(new String(buffer, 0, count));
        }
        System.out.println(sb.toString());
        cs.close();

//        String keywords = HanLP.extractKeyword(sb.toString(), 20).toString();
//        System.out.println(keywords);
    }

处理结果:

亲爱的小伙伴们,累了,就放松一下吧!1. Can you can a can as a canner can can a can?­你能够像罐头工人一样装罐头吗?­2. I wish to wish the wish you wish to wish, but if you wish the wish the witch wishes, I won‘t wish the wish you wish to wish.­  我希望梦想着你梦想中的梦想,但是如果你梦想着女巫的梦想,我就不想梦想着你梦想中的梦想。­3. I scream, you scream, we all scream for ice-cream!­  我叫喊,你叫喊,我们都喊着要冰淇淋!­4. How many cookies could a good cook cook if a good cook could cook cookies?­  A good cook could cook as much cookies as a good cook who could cook cookies.­  如果一个好的厨师能做小甜饼,那么他能做多少小甜饼呢?一个好的厨师能做出和其它好厨师一样多的小甜饼。­5. The driver was drunk and drove the doctor‘s car directly into the deep ditch. 这个司机喝醉了,他把医生的车开进了一个大深沟里。­6. Whether the weather be fine or whether the weather be not.­Whether the weather be cold or whether the weather be hot.­We‘ll weather the weather whether we like it or not.­无论是晴天或是阴天。­无论是冷或是暖,­不管喜欢与否,我们都要经受风霜雨露。­7. Peter Piper picked a peck of pickled peppers.­  A peck of pickled peppers Peter Piper picked.­  If Peter Piper picked a peck of pickled peppers,­  Where‘s the peck of pickled peppers Peter Piper picked?­  彼德派柏捏起一撮泡菜。­  彼德派柏捏起的是一撮泡菜。­  那么彼德派捏起的泡菜在哪儿?­8. I thought a thought. But the thought I thought wasn‘t the thought I thought I thought.­  If the thought I thought I thought had been the thought I thought, I wouldn‘t have thought so much.­  我有一种想法,但是我的这种想法不是我曾经想到的那种想法。如果这种想法是我曾经想到的想法,我就不会想那么多了。­9. Amid the mists and coldest frosts,­  With barest wrists and stoutest boasts,­  He thrusts his fists against the posts,­  And still insists he sees the ghosts.­  雾蒙蒙,冰霜冻,­  手腕儿空空,话儿涌,­  只见他猛所拳头往柱子上砸,­  直说自己把鬼碰。­10. Badmin was able to beat Bill at billiards, but Bill always beat Badmin badly at badminton.­   巴德明在台球上能够打败比尔,但是打羽毛球比尔常常大败巴德明。­11. Betty beat a bit of butter to make a better butter.­   贝蒂敲打一小块黄油要做一块更好的奶油面。­12. Rita repeated what Reardon recited when Reardon read the remarks.­  
时间: 2024-08-29 09:39:28

使用lucene query的CharFilter 去掉字符中的script脚本和html标签的相关文章

在html中添加script脚本的方法和注意事项

在html中添加script脚本有两种方法,直接将javascript代码添加到html中与添加外部js文件,这两种方法都比较常用,大家可以根据自己需要自由选择 在html中添加<script>脚本的方法: 1.可以直接将javascript代码添加到html中 <script type="text/javascript"> //javascritp代码 </script> 当解释器嵌入<script>代码时,html页面的处理也会被暂时

(五)CodeMirror - 关于htmlmixed中包含script脚本

最近发现个问题,场景如下: 当创建的mode类型为htmlmixed,且内容中包含javascript脚本,且是闭包立即执行: 如果内容是使用JQuery函数.html()插入到DOM中后再创建codeMirror对象: 那么,这时,如果JS执行报错,那么随后创建codeMirror对象也中断了: 解决方法: 可以使用elem.text()方法插入内容,那么有错的JS也不会被执行到,可顺利进行后面的创建 1 var elem = $('#code'); 2 var str = ''<div cl

js 去掉字符中两边的空格

function trim(str){ var string=''; var a = 0; var b = 0; var arr = []; for(var i=0;i<str.length;i++){ if(str.charCodeAt(i) == 32||str.charCodeAt(i)==12288){ a = i+1; }else{ break; } } for(var i=str.length-1;i>=0;i--){ if(str.charCodeAt(i) == 32||str

js 去掉字符中所有空格

function trimAll(str){ var string=''; var arr = []; for(var i=0;i<str.length;i++){ if(str.charCodeAt(i) == 32||str.charCodeAt(i) == 12288){ continue; }else{ arr.push(str[i]); } } return string = arr.join(''); }

去掉字符串中的空格

1)Trim方法 string   tt=" aaa "; tt=tt.Trim()       去字符串首尾空格的函数 tt=tt.TrimEnd() 去掉字符串尾空格 tt=tt.TrimStart() 去掉字符串首空格 (2)通过ASCII码值去掉字符中的空格 由于空格的ASCII码值是32,因此,在去掉字符串中所有的空格时,只需循环访问字符串中的所有字符,并判断它们的ASCII码值是不是32即可.去掉字符串中所有空格的关键代码如下: CharEnumerator CEnumer

17、如何对字符串进行左, 右, 居中对齐 18、如何去掉字符串中不需要的字符 19、如何读写文本文件 20、如何处理二进制文件 21、如何设置文件的缓冲

17.如何对字符串进行左, 右, 居中对齐 info = "GBK" print(info.ljust(20)) print(info.ljust(20,'#')) print(info.rjust(20,'#')) print(info.center(20,"#")) print(format(info,'<20')) print(format(info,'>20')) print(format(info,'^20')) result: GBK GBK

转:假设有一个字符串aabcad,请编写一段程序,去掉字符串中不相邻的重复字符。

假设有一个字符串aabcad,请编写一段程序,去掉字符串中不相邻的重复字符.即上述字串处理之后结果是为:aabcd; 分析,重点考查 char 与int 的隐式转换.程序如下: -(void) removeRepeat:(NSString *)aNum { NSMutableArray *mArr = [[NSMutableArray alloc]initWithCapacity:10]; for(int i = 0; i<aNum.length; i++) { [mArr addObject:

问题19:如何去掉字符串中不需要的字符

案例一:过滤空白字符: 案例二:过滤\r字符: 案例三:去掉文本中的unicode组合符:拼音的音调: #具体实现,参加:Python:删除字符串中的字符 方案一:字符串strip().lstrip().rstrip()方法,去掉字符串两端字符: 方案二:使用切片 + 拼接的方式,删除单个固定位置的字符: 方案三:字符串的replace()方法,或者正则表达式re.sub(),删除任意位置字符: 方案四:字符串translate()方法,可以同时删除多种不同字符: 原文地址:https://ww

C#(去、过滤)掉字符中的换行符

原文:C#(去.过滤)掉字符中的换行符 原文地址: http://www.feedou.com/articlepickservlet?commandkey=singlearticle&articleID=5c42b75f1b435084011b49ba083217d4 本文介绍的方法需要先导入命名空间:usingSystem.Text.RegularExpressions; 字符串里所有的的换行符都去掉:textStr = Regex.Replace(textStr, @"[/n/r]&