Part of Slepp's ProjectsPastebinTURLImagebinFilebin
Feedback -- English French German Japanese
Create Upload Newest Tools Donate
Sign In | Create Account

Advertising

Untitled
Thursday, September 13th, 2012 at 11:46:26pm MDT 

  1. /* Program to query-biased generate snippets that reflect the content
  2.    of a sequence of paragraphs of text
  3.  
  4.    Skeleton written by Alistair Moffat, August 2012.
  5.    Modified by XXXX, August 2012.
  6.  */
  7.  
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <ctype.h>
  12. #include <string.h>
  13. #include <strings.h>
  14. #include <assert.h>
  15.  
  16. /* constants and fixed strings */
  17.  
  18.         /* HTML formatting strings */
  19. #define TOP "<html><head><title>Algorithms are Fun!</title></head>" \
  20.                 "<body><h3>Algorithms are Fun!</h3><ol>"
  21. #define PAR "<p><li>"
  22. #define BOT "</ol></body></html>"
  23. #define HIGH1 "<font color="red"><b>"
  24. #define HIGH2 "</b></font>"
  25.  
  26.         /* maximum number of characters per word */
  27. #define MAX_WORD_LEN 23
  28.         /* maximum number of words per paragraph */
  29. #define MAX_PARA_LEN 10000
  30.  
  31.         /* return code from get_word if end of paragraph found */
  32. #define PARA_END 1
  33.         /* return code from get_word if a word successfully found */
  34. #define WORD_FND 2
  35.  
  36.         /* terminating punctuation that may follow a word */
  37. #define TERM_PUNCT ".,;:!?"
  38.         /* the string the separates paragraphs */
  39. #define PARA_SEPARATOR "\n========\n"
  40.  
  41.         /* maximum words in any output snippet */
  42. #define MAX_SNIPPET_LEN 30
  43.         /* minimum words in any output snippet */
  44. #define MIN_SNIPPET_LEN 20
  45.  
  46.         /* maximum terms allowed on command line */
  47. #define MAX_TERMS 50
  48.  
  49. /* type definitions */
  50.  
  51. typedef char word_t[MAX_WORD_LEN+1];
  52.  
  53. typedef struct {
  54.         int nwrds;
  55.         word_t words[MAX_PARA_LEN];
  56.         /* you can add extra fields here if wish */
  57. } para_t;
  58.  
  59. /* you can add extra types here if you wish */
  60.  
  61. /* function prototypes */
  62.  
  63. void    start_output();
  64. void    new_paragraph();
  65. void    end_output();
  66. int     get_word(word_t w, int limit);
  67. void    put_word(word_t w);
  68. void    get_paragraph(para_t p, int limit);
  69. void    put_paragraph(para_t p);
  70.  
  71. /* where it all happens */
  72.  
  73. int
  74. main(int argc, char **argv) {
  75.         word_t oneword;
  76.         para_t oneparagraph;
  77.         start_output();
  78.         new_paragraph();
  79.         while (get_word(oneword, MAX_WORD_LEN) != EOF) {
  80.         if (get_word(oneword, MAX_WORD_LEN)==PARA_END){
  81.                 new_paragraph();
  82.         }else if (get_word(oneword==WORD_FND){
  83.                 get_paragraph(oneparagraph);
  84.         }
  85.         put_paragraph(oneparagraph);
  86.         }
  87.         }
  88.  
  89.         end_output();
  90.         return 0;
  91. }
  92.  
  93. /* prelude output, to set up the HTML file */
  94.  
  95. void
  96. start_output() {
  97.         printf("%s\n", TOP);
  98. }
  99.  
  100. /* start a new output paragraph in the HTML file */
  101.  
  102. void
  103. new_paragraph() {
  104.         printf("%s\n", PAR);
  105. }
  106.  
  107. /* and close off the HTML output */
  108.  
  109. void
  110. end_output() {
  111.         printf("%s\n", BOT);
  112. }
  113.  
  114. /* extract a single word out of the standard input, but not
  115.    more than "limit" characters in total. One character of
  116.    sensible trailing punctuation is retained.  Argument array
  117.    w must be limit+1 characters or bigger. */
  118.  
  119. int
  120. get_word(word_t w, int limit) {
  121.         int c,i;
  122.         /* first, skip over any non alphanumerics */
  123.         while ((c=getchar())!=EOF && !isalnum(c)) {
  124.        
  125.         /* is there a paragraph boundary? */
  126.         if (c==PARA_SEPARATOR[i]){
  127.                 i++
  128.         }       
  129.                 i = 0; /* resets the index */
  130.                 return PARA_END;
  131.                 }
  132.         }else{
  133.         i = 0;
  134.         }
  135.         if (c==EOF) {
  136.                 return EOF;
  137.         }
  138.  
  139.         /* ok, first character of next word has been found */
  140.         *w = c;
  141.         w += 1;
  142.         limit -= 1;
  143.         while (limit>0 && (c=getchar())!=EOF && isalnum(c)) {
  144.                 /* another character to be stored */
  145.                 *w = c;
  146.                 w += 1;
  147.                 limit -= 1;
  148.         }
  149.         /* take a look at that next character, is it trailing
  150.            punctuation? */
  151.         if (strchr(TERM_PUNCT, c) && (limit>0)) {
  152.                 /* yes, it is */
  153.                 *w = c;
  154.                 w += 1;
  155.                 limit -= 1;
  156.         }
  157.  
  158.         /* now close off the string */
  159.         *w = '\0';
  160.         return WORD_FND;
  161. }
  162.  
  163. /* write out a non-bold word */
  164.  
  165. void
  166. put_word(word_t w) {
  167.         printf("%s\n", w);
  168. }
  169.  
  170. void
  171. get_paragraph(para_t p, int limit){
  172.         while(get_word()!=EOF && get_word()!=PARA_END){
  173.                 p.words[i] = word_t   
  174.                 p.nwrds += 1;
  175.         }
  176.         if (p.nwrds>limit){
  177.                 return EXIT_ERROR;
  178. }
  179. }
  180.                
  181.  
  182. void
  183. put_paragraph(para_t p){
  184.         for (i=0;i<p.nwrds;i++{
  185.         printf("%s\n", p.words[i]);
  186. }
  187. }
  188.  
  189. void
  190. put_highlight(word_t t){
  191.         printf("%s%s%s", HIGH1, t, HIGH2);
  192. }

advertising

Update the Post

Either update this post and resubmit it with changes, or make a new post.

You may also comment on this post.

update paste below
details of the post (optional)

Note: Only the paste content is required, though the following information can be useful to others.

Save name / title?

(space separated, optional)



Please note that information posted here will expire by default in one month. If you do not want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords. All illegal activities will be reported and any information will be handed over to the authorities, so be good.

worth-right