Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2660067
  • 博文数量: 416
  • 博客积分: 10220
  • 博客等级: 上将
  • 技术积分: 4193
  • 用 户 组: 普通用户
  • 注册时间: 2006-12-15 09:47
文章分类

全部博文(416)

文章存档

2022年(1)

2021年(1)

2020年(1)

2019年(5)

2018年(7)

2017年(6)

2016年(7)

2015年(11)

2014年(1)

2012年(5)

2011年(7)

2010年(35)

2009年(64)

2008年(48)

2007年(177)

2006年(40)

我的朋友

分类: C/C++

2007-11-11 09:02:25

#include "stdafx.h"
#include
#include
#include
#include

using namespace std;

typedef pair location;
typedef vector loc;
typedef vector text;
typedef pair text_loc;
typedef map::value_type  valType;

text_loc*  separate_words(const vector *text_file)
{
 vector *words=new vector;
 vector *locations =new vector;
 short line_pos=0; //行(句子条数)

 for(; line_pos < text_file->size(); ++line_pos)
 {
  short word_pos=0; //列(一行单词个数)
  string textline=(*text_file)[line_pos];
  string::size_type pos =0 ,prev_pos=0;
  while(( pos=textline.find_first_of(' ',pos)) !=string::npos)
  {
   words->push_back( textline.substr(prev_pos, pos-prev_pos));
   locations->push_back( make_pair(line_pos,word_pos));
   ++word_pos;  prev_pos=++pos;
  }

  words->push_back( textline.substr(prev_pos,pos-prev_pos));
  locations->push_back( make_pair(line_pos,word_pos));
 }
 return   new text_loc(words,locations);
}


map* build_word_map(const text_loc *text_locations)
{
 map *word_map=new map;
 vector *text_words=text_locations->first;
 vector *text_locs=text_locations->second;

 register int elem_cnt=text_words->size();
 for(int ix=0;ix {
  string textword=(*text_words)[ix];
  if(textword.size()<3)
   continue;
  if(!word_map->count(textword)) //是否有此关键字,无则新加键/值,有则只加新值,形成一键一(多)值关系
  {
   loc *ploc=new vector;
   ploc->push_back((*text_locs)[ix]);
   word_map->insert(valType(textword,ploc));
  }
  else
   (*word_map)[textword]->push_back((*text_locs)[ix]);
 }
 return word_map;
}


void main()

  string ia[4]={"Alice Emma has long",
  "wind blows",
  "bird has flight",
  "Daddy, shush, has"};

 vector  text_file1(ia,ia+4);
 const  vector *text_file=&text_file1;
 const text_loc *text_locations=separate_words(text_file);
 map* map1 =  build_word_map( text_locations);
 vector* locat=(*map1)["has"];
 for(int i=0;i<(*locat).size();i++)
 {
  cout << (*locat)[i].first << " " << (*locat)[i].second << endl;
 }
}

阅读(1217) | 评论(0) | 转发(0) |
0

上一篇:处理标点符号

下一篇:const关键字及引用

给主人留下些什么吧!~~